-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2024.5.6.exercise.c
64 lines (59 loc) · 978 Bytes
/
2024.5.6.exercise.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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int datatype;
typedef struct stack
{
datatype data;
struct stack* next;
}stack;
stack* creatnode()
{
stack* newnode = (stack*)malloc(sizeof(stack));
if (newnode == NULL)
{
printf("NULL\n");
return NULL;
}
printf("请输入入栈的元素\n");
datatype element;
scanf("%d", &element);
newnode->data = element;
newnode->next = NULL;
return newnode;
}
void push(stack** s)
{
stack* newnode = creatnode();
newnode->next = *s;
*s = newnode;
}
datatype pop(stack** s)
{
datatype poped;
stack* temp;
temp = *s;
poped = temp->data;
*s = temp->next;
free(temp);
temp = NULL;
return poped;
}
int main()
{
stack* s = NULL;
printf("有多少元素要入栈\n");
int n;
scanf("%d", &n);
int i = 0;
for (i = 0; i < n; i++)
{
push(&s);
}
for (i = 0; i < n; i++)
{
printf("%d ", pop(&s));
}
return 0;
}