您的位置:首页 > 理论基础 > 数据结构算法

数据结构之链栈

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: