【数据结构】链栈
来源:互联网 发布:上海网络工程有限公司 编辑:程序博客网 时间:2024/06/03 18:42
Lstack.h
#pragma once#include<iostream>#include <stdlib.h>using namespace std;#define Elemtype inttypedef struct Nstack{ Elemtype data; Nstack* next;}Node;//节点typedef struct{ Node* top; int size;}Lstack;//栈结构void Init(Lstack *st){ st->top = (Node*)malloc(sizeof(Node)); if (!st->top) return; st->top->next= NULL; st->top = NULL; st->size = 0;}void Push(Lstack *st, Elemtype &x){ Node* p = (Node*)malloc(sizeof(Node)); if (!p) return ; p->data = x; p->next = st->top; st->top = p; st->size++;}bool Empty(Lstack *st){ return st->size==0;}void Pop(Lstack *st, Elemtype&e){ if (!Empty(st)) { cout << "栈为空,不能删除!" << endl; return; } Node*cur = st->top; e = cur->data; st->top = st->top->next; free(cur); st->size--;}void Gettop(Lstack *st,Elemtype &e){ if (!Empty(st)) { cout << "栈为空,没有栈顶元素!" << endl; return; } Node* temp = st->top; e = st->top->data; cout << "栈顶元素:" << e << endl;}void Length(Lstack *st){ cout << "栈长为:" << st->size << endl;}void Clear(Lstack *st){ if (Empty(st)) return; st->size = 0; Node* p=NULL; while (st->top!=NULL) { p = st->top; st->top = p->next; free(p); }}void Destroy(Lstack *st){ Clear(st); free(st); st = NULL;}void show(Lstack *st){ Node* p = st->top; while (p) { cout << p->data; p = p->next; cout << "-->"; } cout << "栈底"<< endl;}
mian.cpp
#include"Lstack.h"void main(){ Lstack q; Elemtype item; int select = 1; while (select) { cout << "**********************************" << endl; cout << "* [1] init [2] push *" << endl; cout << "* [3] gettop [4] length *" << endl; cout << "* [5] pop [6] clear *" << endl; cout << "* [7] destroy [8] show *" << endl; cout << "* [0] quit *" << endl; cout << "**********************************" << endl; cout << "please chose:>"; cin >> select; switch (select) { case 1: Init(&q); break; case 2: cout << "请输入要入栈的元素:"; cin >> item; Push(&q, item); break; case 3: Gettop(&q, item); break; case 4: Length(&q); break; case 5: Pop(&q, item); cout << "出栈元素为:" << item << endl; break; case 6: Clear(&q); break; case 7: Destroy(&q); cout<<"栈已经被销毁!不能再插入数据!"<<endl; return; case 8: show(&q); break; case 0: cout << "退出成功!" << endl; break; default: break; } }}
0 0
- 数据结构-链栈
- 【数据结构】链栈
- 《数据结构》链栈
- 数据结构-链栈
- 数据结构--链栈
- 数据结构-链栈
- 数据结构--链栈
- 【数据结构】链栈_LinkStack
- 数据结构与算法--链栈
- 数据结构_链栈
- 数据结构 - 链栈(C)
- 数据结构——链栈
- C语言数据结构-链栈
- 数据结构2链栈
- 数据结构-链栈(C语言)
- 数据结构 -- 链栈java实现
- C语言-数据结构-链栈
- C数据结构-链栈
- Unity3d 中按钮操作
- 我的BIOS之行(1)-CPU Introduction & Multi-Processor
- 黑马程序员----多线程2(安全问题、同步代码块、同步函数、死锁)
- MySQL各版本的区别
- linux vi常用命令(显示行号&&替换字符串)
- 【数据结构】链栈
- IT研发测试类笔试(第二篇
- Android环境搭建出现的种种问题(免费FQ)
- Linux的LVM(Logical Volume Manager)
- css hack
- 【java基础】Hashcode与equals
- 【数据结构】顺序栈
- 昨天的面试问题总结
- 层叠按钮,展开、收起(安卓)