Tuesday, August 5, 2008

C source code for 2 Linked List Implementation Of Stack

#define TRUE 1

# define FALSE 0

typedef struct stack_element {

int stack_data;

struct stack_element *next;

}stack_inf;

typedef struct stack_element_top{

stack_inf *stack_top;

}stack;

stack *create()

{

stack *s;

s=(stack *)malloc(sizeof(stack));

s->stack_top=NULL;

return(s);

}

int isempty (stack *s)

{

if (s->stack_top == NULL)

return TRUE;

else

return FALSE;

}

void push (stack *s,int x)

{

stack_inf *p;

p=(stack_inf *)malloc(sizeof(stack_inf));

p->stack_data=x;

p->next=NULL;

if(isempty(s))

s->stack_top=p;

else

{

p->next=s->stack_top;

s->stack_top=p;

}

return;

}

int pop(stack *s)

{

stack_inf *p;

int x;

if(isempty(s))

printf(“\nStack is empty !\n”);

else

{

p=s->stack_top;

x=p->stack_data;

s->stack_top=p->next;

free(p);

return(x);

}

}

No comments:

Your Title