Skip to content

Instantly share code, notes, and snippets.

@vinitkumar
Created November 11, 2011 17:38
Show Gist options
  • Save vinitkumar/1358651 to your computer and use it in GitHub Desktop.
Save vinitkumar/1358651 to your computer and use it in GitHub Desktop.
#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