⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack2.c

📁 栈的基本操作
💻 C
字号:
#define NULL 0
#define OVERFLOW -2
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
typedef struct STNode{
int data;
struct STNode *next;
}STNode,*StackPtr;

typedef struct LinkStack{
StackPtr top,base;
}LinkStack;
/*这里的typedef也可以这样定义
typedef struct{
StackPtr top,base;
}LinkStack;
*/
InitStack(LinkStack *S)
{

 S->top=(StackPtr)malloc(sizeof(STNode));

 S->base=S->top;
/* printf("InitStack() %o,%o",S->top,S->base);*/
}

StackEmpty(LinkStack *S)
{
 if(S->top==S->base)
 return TRUE;
 else return FALSE;
 }

int Pop(LinkStack *S,int *e)
{
 StackPtr p=NULL;
 if(StackEmpty(S))
   {return ERROR;}
 else if (S->top->next==S->base)
    {/* printf("the first choice");*/
      (*e)=S->base->data;
      free(S->base);
      S->base=S->top;
      }
 else 
     { 
       p=S->top->next->next;
       (*e)=S->top->next->data;
       free(S->top->next);
       S->top->next=p;
      }
/* printf("Pop %o,%o\n",S->top,S->base);*/

}

int Push(LinkStack *S,int e)
{
 
 StackPtr p;
 p=(StackPtr)malloc(sizeof(STNode));
 p->data=e;
 if(S->top==S->base)
   S->base=p;
 p->next=S->top->next;
 S->top->next=p;
/* printf("Push %o,%o",S->top,S->base);*/
}

main()
{
 int data1,data2,n,i;
 LinkStack S;
 InitStack(&S);
 printf("please input the total:  ");
 scanf("%d",&n);
 printf("\n");
 for(i=1;i<n+1;i++)
 {
  printf("please input the %dth of the stack  ",i);
  scanf("%d",&data1);
  Push(&S,data1);
 }
 printf("\n-------------------------------------------\n");
 for(i=0;i<n;i++)
  {
   Pop(&S,&data2);
   printf("%5d",data2);
  }

}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -