[C] Header Files

B

Bgag

Hallo zusammen!

Ich habe ein kleines Problem. Ich habe einen dynamischen Stack in C implementiert und möchte diesen nun über eine Header-Datei oder noch besser als DLL nutzen können. Nun die Frage wie kann ich das machen und falls ich es als DLL machen kann, wie nutze ich sie? In der Erklärung von R&K habe ich dazu nichts gefunden.

Die stack.c enthält alle benötigten Funktionen zum Umgang mit Stacks und auch die Functionen zum Testen (main bzw. Eingabe & Ausgabe).

MfG, Andy

C:
/* include needed packages */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

typedef char *tdata;

typedef struct node
{
    struct node *previous;
    tdata data;
} tnode;

typedef struct stack
{
    tnode *top;
} tstack;

/* the stack operations */
tstack *init(void);
int isempty(tstack *s);
void push(tstack *stack, tnode *node);
tnode *pop(tstack *stack);
tnode *peek(tstack *stack);
void clear(tstack *stack);

/* functions for the user interface */
tnode *readdata(void);
void print(tnode *entry);
void printall(tstack *stack);
void menu(void);

int main(void)
{
    tstack *stack = init();
    tnode *node;
    char input;

    menu();

    while(1)
    {
        input = getchar();
        input = toupper(input);

	 system("cls");

	 menu();

        switch(input)
        {
            case 'A':
                node = readdata();
                push(stack, node);
                break;
            case 'R':
                node = pop(stack);
                print(node);
	  	   getchar();
                break;
            case 'P':
                node = peek(stack);
                print(node);
	  	   getchar();
                break;
            case 'V':
                printall(stack);
	  	   getchar();
                break;
            case 'C':
                clear(stack);
                break;
            case 'Q':
                exit(0);
                break;
            default:
                break;
        }
    }
}

tstack *init(void)
{
    /* define a stack pointer */
    tstack *stack;

    /* allocate stack storage */
    stack = malloc(sizeof(tstack));

    /* set to item to zero */
    stack->top = NULL;

    return stack;
}

int isempty(tstack *s)
{
        return (s->top == NULL) ? 1 : 0;
}

void push(tstack *stack, tnode *node)
{
    /* use old top as previous element of the new entry */
    node->previous = stack->top;

    /* set new element to the top */
    stack->top = node;
}

tnode *pop(tstack *stack)
{
    /* create a pointer at a temporary element */
    tnode *temp = NULL;

    /* check if there is something in the queue */
    if( stack->top != NULL )
    {
        /* get the first element from the right side */
        temp = stack->top;

        /* set the next element to the first of the right side */
        stack->top = temp->previous;

        /* free the storage */
        free( temp );
    }

	return temp;
}

tnode *peek(tstack *stack)
{
	tnode *top = NULL;

	if (stack->top == NULL)
	{
		printf ("Stack is empty!\n");
	}

	else
	{
		top = stack->top;
	}

 	return top;
}

void clear(tstack *stack)
{
	while(stack->top != NULL)
	{
		pop(stack);
	}
}

tnode *readdata(void)
{
    tdata data = malloc(15 * sizeof(char));
    scanf("%s", data);

    tnode *node = malloc(sizeof(tnode));
    node->previous = NULL;
    node->data = data;

    return node;
}

void print(tnode *node)
{
	if(node != NULL)
	{
 		printf("--> %s\n", node->data);
	}
}

void printall(tstack *stack)
{
    tnode *current = stack->top;

    if (current == NULL)
	{
		printf ("Stack is empty!\n");
	}

    else
	{
		while (current != NULL)
		{
			printf ("%s\n", current->data);
			current = current->previous;
		}
	}

    printf ("\n");
}

void menu(void)
{
    printf("----------------\n");
    printf("(A)dd Node\n");
    printf("(R)emove Node\n");
    printf("(P)eek Node\n");
    printf("(V)iew Stack\n");
    printf("(C)lear Stack\n");
    printf("(Q)uit\n");
    printf("----------------\n\n");
}
 
Hi,

deepthroat sagts ;) EInfach ne Dll erstellen und die Funktionen in ne .c Datei.
Die definitionen der Funktionen in ne .h und diese dann in jedes beliebige Programm includen.

Dann noch die .lib der .dll zu dem Programm hinzulinken und es sollte alles gehen.

Gruß
Anfänger
 
Zurück