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

时间:2017-05-28 17:19:03

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

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

#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);
}

3 个答案:

答案 0 :(得分:3)

简单错误:在全局分配内存时,启动函数调用(malloc也是一个函数)。函数调用只能在main或其他函数内部进行。因此,请宣布不要在全球范围内使用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为null或者没有分配一些内存并将其分配给head,则在你的addnode函数内部。

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

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

答案 1 :(得分: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));
               ^

答案 2 :(得分:0)

我不确定这是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);
    }

}