IMPLEMENT PUSH OPERATION ON STACK USING LINKED LIST

/*PROGRAM TO IMPLEMENT PUSH OPERATION ON STACK USING LINKED LIST*/
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *top=NULL,*ptr,*temp;
int num;
char ch;
clrscr();
printf(“Pushing elements into the list:\n”);
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf(“Enter the element to be pushed: “);
scanf(“%d”,&num);
temp->data=num;
temp->link=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->link=top;
top=temp;
}
fflush(stdin);
printf(“Do you want to push more elements??\n press y for yes otherwise n\t “);
scanf(“%c”,&ch);
}while(ch==’y’||ch==’Y’);
printf(“Elements in the stack are: \n”);
for(ptr=top;ptr!=NULL;ptr=ptr->link)
{
printf(“\t%d”,ptr->data);
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *