File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+
4+ struct Node
5+ {
6+ int data ;
7+ struct Node * next ;
8+ };
9+
10+ struct Node * head = NULL ;
11+
12+ void push (int value )
13+ {
14+ struct Node * n = (struct Node * )malloc (sizeof (struct Node * ));
15+ if (head == NULL )
16+ {
17+ head = n ;
18+ n -> data = value ;
19+ n -> next = NULL ;
20+ return ;
21+ }
22+
23+ n -> next = head ;
24+ n -> data = value ;
25+ head = n ;
26+ }
27+
28+ void pop ()
29+ {
30+ struct Node * temp = head ;
31+ if (head == NULL )
32+ {
33+ printf ("\nStack is empty.\n" );
34+ return ;
35+ }
36+
37+ head = head -> next ;
38+ temp -> next = NULL ;
39+ }
40+
41+ void peek ()
42+ {
43+ if (head == NULL )
44+ {
45+ printf ("\nStack is empty.\n" );
46+ return ;
47+ }
48+
49+ printf ("The top element is: %d\n" , head -> data );
50+ }
51+
52+ void display ()
53+ {
54+ struct Node * temp = head ;
55+ if (head == NULL )
56+ {
57+ printf ("\nStack is empty.\n" );
58+ return ;
59+ }
60+ printf ("\nElements of stack are:\n" );
61+ while (temp != NULL )
62+ {
63+ printf ("%d\n" , temp -> data );
64+ temp = temp -> next ;
65+ }
66+ }
67+
68+ int main ()
69+ {
70+ while (1 )
71+ {
72+ printf ("\n=======Menu=======\n" );
73+ printf ("1. Push\n" );
74+ printf ("2. Pop\n" );
75+ printf ("3. Peek\n" );
76+ printf ("4. Display\n" );
77+ printf ("5. Exit\n\n" );
78+ int c , n ;
79+ scanf ("%d" , & c );
80+ switch (c )
81+ {
82+ case 1 :
83+ printf ("Enter the element to be inserted: " );
84+ scanf ("%d" , & n );
85+ push (n );
86+ break ;
87+ case 2 :
88+ pop ();
89+ break ;
90+ case 3 :
91+ peek ();
92+ break ;
93+ case 4 :
94+ display ();
95+ break ;
96+ case 5 :
97+ exit (0 );
98+ break ;
99+
100+ default :
101+ printf ("Please select a valid option." );
102+ }
103+ }
104+
105+ return 0 ;
106+ }
You can’t perform that action at this time.
0 commit comments