ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
C program to perform stack operation using singly linked list
 Question Submitted By :: Chandrakala
I also faced this Question!!     Rank Answer Posted By  
 
  Re: C program to perform stack operation using singly linked list
Answer
# 1
#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 ?    6 Yes 3 No
Saad
 
  Re: C program to perform stack operation using singly linked list
Answer
# 2
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 ?    0 Yes 0 No
Bin Fang
 
 
 
  Re: C program to perform stack operation using singly linked list
Answer
# 3
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 ?    0 Yes 0 No
Bin Fang
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
How can we open a file in Binary mode and Text mode?what is the difference? PanTerra1
Who had beaten up hooligan "CHAKULI" in his early college days?  1
The differences between Windows XP and Windows Visa HCL7
implement general tree using link list Wipro1
Can we include one C program into another C program if yes how? Infosys4
CAN WE DEFINE ANY FUNCTION WITHIN A FUNCTION.  6
what is the other ways to find a logic to print whether a number is an even or odd wit out using % symbol??????? i know three different ways to print it. so i need any other different logic>>>>> TCS3
What should be keep precautions while using the recursion method?  1
I have an array of 100 elements, each of which is a random integer. I want to know which of the elements: a) are multiples of 2 b) are multiples of 2 AND 5 c) have a remainder of 3 when divided by 7  1
will u give me old quesrion papers for aptitude for L & t info tech? L&T1
int i; i=2; i++; if(i=4) { printf(i=4); } else { printf(i=3); } output of the program ? Mascot9
difference of two no's with out using - operator  6
what's the return value of malloc()  8
What is an volatile variable? HP12
Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not require more than the basic operators (if, for, math operators, etc.). • Assumptions • Don’t worry about overflow or underflow • Stop at the 1st invalid character and return the number you have converted till then, if the 1st character is invalid return 0 • Don’t worry about exponential (e.g. 1e10), instead you should treat ‘e’ as an invalid character • Write it like real code, e.g. do error checking • Go though the string only once • Examples • “1.23” should return 1.23 • “1a” should return 1 • “a”should return 0 Qualcomm5
if ENTERED FIVE DIGITS DESIGN A PROGRAM THAT WILL FIND CORRESPONDING VALUE FROM ASCII TABLE  1
YBJBU6  1
Write a program to compare two strings without using the strcmp() function Accenture14
the format specified for hexa decimal is a.%d b.%o c.%x d.%u TCS4
Is the following code legal? struct a { int x; struct a b; }  3
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com