C-栈


#include <stdio.h>
#include <stdlib.h>
#define ata 100
#define b 100
typedef struct
{
    int *base;
    int *top;
    int stacksize;
}sqstack;
sqstack s;
int n;
int initsatck()
{
    int e;
    int i;
   s.base=(sqstack*)malloc(ata*sizeof(sqstack));
    s.top=s.base;
    s.stacksize=ata;
    printf("请输入要读入栈内的数据个数:\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
 { printf("输入元素:\n");
    scanf("%d",&e);
   *(s.top)=e;
    s.top=s.top+1;
 }

}
void push()
{
    int e;
    s.top=s.base+n;
     scanf("%d",&e);
    *(s.top)++=e;
     n++;
}
void display()
{
      int e;
    while(s.top!=s.base)
    {e=*(s.top-1);
     printf("取出栈顶元素:%d\n",e);
     s.top=s.top-1;
    }
}
void dele()
{
    s.top=s.base+n;
   n--;
   s.top=s.top-1;
}
void main()
{
    int e,z;
    sqstack s;
    s.base=(sqstack*)malloc(ata*sizeof(sqstack));
    initsatck();
    display();
    printf("在栈顶操作元素,1为删除,2为插入\n");
    scanf("%d",&z);
    do
    {
      if(z==2)
      {
          printf("在栈顶插入元素:\n");
          push();
      }
      if(z==1)
      {
          printf("在栈顶删除元素:\n");
          dele();
      }
      display();
      printf("在栈顶操作元素,1为删除,2为插入\n");
      scanf("%d",&z);
    }while(z);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/sheyun1876/article/details/80582346
C-