C program to perform stack operation using singly linked list
Answer Posted / saad
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct node
{
int data;
struct node* next;
};
typedef struct node node;
node* curr_ptr=NULL;
node* head=NULL;
int count=0;
#define max 5
main()
{
int stack_size;
int choice;
int ele;
clrscr();
while(1)
{
printf("\n\n\t1.Push\n\t2.Pop\n\t3.Display\n\t4.Exit\n");
printf("\n\tEnter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: if(count==max)
{
printf("\n\n\t\tSTACK OVERFLOW!!!");
clrscr();
}
else
{
printf("\n\n\t\tEnter The Element To Push: ");
scanf("%d",&ele);
Push(ele);
}
break;
case 2: if(count==0)
{
printf("\n\n\t\tSTACK UNDERFLOW!!!");
}
else
Pop();
break;
case 3: Display();
break;
case 4: exit();
break;
}
}
}
Push(int info)
{
node* new_node;
new_node=(node*)malloc(sizeof(node));
new_node->data=info;
new_node->next=NULL;
if(head==0)
{
head=new_node;
curr_ptr=head;
count++;
}
else
{
curr_ptr->next=new_node;
curr_ptr=curr_ptr->next;
count++;
}
return;
}
Pop()
{
node* pre_node, *temp1;
node* temp=head;
if(count==1)
{
head=0;
free(temp);
printf("\n\n\t\tNode is Deleted");
return;
}
else
{
while(temp->next!=0)
{
pre_node=temp;
temp=temp->next;
}
printf("%d",pre_node->data);
temp1=pre_node->next;
pre_node->next=NULL;
free(temp1);
}
return;
}
Display()
{
node* temp=head;
if(head==NULL)
{
printf("\n\tList Empty!!!");
return;
}
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
return;
}
Is This Answer Correct ? | 24 Yes | 5 No |
Post New Answer View All Answers
Why we write conio h in c?
What is #define size in c?
What is define directive?
main(){char *str;scanf("%s",str);printf("%s",str); }The error in the above program is: a) Variable 'str' is not initialised b) Format control for a string is not %s c) Parameter to scanf is passed by value. It should be an address d) none
Why & is used in scanf in c?
how do you programme Carrier Sense Multiple Access
What are the types of c language?
What are the different categories of functions in c?
write a program using linked list in which each node consists of following information. Name[30] Branch Rollno Telephone no i) Write the program to add information of students in linked list
difference between object file and executable file
Is c compiled or interpreted?
What are the different types of control structures?
What is the difference between c &c++?
Write a C++ program to generate 10 integer numbers between - 1000 and 1000, then store the summation of the odd positive numbers in variable call it sum_pos, then find the maximum digit in this variable regardless of its digits length.
how can i write a program that prints out a box such that whenever i press any key8(coordinate number) on the keyboard, the box moves.