用链表实现栈C++实现源码
来源:互联网 发布:佛山市2015年经济数据 编辑:程序博客网 时间:2024/06/05 05:42
头文件:
//Stack.hstruct node;typedef struct node *Stack;Stack CreateStack();void Push( int, Stack );void PrintStack( Stack );void Pop(Stack);int StackLength( Stack );
源文件:
//Stack.cpp#include "Stack.h"#include <iostream>using namespace std;struct node{int data;node *next;};Stack CreateStack(){Stack S;S = (node *)malloc(sizeof(node));if( S == NULL )cout<<"内存分配失败!"<<endl;S->next = NULL;return S;}void Push( int x, Stack S ){node *tmp;tmp = (node *)malloc(sizeof(node));if( tmp == NULL )cout << "内存分配失败!"<<endl;else{tmp->data = x;tmp->next = S->next;S->next = tmp;}}void Pop(Stack S){node *top;if( S->next == NULL )cout << "Pop失败:空栈!"<<endl;else{top = S->next;S->next = S->next->next;free(top);}}int StackLength( Stack S){int n = 0;while( S->next != NULL ){S = S->next;n++;}return n;}void PrintStack(Stack S){cout << "Stack: " << endl;S = S->next; //第一个S,S为空while( S != NULL ){//cout << "S!=NULL"<<endl;cout << S->data ;S = S->next;}cout << endl;}
测试文件:
//main.cpp#include "Stack.h"#include <iostream>using namespace std;void main(){int n;int x;Stack S = CreateStack();cout << "输入n:";cin >> n;while( n-- ){cout <<" 输入数字:";cin >> x;Push( x, S);}PrintStack(S);int m;cout << "输入Pop个数m:";cin >> m;while(m--){Pop(S);}PrintStack(S);cout << "栈的长度:" << StackLength(S) << endl;PrintStack(S);system("PAUSE");}
- [C++]Vector源码实现
- 栈的C语言实现源码
- 用链表实现栈C++实现源码
- c语言实现ping源码
- CAS锁c源码实现
- [150421][C++]数据结构复习——栈实现源码
- 数据结构---栈实现(源码)
- C#、ASP.NET 加密算法&实现源码
- 红黑树的C实现完整源码
- 红黑树c源码实现与剖析
- Linux下c语言实现ffmp源码
- 红黑树----红黑树的C实现完整源码
- 二叉排序树C实现(含完整源码)
- 二叉排序树C实现(含完整源码)
- 二叉排序树C实现(含完整源码)
- UDP flood 原理及源码 C实现
- XXX定律C/C++源码实现
- Is It Symmetric C/C++源码实现
- C++ win32 开发 GDIPlus 贴图
- java 批量重命名文件
- VC里的"#define new DEBUG_NEW"
- HDU1542--Atlantis
- 牢记25个最佳的SSH命令
- 用链表实现栈C++实现源码
- 初爱
- Delphi CxGrid 汇总(2)
- 混杂设备
- jsp进度条
- sort()函数
- oralce存储过程的基本语法
- oracle rpad()函数
- objective 取消执行的延迟函数 -- 线程