Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


C program to perform stack operation using singly linked list

Answers were Sorted based on User's Feedback



C program to perform stack operation using singly linked list..

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

C program to perform stack operation using singly linked list..

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

C program to perform stack operation using singly linked list..

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

Post New Answer

More C Interview Questions

A program to write a number of letters and numbers, such as counting and display

0 Answers  


can any one provide me the notes of data structure for ignou cs-62 paper

0 Answers   Ignou,


what are advantages of U D F?

1 Answers   Google,


define c

6 Answers   HCL, TCS,


What is a char c?

0 Answers  


A.C func() { pritnf(" in fuction %d",MACRO); } MAIN.c testfunc() { #define MACRO 10 printf("in test function %d", MACRO); } main() { printf("in main %d",MACRO); func(); testfunc(); getch(); }

2 Answers   Wipro,


What is the output for the below program? void main() { float me=1.1; double you=1.1; if(me==you) printf("love c"); else printf("know c"); }

7 Answers  


write a program for egyptian fractions in c?

1 Answers   Satyam,


write a program to display reverse of a number using for loop?

14 Answers  


#include<stdio.h> #include<conio.h> void main() { float a; clrscr(); a=0.5; if(a==0.5) printf("yes"); else printf("no"); getch(); }

9 Answers   TCS,


What are the key features in c programming language?

0 Answers  


What is a stream in c programming?

0 Answers  


Categories