您的位置:首页 > 其它

4.3 堆栈的链表实现

2013-08-22 13:42 141 查看
链表堆栈的简单操作:

链表创建一个空栈 满足两点:

1. 建立一个头结点。

2.设置next指针为NULL

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

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

/***********************************************************/

// 程序名称:StackOfLink.cpp

// 程序目的:设计一个链表实现的栈的程序

// 程序来源:数据结构与算法分析(C语言描述) P-46

// 日期:2013-8-22 13:34:46

/***********************************************************/

#include <stdio.h>

#include <stdlib.h>

#define Error( str ) FatalError( str )

#define FatalError( str ) fprintf( stderr, "%s\n", str ), exit( 1 )

typedef int ElementType;

typedef struct Node

{

ElementType Element;

struct Node* nextNode;

} *ptrToNode;

typedef ptrToNode Stack;

int IsEmpty(Stack s);

Stack CreateStack(void);

void DisposeStack(Stack s);

void MakeEmpty(Stack s);

void Push(ElementType x, Stack s);

void Pop(Stack s);

ElementType Top(Stack s);

void PrintStack(Stack s);

/************************************************************************/

// 主程序

/************************************************************************/

int main(void)

{

Stack linkStack;

int maxElement = 5;

linkStack = CreateStack();

if (IsEmpty(linkStack))

printf("创建了空栈!\n");

printf("将元素压入栈中, 依次为:\n");

for (int i=0; i<maxElement; i++)

Push(i+3, linkStack); // 压入栈

PrintStack(linkStack); // 打印链表元素

printf("栈顶元素为:%d\n", Top(linkStack));

Pop(linkStack);

printf("执行一次弹出后,栈顶元素变为:%d\n", Top(linkStack));

DisposeStack(linkStack); // 释放栈中内存

return 0;

}

/************************************************************************/

// 栈是否为空

/************************************************************************/

int IsEmpty(Stack s)

{

return NULL == s->nextNode;

}

/************************************************************************/

// 栈的创建

/************************************************************************/

Stack CreateStack(void)

{

Stack s;

s = (Stack)malloc(sizeof(struct Node));

if (NULL == s)

FatalError("空间不足,栈内存分配失败!");

s->nextNode = NULL;

MakeEmpty(s);

return s;

}

/************************************************************************/

// 栈的释放

/************************************************************************/

void DisposeStack(Stack s)

{

MakeEmpty(s);

free(s);

}

/************************************************************************/

// 创建一个空栈

/************************************************************************/

void MakeEmpty(Stack s)

{

if( s == NULL )

Error( "必须先使用CreateStack创建栈" );

else

while( !IsEmpty(s) )

Pop(s);

}

/************************************************************************/

// 进栈(元素压入栈中)

/************************************************************************/

void Push(ElementType x, Stack s)

{

ptrToNode tempCell;

tempCell = (ptrToNode)malloc(sizeof(struct Node));

if (NULL == tempCell)

FatalError("空间不足,元素压入失败!\n");

else

{

tempCell->Element = x;

tempCell->nextNode = s->nextNode;

s->nextNode = tempCell;

}

}

/************************************************************************/

// 出栈(从栈中弹出元素)

/************************************************************************/

void Pop(Stack s)

{

ptrToNode firstCell;

if (IsEmpty(s))

Error("栈为空!");

else

{

firstCell = s->nextNode;

s->nextNode = s->nextNode->nextNode;

free(firstCell);

}

}

/************************************************************************/

// 获得栈顶值

/************************************************************************/

ElementType Top(Stack s)

{

if (!IsEmpty(s))

return s->nextNode->Element;

Error("栈顶为空, 程序退出!");

return 0;

}

/************************************************************************/

// 打印栈中元素

/************************************************************************/

void PrintStack(Stack s)

{

ptrToNode firstNode, tempNode;

firstNode = s;

int firstOne = 0;

printf("(top->bottom): ");

while(!IsEmpty(s))

{

printf("%d ", firstNode->nextNode->Element);

if (++firstOne == 1)

tempNode = firstNode->nextNode; // 保留起初位置

firstNode->nextNode = firstNode->nextNode->nextNode;

}

firstNode->nextNode = tempNode; // 还原原来的栈

return;

}

输出结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: