帮别人做作业, 用c语言实现一个字符stack

程序要实现的效果如下图

实现代码:

#include <stdio.h>
#include <stdlib.h>

void reverse(char ar[], int n)
{
    char c;
    int i = 0;
    for ( i = 0; i <= n ; i++){
        c = ar[i];
        ar[i] = ar[n];
        ar[n] = c;
        n--;}

}


int main(void){

    int sizeOfStack,i,iUserChoice;
    char *str;
    char cPush,cPop;

    printf("Enter the amount of character to be input :");

    scanf("%d", &sizeOfStack);

    str = (char*)realloc(NULL, sizeof(char)*sizeOfStack);
    
    for(i =0;i<sizeOfStack;i++)
    {
        printf("
Enter the character to be pushed : ");
        scanf(" %c", &str[i]);
    }

    //add end char to tail 
    str[sizeOfStack]='';

    
    // reverse array  
    reverse(str,sizeOfStack-1);

    
    //printf("the final array is :%s
", str);


    while(1)
    {
        printf( "1.Push an element on the stack
"
          "2.Pop an element from the stack
"
        "3.Display the top element
"
        "4.Display all stack element
"
        "5.Display size of the stack
"
        "6.Display the final stack and its reversed version
"
        "7.quit
"
        );


        printf("Enter your choice :");
        scanf(" %d", &iUserChoice);

        switch (iUserChoice) {
            case 1:    
                printf("
Enter the element to be pushed : ");
                scanf(" %c", &cPush);

                // re allocate array size
                str = (char*)realloc(str, sizeof(char)*(sizeOfStack+1));
                 
                for(i=sizeOfStack+1;i>0;i--)
                    str[i]=str[i-1];
                str[0]=cPush;
                str[sizeOfStack+1]='';

                sizeOfStack=sizeOfStack+1;


                //printf("the final array is :%s
", str);


                break;

            case 2:    
                printf("
pop element is : ");
                scanf(" %c", &cPop);
                for(i=0;i<sizeOfStack;i++)
                    str[i]=str[i+1];
        
                sizeOfStack=sizeOfStack-1;

                //printf("the final array is :%s
", str);

                break;

            case 3: 
                printf("
element at the top is:%c",str[0]);
                printf("
");
                break;

            case 4: 
                printf("
stack is:
");
                for(i=0;i<sizeOfStack;i++)
                {
                    printf("%c     ",str[i]);
                }
                printf("
");

                break;
            case 5:
                printf("
size of stack is:%d",sizeOfStack);
                printf("
");
                break;

            case 6:
                printf("
the initial stack is:
");
                for(i=0;i<sizeOfStack;i++)
                {
                    printf("%c     ",str[i]);
                }
                printf("
");


                printf("
the final stack is:
");
                for(i=sizeOfStack-1;i>=0;i--)
                {
                    printf("%c     ",str[i]);
                }
                printf("
");
                break;

            case 7:    
                exit(0);

        }

    }
    return 0;
}

要点 : 

用  realloc 函数重新分配数组大小

读取单个字符时, 注意前面加空格   scanf(" %d", &iUserChoice);

编译方法 :   Linux  :  gcc -o Source Source.c

      windows :   cl Source.c

原文地址:https://www.cnblogs.com/lthxk-yl/p/8572916.html