adplus-dvertising

C program to implement Stack using Linked List

frame-decoration

C program to implement Stack using Linked List

Problem Description

This C Program implement a stack using linked list. Stack is a Last in, First out (LIFO) abstract data type and linear data structure. Linked list is a data structure consisting of a group of nodes which together represent a sequence.

C program to implement Stack using Linked List - Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
                 
  /*
 * C Program to Implement a Stack using Linked List
 */
#include <stdio.h>
#include <stdlib.h>
 
struct node
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;
 
int top_element();
void push(int data);
void pop();
void display_stack();
void destroy();
void stack_count();
void create();
 
int count = 0;
 
void main()
{
    int no, c, e;
 
    printf("\n 1 - Push operation");
    printf("\n 2 - Pop operation");
    printf("\n 3 - Top of Stack");
    printf("\n 4 - Exit");
    printf("\n 5 - Display");
    printf("\n 6 - Stack Count");
    printf("\n 7 - Destroy stack");
 
    create();
 
    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &c);
 
        switch (c)
        {
        case 1:
            printf("Enter data : ");
            scanf("%d", &no);
            push(no);
            break;
        case 2:
            pop();
            break;
        case 3:
            if (top == NULL)
                printf("No elements in stack");
            else
            {
                e = top_element();
                printf("\n Top element : %d", e);
            }
            break;
        case 4:
            exit(0);
        case 5:
            display_stack();
            break;
        case 6:
            stack_count();
            break;
        case 7:
            destroy();
            break;
        default :
            printf(" Wrong choice, Please enter correct choice  ");
            break;
        }
    }
}
 
/* Create empty stack */
void create()
{
    top = NULL;
}
 
/* Count stack elements */
void stack_count()
{
    printf("\n No. of elements in stack : %d", count);
}
 
/* Push data into stack */
void push(int data)
{
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        top->info = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->ptr = top;
        temp->info = data;
        top = temp;
    }
    count++;
}
 
/* Display stack elements */
void display_stack()
{
    top1 = top;
 
    if (top1 == NULL)
    {
        printf("Stack is empty");
        return;
    }
 
    while (top1 != NULL)
    {
        printf("%d ", top1->info);
        top1 = top1->ptr;
    }
 }
 
/* Pop Operation on stack */
void pop()
{
    top1 = top;
 
    if (top1 == NULL)
    {
        printf("\n Stack is empty.");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;
    count--;
}
 
/* Return top element */
int top_element()
{
    return(top->info);
}
 
 
/* Destroy entire stack */
void destroy()
{
    top1 = top;
 
    while (top1 != NULL)
    {
        top1 = top->ptr;
        free(top);
        top = top1;
        top1 = top1->ptr;
    }
    free(top1);
    top = NULL;
 
    printf("\n All stack elements destroyed");
    count = 0;
}
 
     

Program Output

 1 - Push operation
 2 - Pop operation
 3 - Top of Stack
 4 - Exit
 5 - Display
 6 - Stack Count
 7 - Destroy stack
 Enter choice : 1
Enter data : 10

 Enter choice : 1
Enter data : 20

 Enter choice : 1
Enter data : 30

 Enter choice : 1
Enter data : 40

 Enter choice : 1
Enter data : 50

 Enter choice : 2

 Popped value : 50
 Enter choice : 3

 Top element : 40
 Enter choice : 5
40 30 20 10
 Enter choice : 6

 No. of elements in stack : 4
 Enter choice : 7

 All stack elements destroyed
 Enter choice :

Program Explanation

1. This C program consist of seven functions beside the main function. These functions are for push and pop operations, to get top element of stack, to display the stack, to count the number of elements in stack, to destroy the stack and to exit from the program.

2. The selection to perform specific function is done through the use of switch statement.

3. Function create simply creates a empty node with top = null.

4. The push function first allocate the memory for a node and assign the number to the info field of list. The pointers are updated using a temp node.