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
#define STACK_H
#include <stdlib.h>
typedef int ElemType;
typedef struct Node {
struct Node* pre;
struct Node* next;
ElemType key;
}Node;
typedef struct Stack {
Node* Head;
Node* Top;
}Stack;
void (Stack*);
void push(Stack*, ElemType);
void print(Stack*);
void clear(Stack*);
void destroy(Stack*);
bool isEmpty(Stack*);
ElemType pop(Stack*);
Node* createNode(ElemType);
int size(Stack*);
#endif
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
// stack.c
#include <stdlib.h>
#include "stack.h"
#include "stdio.h"
void (Stack* S) {
S->Head = S->Top = (Node*)malloc(sizeof(Node));
if(S->Head) {
S->Head->pre = S->Head->next = NULL;
S->Head->key = 0;
}
else
exit(0);
}
void push(Stack* S, ElemType value) {
if(S->Head) {
Node* pNewNode = createNode(value);
S->Top->next = pNewNode;
pNewNode->pre = S->Top;
S->Top = pNewNode;
}
else
printf("Initialize stack first.n");
}
ElemType pop(Stack* S) {
if(S->Top != S->Head) {
Node* pNode = S->Top;
ElemType ret = pNode->key;
S->Top = pNode->pre;
S->Top->next = NULL;
free(pNode);
return ret;
}
else {
printf("error:underflown");
return -1;
}
}
Node* createNode(ElemType value) {
Node* pNewNode = (Node*)malloc(sizeof(Node));
if(pNewNode) {
pNewNode->key = value;
pNewNode->pre = pNewNode->next = NULL;
return pNewNode;
}
else
exit(0);
}
bool isEmpty(Stack* S) {
return S->Head == S->Top;
}
void print(Stack* S) {
if(S->Head) {
Node* pNode = S->Head->next;
while(pNode) {
printf("%d ",pNode->key);
pNode = pNode->next;
}
}
}
void clear(Stack* S) {
while(S->Top != S->Head) {
Node* pNode = S->Top->pre;
pNode->next = NULL;
free(S->Top);
S->Top = pNode;
}
}
void destroy(Stack* S) {
clear(S);
free(S->Head);
}
int size(Stack* S) {
Node* pNode = S->Top;
int i=0;
while(pNode != S->Head) {
++i;
pNode = pNode->pre;
}
return i;
}

队列

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
// queue.h
#ifndef QUEUE_H
#define QUEUE_H
typedef int ElemType;
typedef struct Node {
struct Node* next;
ElemType key;
}Node;
typedef struct Queue {
Node* Head;
Node* Tail;
}Queue;
void initQueue(Queue*);
void push(Queue*, ElemType);
void pop(Queue*);
void clear(Queue*);
void destroy(Queue*);
void print(Queue*);
bool isEmpty(Queue*);
int size(Queue*);
ElemType front(Queue*);
ElemType back(Queue*);
Node* createNode(ElemType);
#endif
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
94
95
96
// queue.c
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
void initQueue(Queue* Q) {
Q->Head = Q->Tail = (Node*)malloc(sizeof(Node));
if(Q->Head) {
Q->Head->next = NULL;
Q->Head->key = 0;
}
else
exit(0);
}
void push(Queue* Q, ElemType value) {
if(Q->Head) {
Node* pNewNode = createNode(value);
Q->Tail->next = pNewNode;
Q->Tail = pNewNode;
}
else
printf("Initialize queue first.n");
}
void pop(Queue* Q) {
if(Q->Head != Q->Tail) {
Node* pNode = Q->Head->next;
Q->Head->next = pNode->next;
if(pNode == Q->Tail)
Q->Tail = Q->Head;
free(pNode);
}
}
void clear(Queue* Q) {
while(Q->Head != Q->Tail)
pop(Q);
}
void destroy(Queue* Q) {
clear(Q);
free(Q->Head);
}
bool isEmpty(Queue* Q) {
return Q->Head == Q->Tail;
}
int size(Queue* Q) {
if(Q->Head) {
int ret = 0;
Node* pNode = Q->Head;
while (pNode != Q->Tail) {
++ret;
pNode = pNode->next;
}
return ret;
}
else
exit(0);
}
ElemType front(Queue* Q) {
if(Q->Head->next)
return Q->Head->next->key;
else
return 0;
}
ElemType back(Queue* Q) {
return Q->Tail->key;
}
Node* createNode(ElemType value) {
Node* pNewNode = (Node*)malloc(sizeof(Node));
if (pNewNode) {
pNewNode->next = NULL;
pNewNode->key = value;
return pNewNode;
}
else
exit(0);
}
void print(Queue* Q) {
if(Q->Head) {
Node* pNode = Q->Head;
while(pNode != Q->Tail) {
printf("%d ",pNode->next->key);
pNode = pNode->next;
}
}
}