数据结构C语言—线性表【栈】动态链栈(malloc动态分配实现)
LinkStackMalloc.h
#define NOEXIST -1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE 1
#define OVERFLOW 1
typedef int Status;
typedef int DataType;
typedef int *Pt;
typedef struct LNode
{
DataType data;
struct LNode* next;
}LNode,*LinkList;
typedef struct LinkStack
{
LinkList base;
LinkList top;
DataType LinkStackLength;
}LinkStack,*LinkStackElem;
Status InitStack(LinkStackElem S);
Status ClearStack(LinkStackElem S);
Status DestoryStack(LinkStackElem S);
Status IsEmptyStack(LinkStackElem S);
DataType StackLength(LinkStackElem S);
Status GetTop_Stack(LinkStackElem S,Pt e);
Status Push_Stack(LinkStackElem S,DataType e);
Status Pop_Stack(LinkStackElem S,Pt e);
Status StackTraverse(LinkStackElem S,Status (*Visit)(LinkStackElem,LinkList,DataType,DataType,DataType));
Status Visit(LinkStackElem S,LinkList p,DataType i,DataType j,DataType len);
LinkStackMalloc.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkStackMalloc.h"
Status InitStack(LinkStackElem S)
{
S->base=(LinkList)malloc(sizeof(LNode));
if(S->base!=NULL)
{
puts("==========开始【初始化栈】操作!==========");
S->top=S->base;
S->base->next=NULL;
S->base->data=0;
S->LinkStackLength=0;
puts("==========完成【初始化栈】操作!==========");
return OK;
}
puts("==========开始【初始化栈】操作!==========");
S->base=NULL;
puts("==========失败【初始化栈】操作!==========");
return ERROR;
}
Status ClearStack(LinkStackElem S)
{
if(S->base==NULL)
{
puts("\n==========栈不存在!不能执行【清空】操作!==========");
return NOEXIST;
}
S->top=S->base;
if(S->LinkStackLength==0)
{
puts("\n==========开始【清空】动态链栈S!==========");
puts("\n==========【清空】动态链序栈S完成!==========");
return OK;
}
else
{
puts("\n==========开始【清空】动态链栈S!==========");
LinkList p=S->base->next;
LinkList q=p;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
S->LinkStackLength=0;
S->top=S->base;
S->base->next=NULL;
puts("\n==========【清空】动态链栈S完成!==========");
return OK;
}
}
Status DestoryStack(LinkStackElem S)
{
if(S->base==NULL)
{
puts("\n==========栈不存在!不能执行【销毁】操作!==========");
return NOEXIST;
}
S->top=S->base;
if(S->LinkStackLength==0)
{
puts("\n==========开始【销毁】动态链栈S!==========");
puts("\n==========【销毁】动态链序栈S完成!==========");
return OK;
}
else
{
puts("\n==========开始【销毁】动态链栈S!==========");
LinkList p=S->base->next;
LinkList q=p;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
S->LinkStackLength=0;
S->top=S->base;
S->base->next=NULL;
free(S->base);
S->base=NULL;
puts("\n==========【销毁】动态链栈S完成!==========");
return OK;
}
}
Status IsEmptyStack(LinkStackElem S)
{
if(S->base==NULL)
{
puts("==========栈不存在!不能执行【判空】操作!==========");
return NOEXIST;
}
if(S->base==S->top)
{
return TRUE;
}
else
{
return FALSE;
}
}
DataType StackLength(LinkStackElem S)
{
if(S->base==NULL)
{
puts("==========栈不存在!不能执行【返回当前栈中存放元素个数】操作!==========");
return NOEXIST;
}
return S->LinkStackLength;
}
Status GetTop_Stack(LinkStackElem S,Pt e)
{
if(S->base==NULL)
{
*e=-6699;
puts("==========栈不存在!不能执行【返回栈顶元素】操作!==========");
return NOEXIST;
}
if(S->base==S->top)
{
*e=-6699;
puts("==========栈S为空!不能执行【返回栈顶元素】操作!==========");
return ERROR;
}
else
{
*e=S->top->data;
printf("当前动态链栈S中,栈顶元素是:%d\n",*e);
return OK;
}
}
Status Push_Stack(LinkStackElem S,DataType e)
{
if(S->base==NULL)
{
puts("==========栈不存在!不能执行【入栈】操作!==========");
return NOEXIST;
}
else
{
LinkList p;
p=(LinkList)malloc(sizeof(LNode));
if(p!=NULL)
{
p->data=e;
p->next=S->top->next;
S->top->next=p;
S->top=p;
S->LinkStackLength++;
return OK;
}
else
{
puts("待入栈元素申请结点失败!");
return ERROR;
}
}
}
Status Pop_Stack(LinkStackElem S,Pt e)
{
if(S->base==NULL)
{
*e=-6699;
puts("==========栈不存在!不能执行【出栈】操作!==========");
return NOEXIST;
}
if(S->base==S->top)
{
*e=-6699;
puts("==========栈空!不能执行【出栈】操作!==========");
return ERROR;
}
else
{
LinkList p=S->base;
DataType i=0,len=StackLength(S);
while(i!=len-1)
{
p=p->next;
i++;
}
free(S->top);
S->top=p;
S->LinkStackLength--;
return OK;
}
}
Status StackTraverse(LinkStackElem S,Status (*Visit)(LinkStackElem,LinkList,DataType,DataType,DataType))
{
if(S->base==NULL)
{
puts("\n==========栈不存在!不能执行【遍历】操作!==========");
return NOEXIST;
}
if(S->base==S->top)
{
puts("\n==========开始遍历检查动态链栈S==========");
printf("栈顶指针----->头节点0:空<-----栈底指针");
puts("\n==========遍历检查动态链栈S完成==========");
return ERROR;
}
else
{
puts("==========开始遍历检查动态链栈S==========");
LinkList p=S->base;
DataType i,j,len=StackLength(S);
for(i=len,j=0;i>=0;p=S->base,j=0,i--)
{
Visit(S,p,i,j,len);
}
puts("==========遍历检查动态链栈S完成==========");
return OK;
}
}
Status Visit(LinkStackElem S,LinkList p,DataType i,DataType j,DataType len)
{
while(p!=NULL)
{
if(j==i)
{
break;
}
p=p->next;
j++;
}
if(i==len)
{
printf("栈顶指针----->%d:%d\n",len,S->top->data);
printf(" A\n |\n");
}
if(i<len&&i>0)
{
printf(" %d:%d\n",j,p->data);
printf(" A\n |\n");
}
if(i==0)
{
printf("栈底指针----->%d:\n",i);
}
return OK;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkStackMalloc.h"
int main()
{
LinkStack S;
DataType e;
InitStack(&S);
puts("请按先后顺序,输入要入栈的元素(空格分隔,f结束):");
while(1)
{
int n,k;
k=scanf("%d",&n);
if(k==0)
{
break;
}
Push_Stack(&S,n);
}
StackTraverse(&S,Visit);
if(IsEmptyStack(&S))
{
puts("\n==========动态链栈S【是空栈】==========");
}
else
{
puts("\n==========动态链栈S【不是空栈】==========");
}
if(StackLength(&S)!=NOEXIST)
{
printf("当前动态链栈S中已存在元素个数是:%d!\n",StackLength(&S));
}
GetTop_Stack(&S,&e);
Pop_Stack(&S,&e);
if(e!=-6699)
{
printf("已经弹出栈顶元素:%d!\n",e);
}
StackTraverse(&S,Visit);
ClearStack(&S);
StackTraverse(&S,Visit);
if(IsEmptyStack(&S))
{
puts("\n==========动态链栈S【是空栈】==========");
}
else
{
puts("\n==========动态链栈S【不是空栈】==========");
}
fflush(stdin);
puts("请按先后顺序,输入要入栈的元素(空格分隔,f结束):");
while(1)
{
int n,k;
k=scanf("%d",&n);
if(k==0)
{
break;
}
Push_Stack(&S,n);
}
StackTraverse(&S,Visit);
DestoryStack(&S);
StackTraverse(&S,Visit);
IsEmptyStack(&S);
return 0;
}
运行结果示例
------------------------------------------------------第七次发文章有点激动啊!----------------------------------------------------- -----------------------------------------------------【数据结构代码自编练习】------------------------------------------------------ ----------------------------------------------------------------【TDTX】-----------------------------------------------------------------
|