首页 > 解决方案 > 即使每个堆栈的代码中使用了不同的变量,值也会存储在不同的堆栈中

问题描述

我想创建 4 个堆栈,每个主题 1 个。然后在每个堆栈中存储要提交的每个作业的剩余天数。这是我写的代码。该代码除了 1 个缺陷外有效:
当值存储在一个堆栈中时,它们也会显示在其他堆栈中,值为 0 和相同数量的元素,例如:我将值 5,20,7 存储在 ds exit 的堆栈中该堆栈并转到 dsgt 堆栈并存储 9,11,然后在打印 dsgt 堆栈中的值时,代码将打印为“0 0 0 9 11”,此处 0 0 0 是第一个堆栈的 3 个元素。现在退出 dsgt 堆栈并返回 ds 堆栈并打印其值,输出为 "5 7 20 0 0" ,这里 0 0 是 dsgt 堆栈的元素,我不想用 ds 和 rest 打印的堆栈。

#include <stdio.h>
#include <conio.h>
#define MAXSIZE 20
int st1[MAXSIZE];
int st2[MAXSIZE];
int st3[MAXSIZE];
int st4[MAXSIZE];
int top = -1;
void push(int st[], int item);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);
void sort(int st[]);

int main() {
    int subj;
    printf("\nEnter the number associated with the Subject assignment that has to be tracked\n");
    int choice = 0, item1;
    do {
            printf("\n 1. Data Structures");
            printf("\n 2. DSGT ");
            printf("\n 3. CG ");
            printf("\n 4. Math");
            printf("\n 5. Exit Code");
            printf("\n Enter Your Choice");
            scanf("%d", & choice);
            switch (choice) {
            case 1:
                    ds();
                    break;
            case 2:
                    dsgt();
                    break;
            case 3:
                    cg();
                    break;
            case 4:
                    math();
                    break;
            case 5:
                    printf("Exited");
                    break;
            default:
                    printf("\n Wrong Input");
            }
    } while (choice != 5);

    return 0;
}

int dsgt() {
    int choice, item2;
    do {
            printf("Stack Operation \n");
            printf("\n 1. Add A New Assignment");
            printf("\n 2. Remove the latest Completed Assignment ");
            printf("\n 3. View the latest Pending Assignment ");
            printf("\n 4. View All the pending Assignments");
            printf("\n 5. Exit Code");
            printf("\n Enter Your Choice");
            scanf("%d", & choice);
            switch (choice) {
            case 1:
                    printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
                    scanf("\n %d", & item2);
                    push(st2, item2);
                    sort(st2);
                    break;
            case 2:
                    item2 = pop(st2);
                    printf("\n Removed Assignment is: \n %d", item2);
                    break;
            case 3:
                    item2 = peek(st2);
                    printf("\n The Latest Assignment to be Submitted is:%d", item2);
                    break;
            case 4:
                    display(st2);
                    break;
            case 5:
                    printf("Exited");
                    break;
            default:
                    printf("\n Wrong Input");
            }
    } while (choice != 5);
}

int ds() {
    int choice, item1;
    do {
            printf("Stack Operation \n");
            printf("\n 1. Add A New Assignment");
            printf("\n 2. Remove the latest Completed Assignment ");
            printf("\n 3. View the latest Pending Assignment ");
            printf("\n 4. View All the pending Assignments");
            printf("\n 5. Exit Code");
            printf("\n Enter Your Choice");
            scanf("%d", & choice);
            switch (choice) {
            case 1:
                    printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
                    scanf("\n %d", & item1);
                    push(st1, item1);
                    sort(st1);
                    break;
            case 2:
                    item1 = pop(st1);
                    printf("\n Removed Assignment is: \n %d", item1);
                    break;
            case 3:
                    item1 = peek(st1);
                    printf("\n The Latest Assignment to be Submitted is:%d", item1);
                    break;
            case 4:
                    display(st1);
                    break;
            case 5:
                    printf("Exited");
                    break;
            default:
                    printf("\n Wrong Input");
            }
    } while (choice != 5);
}

