C program to perform stack operation using singly linked list
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / bin fang
the above has bugs! for example, the count is not
decremented in Pop function...
I rewrote the Push and Pop code as follows:
node *cur = NULL;
node *head = NULL;
void Push(int info)
{
node *new;
new = (node *)malloc(sizeof(node));
new->data = info;
new->next = NULL;
if (head == NULL)
head = new;
else
cur->next = new;
cur = new;
count++;
}
void Pop(void)
{
node *pre = NULL;
node *temp = head;
while (temp != cur) {
pre = temp;
temp = temp->next;
}
printf("\n\tNode (%d) is deleted.", cur->data);
free(cur);
count--;
cur = pre;
if (cur)
cur->next = NULL;
else
head = NULL;
}
Is This Answer Correct ? | 10 Yes | 1 No |
Answer / bin fang
And much better and simpler implementation should be like
this:
void Push(int info)
{
node *temp;
temp = (node *)malloc(sizeof(node));
temp->data = info;
temp->next = head;
head = temp;
count++;
}
void Pop(void)
{
node *temp;
temp = head;
head = head->next;
free(temp);
count--;
}
Is This Answer Correct ? | 7 Yes | 2 No |
What does a function declared as pascal do differently?
What are the different flags in C? And how they are useful? And give example for each in different consequences?
18)struct base {int a,b; base(); int virtual function1(); } struct derv1:base{ int b,c,d; derv1() int virtual function1(); } struct derv2 : base {int a,e; } base::base() { a=2;b=3; } derv1::derv1(){ b=5; c=10;d=11;} base::function1() {return(100); } derv1::function1() { return(200); } main() base ba; derv1 d1,d2; printf("%d %d",d1.a,d1.b) o/p is a)a=2;b=3; b)a=3; b=2; c)a=5; b=10; d)none 19) for the above program answer the following q's main() base da; derv1 d1; derv2 d2; printf("%d %d %d",da.function1(),d1.function1(),d2.function1 ()); o/p is a)100,200,200; b)200,100,200; c)200,200,100; d)none 20)struct { int x; int y; }abc; you can not access x by the following 1)abc-->x; 2)abc[0]-->x; abc.x; (abc)-->x; a)1,2,3 b)2&3 c)1&2 d)1,3,4
WAP to convert text into its ASCII Code and also write a function to decode the text given?
what is the use of getch() function in C program.. difference b/w getch() and getche()??
29 Answers HCL, IBM, Infosys, TCS, Wipro,
How can I dynamically allocate arrays?
what is the use of using linked list and array?
how to use virual function in real time example
The __________ attribute is used to announce variables based on definitions of columns in a table?
for(;;) printf("C language") What is out put of above??
2 Answers Practical Viva Questions,
How are strings stored in c?
Write a program in c to print 1 121 12321 1234321 123454321
11 Answers ANR, College School Exams Tests, Mu Sigma, Wipro,