@@ -94,23 +94,29 @@ void getnode(node *n)
94
94
//if there are no elements, it prints: no elements present
95
95
void display (node n );
96
96
97
- //push function inserts the element at start of the list
98
- //it is identical to insertfront function in linkedlist_pure
99
- void push (node * n , data x );
97
+ //the stack is defined as a structure named stack
98
+ typedef struct
99
+ {
100
+ node head ;
101
+ }stack ;
100
102
103
+ //push function inserts the element at start of the list
104
+ //it is similar to insertfront function in linkedlist_pure
105
+ void push (stack * s , data x );
101
106
102
107
//pop function deletes the element at the specified position pos and returns the deleted data
103
108
//if pos is negative the last element is deleted
104
- //it is identical to deletefront function in linkedlist_pure
109
+ //it is similar to deletefront function in linkedlist_pure
105
110
//if there are no elements in the linked list, function returns data with all values set to NULL or zero
106
- data pop (node * n );
111
+ data pop (stack * s );
107
112
108
113
//the following is the main function
109
114
int main ()
110
115
{
111
116
//this is an implementation of stack using linked list
112
117
113
- node a = NULL ;
118
+ stack s ;
119
+ s .head = NULL ;
114
120
115
121
while (1 )
116
122
{
@@ -123,17 +129,17 @@ int main()
123
129
data d = getdata ();
124
130
125
131
//use the following to push
126
- push (& a , d );
132
+ push (& s , d );
127
133
}
128
134
else if (n == 2 )
129
135
{
130
- display (a );
136
+ display (s . head );
131
137
}
132
138
else if (n == 3 )
133
139
{
134
140
//the following pops the top of stack and prints it
135
141
printf ("popped element : " );
136
- printdata (pop (& a ));
142
+ printdata (pop (& s ));
137
143
printf ("\n\n" );
138
144
}
139
145
else
@@ -143,24 +149,16 @@ int main()
143
149
}
144
150
}
145
151
146
- // NOTE: all the following functions other than display take node *n as one of the inputs
147
- // be careful while using node *n
148
- //
149
- // inside the function n represents the local variable
150
- // all changes to n affects only the local variable n, the linked list will not be affected
151
- //
152
- // **********************************************************************************
153
- // but *n is not a local variable, all changes made to *n will modify the linked list
154
- // **********************************************************************************
155
-
156
- void push (node * n , data x )
152
+ void push (stack * s , data x )
157
153
{
158
- node temp = * n ;
159
- getnode (n );
160
- insertdata (& (( * n ) -> d ) ,x );
161
- ( * n ) -> next = temp ;
154
+ node temp = s -> head ;
155
+ getnode (& ( s -> head ) );
156
+ insertdata (& (s -> head -> d ) ,x );
157
+ s -> head -> next = temp ;
162
158
}
163
159
160
+
161
+ //I have used the same display function as in linked list and haven't written a new one
164
162
void display (node n )
165
163
{
166
164
//this prints a message for empty linked-list
@@ -179,19 +177,19 @@ void display(node n)
179
177
printf ("\n\n" );
180
178
}
181
179
182
- data pop (node * n )
180
+ data pop (stack * s )
183
181
{
184
182
//this checks for list empty condition
185
- if (* n == NULL )
183
+ if (s -> head == NULL )
186
184
{
187
185
//the list is empty, cant delete
188
186
data d = getnull ();
189
187
return d ;
190
188
}
191
189
192
- node temp = * n ;
190
+ node temp = s -> head ;
193
191
data temp2 = temp -> d ;
194
- * n = ( * n ) -> next ;
192
+ s -> head = s -> head -> next ;
195
193
free (temp );
196
194
return temp2 ;
197
195
}
0 commit comments