Created
November 11, 2011 17:38
-
-
Save vinitkumar/1358651 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstdlib> | |
#include<stdio.h> | |
using namespace std; | |
struct node /* declaration of a node*/ | |
{ | |
int data; | |
struct node *link; | |
}; | |
struct node *start=NULL; /* empty list: initialization*/ | |
struct node *top, *temp; | |
void push(struct node *); | |
void pop(); | |
void display(); | |
int main(void) | |
{ | |
int opt; | |
do /* main menu*/ | |
{ | |
printf("\n 1.Push element into linked stack\n"); | |
printf("\n 2.Pop element from linked stack\n"); | |
printf("\n 3.Display stack contents\n"); | |
printf("\n 4.Quit\n"); | |
printf("\nEnter option: "); | |
scanf("%d",&opt); | |
switch(opt) | |
{ | |
case 1: | |
top=(struct node *)calloc(1,sizeof(struct node)); | |
push(top); | |
break; | |
case 2: | |
pop(); | |
break; | |
case 3: | |
display(); | |
break; | |
case 4: | |
exit(1); | |
} | |
} while(1); | |
} | |
/* push operation*/ | |
void push(struct node *t) | |
{ | |
int item; | |
printf("\nEnter the element to be pushed \n"); | |
scanf(" %d",&item); | |
t->data=item; | |
if(start==NULL) | |
{ t->link=0; | |
start=t; | |
} | |
else | |
{ | |
t->link=start; | |
start=t; | |
} | |
} | |
void pop() /*pop operation*/ | |
{ | |
if(start==NULL) | |
printf("\nStack is empty! Pop impossible!\n"); | |
else | |
{ | |
temp=start; | |
start=start->link; | |
free(temp); | |
} | |
} | |
void display() /*display stack */ | |
{ | |
if(start == NULL) | |
printf("\nStack is empty!\n"); | |
else | |
{printf("contents of the stack: \n"); | |
temp=start; | |
while(temp->link !=0) | |
{ | |
printf("\n %d ",temp->data); | |
temp=temp->link; | |
} | |
printf("\n %d ",temp->data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment