2015-02-09 91 views
1

我有以下代码 我“米转换存储到链接列表字符串 例:ABC A-> B-> C-> NULL字符串链表使用双指针

问题 : 当打印列表中,它是不给所需output.Following是代码和样品输入/输出

代码

#include<stdio.h> 
#include<stdlib.h> 
typedef struct node 
{ 
    char ch; 
    struct node *next; 
}node; 
void create(node **head,char ch) 
{ 
    node *new; 
    new=malloc(sizeof(node)); 
    new->next=NULL; 
    new->ch=ch; 
    if(*head==NULL) 
    { 
     *head=new; 
     printf("%c",(*head)->ch); 
     return ; 
    } 
    while((*head)->next) 
    { 
     (*head)=(*head)->next; 
    } 
    (*head)->next=new; 


} 
void printList(node *head) 
{ 
    printf("\nThe list has - "); 
    while(head) 
    { 
     printf("%c",head->ch); 
     head=head->next; 
    } 
    printf("\n\n"); 
} 
int main() 
{ 
    node *head=NULL; 
    int i=0; 
    char *str=NULL; 
    str=malloc(sizeof(char)*15); 
    printf("\nEnter the string - "); 
    scanf("%s",str); 

    while(str[i]!='\0') 
    { 
     create(&head,str[i]); 
     i++; 
    } 
    printList(head); 
    return 0; 
} 

采样输入/输出

输入1

Enter the string - abc 
a 
The list has - bc 

输入2

Enter the string - abcde 
a 
The list has - de 

输入3

Enter the string - ab 
a 
The list has - ab 

注:

如果我改变我的创造功能,这一点,一切都只是正常工作! 我想知道这里有什么区别? 它与双指针有关吗?

void create(node **head,char ch) 
{ 
    node *new,*ptr; 
    new=malloc(sizeof(node)); 
    new->next=NULL; 
    new->ch=ch; 
    ptr=*head; 
    if(ptr==NULL) 
    { 
     ptr=new; 
     return; 
    } 
    while(ptr->next) 
    { 
     ptr=ptr->next; 
    } 
    ptr->next=new; 

} 

谢谢!

+1

你没有描述所需的输出 – user590028 2015-02-09 17:44:42

+0

@ user590028是不是很明显..我说我试图将字符串复制到链接列表。 – psychoCoder 2015-02-09 18:29:24

回答

3

有在第一代码剪断你插入功能的问题,即你的时候移动*head,所以你插入的最后一个节点到头部的最后一个节点

a->b->c->d 
     | 
     | 

Head is at c now 
前指向一个列表

所以你不应该移动头部,只使用临时变量来获得头部的值和移动温度。

a->b->c->d 
|  | 
|  | 
Head temp 

有它的东西做的双指针?

不,它只是在第二个片段中使用ptr作为临时指针,并且不会移动您的代码,如上所示。

+0

由于我在while循环中使用了条件'(* head-> next)',因此当'(* head)'应该指向'd'并且'(* head-> next = NULL)'。当head指向'c'时,为什么循环会退出? – psychoCoder 2015-02-09 18:43:47

+1

@NitinPandey当(* head)指向c'(* head) - > next = NULL' ..这就是当你继续前进并添加一个节点(d),所以现在你的'* head'呆在那里..明白了? – Gopi 2015-02-09 18:45:23

+0

哦!是的是的..我在想什么?得到它了 !谢谢 – psychoCoder 2015-02-09 19:39:16

0

Gopi已经指出了你的代码的问题。如果区分将第一个节点插入空列表的两种情况(在这种情况下,您必须更新head)并将其附加到现有列表,您可以使用该建议来插入新节点。 (您已经看到了两种情况。)

但是指针指针策略增加了一个间接级别,您可以在这里使用这个级别,但没有区分:head保存指向头节点的指针。如果使用head来遍历列表,则head应始终指向指向当前节点的指针。如果当前节点是NULL,指定新的节点,即覆盖指针:

void create(node **head, char ch) 
{ 
    /* create new node */ 
    node *nd = malloc(sizeof(*nd)); 
    nd->next=NULL; 
    nd->ch=ch; 

    /* advance to end of list */ 
    while (*head) { 
     head = &(*head)->next; 
    } 

    /* assign */ 
    *head = nd; 
} 

顺便说一句,你的第二个功能不工作得很好,因为你永远不更新的头。你会得到一个空列表和内存泄漏。

+0

它的工作原理是,我永远不会改变头部,并使用临时指针'ptr'遍历列表,并在最后插入。那么这种方法有什么问题吗? – psychoCoder 2015-02-09 18:33:24

+0

当我运行你的代码并用上一段代码中的代码替换'create'函数时,我得到了我期望的结果:一个空的列表。用临时指针遍历列表是可以的,因为您不会更改列表。但是,当列表可以更改时,您必须反映更新。这里至关重要的情况是,当你将第一个节点添加到一个空列表时:'head'必须改变;插入后不能为NULL。之后,你的方法就可以工作,因为你改变了节点结构中的一个值。 – 2015-02-09 20:05:26

+0

噢,我有一个不同的代码,你是对的,我忘了在第二个代码片段的'(ptr == NULL)'部分中放置'* head = new'。在我的笔记本电脑上,我在那里工作!刚才注意到了 – psychoCoder 2015-02-10 06:04:58