【问题标题】:Creating Linked-list of characters and printing it创建字符的链接列表并打印它
【发布时间】:2017-10-29 00:37:39
【问题描述】:

我正在尝试使用此代码实现链接列表。此代码成功符合但导致分段错误(核心转储)错误。我该如何解决这个问题?

#include<stdio.h>
#include<stdlib.h>
struct node{
    char ch;
    struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
    if(head==NULL) {
        head->ch=ch;
        head->next=NULL;
    }   
    else {
        struct node *New=(struct node *) malloc (sizeof(struct node));
        for(p1=head;p1->next!=NULL;p1=p1->next);
            p1->next=New;
    }
}
void main() {
    char ch,frm,to;
    printf("\nEnter the string");
    while((ch=getchar())!='\0')
        addnode(ch);
    for(p1=head;p1!=NULL;p1=p1->next)
        printf("\n%c",p1->ch);
}

【问题讨论】:

  • 当你分配一个新节点时,你永远不会给它分配任何值。所以ch 是未知的,next 可以指向任何地方。 addnode(ch) 实际上并没有使用 ch 的事实应该是一个警告信号......
  • 罗杰那,谢谢;

标签: c string data-structures linked-list segmentation-fault


【解决方案1】:

这效果更好,我克服了错误:)。我错过了那里的指针清晰度,在这里得到了纠正..

#include<stdio.h>
#include<stdlib.h>
struct Node{
    char ch;
    struct Node *next;
};
struct Node head={'\0',NULL};
struct Node *p1=NULL;
void add(char ch){
    if(head.ch=='\0')
        head.ch=ch;
    else{
    struct Node *new=(struct node *)malloc(sizeof(struct Node));
    new->ch=ch;
    for(p1=&head;p1->next!=NULL;p1=p1->next);
    p1->next=new;
    }
}
void main(){
    char c;
    while((c=getchar())!='\n')
        add(c);
    for(p1=&head;p1!=NULL;p1=p1->next)
        printf("%c\n",p1->ch);
}

但我仍然收到警告说,

从不兼容的指针类型初始化[默认启用]

struct Node *new=(struct node *)malloc(sizeof(struct Node));
               ^

【讨论】:

  • 是的!它会。由于拼写错误:在 struct Node *new=(struct ///n///ode *)malloc(sizeof(struct Node)) 中将 n 设为大写 ​​N休息就够了
  • malloc的返回不需要强制转换,没有必要。有关详细说明,请参阅:Do I cast the result of malloc?
【解决方案2】:

我不确定这是 c 方式..但是您必须考虑您的代码如何释放分配的指针...可能像空闲列表函数..

这是我的方式。

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

    struct node{
        char ch;
        struct node *next;
    };

    struct node * addnode(struct node *head, struct node *p1, char ch) {
        if(head==NULL) {
            printf("......return 2... \r\n");
            head=(struct node *)malloc(sizeof(struct node));
            head->ch=ch;
            head->next=NULL;

            return head;
        }
        else {
            struct node *New=NULL;
            printf("......return ... \r\n");

            New=(struct node *) malloc (sizeof(struct node));
            New->ch = ch;
            New->next=NULL;

            for(p1=head;p1->next!=NULL;p1=p1->next);

            p1->next=New;

            return head;

        }
    }

    void main() {

        char ch,frm,to;
        struct node *head=NULL, *p1=NULL;

        printf("\nEnter the string \n");


        while((ch=getchar())!='q')
            head = addnode(head, p1, ch);

        for(p1=head;p1!=NULL;p1=p1->next)
        {
            printf("\n%c",p1->ch);
        }

    }

另一个。

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

typedef struct node{
    char ch;
    struct node *next;
} *pNODE, NODE;


pNODE addnode2(pNODE head, pNODE p1, char ch) {
    if(head==NULL) {
        printf("......return 2... \r\n");
        head=(pNODE)malloc(sizeof(NODE));
        head->ch=ch;
        head->next=NULL;

        return head;
    }
    else {
        struct node *new=NULL;
        printf("......return ... \r\n");

        new=(pNODE) malloc (sizeof(NODE));
        new->ch = ch;
        new->next=NULL;

        for(p1=head;p1->next!=NULL;p1=p1->next);

        p1->next=new;

        return head;

    }
}

void main() {

    char ch,frm,to;
    pNODE head=NULL;
    pNODE p1=NULL;

    printf("\nEnter the string \n");


    while((ch=getchar())!='q')
        head = addnode2(head, p1, ch);

    for(p1=head;p1!=NULL;p1=p1->next)
    {
        printf("\n%c",p1->ch);
    }

}

【讨论】:

  • 但这也导致了同样的错误,我发现这是通过转换新节点发生的。然后我将代码更改为struct Node *new=malloc(sizeof(struct Node)); 但是非常感谢新的方式
  • 嗯,new 是某种 c++ 关键字 pal。
  • 好吧,new 是某种 c++ 关键字 pal。它不会用你的 c++ 编译器编译。我也有任何微软或 gcc 编译器的警告消息。我刚刚添加了一个示例。
【解决方案3】:

简单的错误首先:当你在全局分配内存时,你会发起一个函数调用(malloc 也是一个函数)。函数调用只能在 main 或其他函数内部进行。所以只需声明 head 不要在全局中使用 malloc 。

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

struct node{
char ch;
struct node *next;
};
struct node *head=NULL;

struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
    struct node *New=(struct node *) malloc (sizeof(struct node));
    head=New;
    New->ch=ch;
    New->next=NULL;
}

else {
    struct node *New=(struct node *) malloc (sizeof(struct node));
    New->ch=ch;
    New->next=NULL;
    for(p1=head;p1->next!=NULL;p1=p1->next);
        p1->next=New;
}
}

void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\n')
    addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
    printf("\n%c",p1->ch);
}
  • 第二个错误:当您检查 head 是否为空或未分配一些内存并将其分配给 head 时,在 addnode 函数内部。

  • 第三个错误:在您的 getchar() 中检查,直到找到一个新行而不是空字符。

  • 第四个错误:将 ch 分配给 New 并设置 New->next=null。你几乎完全忘记了这一点。

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多