数据结构系列6 - 栈 - 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
/*************************************************************************
* File Name: stack.c
* Author: TyrantLucifer
* E-mail: TyrantLucifer@gmail.com
* Blog: https://tyrantlucifer.com
* Created Time: Wed 20 Jan 2021 12:24:26 PM CST
************************************************************************/
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int data;
struct Node* next;
}Node;

Node* initStack(){
Node* L = (Node*)malloc(sizeof(Node));
L->data = 0;
L->next = NULL;
return L;
}

void push(Node* L, int data){
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = L->next;
L->next = node;
L->data++;
}

int pop(Node* L){
if(L->data == 0){
return 0;
}
else{
Node* node = L->next;
int data = node->data;
free(node);
return data;
}
}

int isEmpty(Node* L){
if(L->data == 0 || L->next == NULL){
return 1;
}
else{
return 0;
}
}