2017-05-28 77 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。你幾乎完全忘記了這一點。