数据结构—链栈的操作及实现(C/C++)
文章目录
1.0初始化定义
#include<iostream>
#include<stdlib.h>
#define MAXSIZE 100 //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int SElemType; // 自己定义的类型
typedef int status; // 上同
using namespace std;
1.1栈链的结构体定义
typedef struct stackNode
{
SElemType data; //链栈的数据域
struct stackNode* next; //链栈的指针域
}stackNode;
typedef struct stack //栈结构
{
stackNode *top; //stackNode类型的栈顶指针
int size; //元素个数计数
}LinkStack;
1.2栈链的初始化
status Initstack(LinkStack *S)
{
if((S->top=new stackNode )==NULL) //判断分配动态空间是否成功
{
cout<<"分配空间失败辽!";
return ERROR;
}
else
{
S->top=NULL; //因为设置了栈顶指针,因此要置空,避免出现野指针
S->size=0; //长度为0
}
return 0;
}
1.3判断栈链是否为空
status Empystack(LinkStack* S)
{
if(S->top==NULL)
{
cout<<"栈链为空!";
}
else
{
cout<<"栈链非空!";
}
return 0;
}
1.4栈链的入栈操作
status Push(LinkStack *S,SElemType &e)
{
stackNode* p; //声明一个p指针
p = new stackNode;
p->data = e; //数据域
p->next = S->top; //链接到栈上
S->top= p; //移动指针
S->size ++; //栈长度+1
return OK;
}
status Empy(LinkStack *S)
{
if(S->size==0) //如果栈长度为0,自然为空
{
return OK;
}
else
{
return 0;
}
}
1.5栈链的出栈操作
status Pop(LinkStack* S, SElemType *e)
{
if (Empy(S))
{
return 0;
}
else
{
*e=S->top->data; //临时保存栈顶指针的数据域
stackNode* p=S->top; //p指向栈顶指针,避免丢失
S->top=S->top->next; //S指向S的next
delete p; //释放p
S->size--; //栈长度-1
return 1;
}
}
1.6求栈链的长度
void length(LinkStack* S)
{
cout<<"此时栈链的长度为:"<<S->size<<endl;
}
1.7栈链的输出(打印)
status printstack(LinkStack* S)
{
stackNode*p=S->top;
cout<<"当前链栈为:";
while(p)
{
cout<<"\40"<<p->data;
p=p->next;
}
cout<<endl;
return 0;
}
1.8 C主函数代码
int main()
{
int n,e;
LinkStack *S;
S=(LinkStack*)malloc(sizeof(LinkStack));
Initstack(S);
printf("输入顺序为:");
for(int i=1;i<=10;i++)
{
printf("%2d",i);
}
printf("\n");
for(int i=1;i<=10;i++)
{
Push(S,i);
}
printstack(S);
printf("\n");
length(S);
printf("\n");
printf("请输入需要出栈的次数:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
Pop(S, &e);
}
printf("\n");
printstack(S);
printf("当前栈顶元素为:%d",GetTop(S,e));
printf("\n");
length(S);
Empystack(S);
printf("\n");
return 0;
}
1.9 C++主函数代码
int main()
{
int n,e;
LinkStack *S;
S=new LinkStack;
Initstack(S);
cout<<"输入顺序为:";
for(int i=1;i<=10;i++)
{
cout<<"\40"<<i;
}
cout<<endl;
for(int i=1;i<=10;i++)
{
Push(S,i);
}
printstack(S);
cout<<endl;
length(S);
cout<<endl;
cout<<"请输入需要出栈的次数:";
cin>>n;
for(int i=1;i<=n;i++)
{
Pop(S, &e);
}
cout<<endl;
printstack(S);
cout<<"当前栈顶元素为:"<<GetTop(S,e);
cout<<endl;
length(S);
Empystack(S);
cout<<endl;
return 0;
}
2.0 C完整代码
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100 //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int SElemType; // 自己定义的类型
typedef int status; // 上同
typedef struct stackNode
{
SElemType data; //链栈的数据域
struct stackNode* next; //链栈的指针域
}stackNode;
typedef struct stack //栈结构
{
stackNode *top; //stackNode类型的栈顶指针
int size; //元素个数计数
}LinkStack;
//栈链的初始化
status Initstack(LinkStack *S)
{
if((S->top=(stackNode*)malloc(sizeof(stackNode)))==NULL) //判断分配动态空间是否成功
{
printf("分配空间失败辽!");
return ERROR;
}
else
{
S->top=NULL; //因为设置了栈顶指针,因此要置空,避免出现野指针
S->size=0; //长度为0
}
return 0;
}
//链栈入栈
status Push(LinkStack *S,SElemType &e)
{
stackNode* p; //声明一个p指针
p=(stackNode*)malloc(sizeof(stackNode));
p->data = e; //数据域
p->next = S->top; //链接到栈上
S->top= p; //移动指针
S->size ++; //栈长度+1
return OK;
}
status Empy(LinkStack *S)
{
if(S->size==0) //如果栈长度为0,自然为空
{
return OK;
}
else
{
return 0;
}
}
//链栈出栈
status Pop(LinkStack* S, SElemType *e)
{
if (Empy(S))
{
return 0;
}
else
{
*e=S->top->data; //临时保存栈顶指针的数据域
stackNode* p=S->top; //p指向栈顶指针,避免丢失
S->top=S->top->next; //S指向S的next
delete p; //释放p
S->size--; //栈长度-1
return 1;
}
}
//判断栈空
status Empystack(LinkStack* S)
{
if(S->top==NULL)
{
printf("栈链为空!");
}
else
{
printf("栈链非空!");
}
return 0;
}
//取栈链的栈顶元素
status GetTop(LinkStack* S,SElemType &e)
{
if(S->top ==NULL)
{
return 0;
}
else
{
e = S->top -> data;
return e;
}
}
//打印栈链的元素
status printstack(LinkStack* S)
{
stackNode*p=S->top;
printf("当前链栈为:");
while(p)
{
printf("%2d",p->data);
p=p->next;
}
printf("\n");
return 0;
}
//求栈链的长度
void length(LinkStack* S)
{
printf("此时栈链的长度为:%d\n",S->size);
}
int main()
{
int n,e;
LinkStack *S;
S=(LinkStack*)malloc(sizeof(LinkStack));
Initstack(S);
printf("输入顺序为:");
for(int i=1;i<=10;i++)
{
printf("%2d",i);
}
printf("\n");
for(int i=1;i<=10;i++)
{
Push(S,i);
}
printstack(S);
printf("\n");
length(S);
printf("\n");
printf("请输入需要出栈的次数:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
Pop(S, &e);
}
printf("\n");
printstack(S);
printf("当前栈顶元素为:%d",GetTop(S,e));
printf("\n");
length(S);
Empystack(S);
printf("\n");
return 0;
}
2.1 C++完整代码
#include<iostream>
#include<stdlib.h>
#define MAXSIZE 100 //定义数组的最大长度
#define OK 1
#define OVERFLOW -1
#define ERROR -1
typedef int SElemType; // 自己定义的类型
typedef int status; // 上同
using namespace std;
typedef struct stackNode
{
SElemType data; //链栈的数据域
struct stackNode* next; //链栈的指针域
}stackNode;
typedef struct stack //栈结构
{
stackNode *top; //stackNode类型的栈顶指针
int size; //元素个数计数
}LinkStack;
//栈链的初始化
status Initstack(LinkStack *S)
{
if((S->top=new stackNode )==NULL) //判断分配动态空间是否成功
{
cout<<"分配空间失败辽!";
return ERROR;
}
else
{
S->top=NULL; //因为设置了栈顶指针,因此要置空,避免出现野指针
S->size=0; //长度为0
}
return 0;
}
//链栈入栈
status Push(LinkStack *S,SElemType &e)
{
stackNode* p; //声明一个p指针
p = new stackNode;
p->data = e; //数据域
p->next = S->top; //链接到栈上
S->top= p; //移动指针
S->size ++; //栈长度+1
return OK;
}
status Empy(LinkStack *S)
{
if(S->size==0) //如果栈长度为0,自然为空
{
return OK;
}
else
{
return 0;
}
}
//链栈出栈
status Pop(LinkStack* S, SElemType *e)
{
if (Empy(S))
{
return 0;
}
else
{
*e=S->top->data; //临时保存栈顶指针的数据域
stackNode* p=S->top; //p指向栈顶指针,避免丢失
S->top=S->top->next; //S指向S的next
delete p; //释放p
S->size--; //栈长度-1
return 1;
}
}
//判断栈空
status Empystack(LinkStack* S)
{
if(S->top==NULL)
{
cout<<"栈链为空!";
}
else
{
cout<<"栈链非空!";
}
return 0;
}
//取栈链的栈顶元素
status GetTop(LinkStack* S,SElemType &e)
{
if(S->top ==NULL)
{
return 0;
}
else
{
e = S->top -> data;
return e;
}
}
//打印栈链的元素
status printstack(LinkStack* S)
{
stackNode*p=S->top;
cout<<"当前链栈为:";
while(p)
{
cout<<"\40"<<p->data;
p=p->next;
}
cout<<endl;
return 0;
}
//求栈链的长度
void length(LinkStack* S)
{
cout<<"此时栈链的长度为:"<<S->size<<endl;
}
int main()
{
int n,e;
LinkStack *S;
S=new LinkStack;
Initstack(S);
cout<<"输入顺序为:";
for(int i=1;i<=10;i++)
{
cout<<"\40"<<i;
}
cout<<endl;
for(int i=1;i<=10;i++)
{
Push(S,i);
}
printstack(S);
cout<<endl;
length(S);
cout<<endl;
cout<<"请输入需要出栈的次数:";
cin>>n;
for(int i=1;i<=n;i++)
{
Pop(S, &e);
}
cout<<endl;
printstack(S);
cout<<"当前栈顶元素为:"<<GetTop(S,e);
cout<<endl;
length(S);
Empystack(S);
cout<<endl;
return 0;
}
2.2运行截图:
2.3总结
**如果有写得不对的地方,希望大家可以指正交流!
希望大家多多关注博主~~!**