Jan

23

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

struct node

{

int data;

struct node *link;

};

void push(struct node**, int);

int pop(struct node **);

void traverse(struct node**);

void delstack(struct node**);

void main()

{

struct node *s=NULL;

int i;

clrscr();

push(&s,8);

push(&s,20);

push(&s,-4);

push(&s,15);

push(&s,18);

push(&s,12);

push(&s,16);

push(&s,25);

push(&s,0);

push(&s,10);

push(&s,100);

//traverse(&s);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d”,i);

i=pop(&s);

printf(“\nItem popped: %d\n”,i);

traverse(&s);

delstack(&s);

}

void push(struct node **top,int item){

struct node *temp;

temp=(struct node*) malloc(sizeof(struct node));

if(temp==NULL)

{

printf(“\nStack is full”);

}

temp->data=item;

temp->link=*top;

*top=temp;

}

int pop(struct node **top){

struct node *temp;

int item;

if(*top==NULL){

printf(“\n Stack is empty”);

return NULL;

}

temp=*top;

item=temp->data;

*top=(*top)->link;

free(temp);

return item;

}

void delstack(struct node **top){

struct node *temp;

if(*top==NULL)

return;

while(*top!=NULL){

temp=*top;

*top=(*top)->link;

free(temp);

}

}

void traverse(struct node**top){

struct node *temp;

int item;

if(*top==NULL){

printf(“\n Stack is empty”);

return;

}

while(*top!=NULL){

temp=*top;

item=temp->data;

*top=(*top)->link;

printf(“%d\n”,item);

}

}


Leave a Reply