The chain structure representation of the data structure _________ stack

The chain structure representation of the data structure _________ stack

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <malloc.h>
using namespace std;
#define OK  1
#define ERROR -1
#define TURE 1
#define FALSE 0
typedef int Status;
typedef int SElemType;
typedef struct node
{
 SElemType date;
 int stacklength;
 struct node *next;
}SNode,*SList;
//构造一个空栈S 
Status InitStack(SList &S);
//销毁栈S,栈S不再存在 
Status DestoryStack(SList &S);
//把s置为空栈 
Status ClearStack(SList &S);
//若s为空战则返回ture,否则返回FALSE 
Status StackEmpty(SList S);
//获取栈顶元素,若栈不为空,则用e返回s的栈顶元素,并返回OK,否则返回ERROR
Status GetTop(SList S, SElemType &e);
//插入元素e作为新的栈顶元素
Status Push(SList &S, SElemType e);
//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
Status Pop(SList &S, SElemType &e);
//读出元素
Status vi(SElemType e);
//从栈底到栈顶依次对栈中的每个元素调用函数visit(). 一旦visit()失败, 则操作失败
Status StackTraverse(SList S, Status(*visit)(SElemType));
int main()
{
 SList S;
 InitStack(S);
 if(StackEmpty(S))
 {
  printf("创建栈之后,栈为空"); 
 }
 for(int i = 0;i<6;i++)
 {
  Push(S,i);
 }
 if(!StackEmpty(S))
 {
  printf("添加元素之后,栈不为空\n");
 }
 StackTraverse(S,vi); //遍历栈中元素 
 printf("栈的长度:%d\n",S->stacklength);
 SElemType e;
 GetTop(S,e);
 printf("栈顶元素:%d\n",e);
 Pop(S,e);
 printf("删除的栈顶元素:%d\n",e);
 StackTraverse(S,vi);//遍历数组元素
 ClearStack(S);
 if(StackEmpty(S))
 {
  printf("清空栈之后\n"); 
  }
  DestoryStack(S);
   return 0;
 }
//构造一个空栈
 Status InitStack(SList &S)
 {
  S = (SList)malloc(sizeof(SNode));
  if(!S)
  {
   printf("存储分配失败!\n");
   return ERROR;
  }
  S->next = NULL;
  S->stacklength = 0;
  return OK;
 }
 //销毁栈S,栈S不再存在
 Status DestoryStack(SList &S)
 {
  S->stacklength = 0;  //栈以销毁,空间以释放,当前栈中元素为0
  while(S)
  {
   SList e = S;
   free(e);
   S = S->next; 
   } 
   return OK;
 }
//把s置为空栈
 Status ClearStack(SList &S)
 {
  while(S->next)
  {
   SList e = S;
   free(e);
   S=S->next;
  }
  S->next = NULL;
  return OK;
  }
  //若S为空栈,则放回ture,否则返回false
  Status StackEmpty(SList S)
  {
   if(!S->next)
   {
    return TURE;
   }
   return FALSE;
   } 
   /*获取栈顶元素,
   若栈不为空,则用e返回S的栈顶元素,并返回OK,
   否则返回ERROR*/
   Status GetTop(SList S,SElemType &e)
   {
     if(!S->next)
     {
      return ERROR;
   } 
   e = S ->next->date;
   return OK;
 }
 //插入元素e作为新的栈顶元素
 Status Push(SList &S,SElemType e)
 {
  SList q = (SList)malloc(sizeof(SNode));
  q->date = e;
  q->next = S->next;
  S->next = q;
  S->stacklength++;
  return OK;
  } 
  //若栈不空,则删除S的栈顶元素,用e返回值,并返回OK ,否则 返回ERROR 
 Status Pop(SList &S,SElemType &e)
 {
  if(!S->next)
  {
   return ERROR;
  }
  S->stacklength--;
  SList q = S->next;
  S = q->next;
  e = q->date;
  free(q);
  return OK; 
  } 
 //读出元素
 Status StackTraverse(SList S,Status(*visit)(SElemType)) 
 {
  SElemType e;
  printf("遍历数组元素:");
  SList p= S->next;
  while(p)
  {
   e = p->date;
   p = p->next;
   if(!(*visit)(e))
   {
    return ERROR;
   }
   printf("\n");
   return OK;
   } 
 }


Guess you like

Origin blog.csdn.net/weixin_44192389/article/details/89425765