您的位置:首页 > 其它

编写程序,实现顺序栈的创建、进栈和出栈等基本操作算法。

2017-03-22 14:34 591 查看
 

编写程序,实现顺序栈的创建、进栈和出栈等基本操作算法。

(1) 创建包含任意个元素的顺序栈。

(2) 进栈一个任意元素。

(3) 出栈一个任意元素。

(4) 取栈顶元素并输出。

*(5) 删除栈中值为给定值的元素。

*(6) 利用顺序表实现顺序栈元素的逆置。

 

      #include<stdio.h>

    #include<malloc.h>

    #include<ctype.h>

    #include <time.h>

    typedef int ElemType;

    typedef struct

    {

      ElemType *base;

      ElemType *top;

      ElemType stacksize;

    }SqStack;

    SqStack initiallist();

    SqStack insert(SqStack,int);

    SqStack insert1(SqStack);

    void rever(SqStack,int);

    void deletely(SqStack);

    int main()

    {

        int i=0;

        int j=0;

        int n;

        printf("\n请输入一个大于0的值使得在这个范围内随机产生栈中元素个数");

        scanf("%d",&n);

        srand((int)time(NULL));

        int m1=rand()%n+1;

        int a
;

        SqStack q;

        SqStack q1;

        SqStack q2;

        SqStack q3;

        q=initiallist();

        q2=insert1(q);

        q1=insert(q,m1);

        q3=q1;

        printf("输出栈顶元素为");

        printf("%d",*--(q1.top));

        *++(q1.top);

        printf("\n输出栈的所有元素:");

        printf("\n");

        while(q1.top!=q1.base)

        a[i++]=*--(q1.top),printf("%d\n",*(q1.top)),j++;

         srand((int)time(NULL));

         int m2=rand()%(j);

         printf("出栈的随机元素为");

         printf("%d",a[m2]);

         printf("\n");

         rever(q3,m1);

         deletely(q3);

    }

    SqStack initiallist()

    {

    SqStack S;

    int MAXSIZE;

    printf("请输入栈中最大储存的元素个数");

    scanf("%d",&MAXSIZE);

    S.base=new int[MAXSIZE];

    if(!*S.base)

      exit(-1);

      S.top=S.base;

      S.stacksize=MAXSIZE;

      return S;

    }

 

 

   SqStack insert(SqStack q,int m1)

    {

        int k;

        int m[m1];

        printf("栈中元素个数为:");

        printf("%d",m1);

        printf("\n请输入一个大于等于0的值使得在0到这个范围内随机产生栈中元素");

        scanf("%d",&k);

        for(int j=0;j<m1;j++)

        m[j]=rand()%k;

        printf("请输出产生的随机元素\n");

        for(int k1=0;k1<m1;k1++)

        printf("%d\n",m[k1]);

        if(q.top-q.base==q.stacksize)

        printf("栈已经满");

        for(int i=0;i<m1;i++)

       {

        *(q.top)=m[i];

         q.top++;

       }

        return q;

    }

 

 

    SqStack insert1(SqStack q)

    {

        int k;

        printf("\n任意输入一个数");

        scanf("%d",&k);

        if(q.top-q.base==q.stacksize)

        printf("栈已经满");

        *(q.top)=k;

        q.top++;

        printf("\n任意入栈的一个元素是");

        printf("%d",*--(q.top));

        printf("\n");

    }

   void deletely(SqStack q1)

    {

        int t;

        int m1=0;

        int j=0;

        int a[10];

        printf("\n请输入你要删除的元素");

        scanf("%d",&t);

        *(--q1.top);

        while(*q1.top!=t)

        a[m1++]=(*q1.top)--;

        if(*(q1.top)==NULL)

        exit(-1);

        printf("\n删除成功");

        printf("\n输出第一个找到的想要删除的数是");

        printf("%d",*(q1.top));

        for(int i=0;i<=m1;i++)

        ++(*q1.top)=a[i];

         while(q1.top!=q1.base)

         {

         printf("\n%d",*(q1.top));

         *--(q1.top);

         }

    }

   void rever(SqStack q1,int m1)

{

     int t;

     int b[m1];

     int p=m1-1;

     int j=0;

     printf("把栈中的元素弹出放在一个数组中,数组元素分别为\n");

     for(int i=0;i<m1;i++)

     {

    --q1.top;

     b[i]=*(q1.top);

     printf("%d\n",b[i]);

     }

     if(m1%2!=0)

   {

     while(j!=p)

     {

     t=b[j];

     b[j]=b[p];

     b[p]=t;

     ++j;

     --p;

     }

 

     printf("逆置后的数组元素为\n");

    for(int i1=0;i1<m1;i1++)

     printf("%d\n",b[i1]);

 

      while(q1.top!=q1.base)

      --(q1.top);

 

     for(int k=m1-1;k>=0;k--)

      {

        ++(q1.top);

       *(q1.top)=b[k];

      }

     printf("输出逆置后的栈元素\n");

     while(q1.top!=q1.base)

     {

      printf("%d\n",*(q1.top));

      --(q1.top);

      }

   }

 

     else

        {

            for(int v=1;v<=m1/2;v++)

         {

         t=b[j];

         b[j]=b[p];

         b[p]=t;

         ++j;

         --p;

         }

 

    printf("逆置后的数组元素为\n");

    for(int i1=0;i1<m1;i1++)

     printf("%d\n",b[i1]);

 

    while(q1.top!=q1.base)

      --(q1.top);

     for(int k=m1-1;k>=0;k--)

     {

      ++(q1.top);

      *(q1.top)=b[k];

      }

     printf("输出逆置后的栈元素\n");

     while(q1.top!=q1.base)

     {

      printf("%d\n",*(q1.top));

      --(q1.top);

      }

        }

 

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编写程序
相关文章推荐