【问题标题】:inserting string in stack using linked list使用链表在堆栈中插入字符串
【发布时间】:2015-07-20 17:28:53
【问题描述】:

我对下面的代码感到困惑。为什么输入会覆盖堆栈中的元素。

例如我先输入“abc” 那么堆栈将是“123” 但是当我输入另一个字符串时,让我们说“234” 堆栈将为“234”“234”

但是当我使用 int 数据类型而不是 char 作为输入时,没有错误。

谢谢!

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

struct node{
    char* info;
    struct node *ptr;
}*top,*top1,*temp;

int count = 0;

/* Push data into stack */
void pushy(char* data){
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        strcpy(top->info, data);
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->ptr = top;
        strcpy(temp->info, data);
        top = temp;
    }
    count++;
 }

/* Display stack elements */
void display(){
    top1 = top;

    if (top1 == NULL)
    {
        printf("Stack is empty");
        return;
    }

    while (top1 != NULL)
    {
        printf("%s ", top1->info);
        top1 = top1->ptr;
    }
 }

/* Pop Operation on stack */
void pop(){
    top1 = top;

    if (top1 == NULL)
    {
        printf("\n Error : Trying to pop from empty stack");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %s", top->info);
    free(top);
    top = top1;
    count--;
}

/* Return top element */
char* topelement(){
    return(top->info);
}

int main()
{
    int ch=0;


    printf("\n 1 - Push");
    printf("\n 2 - Pop");
    printf("\n 3 - Top");
    printf("\n 4 - Display");
    printf("\n 5 - Exit");

    top = NULL;

    while (1)
    {
        char no[10]={NULL};
        char* e;

        printf("\n Enter choice : ");
        scanf("%d", &ch);

        if(ch==1){
            printf("Enter data : ");
            scanf("\n\n%s", &no);
            pushy(no);
        }
        else if(ch==2){
            pop();
        }
        else if(ch==3){
            if (top == NULL)
                printf("No elements in stack");
            else
            {
                e = topelement();
                printf("\n Top element : %d", e);
            }
        }
        else if(ch==4)
            display();            
        else if(ch==5)
            exit(0);
        else
             printf("Invalid");
   }
}  

【问题讨论】:

  • 快速浏览:您没有为struct node 的成员info 分配内存,因此可能所有info 都指向同一个内存位置。通常你会遇到分段错误。将strcpy 更改为strdup,以自动分配内存并创建字符串的副本。也不要忘记在pop函数中释放top-&gt;info之前的free(top)
  • 不要在 C 中转换 malloc

标签: c linked-list stack


【解决方案1】:

至少函数 pushy 具有未定义的行为,因为它试图覆盖未由函数分配的内存

void pushy(char* data){

    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        strcpy(top->info, data);
        ^^^^^^^^^^^^^^^^^^^^^^^ 
    }
    //...

您应该首先分配将由数据成员top-&gt;info 指向的内存,您将在其中复制参数data 指向的字符串

例如

void pushy(char* data){

    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        top->info = malloc( ( strlen( data ) + 1 ) * sizeof( char ) );
        strcpy(top->info, data);
    }
    //...

函数可以这样写

void pushy( const char* data )
{
    temp = ( struct node * )malloc( sizeof( struct node ) );

    temp->ptr = top;
    temp->info = ( char * )malloc( ( strlen( data ) + 1 ) * sizeof( char ) );
    strcpy( temp->info, data );

    top = temp;
    ++count;
}

当您在函数pop() 中释放节点本身时,您还必须释放分配给top-&gt;info 的内存。
考虑到不需要像 global 那样声明变量 top1temp

也在本次通话中

scanf("\n\n%s", &no);

第二个参数应简单地指定为no 而不是&amp;no

在这次通话中

printf("\n Top element : %d", e);

您必须使用格式说明符 %s 而不是 %d

【讨论】:

  • @oreopeppermint 你是什么意思?
  • 你说函数 pushy 可以像第三个代码那样编写。所以我将函数 pushy 更改为该代码。我误会了吗?
  • @VladfromMoscow 第三个例子top-&gt;info = ( char * )malloc( ( strlen( data ) + 1 ) * sizeof( char ) ); strcpy( top-&gt;info, data );top 应该是temp。它的错字。
  • @BLUEPIXY 谢谢。由于“复制和粘贴”,这是一个错字。:)
  • @oreopeppermint 请参阅我的帖子以及更新的函数定义。:)
【解决方案2】:
scanf("\n\n%s", &no);

从上述语句中删除“&”。

另外,根据您的代码,最好将信息作为char array 而不是char *

struct node{
    char info[20];
    struct node *ptr;
}*top,*top1,*temp;

【讨论】:

    【解决方案3】:

    没有为节点中的字符串分配内存:

    top =(struct node *)malloc(1*sizeof(struct node));
    top->ptr = NULL;
    strcpy(top->info, data); 
    

    当调用strcpy 时top->info 指针无效。您必须分配内存,将其分配给指针 top->info,然后将字符串复制到该内存中。

    在释放整个节点之前,不要忘记释放持有我的信息指针的内存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2021-06-08
      • 2016-04-11
      • 2011-04-22
      • 2019-05-04
      • 1970-01-01
      相关资源
      最近更新 更多