Skip to content

Commit 4e8130a

Browse files
committed
Update linkedlist_stack.c
1 parent 8aa8b8c commit 4e8130a

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

linkedlist_stack.c

+26-28
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,29 @@ void getnode(node *n)
9494
//if there are no elements, it prints: no elements present
9595
void display(node n);
9696

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;
100102

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);
101106

102107
//pop function deletes the element at the specified position pos and returns the deleted data
103108
//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
105110
//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);
107112

108113
//the following is the main function
109114
int main()
110115
{
111116
//this is an implementation of stack using linked list
112117

113-
node a = NULL;
118+
stack s;
119+
s.head = NULL;
114120

115121
while (1)
116122
{
@@ -123,17 +129,17 @@ int main()
123129
data d = getdata();
124130

125131
//use the following to push
126-
push(&a, d);
132+
push(&s, d);
127133
}
128134
else if (n == 2)
129135
{
130-
display(a);
136+
display(s.head);
131137
}
132138
else if (n == 3)
133139
{
134140
//the following pops the top of stack and prints it
135141
printf("popped element : ");
136-
printdata(pop(&a));
142+
printdata(pop(&s));
137143
printf("\n\n");
138144
}
139145
else
@@ -143,24 +149,16 @@ int main()
143149
}
144150
}
145151

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)
157153
{
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;
162158
}
163159

160+
161+
//I have used the same display function as in linked list and haven't written a new one
164162
void display(node n)
165163
{
166164
//this prints a message for empty linked-list
@@ -179,19 +177,19 @@ void display(node n)
179177
printf("\n\n");
180178
}
181179

182-
data pop(node *n)
180+
data pop(stack *s)
183181
{
184182
//this checks for list empty condition
185-
if(*n == NULL)
183+
if(s->head == NULL)
186184
{
187185
//the list is empty, cant delete
188186
data d = getnull();
189187
return d;
190188
}
191189

192-
node temp = *n;
190+
node temp = s->head;
193191
data temp2 = temp->d;
194-
*n = (*n)->next;
192+
s->head = s->head->next;
195193
free(temp);
196194
return temp2;
197195
}

0 commit comments

Comments
 (0)