我有以下代码 我“米转换存储到链接列表字符串 例: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;
}
谢谢!
你没有描述所需的输出 – user590028 2015-02-09 17:44:42
@ user590028是不是很明显..我说我试图将字符串复制到链接列表。 – psychoCoder 2015-02-09 18:29:24