实验三、链栈
2017-10-16 16:47
211 查看
用单链表的头部作为栈顶
入栈操作:申请一个结点,将结点插入栈顶
出栈操作:暂存栈顶元素,将栈顶结点摘链
入栈操作:申请一个结点,将结点插入栈顶
出栈操作:暂存栈顶元素,将栈顶结点摘链
#include using namespace std; struct Node { int data; Node *next; }; class linkstack { public: linkstack() { top = NULL; } ~linkstack() {}; void push(int x); int pop(); void print(); int gettop() { if (top != NULL)return top->data; } int empty() { return(top == NULL) ? 1 : 0; } private: Node *top; }; void linkstack::push(int x) { Node *s; s = new Node; s->data = x; s->next = top; top = s; } int linkstack::pop() { if (top == NULL)throw"下溢"; int x = top->data; Node *p; p = top; top = top->next; delete p; return x; } void linkstack::print() { Node *p; p = top; while (p != NULL) { cout << p->data; p = p->next; } } int main() { linkstack a; a.push(6); a.print(); }
相关文章推荐
- 实验4:栈和队列的基本操作实现及其应用之《链栈》
- 实验三 链栈(c++)
- 实验3_2 实现链栈各种基本运算的算法
- 实验四 顺序栈、链栈(JavaScript)
- 实验4:栈和队列的基本操作实现及其应用——链栈
- 实验四:顺序栈和链栈
- 实验四 链栈
- 简单迷宫实验(链栈)
- 实验三 顺序栈和链栈
- (实验三)《数据结构》第三章 顺序栈与链栈的验证
- 实验4:栈和队列的基本操作实现及其应用之《链栈》
- 顺序栈和链栈的实现(实验3.1)
- 实验之链栈
- 链栈基本运算(实验题3.2)
- 实验4:栈和队列的基本操作实现及其应用之《链栈》
- 数据结构|链栈的实现(实验3.2)
- 数据结构第三章实验2.实现链栈的各种基本运算
- Linux多任务编程(二)---fork()函数及其基础实验
- 第四章实验:统计文本单词的个数
- C++上机实验3-4