Friday, August 23, 2013

How to write Simple Stack Program using Linked List

#include<conio.h>
#include<stdio.h>

void PUSH();
void PEEP();
void POP();

struct node{
int data;
struct node *link;

}*head,*top=NULL,*newx,*item;


void main()
{
int i,size,value;
clrscr();

printf("How many Element want to insert :");
scanf("%d",&size);

for(i=0;i<size;i++)
{
printf("\nEnter Value : ");
scanf("%d",&value);

PUSH(value);
}

POP();

POP();

PEEP();

getch();
}

void PUSH(int value)
{
newx=(struct node*)malloc(sizeof(struct node));

if(newx==NULL)
{
clrscr();
printf("\n\t Memory is Full !");
}
else
{
newx->data=value;
newx->link=top;
top=newx;
head->link=top;
}
}

void POP()
{
if(top==NULL)
{
printf("\nStack is Underflaw");
}
else
{
item->data=top->data;
head->link=top->link;
top=top->link;

printf("\nValue %d is POPED OUT ",item->data);
}
}

void PEEP()

{
item->data=top->data;
printf("\nTop most Value =>%d",item->data);
}

No comments:

Post a Comment