-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkStack.cpp
71 lines (60 loc) · 1.26 KB
/
LinkStack.cpp
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
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct LStack{
int num;
LStack *next;
}LStack;
LStack *InitStack(LStack *head){
head = (LStack *)malloc(sizeof(LStack));
head->next = NULL;
return head;
}
int IsNull(LStack *lstack){
if(lstack->next == NULL){
return 1;
}else{
return 0;
}
}
void PushElem(LStack *lstack , int num){
LStack *temp = (LStack*)malloc(sizeof(LStack));
temp->num = num;
temp->next = lstack->next;
lstack->next = temp;
}
void PopElem(LStack *lstack){
if(IsNull != 0){
LStack *temp;
temp = lstack->next;//命名删除数,便于free
lstack->next = temp->next;
free(temp);
}else{
cout << "栈空" <<endl;
}
}
void PrintStack(LStack *lstack){
while(lstack->next != NULL){
lstack = lstack->next;
cout << lstack->num <<endl;
}
}
int main(){
LStack *head;
LStack *lstack;
lstack = InitStack(head);
cout <<"插入数的个数" <<endl;
int n , num;
cin >>n;
for(int i = 0 ; i< n ;i ++){
cin >>num;
PushElem(lstack , num);
}
cout <<"删除的个数" <<endl;
cin >>n;
for(int i = 0 ; i < n ; i ++){
PopElem(lstack);
}
PrintStack(lstack);
return 0;
}