File tree 1 file changed +154
-0
lines changed
1 file changed +154
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Program : Stack implementation using Linked List in C.
3
+ Author : Aman Kumar
4
+ */
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+
9
+ struct node {
10
+ int data ;
11
+ struct node * next ;
12
+ }* top = NULL ,* newn , * x ;
13
+
14
+ void push ();
15
+ void pop ();
16
+ void peek ();
17
+ void search ();
18
+ void min ();
19
+ void max ();
20
+ void display ();
21
+
22
+ int main ()
23
+ {
24
+ int ch ,x ;
25
+ while (1 )
26
+ {
27
+ printf ("Please select an option: \n 1. Push \t\t\t 2. Pop \n 3. Peek \t\t\t 4. Search \n 5. Minimum \t\t\t 6. Maximum \n 7. Display \t\t\t 8. Exit\n\n" );
28
+ scanf ("%d" ,& ch );
29
+
30
+ switch (ch )
31
+ {
32
+ case 1 :
33
+ push ();
34
+ break ;
35
+ case 2 :
36
+ pop ();
37
+ break ;
38
+ case 3 :
39
+ peek ();
40
+ break ;
41
+ case 4 :
42
+ search ();
43
+ break ;
44
+ case 5 :
45
+ min ();
46
+ break ;
47
+ case 6 :
48
+ max ();
49
+ break ;
50
+ case 7 :
51
+ display ();
52
+ break ;
53
+ case 8 :
54
+ exit (0 );
55
+ break ;
56
+ default :
57
+ printf ("Invalid Input \n" );
58
+ break ;
59
+ }
60
+ }
61
+ return 0 ;
62
+ }
63
+
64
+ void push ()
65
+ {
66
+ newn = (struct node * )malloc (sizeof (struct node ));
67
+ printf ("Enter the element : \n" );
68
+ scanf ("%d" ,& newn -> data );
69
+
70
+ if (top == NULL )
71
+ newn -> next = NULL ;
72
+ else
73
+ newn -> next = top ;
74
+ top = newn ;
75
+ printf ("Element(%d) pushed in stack successful\n" ,newn -> data );
76
+ }
77
+
78
+ void pop ()
79
+ {
80
+ x = top ;
81
+ if (top == NULL )
82
+ printf ("Stack is Underflow. \n" );
83
+ else
84
+ top = x -> next ;
85
+ }
86
+
87
+ void peek ()
88
+ {
89
+ x = top ;
90
+ printf ("Top element is : \t %d \n" ,top -> data );
91
+ }
92
+
93
+ void search ()
94
+ {
95
+ int n ,k = 0 ;
96
+ x = top ;
97
+ printf ("Enter the element which you want to search: \n" );
98
+ scanf ("%d" ,& n );
99
+ while (x != NULL )
100
+ {
101
+ if (x -> data == n )
102
+ k = 1 ;
103
+ x = x -> next ;
104
+ }
105
+ (k == 1 )?printf ("Element(%d) found in stack\n" ,n ):printf ("Element(%d) is not in stack\n" ,n );
106
+ }
107
+
108
+ void min ()
109
+ {
110
+ x = top ;
111
+ int temp ;
112
+ int k = x -> data ;
113
+ while (x != NULL )
114
+ {
115
+ if (x -> data < k )
116
+ {
117
+ temp = x -> data ;
118
+ x -> data = k ;
119
+ k = temp ;
120
+ }
121
+ x = x -> next ;
122
+ }
123
+ printf ("Minimum element in stack is : %d\n" ,k );
124
+ }
125
+
126
+ void max ()
127
+ {
128
+ x = top ;
129
+ int temp ;
130
+ int k = x -> data ;
131
+ while (x != NULL )
132
+ {
133
+ if (x -> data > k )
134
+ {
135
+ temp = x -> data ;
136
+ x -> data = k ;
137
+ k = temp ;
138
+ }
139
+ x = x -> next ;
140
+ }
141
+ printf ("Maximum element in stack is: %d\n" ,k );
142
+ }
143
+
144
+ void display ()
145
+ {
146
+ x = top ;
147
+ printf ("Printing elements of stack: \n" );
148
+ while (x != NULL )
149
+ {
150
+ printf ("%d\n" ,x -> data );
151
+ x = x -> next ;
152
+ }
153
+ }
154
+
You can’t perform that action at this time.
0 commit comments