簡體   English   中英

如何在C中使用數組將字符串存儲在堆棧中?

[英]How to store string in stack using array in C?

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

#define max 100

int a[max];
int top = -1;
char x;

int isempty() ;
int isfull() ;
void push() ;
int pop() ;
void display() ;

void main()
{
    int ch;

    do
    {

        printf("\n 1. Push");
        printf("\n 2. Pop");
        printf("\n 3. Display");
        printf("\n 4. Exit");
        scanf("%d",&ch);

    switch (ch) {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:

            break;
        default:
            printf("Invalid Choice");
            break;
        }
    }while(ch!=4)
}

int isfull(){
if ( (top == max-1))
{
    return 1;
}
else
{
    return 0;
}
}

int isempty(){
if((top==-1))
{
    return 1;
}
else
{
    return 0;
}
}

void push(){
if (isfull())
{
    printf("Stack is full");
}
else
{
    printf("Enter element to add");
    scanf("%s",x);
    top++;
    strcpy(a[top],x);
    }
}

int pop(){
if(isempty())
{
    printf("Stack is empty");
    exit(0);
}
else{
    strcpy(x,a[top]);
    printf("%s",x);
    top --;
    }
}

void display()
{
if(isempty())
{
    printf("NO data to display");
    exit(0);
}
else
{
    int i;
    for(i=0;i<top+1;i++)
    {
        printf("%s \n",a[i]);
        }
    }
}

在運行推入操作時,它將以非零錯誤退出,而不執行任何操作。

我們必須通過使用array和push&pop將字母數字值添加到堆棧中。 它應該接受一個字符串值,並在選擇顯示時將其存儲在堆棧數組中,它應該顯示所有值。

有人可以幫我糾正此代碼中的錯誤的原因,這就是為什么它沒有為字符串分配或存儲任何值的原因。

該答案提供了使用上面發布的代碼時OP所遇到的所有問題。 解決方案的第一部分提供了對這些錯誤的分析以及如何處理它們。 第二部分提供OP想要實現的解決方案。 (將字符串存儲在堆棧中)。

您正在訪問一個未初始化的變量,並將其的垃圾值與4進行比較。 我建議do-while

 do{
    printf("\n 1. Push");
    printf("\n 2. Pop");
    printf("\n 3. Display");
    printf("\n 4. Exit");
    scanf("%d",&ch);
 ....

 }while(ch!=4);

這個想法很簡單。 我們將在ch進行輸入並完成所有工作。 在所有工作結束時進行比較,因此,我們始終以ch的確定值進行工作。

以前,從您的問題看來, ch中的垃圾值不等於4 這就是為什么它甚至沒有進入循環的原因。

您已將程序中的每個變量都設為全局變量。 當您遇到必須跟蹤數據突變的錯誤時,就會理解調試它的真正痛苦。 不要使用這樣的全局變量。

重大錯誤

您永遠不能將字符串存儲在int數組中。

scanf("%s",x); This is wrong.

您正在使用%s指定符輸入字符。 您應該使用%c 然后,您也不能使用strcpy()復制它。

而且,您不需要strcpy()您可以簡單地通過使用來實現。 您可以將char存儲在int變量和int數組中。

更正后的代碼為( push()函數):

if( scanf(" %c",&x) == 1){
   a[++top]=x;
}

pop()函數中使用的方式相同

x = a[top--];
printf("%c",x);

同樣在display()函數中,您需要更改

printf("%c \n",a[i]);

同樣,如果您確定只使用字符,為什么不使用char數組。

char a[100]非常適合。

這些也是程序生成的錯誤/警告的列表(不使用任何標志)

main.c: In function ‘main’: main.c:48:1: error: expected ‘;’ before ‘}’ token  }  ^ main.c: In function ‘pop’: main.c:93:12: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy(x,a[top]);
            ^ In file included from main.c:2:0: /usr/include/string.h:125:14: note: expected ‘char * restrict’ but argument is of type ‘char’  extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
              ^~~~~~ main.c:93:14: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
     strcpy(x,a[top]);
              ^ In file included from main.c:2:0: /usr/include/string.h:125:14: note: expected ‘const char * restrict’ but argument is of type ‘int’  extern char *strcpy (char *__restrict
__dest, const char *__restrict __src)
              ^~~~~~ main.c: In function ‘main’: main.c:114:1: error: expected declaration or statement at end of input  }  ^

使用標志作為alk表示,錯誤和警告將更多。


為了存儲字符串,您需要考慮使用2d char數組。 那是實現字符串的一種方法。

(出於說明目的。僅合並更改。該代碼具有最少的錯誤檢查/沒有錯誤檢查。)

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

#define max 100

char a[max][max];
int top = -1;
char x[max];

int isempty() ;
int isfull() ;
void push() ;
int pop() ;
void display() ;

int main()
{
    int ch;

    do
    {

        printf("\n 1. Push");
        printf("\n 2. Pop");
        printf("\n 3. Display");
        printf("\n 4. Exit\n");
        scanf("%d",&ch);

        switch (ch) {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        case 4:

            break;
        default:
            printf("Invalid Choice");
            break;
        }
    }while(ch!=4);
    return 0;
}

int isfull(){
    if ( (top == max-1))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int isempty(){
    if((top==-1))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void push(){
    if (isfull())
    {
        printf("Stack is full");
    }
    else
    {
        printf("Enter element to add");
        scanf("%s",x);
        top++;
        strcpy(a[top],x);
    }
}

int pop(){
    if(isempty())
    {
        printf("Stack is empty");
        exit(0);
    }
    else{
        strcpy(x,a[top]);
        printf("%s",x);
        top--;
    }
}

void display()
{
    if(isempty())
    {
        printf("NO data to display");
        exit(0);
    }
    else
    {
        int i;
        for(i=0;i<top+1;i++)
        {
            printf("%s \n",a[i]);
        }
    }
}

在此解決方案中,我們使用了兩個主要更改

說明

char x更改為char x[max] ,還將aint [max]更改為char a[max][max] 現在這樣做可以存儲以null結尾的char數組。 二維字符數組用於在此處實現堆棧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM