Wednesday 22 August 2012

Stack implementation using linked list | How to do code

/*How to write  Program for stack implementation using linked list */

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

struct node
{
int info;
struct node *link;
}*top=NULL;

void push()
{
int n;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("\nenter the element u want to insert:- ");
scanf("%d",&n);
temp->info=n;
temp->link=top;
top=temp;
}

void pop()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstack is empty");
else
{
printf("\n%d is deleted from stack\n",top->info);
top=top->link;
free(temp);
}
}

void display()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstackis empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}

void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for display");
printf("\n4:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
stack implementation using linked list
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: