-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path线性表实现栈.cpp
More file actions
62 lines (56 loc) · 1.05 KB
/
Copy path线性表实现栈.cpp
File metadata and controls
62 lines (56 loc) · 1.05 KB
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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
Node* next;
} Node;
typedef Node* Nodep;
int Pop(Nodep head);
void Push(Nodep head, int data);
Nodep Create(int data);
int main(void) {
Nodep head = Create(-1);
Push(head, 3);
for (int i = 0; i < 10; i++) {
Push(head, i);
}
for (int i = 0; i < 6; i++) {
printf("%d ", Pop(head));
}
printf("\n");
for (int i = 0; i < 5; i++) {
printf("%d ", Pop(head));
}
return 0;
}
Nodep Create(int data) {
Nodep temp = (Nodep)malloc(sizeof(Node));
temp->data = data;
temp->next = NULL;
return temp;
}
int Pop(const Nodep head) {
static Nodep temp = NULL, last = NULL;
int data;
temp = last = head;
while (last->next != NULL) {
temp = last;
last = last->next;
}
if (temp == head) {
printf("\n空表无法弹出元素!");
return -1;
}
data = last->data;
free(last);
temp->next = NULL;
return data;
}
void Push(const Nodep head, const int data) {
static Nodep temp = NULL;
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = Create(data);
}