-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path链栈.cpp
93 lines (92 loc) · 1.28 KB
/
链栈.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
#include<malloc.h>
typedef struct stack
{
struct stack* next;
int data;
}stacknode,*listack;
bool initlistack(listack& s)
{
s= NULL;
return true;
}
bool gettop(listack s,int &x)
{
if (s == NULL)
{
return false;
}
x = s->data;
return true;
}
bool push(listack &s, int x)
{
stacknode* p = (stacknode*)malloc(sizeof(stacknode));
p->data = x;
p->next = s;
s = p;
return true;
}
int pop(listack& s)
{
int x;
if (s == NULL)
{
return false;
}
x = s->data;
stacknode* p = (stacknode*)malloc(sizeof(stacknode));
p = s;
s = s->next;
free(p);
return x;
}
bool empty(listack s)
{
if (s == NULL)
{
return false;
}
return true;
}
int lengthstack(listack s)
{
stacknode* p = s;
int len = 0;
while (p!=NULL)
{
len++;
p = p->next;
}
return len;
}
void converse(int num, int mode)
{
int temp = 0;
listack s;
initlistack(s);
while (num != 0)
{
temp = num % mode;
num = (num - temp) / mode;
push(s,temp);
}
int len = lengthstack(s);
for (int i = 0; i < len; i++)
{
int e = pop(s);
printf("%d",e);
}
printf("\n");
}
int main(void)
{
listack s;
initlistack(s);
push(s,10);
push(s,20);
int x = pop(s);
printf("%d",x);
converse(159,8);
return 0;
}