首页 > 解决方案 > 无法列出堆栈

问题描述

我正在尝试将推送、弹出和列表功能放入堆栈。它应该首先推送 4 个字符串(我将它们写为数组,因为 %s 无法正常运行),然后弹出 3 个字符串,并列出最后一个字符串。但是,代码无法正常运行。有人可以帮助我吗?

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
#define MAX 4

int point = -1;
char item[MAX][20];

void push(char item[point])
{
    point++;
};

void pop(char item[point])
{
    point--;
};

void list()
{
    if (point > -1) {
        for (int i = 0; i <= point; i++) {
            printf("%c\n", item[point-i]);
        }
    }
};


int main()
{
    //AB01
    char arrA[4] = {'A','B','0','1'};

    for(int i=0;i<4;i++)
    {
        printf("%c",arrA[i]);
    }
    point++;

    printf("\n");

    push (arrA[4]);

    //AB02
    char arrB[4] = {'A','B','0','2'};

    for(int i=0;i<4;i++)
    {
        printf("%c",arrB[i]);
    }
    point++;

    push (arrB[4]);

    printf("\n");


    //AB03
    char arrC[4] = {'A','B','0','3'};

    for(int i=0;i<4;i++)
    {
        printf("%c",arrC[i]);
    }
    point++;

    push (arrC[4]);

    printf("\n");


    //AB04
    char arrD[4] = {'A','B','0','4'};

    for(int i=0;i<4;i++)
    {
        printf("%c",arrD[i]);
    }
    point++;

    push (arrD[4]);

    printf("\n");


    //Delete 3 strings
    pop(arrA[point]);
    pop(arrB[point]);
    pop(arrC[point]);

    list();

    return 0;
}

似乎list无法正常运行。因为我使用数组来显示字符串,这让我无法正确列出(因为我在 中再次使用了一个数组list(),然后它变成了 2 个数组)。如果我想让我的字符串(数组中的字符)被列出,我该怎么办?

标签: cstackpush

解决方案


推荐阅读