-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_linkedlist_optimal.c
57 lines (45 loc) · 977 Bytes
/
stack_linkedlist_optimal.c
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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define EMPTY (-1)
#define INT_MIN (-2147483648)
#define STACK_EMPTY INT_MIN
typedef struct node{
int value;
struct node *next;
} node;
bool push(node **head, int value){
node *newnode = malloc(sizeof(node));
newnode->value = value;
newnode->next = (*head);
(*head) = newnode;
return true;
}
int pop(node **head){
if((*head) == NULL)
return STACK_EMPTY;
int result = (*head)->value;
node *temp = (*head);
(*head) = (*head)->next;
free(temp);
return result;
}
int main(){
node *head1 = NULL;
node *head2 = NULL;
node *head3 = NULL;
push(&head1, 56);
push(&head1, 13);
push(&head1, 12);
push(&head2, 17);
push(&head2, 19);
push(&head2, 28);
push(&head3, 26);
push(&head3, 24);
push(&head3, 32);
int t;
while((t=pop(&head2))!=STACK_EMPTY){
printf("t = %d\n", t);
}
return 0;
}