STACKS USING LINKED LIST implementation in C++
|
Download Attachment
|
/***********************************************/ /*PROGRAM TO IMPLEMENT STACKS USING LINKED LIST*/ /***********************************************/
#include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h>
struct stack { int info; struct stack *next; }*top,*newnode,*ptr;
void menu(); void display(); int underflow(); void push(int); void pop();
void main() { clrscr(); menu(); }
void menu() { int choice,item; printf("MENU"); printf("\n1. Push into the stack"); printf("\n2. Pop from stack"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); printf("\nEnter the item tobe pushed: "); scanf("%d",&item); push(item); clrscr(); printf("\nAfter push operation stack is:\n"); display(); getch(); clrscr(); menu(); break; case 2: clrscr(); if(underflow()==1) { pop(); if(underflow()==1) { printf("\nAfter pop operation stack is:\n"); display(); } } getch(); clrscr(); menu(); break; case 3: clrscr(); if(underflow()==1) { printf("The stack is:\n"); display(); } getch(); clrscr(); menu(); break; case 4: exit(1); default: clrscr(); printf("Your choice is wrong\n\n"); menu(); } }
int underflow() { if(top==NULL) { printf("\nStack is empty"); return(0); } else { return(1); } }
void push(int item) { newnode=(struct stack*)malloc(sizeof(struct stack)); newnode->info=item; if(top==NULL) { top=newnode; newnode->next=NULL; } else { newnode->next=top; top=newnode; } }
void pop() { top=top->next; }
void display() { int i; ptr=top; i=1; while(ptr!=NULL) { ptr=ptr->next; i++; } ptr=top; i=i-1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i--; } }
|
|
|
|
|
Share this article
| Print |
Article read by 3149 times
|
|
|