-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
57 lines (46 loc) · 806 Bytes
/
stack.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
#include <stdlib.h>
#include <stdio.h>
typedef struct item item;
typedef item *itp;
typedef struct item {
int data;
itp next;
} item;
int is_empty(itp head){
if (head == NULL){
return 1;
}
return 0;
}
int size(itp head){
int cnt=0;
if (head != NULL){
while (head->next != NULL){
cnt ++;
head = head->next;
}
return cnt +1;
}
else{
return 0;
}
}
void push(itp *head,int dt){
itp p;
p = malloc(sizeof(item));
p->data = dt;
p->next = *head ;
*head = p;
}
int pop(itp *head){
int val = (*head)->data;
itp p = *head;
*head = (*head)->next;
free(p);
return val;
}
int peek(itp *head){
if (*head != NULL){
return (*head)->data;
}
}