c - 创建字符链接列表并打印它



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

#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())!='')
        addnode(ch);
    for(p1=head;p1!=NULL;p1=p1->next)
        printf("n%c",p1->ch);
}

首先是简单的错误:当你在全局中分配内存时,你发起了一个函数调用(malloc也是一个函数(。函数调用只能在主函数或其他函数中进行。所以只需声明头部不要在全局中使用 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);
}
  • 第二个错误:在你的addnode函数中,当你chekk是否head为null或不分配一些内存并将其分配给head时。

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

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

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

#include<stdio.h>
#include<stdlib.h>
struct Node{
    char ch;
    struct Node *next;
};
struct Node head={'',NULL};
struct Node *p1=NULL;
void add(char ch){
    if(head.ch=='')
        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("%cn",p1->ch);
}

但我仍然收到警告说,

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

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

我不确定这是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... rn");
            head=(struct node *)malloc(sizeof(struct node));
            head->ch=ch;
            head->next=NULL;
            return head;
        }
        else {
            struct node *New=NULL;
            printf("......return ... rn");
            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... rn");
        head=(pNODE)malloc(sizeof(NODE));
        head->ch=ch;
        head->next=NULL;
        return head;
    }
    else {
        struct node *new=NULL;
        printf("......return ... rn");
        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);
    }
}

最新更新