2017-05-28 73 views
1

我想要使用此代码实现链接列表。此代码成功顺利但结果在分段错误(核心转储)错误。如何解决此问题?创建链接的字符列表并打印它

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

当你分配一个新节点时,你永远不会为它分配任何值。所以'ch'将是未知的,并且'next'可能指向任何地方。 'addnode(ch)'实际上并不使用'ch'这个事实应该是一个警告信号...... – jasonharper

+0

罗杰那,谢谢你; –

回答

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)); 
      ^
+1

“new”是一个C++命令,你应该重命名为 – Thomas

+0

Yepp!它会。由于存在拼写错误:将n作为首字母N在 struct Node * new =(struct /// n /// ode *)malloc(sizeof(struct Node));休息足够好 –

+0

没有必要施放'malloc'的返回,这是没有必要的。请参阅:[**我是否将malloc的结果?**](http://stackoverflow.com/q/605845/995714)进行了详细说明。 –

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

} 
+0

但是这也导致了同样的错误,我发现通过投射新节点发生了错误。然后我将代码更改为, 'struct Node * new = malloc(sizeof(struct Node));' 但是非常感谢为新的**方式** –

+1

好吧,新是一些C++关键字朋友。 – tommybee

+1

好吧,新是一些C++关键字朋友。它不会用你的C++编译器进行编译。我也有微软或gcc编译器的任何警告消息。我刚刚添加了一个例子。 – tommybee

3

第一个简单的错误:当你在全局分配内存时,你启动一个函数调用(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如果头为空或不分配一些内存并指定为负责人。

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

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