int cg() {
    int choice, item3;
    do {
            printf("Stack Operation \n");
            printf("\n 1. Add A New Assignment");
            printf("\n 2. Remove the latest Completed Assignment ");
            printf("\n 3. View the latest Pending Assignment ");
            printf("\n 4. View All the pending Assignments");
            printf("\n 5. Exit Code");
            printf("\n Enter Your Choice");
            scanf("%d", & choice);
            switch (choice) {
            case 1:
                    printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
                    scanf("\n %d", & item3);
                    push(st3, item3);
                    sort(st3);
                    break;
            case 2:
                    item3 = pop(st3);
                    printf("\n Removed Assignment is: \n %d", item3);
                    break;
            case 3:
                    item3 = peek(st3);
                    printf("\n The Latest Assignment to be Submitted is:%d", item3);
                    break;
            case 4:
                    display(st3);
                    break;
            case 5:
                    printf("Exited");
                    break;
            default:
                    printf("\n Wrong Input");
            }
    } while (choice != 5);
}

int math() {
    int choice, item4;
    do {
            printf("Stack Operation \n");
            printf("\n 1. Add A New Assignment");
            printf("\n 2. Remove the latest Completed Assignment ");
            printf("\n 3. View the latest Pending Assignment ");
            printf("\n 4. View All the pending Assignments");
            printf("\n 5. Exit Code");
            printf("\n Enter Your Choice");
            scanf("%d", & choice);
            switch (choice) {
            case 1:
                    printf("\n Enter the Number of Days Left for the Assignment to be Submitted");
                    scanf("\n %d", & item4);
                    push(st4, item4);
                    sort(st4);
                    break;
            case 2:
                    item4 = pop(st4);
                    printf("\n Removed Assignment is: \n %d", item4);
                    break;
            case 3:
                    item4 = peek(st4);
                    printf("\n The Latest Assignment to be Submitted is:%d", item4);
                    break;
            case 4:
                    display(st4);
                    break;
            case 5:
                    printf("Exited");
                    break;
            default:
                    printf("\n Wrong Input");
            }
    } while (choice != 5);
}

void push(int st[], int item) {
    if (top == MAXSIZE - 1) {
            printf("\n You Have a lot of Assignments Due, GET WORKING!!!");
    } else {
            top = top + 1;
            st[top] = item;
    }
}
int pop(int st[]) {
    int item;
    if (top == -1) {
            printf("Great Work No Assignment Are Pending");
            return 0;
    } else {
            item = st[top];
            top = top - 1;
    }
    return item;
}
int peek(int st[]) {
    int item;
    if (top == -1) {
            printf("Great Work No Assignment Are Pending");
            return 0;
    } else {
            item = st[top];
            return item;
    }
}
void display(int st[]) {
    if (top == -1) {
            printf("Great Work No Assignment Are Pending");
    } else {
            for (int i = top; i >= 0; i--) {
                    printf("\n%d\n", st[i]);
            }
    }
}
void sort(int st[]) {
    int tmp, i, j;
    for (int i = top; i >= 0; i--) {
            for (j = i + 1; j <= top; j++) {
                    if (st[i] < st[j]) {
                            tmp = st[j];
                            st[j] = st[i];
                            st[i] = tmp;
                    }
            }
    }
}

标签: cinputstack

解决方案


我建议您将 astructure用于包含数组和top值的堆栈,例如

typedef struct{
    int stack[MAXSIZE];
    int top;
}stack_t;

然后你可以声明你的堆栈并初始化它们:

stack_t st1 = {{0}, -1};
stack_t st2 = {{0}, -1};
stack_t st3 = {{0}, -1};
stack_t st4 = {{0}, -1};

或将它们放在一个数组中

stack_t stacks[4]={
    {{0}, -1},
    {{0}, -1},
    {{0}, -1},
    {{0}, -1}, };

您需要如下声明您的函数并相应地更新它们的实现:

void push(stack_t* st, int item);
int pop(stack_t* st);
int peek(stack_t st);
void display(stack_t st);
void sort(stack_t* st);
void dsgt(void);
void cg(void);
void math(void);
void ds(void);

, push,函数然后都使用指向例如pop的指针sortstack_t

int pop(stack_t* st) {
    int item =0;
    if (st->top == -1) {
        printf("Great Work No Assignment Are Pending");
    } else {
        item = st->stack[st->top];
        st->top = st->top - 1;
    }
    return item;
}

displaypeek函数可以使用stack_t参数,因为它们不会改变它。

如果将所有堆栈结构放在一个数组中,则可以使用全局变量作为stack_t数组的索引,并减少选择代码的重复。


推荐阅读