answersLogoWhite

0


Best Answer

struct node

{

int info;

struct node *link;

} *top=NULL;

int main()

{

int choice;

while(1)

{ printf("1.Push\n");

printf("2.Pop\n");

printf("3.Display\n");

printf("4.Quit\n");

printf("Enter your choice : ") ;

scanf("%d", &choice);

switch(choice)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(1);

default :

printf("Wrong choice\n");

}/*End of switch */

}/*End of while */

return 0;

}/*End of main() */

void push()

{

struct node *tmp;

int pushed_item;

tmp = malloc(sizeof(struct node));

printf("Input the new value to be pushed on the stack : ");

scanf("%d",&pushed_item);

tmp->info=pushed_item;

tmp->link=top;

top=tmp;

}/*End of push()*/

void pop()

{

struct node *tmp;

if(top == NULL)

printf("Stack is empty\n");

else

{ tmp=top;

printf("Popped item is %d\n",tmp->info);

top=top->link;

free(tmp);

}

}/*End of pop()*/

void display()

{ struct node *ptr;

ptr=top;

if(top==NULL)

printf("Stack is empty\n");

else

{

printf("Stack elements :\n");

while(ptr!= NULL)

{

printf("%d\n",ptr->info);

ptr = ptr->link;

}/*End of while */

}/*End of else*/

}/*End of display()*/

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Stack push allocates an element, copies the data into the element, and inserts it at the head of the list. It throws a memory full exception when it cannot allocate an element.

Stack pop removes and deallocates the element at the head of the list after copying the data from the element. It throws a stack empty exception when the list is found to be empty.

THE PROGRAM GOES LIKE THIS:

//linked list implementation of stack

#include

#include

#include

#include

struct node

{

char info[15];

struct node *link;

}*start=NULL;

int push()

{

struct node *temp;

temp=(struct node*)malloc(sizeof(struct node));

printf("\nEnter the string::\n");

scanf("%s",temp->info);

temp->link=start;

start=temp;

return 0;

}

int pop()

{

struct node *temp;

if(start==NULL)//empty stack

{

printf("\nUnderflow!\n");

return 0;

}

temp=start;

start=temp->link;

printf("\nString popped is:%s\n",temp->info);

free(temp);

return 0;

}

int display()

{

struct node *temp;

if(start==NULL)//empty stack

{

printf("\nEmpty Stack!\n");

return 0;

}

temp=start;

printf("\nThe stack is:\n");

while(temp!=NULL)

{

printf("%s\n",temp->info);

temp=temp->link;

}

return 0;

}

int main()

{

int choice;

while(1)

{

printf("\nLinked list implementation of stack:\nWhat do u wanna do?:\n1.Push\n2.Pop\n3.Display\n4.Exit\nEnter ur choice........\n");

scanf("%d",&choice);

switch(choice)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(1);

break;

default:

printf("\nSoooooory!!!!Wrong choice .plz try again..\n");

}

}//end of while

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

One way to implement a stack using a list is to use a forward list (a uni-directional or singly-linked list), where all insertions and extractions occur at the head. Each node points to the next so the list only needs to maintain a pointer to the current head of the list. The memory overhead is minimal (one pointer per node, plus one pointer to keep track of the head node), however there will be a slight performance penalty as a result of the inherent indirection as well as the need to allocate new memory for each new node. The latter problem can be addressed by using your own memory allocator to reserve one or more "pools" of memory within which to construct new nodes.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you implement stack using singly linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it possible to implement stack and queues using linkes list?

Yes it is possible to implement stack and queue using linked list


How do you implement a doubly linked list by using singly linked list?

To implement a doubly linked list using a singly linked list, you can create two nodes in each element of the singly linked list - one for the next element and another for the previous element. This way, each node will have access to both its previous and next nodes, effectively creating a doubly linked list structure using a singly linked list implementation.


How do you implement stack without array?

Stacks are often implemented using the same node structure as a linked list.


Explain The merits of using a deque to implement a stack in data structure?

Explain The merits of using a deque to implement a stack in data structure


What is the C plus plus programs to implement the stack ADT using a singly linked list?

#include<iostream> #include<time.h> #include<forward_list> int main() { // seed random number generator srand ((unsigned) time(NULL)); // a forward_list is a singly-linked list std::forward_list<int> stack; // push 10 integers onto stack for (int loop=0; loop<10; ++loop) { int num=rand(); std::cout<<"Pushing "<<num<<std::endl; stack.push_front (num); } // pop all integers while (!stack.empty()) { std::cout<<"Popping "<<stack.front()<<std::endl; stack.pop_front(); } }


What are advantages of stack?

some disadvantages created in stack using array then that problem solve to linked list use in stack.First advantage for size of stack not limited in linked list using.second essay to stack programme implement using only one pointer.


Which of the following data structures can be randomly accessed giving loc A. linked list implemented using array B. singly linked list C. double linked list D. both single and double linked list?

Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.


Can you implement merge sort without using recursion?

Sure, recursion can always be substituted with using a stack.


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


Evaluate the polynomial expression using linked list using c?

Evaluating a Polynomial expression using a singly linked list visit : http://myfundatimemachine.blogspot.in/2012/06/polynomial-evaluation-in-c.html


What is the relationship between a stack and an array?

There is no inherent relationship between the two. It's possible to implement a stack using an array to store date, but that's about it.


How do you solve josephus problem using circular linked list?

The Josephus problem is a problem to locate the place for the last survivour. It shows the power of the circular linked list over the singly linked lists.