stack implementation using linked list in c

Solutions on MaxInterview for stack implementation using linked list in c by the best coders in the world

showing results for - "stack implementation using linked list in c"
Taina
20 Mar 2018
1#include <stdio.h>
2#include <stdlib.h>
3#define TRUE 1
4#define FALSE 0
5
6struct node
7{
8    int data;
9    struct node *next;
10};
11typedef struct node node;
12
13node *top;
14
15void initialize()
16{
17    top = NULL;
18}
19
20void push(int value)
21{
22    node *tmp;
23    tmp = malloc(sizeof(node));
24    tmp -> data = value;
25    tmp -> next = top;
26    top = tmp;
27}
28
29int pop()
30{
31    node *tmp;
32    int n;
33    tmp = top;
34    n = tmp->data;
35    top = top->next;
36    free(tmp);
37    return n;
38}
39
40int Top()
41{
42    return top->data;
43}
44
45int isempty()
46{
47    return top==NULL;
48}
49
50void display(node *head)
51{
52    if(head == NULL)
53    {
54        printf("NULL\n");
55    }
56    else
57    {
58        printf("%d\n", head -> data);
59        display(head->next);
60    }
61}
62
63int main()
64{
65    initialize();
66    push(10);
67    push(20);
68    push(30);
69    printf("The top is %d\n",Top());
70    pop();
71    printf("The top after pop is %d\n",Top());
72    display(top);
73    return 0;
74}
75
Camilo
07 Aug 2019
1/*
2 * C Program to Implement a Stack using Linked List
3 */
4#include <stdio.h>
5#include <stdlib.h>
6 
7struct node
8{
9    int info;
10    struct node *ptr;
11}*top,*top1,*temp;
12 
13int topelement();
14void push(int data);
15void pop();
16void empty();
17void display();
18void destroy();
19void stack_count();
20void create();
21 
22int count = 0;
23 
24void main()
25{
26    int no, ch, e;
27 
28    printf("\n 1 - Push");
29    printf("\n 2 - Pop");
30    printf("\n 3 - Top");
31    printf("\n 4 - Empty");
32    printf("\n 5 - Exit");
33    printf("\n 6 - Dipslay");
34    printf("\n 7 - Stack Count");
35    printf("\n 8 - Destroy stack");
36 
37    create();
38 
39    while (1)
40    {
41        printf("\n Enter choice : ");
42        scanf("%d", &ch);
43 
44        switch (ch)
45        {
46        case 1:
47            printf("Enter data : ");
48            scanf("%d", &no);
49            push(no);
50            break;
51        case 2:
52            pop();
53            break;
54        case 3:
55            if (top == NULL)
56                printf("No elements in stack");
57            else
58            {
59                e = topelement();
60                printf("\n Top element : %d", e);
61            }
62            break;
63        case 4:
64            empty();
65            break;
66        case 5:
67            exit(0);
68        case 6:
69            display();
70            break;
71        case 7:
72            stack_count();
73            break;
74        case 8:
75            destroy();
76            break;
77        default :
78            printf(" Wrong choice, Please enter correct choice  ");
79            break;
80        }
81    }
82}
83 
84/* Create empty stack */
85void create()
86{
87    top = NULL;
88}
89 
90/* Count stack elements */
91void stack_count()
92{
93    printf("\n No. of elements in stack : %d", count);
94}
95 
96/* Push data into stack */
97void push(int data)
98{
99    if (top == NULL)
100    {
101        top =(struct node *)malloc(1*sizeof(struct node));
102        top->ptr = NULL;
103        top->info = data;
104    }
105    else
106    {
107        temp =(struct node *)malloc(1*sizeof(struct node));
108        temp->ptr = top;
109        temp->info = data;
110        top = temp;
111    }
112    count++;
113}
114 
115/* Display stack elements */
116void display()
117{
118    top1 = top;
119 
120    if (top1 == NULL)
121    {
122        printf("Stack is empty");
123        return;
124    }
125 
126    while (top1 != NULL)
127    {
128        printf("%d ", top1->info);
129        top1 = top1->ptr;
130    }
131 }
132 
133/* Pop Operation on stack */
134void pop()
135{
136    top1 = top;
137 
138    if (top1 == NULL)
139    {
140        printf("\n Error : Trying to pop from empty stack");
141        return;
142    }
143    else
144        top1 = top1->ptr;
145    printf("\n Popped value : %d", top->info);
146    free(top);
147    top = top1;
148    count--;
149}
150 
151/* Return top element */
152int topelement()
153{
154    return(top->info);
155}
156 
157/* Check if stack is empty or not */
158void empty()
159{
160    if (top == NULL)
161        printf("\n Stack is empty");
162    else
163        printf("\n Stack is not empty with %d elements", count);
164}
165 
166/* Destroy entire stack */
167void destroy()
168{
169    top1 = top;
170 
171    while (top1 != NULL)
172    {
173        top1 = top->ptr;
174        free(top);
175        top = top1;
176        top1 = top1->ptr;
177    }
178    free(top1);
179    top = NULL;
180 
181    printf("\n All stack elements destroyed");
182    count = 0;
183}
queries leading to this page
hoe to make stack value zero in linkliststack using single linked list in ccreate a stack using linked list in clinked list example in c stack overflowimpementing stack using linked listlinked list stack without top c 2b 2bstack implementation using linked list in cppstaqck using linked list using single pointerstack using ll in cstack using linked list in cimplement stack using a linked liststack with linked list code in cdiscuss the stack implemelltation using lists with example linked list stack in cimplement stack with linked listlinked list stack program in ccpp linked list stack capacitystack c linked liststack with linked list in cjava how add linkedlist elemtn to stackhow to implement stack using linked list in c 2b 2boperations to implement in stack using linked list in clinked list in stacklinked list implementation of stack in cexplain stack node in cimplementing a linked list using stackstack linked list c 2b 2b implementationstack as linked listimplement stack using linked list in cc program to implement stack using circular linked liststack implementation using linked list in c tutorialspointprogram for stack using linked listlinked list stack c 2b 2bwhen we implement the stack is using linked listimplenting a stack based on ciruclar linked listwrite a c program 2f algorithm to implement stack adt using singly linked list perform following operations 3a 28i 29 push 28ii 29 pop 28iii 29 peek 28iii 29 display the stack contentslinked list implementation of stack javalinked list in c pop 28 29 push 28 29stack implementation using linked list and with out switch case statement in cimmplementation of stakc using linked listc program for stack using linked listimplement stack using singly linked listimplementation a stack using singly linked list complexityimplement a link based dynamic stack using pointersimplementation of linked list with stackwhich functin of linked list is used in push and popstack program in c using linked liststack implementtion using linked listimplement stack using linked listwap for linked list implement stack pop and pushstack display function in c in lined listwrite a c program 2f algorithm to implement stack adt using singly linked list perform following operations 3a 28i 29 push 28ii 29 pop 28iii 29 peek 28iii 29 display the stack contents program stack using linked list in cstack using sllimplementation of stack using array and linked list in cwhen we implement stack by using linked list thenimplementation of 2 stack in a single linked listpopping from a stack implementation as a singly linked list 2cwhere 27head 27 is maintain as 27top 27 complexitywhen we implement stack using linked liststack in c using linked listsimulate the operations of a stack using a singly linked liststack using linkedlistwrite a program to implement stack using linked list stack linked list implementation cstack implementation linked list in cstack linked list implementation in clist stack popcreation of stack using linked listlinked list implementation of stackstack creation in c code using linked liststack and queue using linked list program in cpopping from a stack implementation as a singly linked list 2cwhere 27head 27 is maintain as 27top 27simple stack operations c with linked liststack using llstacks using linked list in c programming9stack linked list javastack using linked listc programming stack linked listsstack implementation using linkedlist javastack using linked list cstack with singly linked listimplement stack using linkedlistprint stack using linked list pythonimplementation of stack using linked liststack using linked list in c codescopeimplement stack using linked representationdisplay method for stack by linkedlist in c 2b 2bstack implementation using single linked list using cstack implementation using linked list in c 2b 2blinked stack implementationstack using linked list implementation in cstack with linked list program in cimplement stack using linked list n javac 2b 2b program to implement stack using linked liststack using singly linked list cimplementation array stack of nodelinked list implementation of stacks javaprogram code to implement a stack as a linked list stack and linkstack linked list implementation c 2b 2bimplement a link based dynamic stack using pointers c 2b 2bimplementing stack using linked listimplement stack using llstack using linked list javain the linked list implementation of the stackstack linear listimplement stack using link list stack in c 2b 2b using linked liststack in a linked liatimplementation of linked listwith stackstack linked list in c linked are implemented using linked listhow would you implement the push method for a stack implemented as a linked list 3fcode to implement stack using linked liststart assignment implement stack using linklis in cstack linked list implementation javacreate stack using linked listwhen we implement stack by linked listsample code for stack implementation using linked listlinked stackimplementing a stack using a linked list in cpseduo code menu driven c program to implement stack using linked list stack with linked list program in c 2b 2bstack operations using linkedlist in cwhy a list is not used for implementation of stackstack implementation using linked list javastack implementation using linked listlinked list stack cstack using linked list in c programwrite a program to implement stack using linked list and perform push and pop operations stack using linked list without pointer in ccreation of stack using linked list in chow stack is implemented using linked listwhich of the following statement is true about linkedlist i 29 an empty stack will have top 3d null ii 29 the last node is the bottom of stack and its link field is set to null iii 29the first node is considered to be at the topp of the stack c program to implement stack adt using a singly linked list necessary c functions to implement stack using linked listpeek in stack uing linked listpseudo code for menu driven c program to implement stack using linked list stack ked list implementationwrite a c program to implement stack using linked list representation pop function for stack with linked liststack with linked listlinked list stack implementation is emptyimplementation of stack adt using linked list in c 2b 2bstack from linked liststack using linked lisrtpush operation of a stack implemented with singly linked list 28sll 29 is nothing bupushing an element in linked listimllement stack using linked listimplement stack using the linked list2 write a program to implement stack using linked list in cstack implementation in c using linked liststack implementation using linked list and structure c 2b 2blinked list stack implementationstacks using singly linked listimplementing stack as a adt using linked listpopping from a stack implementation as a singly linked listcode to push item in stack using linked list program to implement stack operations using linked list stack implementation in c 2b 2b using linked listadd stack linked list c 2b 2bprogram to implement stack using linked list and perform all the operations of the stack in c for the linked list implementation of the stack 2c where are the pushes and pops performed 3fstack implementation using link in cwrite a program to create a singly linked list in lifo fashion in c 2b 2blinked list stackstzck by linked list declaraction1 write a c program for implementing stack using single link listhow to implement a linked list stack in c 2b 2blinked list in c with functions stack overflowprogram to implement a stack using linked list in cstack as linked lsit javastacks in linked list2 writea program to implement stack using linked list in coutput for stack using linked listlinked stack in cstack linked list implementationlinked list representaion of stack and queuestack using linkedlist in cstack using singly linked listsstack using llstackaslinedlist javawrite a c program 2f algorithm to implement stack adt using singly linked listlinked list implementation of stack using cpush and pop in linked liststack with linked list javastack using linked list in c including capacity in structstack implementation c 2b 2b linked listwrite a c program 2f algorithm to implement stack adt using singly linked list perform following operations 3aimprelement a stack using linked liststack c program using linked listif a singly linked list is used for a stack 2c push 28data 29 will be 3ac 2b 2b stack using linked list overflowimplement a stack using linked liststart assignment implement stack using linklisc 2b 2b stack implementation using linked listwrite a program to implement ation of list as stackstack as linked list cthe linked stack implementation in cstack can be implemented using link list in which nodes are added and removed from same end using single pointer stack push and pop using linked listimplementing stack using linked list in javastauck eith linked listhow to implement stack with linked liststack using linked list insertion at end and deletion at endimplementation of stack using linked list in cwhen we implement stack using linked list then insertion of node is donestack strings using linked lists in cstack in singly linkliststack implementation using linked list and structureimplementing stack in c 2b 2b using linked liststack using linked list program in cin which position a new value is inserted into the stack when implemented using linked listnode stack implementationsingly linked list as stackstack using sll which will take more timestack using linked list isfull methodpop from stack c 2b 2b linked listc program to implement stack using linked listwrite a c program to implement linked stack w a p for implementation of stack using arrays and link list showingh push 2cpop and display operationlinked stacks in cstack using linked list in c 2b 2bwrite a code for stack using linked listapplication of stack using linked listthe only way to implement a stack is using a linked list linked list implementation of stack and queue in clinked list as a stackit is possible to initialize a stack implemented as a linked list in o 281 29 timewrite a pseudocode to implement stack using linked list with all the basic operationsstack with linked list cimplement stack using link list in cstack implementation using linkstack using linked list stack implementation using linked list in cwhen we implememt stck by using liked listlinked list implementation of stacksstack implementation using linked list c 2b 2bimplementation of linkedlist with stackimplement a stack from llstacks using linked list in chow to add all elements of a linked list to a stack c 23stacks using linked listlinked list using stackwrite a function to pop an element and push an element into a stack using sllwrite a program that takes the details of a using structures create a linked stack to store the detailsjava linked list stackstacks using linked list cadd 2 integers stacks and pop in new empty stack linked liststack using linear linked list cstack using linked list algorithmstack que using link listlinked list in c peek and popisfull in stack by linedlistcreating a stack in c with linked listhow to make a linked list function as a stackwrite a c program implement stack adt using singly linked list write a c program using dynamic variables and pointers to construct a stack of integers using singly linked list to perform the following operations 3a a push b pop cc 2b 2b implement a link based dynamic stack using pointersimplementing all the stack operations using linked liststack using linked list string in cimplement a stack using linked list in cstack implement using linked listwap for linked list implement stack pop and push in cimplementing stack with linked list in cmenu driven program for stack in c using linked liststack using linked list and array2 writea program to implement stack using linked list sample code for stack implementation using linked list using java storing linked list in stack using structure in cc code to implement stack using linked listfunction in c to display contents of stack using linked liststack using linked list c 2b 2b programc stack implementation linked listretrun top element in linked list cwhen we implememt stck by using liked list insertiuoin of node isimplementing stack using linked list in cstack implementation sing lnked listit is more efficient to implement stack using linkedlist algorithm stack adt using linked list in cpsuedocode for stack using linked listlinked list implementation in cinsertion and deletion in stack is similar to that of linked listlink based dynamic stackimplementation of stack using linked list in c 2b 2b programsimplement a stack in c using a singly linked list which operation is permissible for linked stack 3f point insert at any position deletion from any position deletion from beginning insert at middle positionmake a ciruclar linked list and implement a stack javalinked implementation of stacklinked stack implemetationstack implemetation using linked listlinked based stack applicaitonsc program to implement stack using singly linked liststack using soubly linked list 2b cstack implementation using linked list in c