数据结构之链栈
2015-10-07 09:13
447 查看
#include <iostream>
#include <cstdlib>
#include <string>
#define OK 1
#define ERROR 0
using namespace std;
typedef struct SNode //结点定义
{
int date;
struct SNode *next;
}SNode;
typedef struct //结构定义
{
SNode *top;
int Length;
}Lstack;
void CreateStack(Lstack*S) //建立空栈
{
cout << "已建立空栈,您可选择显示!" << endl;
S->top = NULL;
}
void DestoryStack(Lstack *S)//链栈的销毁
{
free(S);
cout << "栈已销毁,您可以选择显示!" << endl;
}
int StackEmpty(Lstack*S) //判断链栈是否为空
{
if (S->top = NULL)
{
cout << "为空栈" << endl;
return OK;
}
else
{
cout << "不为空栈" << endl;
return ERROR;
}
}
int StackLength(Lstack*S) //计算链栈长度
{
int i = 0;
//if (StackEmpty(S))
//cout << "栈为空无元素可显示!!!" << endl;
//else{
while (S->top != NULL)
{
S->top = S->top->next;
i++;
}
//}
return i;
}
void pushStack(Lstack*S, int X) //进栈
{
SNode *p;
p = (SNode*)malloc(sizeof(SNode));
p->date = X;
p->next = S->top;
S->top = p;
}
void POPstack(Lstack*S, int n) //出栈
{
SNode *p;
p = S->top;
if (n != p->date)
cout << "这个不是栈顶元素!!!" << endl;
n = p->date;
S->top = p->next;
free(p);
}
int Gettop(Lstack*S) //取栈顶元素
{
//if (StackEmpty(S)) return ERROR;
//else
cout <<"栈顶元素为: "<< S->top->date << " ";
return OK;
}
int Clearstack(Lstack*S) //清空栈
{
S->top = NULL;
cout << "已清空栈,您可以选择一下显示!" << endl;
return 0;
}
void display(Lstack *S)//显示
{
cout << "链栈中元素如下:" << endl;
SNode *p;
p = S->top;
while (p!= NULL)
{
cout<< "【"<< p->date<<"】";
p = p->next;
cout << endl;
}
}
int main_menu()
{
int n;
do
{
//system("cls");
cout << "********链栈的相关操作**********" << endl;
cout << "********************************" << endl;
cout << "***(1)构造空栈***(2)销毁该栈****" << endl;
cout << "********************************" << endl;
cout << "***(3)置为空栈***(4)栈元素个数**" << endl;
cout << "********************************" << endl;
cout << "***(5)判断栈空***(6)取栈顶元素**" << endl;
cout << "********************************" << endl;
cout << "***(7)进栈*******(8)出栈********" << endl;
cout << "********************************" << endl;
cout << "*********(9)链栈的显示**********" << endl;
cout << "请选择菜单1—9" << endl;
cin >> n;
} while (n<1 || n>9);
return n;
}
int main()
{
Lstack *S;
S = (Lstack*)malloc(sizeof(Lstack));
CreateStack(S);
SNode *p;
p = (SNode*)malloc(sizeof(SNode));
for (;;)
{
switch (main_menu())
{
case 1:
{
CreateStack(S);
system("pause");
system("cls");
}
case 2:
{
DestoryStack(S);
system("pause");
system("cls");
break;
}
case 3:
{
Clearstack(S);
system("pause");
system("cls");
cout << "链栈长度为:" << endl;
cout << StackLength(S) << " ";
system("pause");
break;
}
case 4:
{
cout << "栈的长度如下:" << endl;
cout << StackLength(S) << " ";
system("pause");
system("cls");
break;
}
case 5:
{
StackEmpty(S);
system("pause");
system("cls");
break;
}
case 6:
{
Gettop(S);
system("pause");
system("cls");
break;
}
case 7:
{
int N, M;
cout << "请输入链栈中的元素个数:" << endl;
cin >> N;
cout << "请输入链栈中的元素:" << endl;
for (int i = 1; i <=N; i++)
{
cin >> M;
pushStack(S, M);
}
system("pause");
system("cls");
break;
}
case 8:
{
int M;
cout << "请输入要出栈的栈顶元素:" << endl;
cin >> M;
POPstack(S, M);
system("pause");
system("cls");
break;
}
case 9:
{
display(S);
system("pause");
system("cls");
break;
}
default:
break;
}
}
system("pause");
return 0;
}
#include <cstdlib>
#include <string>
#define OK 1
#define ERROR 0
using namespace std;
typedef struct SNode //结点定义
{
int date;
struct SNode *next;
}SNode;
typedef struct //结构定义
{
SNode *top;
int Length;
}Lstack;
void CreateStack(Lstack*S) //建立空栈
{
cout << "已建立空栈,您可选择显示!" << endl;
S->top = NULL;
}
void DestoryStack(Lstack *S)//链栈的销毁
{
free(S);
cout << "栈已销毁,您可以选择显示!" << endl;
}
int StackEmpty(Lstack*S) //判断链栈是否为空
{
if (S->top = NULL)
{
cout << "为空栈" << endl;
return OK;
}
else
{
cout << "不为空栈" << endl;
return ERROR;
}
}
int StackLength(Lstack*S) //计算链栈长度
{
int i = 0;
//if (StackEmpty(S))
//cout << "栈为空无元素可显示!!!" << endl;
//else{
while (S->top != NULL)
{
S->top = S->top->next;
i++;
}
//}
return i;
}
void pushStack(Lstack*S, int X) //进栈
{
SNode *p;
p = (SNode*)malloc(sizeof(SNode));
p->date = X;
p->next = S->top;
S->top = p;
}
void POPstack(Lstack*S, int n) //出栈
{
SNode *p;
p = S->top;
if (n != p->date)
cout << "这个不是栈顶元素!!!" << endl;
n = p->date;
S->top = p->next;
free(p);
}
int Gettop(Lstack*S) //取栈顶元素
{
//if (StackEmpty(S)) return ERROR;
//else
cout <<"栈顶元素为: "<< S->top->date << " ";
return OK;
}
int Clearstack(Lstack*S) //清空栈
{
S->top = NULL;
cout << "已清空栈,您可以选择一下显示!" << endl;
return 0;
}
void display(Lstack *S)//显示
{
cout << "链栈中元素如下:" << endl;
SNode *p;
p = S->top;
while (p!= NULL)
{
cout<< "【"<< p->date<<"】";
p = p->next;
cout << endl;
}
}
int main_menu()
{
int n;
do
{
//system("cls");
cout << "********链栈的相关操作**********" << endl;
cout << "********************************" << endl;
cout << "***(1)构造空栈***(2)销毁该栈****" << endl;
cout << "********************************" << endl;
cout << "***(3)置为空栈***(4)栈元素个数**" << endl;
cout << "********************************" << endl;
cout << "***(5)判断栈空***(6)取栈顶元素**" << endl;
cout << "********************************" << endl;
cout << "***(7)进栈*******(8)出栈********" << endl;
cout << "********************************" << endl;
cout << "*********(9)链栈的显示**********" << endl;
cout << "请选择菜单1—9" << endl;
cin >> n;
} while (n<1 || n>9);
return n;
}
int main()
{
Lstack *S;
S = (Lstack*)malloc(sizeof(Lstack));
CreateStack(S);
SNode *p;
p = (SNode*)malloc(sizeof(SNode));
for (;;)
{
switch (main_menu())
{
case 1:
{
CreateStack(S);
system("pause");
system("cls");
}
case 2:
{
DestoryStack(S);
system("pause");
system("cls");
break;
}
case 3:
{
Clearstack(S);
system("pause");
system("cls");
cout << "链栈长度为:" << endl;
cout << StackLength(S) << " ";
system("pause");
break;
}
case 4:
{
cout << "栈的长度如下:" << endl;
cout << StackLength(S) << " ";
system("pause");
system("cls");
break;
}
case 5:
{
StackEmpty(S);
system("pause");
system("cls");
break;
}
case 6:
{
Gettop(S);
system("pause");
system("cls");
break;
}
case 7:
{
int N, M;
cout << "请输入链栈中的元素个数:" << endl;
cin >> N;
cout << "请输入链栈中的元素:" << endl;
for (int i = 1; i <=N; i++)
{
cin >> M;
pushStack(S, M);
}
system("pause");
system("cls");
break;
}
case 8:
{
int M;
cout << "请输入要出栈的栈顶元素:" << endl;
cin >> M;
POPstack(S, M);
system("pause");
system("cls");
break;
}
case 9:
{
display(S);
system("pause");
system("cls");
break;
}
default:
break;
}
}
system("pause");
return 0;
}
相关文章推荐
- 数据结构之静态链表
- 数据结构之单链表
- 数据结构之顺序表的操作
- 约瑟夫环(N个人围桌,C语言,数据结构)
- 数组实现约瑟夫循环
- 顺序查找
- 【数据结构】树形结构:二叉树&树
- 堆和栈的区别(转过无数次的文章)
- 图论&数据结构——并查集
- 【数据结构】线性结构:栈&队列&数组
- perl数据结构输出 Data::Dumper
- 微软等数据结构+算法面试100题全部答案集锦
- 微软公司等数据结构+算法面试100题(第1-100题)全部出炉
- 横空出世,席卷互联网--评微软等公司数据结构+算法面试100题
- 括号配对问题
- 【自考】时间复杂度—数据结构导论
- 第三章:数据结构与算法javascript描述: 列表
- 分治算法(思想)在数据结构中的应用
- 数据结构实践项目——队列
- 数据结构实践——队列数组