Tuesday 26 July 2016

Stack Using Single Link List

#include<stdio.h>
#include<conio.h>
/*  **********************************
Girfa Student Help
Stack Using Single Link List
Programmer Name : Mritunjoy Sengupta
for DS program visit :http://girfahelp.blogspot.in/p/data.html
    *************************************/
typedef struct n
{
int data;
struct n *next;
}node;
node *start=NULL;
node* create(int);
void push(int);
void pop();
void print();

void main()
{
int n,s,opt;
do
{
clrscr();
printf("\n1. Print\n2. Push \n3. Pop\n0. Exit\n\nEnter Your Choice>> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
print();
getch();
break;
case 2:
printf("Enter Number for push>> ");
scanf("%d",&n);
push(n);
break;
case 3:
if(start==NULL)
{
printf("\nList is empty add some data");
getch();
}
else
pop();
break;
case 0:
break;
default:
printf("Invalid Choice");
getch();
}
}while(opt!=0);
}
node* create(int n)
{
node *nw;
nw=(node*) malloc(sizeof(node));
nw->data=n;
nw->next=NULL;
return nw;
}

void push(int n)

{
node *nw=create(n);
if(start==NULL)
start=nw;
else
{
nw->next=start;
start=nw;
}
}

void pop()
{
node *pt;
pt=start;
if(start==NULL)
printf("\nStack is empty");
else
{
start=start->next;
free(pt);
}
}
void print()
{
  node *pt;
  pt=start;
  if(pt==NULL)
   printf("\n Stack is empty");
  else
  {
    for(pt=start;pt!=NULL;pt=pt->next)
     printf("[%d]",pt->data);
  }
}

No comments:

Post a Comment