zoukankan      html  css  js  c++  java
  • 顺序栈基本操作

    #include<stdio.h>
    #include<stdlib.h>
    #define MAXSIZE 1024
    typedef int elemtype;
    typedef struct SequenStack
    {
        elemtype data[MAXSIZE];
        int top;                        //设置顺序栈的栈顶指针 
    }SequenStack;                        //顺序栈的结构类型
    
    SequenStack * Init_SequenStack();
    int SequenStack_Length(SequenStack *S);
    int SequenStack_Empty(SequenStack *S);
    int SequenStack_Full(SequenStack *S);
    int Push_SequenStack(SequenStack *S,elemtype x);
    int Pop_SequenStack(SequenStack *S);
    void menu();
    
    void menu()
    {   system("cls");
        printf("		1-initial stack
    ");
        printf("		2-input data
    ");
        printf("		3-get length
    ");
        printf("		4-判断栈空
    ");
        printf("		5-判断栈满
    ");
        printf("		6-出栈
    ");
        printf("		7-输出
    ");
        printf("		#-quit
    ");
        printf("Please select:  ");
    }
    int main()
    {
        char cmd;
        SequenStack *S;
        elemtype x;
        int isdo,i,len;
        system("cls");
        menu();
        while((cmd=getchar())!='#')
        {
            switch(cmd)
            {    case '1':   S= Init_SequenStack( );
                            printf("
    Creatied the stack!
    ");
                            printf("
    
    
    			");
                            break;
                case '2':    printf("请插入数据 0结束
    ");
                            scanf("%d",&x);
                            while(x!=0)
                            {
                                isdo=Push_SequenStack(S,x);
                                if(isdo==0)
                                {    printf("栈满结束
    ");
                                    break;
                                }
                                scanf("%d",&x);
                            }
                            printf("
    
    
    			");
                            break;
                case '3':    len=SequenStack_Length(S);
                            printf("
    Caculated the Length of the list...
    ");
                            printf("len=%d
    ",len);
                            printf("
    
    
    			");
                            break;
                case '4':    isdo=SequenStack_Empty(S);
                            if(isdo==0)
                            {
                                printf("栈不是空
    ");
                            }
                            else if(isdo==1)
                            {
                                printf("栈为空
    ");
                            }
                            printf("
    
    
    			");
                            break;
                case '5':    isdo=SequenStack_Full(S);
                            if(isdo==0)
                            {
                                printf("栈不满
    ");
                            }
                            else if(isdo==1)
                            {
                                printf("栈为满
    ");
                            }
                            printf("
    
    
    			");
                            break;
                case '6':    printf("
    Geting the top data of the stack...
    ");
                            isdo=Pop_SequenStack(S);
                            if(isdo==1)
                            {
                                len--;
                                printf("出栈成功
    ");
                                printf("新的栈顶元素:%d
    ",S->data[S->top]);
                            }
                            else if(isdo==0)
                            {
                                printf("出栈失败
    ");
                            }
                            printf("
    
    
    			");
                            break;
                case '7':    while(S->top>=0)
                            {
                                printf("%d	",S->data[S->top]);
                                S->top--;                        
                            }
                            for(i=0;i<len;i++)
                            {
                                S->top++;
                            }
                            printf("
    
    
    			");
                            break;
            }
            fflush(stdin);
            system("pause");
            menu();
        }
        return 0;
    }
    SequenStack * Init_SequenStack()        //初始化 
    {
        SequenStack *S;                    //定义顺序栈指针变量 
        S=(SequenStack *)malloc(sizeof(SequenStack));            //申请内存空间 
        S->top=-1;
        return S; 
    }
    //判断栈空
    int SequenStack_Empty(SequenStack *S)
    {
        if(S->top==-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    } 
    //判断栈满
    int SequenStack_Full(SequenStack *S)
    {
        if(S->top+1==MAXSIZE)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    } 
    //求顺序栈的长度
    int SequenStack_Length(SequenStack *S)
    {
        return S->top+1;
    } 
    //入栈
    int Push_SequenStack(SequenStack *S,elemtype x)
    {
        if(S->top>=MAXSIZE-1)
        {
            return 0;                                        //栈满,插入失败,返回0; 
        }
        else
        {
            S->top++;
            S->data[S->top]=x;
            return 1;
        }
    }
    //出栈
    int Pop_SequenStack(SequenStack *S)
    {
        if(S->top==-1)
        {
            return 0;                                    //栈空 
        }
        else
        {
            S->top--; 
            return 1;
        } 
    } 
  • 相关阅读:
    C#网络编程系列(两)它Socket同步TCPserver
    [LeetCode] ZigZag Conversion [9]
    设计模式迭代器模式
    Android_WebServices_介绍
    (UML两个汇总)九种图。
    你不明白 String 类别
    Mockito使用注意事项
    Xcode6为什么干掉pch(Precompile Prefix Header)&amp;怎样加入pch文件
    atoi()函数的实现
    多种方法求解八数码问题
  • 原文地址:https://www.cnblogs.com/xxs812/p/7977201.html
Copyright © 2011-2022 走看看