-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacklistlog.c
93 lines (81 loc) · 1.14 KB
/
stacklistlog.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "stacklist.h"
void init(sk* s){
s->L = NULL;
s->top = NULL;
return;
}
int isempty(sk* s){
if(s->top == NULL)
return 1;
else
return 0;
}
void Push(sk* s, int d){
struct node *p ;
p = (struct node*)malloc(sizeof(struct node));
if(p){
p->data = d;
p->next = NULL;
}
else
return;
if(isempty(s)){
s->L = p;
s->top = p;
return;
}
else{
s->top->next = p;
s->top = s->top->next;
return;
}
}
int Pop(sk* s){
if(isempty(s)){
printf("stack is empty");
return -1;
}
else{
int tem = s->top->data;
struct node*p = s->L;
struct node*q = p->next;
if(q == NULL){
free(p);
s->L = NULL;
s->top = NULL;
return tem;
}
while(q != s->top){
p = p->next;
q = q->next;
}
s->top = p;
free(q);
s->top->next = NULL;
return tem;
}
}
int Peek(sk s){
if(isempty(&s)){
printf("stack is empty");
return -1;
}
else
return s.top->data ;
}
void print_stack(sk s){
struct node*p = s.L;
if(s.L == NULL){
printf("stack is empty");
return;
}
while(p){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return;
}