前言
数据结构中顺序栈和链栈的C/C++语言描述实现模板,有详细的步骤解析及使用示例。
代码仓库
sqStack.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 94 95 96 97 98 99 100 101 102 103 104 105
| #include <iostream>
using namespace std;
#define MaxSize 100
typedef int ElemType;
typedef struct SqStack { ElemType data[MaxSize]; int topPointer; } SqStack;
void use_example(); void init(SqStack &sqStack); bool push(SqStack &sqStack, ElemType elem); bool pop(SqStack &sqStack, ElemType &elem);
int main() { use_example();
return 0; }
void use_example() { SqStack sqStack;
init(sqStack); cout << sqStack.topPointer << endl;
ElemType elem1 = 100; ElemType elem2 = 200; ElemType elem3 = 300;
push(sqStack, elem1); push(sqStack, elem2); push(sqStack, elem3);
ElemType elem4 = 0; ElemType elem5 = 0;
pop(sqStack, elem4); pop(sqStack, elem5);
cout << elem4 << endl; cout << elem5 << endl;
return; }
void init(SqStack &sqStack) { sqStack.topPointer = -1; }
bool push(SqStack &sqStack, ElemType elem) { if (sqStack.topPointer == MaxSize - 1) { return false; }
++sqStack.topPointer; sqStack.data[sqStack.topPointer] = elem;
return true; }
bool pop(SqStack &sqStack, ElemType &elem) { if (sqStack.topPointer == -1) { return false; }
elem = sqStack.data[sqStack.topPointer]; --sqStack.topPointer;
return true; }
|
linkStack.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 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <iostream>
using namespace std;
typedef int ElemType;
typedef struct LinkNode { ElemType data; struct LinkNode *next; } LinkNode;
void use_example(); void init(LinkNode *&head); void push(LinkNode *&head, ElemType elem); bool pop(LinkNode *&head, ElemType &elem);
int main() { use_example();
return 0; }
void use_example() { LinkNode *head;
init(head);
ElemType elem1 = 100; ElemType elem2 = 200; ElemType elem3 = 300;
push(head, elem1); push(head, elem2); push(head, elem3);
ElemType elem4 = 0; ElemType elem5 = 0;
pop(head, elem4); pop(head, elem5);
cout << elem4 << endl; cout << elem5 << endl;
return; }
void init(LinkNode *&head) { head = (LinkNode *)malloc(sizeof(LinkNode)); head->next = nullptr; }
void push(LinkNode *&head, ElemType elem) { LinkNode *newNode;
newNode = (LinkNode *)malloc(sizeof(LinkNode));
newNode->data = elem;
newNode->next = head->next; head->next = newNode; }
bool pop(LinkNode *&head, ElemType &elem) { if (head->next == nullptr) { return false; }
LinkNode *tempNode = nullptr;
elem = head->next->data;
tempNode = head->next; head->next = tempNode->next;
free(tempNode);
return true; }
|
总结
不同参考资料中,顺序栈和链栈的描述实现各不相同,但基本思想是一致的。作者使用规范的变量命名、提供详细的步骤解析及使用示例,应用C/C++语言将其整合成模板,以帮助理解记忆。
参考资料
- 《2023版数据结构高分笔记》主编:率辉
- 《2023年计算机组成原理考研复习指导》组编:王道论坛
- 《大话数据结构》作者:程杰
作者的话
- 感谢参考资料的作者/博主
- 作者:夜悊
- 版权所有,转载请注明出处,谢谢~
- 如果文章对你有帮助,请点个赞或加个粉丝吧,你的支持就是作者的动力~
- 文章在描述时有疑惑的地方,请留言,定会一一耐心讨论、解答
- 文章在认识上有错误的地方, 敬请批评指正
- 望读者们都能有所收获