C语言 使用开关情况的堆栈操作

kq0g1dla  于 2023-02-11  发布在  其他
关注(0)|答案(1)|浏览(161)
#include <stdio.h>
#define max 20
int stk[max];
int top=-1;
void push(int);
int pop();
void peep();
void display();
int isFull();
int isEmpty();

int main()
{
    int ch,item;
    do
    {
        printf("....Stack Operations....\n");
        printf("Press 1 for Push\n");
        printf("Press 2 for pop\n");
        printf("Press 3 for peep\n");
        printf("Press 4 for display\n");
        
        printf("Enter your choice \n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                    printf("Enter the item :- \n");
                    scanf("%d",&item);
                    push(item);
                    break;
            case 2:
                    int k=pop();
                    printf("The poped element is %d\n",k);
                    break;
            case 3:
                    peep();
                    break;
            case 4:
                    display();
                    break;
            default:
                    printf("Invalid Choice\n");
        }
    }while(ch>=1 && ch<=5);
    
    void push(int item)
    {
        if(isFull())
        printf("Stack Overflow\n");
        else
        {
            top=top+1;
            stk[top]=item;
        }
    }
    int pop()
    {
        int s;
        if(isEmpty())
        printf("Stack Underflow\n");
        else
        {
            s=stk[top];
            top=top-1;
        }
        return s;
    }
    void peep()
    {
        if(isEmpty())
        printf("Stack Underflow\n");
        else
        {
            printf("Topmost Element of the stack is %d",stk[top]);
        }
    }
    void display()
    {
        if(isEmpty())
        printf("Stack Underflow\n");
        else
        {
            for(int i=top;i>=0;i--)
            printf("%d ",stk[i]);
        }
    }
    int isFull()
    {
        if(top==max-1)
        return 1;
        else
        return 0;
    }
    int isEmpty()
    {
        if(top==-1)
        return 1;
        else
        return 0;
    }
}

我希望程序运行所有堆栈操作,但我得到这种类型的特定错误:-

/usr/bin/ld: /tmp/ccHJ1ZkB.o: in function `main':
main.c:(.text+0xf7): undefined reference to `push'
/usr/bin/ld: main.c:(.text+0x103): undefined reference to `pop'
/usr/bin/ld: main.c:(.text+0x12b): undefined reference to `peep'
/usr/bin/ld: main.c:(.text+0x137): undefined reference to `display'
collect2: error: ld returned 1 exit status
r6l8ljro

r6l8ljro1#

1.在另一个函数(main())中定义函数(也称为“嵌套函数”)不是标准C。
1.如果堆栈为空,pop()返回一个未定义的值。我将其更改为返回-1,但实际上您希望添加这样的功能,即告诉调用者错误。
1.在switch语句中引入变量时,需要块{}
1.(不固定)避免使用全局变量,而是将值传递给需要它们的函数(函数式)。通常,然后将相关数据(stktop)分组到struct中。
1.(不固定)按照惯例,常量(如max)应为大写。

#include <stdio.h>

#define max 20
int stk[max];
int top=-1;

int isFull() {
    return top==max-1;
}

int isEmpty() {
    return top==-1;
}

void push(int item) {
    if(isFull()) {
        printf("Stack Overflow\n");
        return;
    }
    stk[top++]=item;
}

int pop() {
    if(isEmpty()) {
        printf("Stack Underflow\n");
        return -1;
    }
    return stk[top--];
}

void peep() {
    if(isEmpty()) {
        printf("Stack Underflow\n");
        return;
    }
    printf("Topmost Element of the stack is %d",stk[top]);
}

void display() {
    if(isEmpty()) {
        printf("Stack Underflow\n");
        return;
    }
    for(int i=top; i>=0; i--)
        printf("%d ", stk[i]);
}

int main() {
    int ch;
    do {
        printf(
            "....Stack Operations....\n"
            "Press 1 for Push\n"
            "Press 2 for pop\n"
            "Press 3 for peep\n"
            "Press 4 for display\n"
            "Enter your choice \n"
        );
        scanf("%d",&ch);
        switch(ch) {
            case 1: {
                printf("Enter the item :- \n");
                int item;
                scanf("%d", &item);
                push(item);
                break;
            }
            case 2:
                printf("The popped element is %d\n", pop());
                break;
            case 3:
                peep();
                break;
            case 4:
                display();
                break;
            default:
                printf("Invalid Choice\n");
        }
    } while(ch>=1 && ch<=5);
}

相关问题