我有一个单链表想要打印出来代码如下:打印的全是问号!且个数为链表的长度的2倍加1,请高手出招吧:
typedef char DataType ;
typedef struct node
{
DataType dt;
struct node *next;
}ListNode;
typedef ListNode * LinkList;ListNode *p;
LinkList head;LinkList CreateListR1(void)
{
DataType ch;
LinkList head=(LinkList)malloc(sizeof(ListNode));
ListNode *s ,*r;
r=head;
while(ch=getchar()!=EOF)
{
s=(ListNode *)malloc(sizeof(ListNode));
s->dt=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
void PrintLinkList(LinkList l)
{
ListNode *p;
int n=0;
p=l;
while(p)
{
printf("%c\n",l->dt);
p=p->next;n++;
}
printf("There are %d Nodes in the LinkList!\n\n",n);
}
void main()
{
LinkList h=CreateListR1();
PrintLinkList(h);
}—————————————————————————————————
┏━★━━◆━━★━┓
♂欢|◢CSDN◣|使♂ ▲自由保存帖子,浏览,关注检测
┃迎|◥论坛助手◤|用┃ ▲完善的CSDN客户端工具
┗━☆━━◇━━━☆┛ ▲自动添加签名......让你更快,更爽,更方便地上CSDN...
http://www.csdn.net/expert/topic/573/573604.xml
http://www.chinaok.net/csdn/csdn.zip
typedef char DataType ;
typedef struct node
{
DataType dt;
struct node *next;
}ListNode;
typedef ListNode * LinkList;ListNode *p;
LinkList head;LinkList CreateListR1(void)
{
DataType ch;
LinkList head=(LinkList)malloc(sizeof(ListNode));
ListNode *s ,*r;
r=head;
while(ch=getchar()!=EOF)
{
s=(ListNode *)malloc(sizeof(ListNode));
s->dt=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
void PrintLinkList(LinkList l)
{
ListNode *p;
int n=0;
p=l;
while(p)
{
printf("%c\n",l->dt);
p=p->next;n++;
}
printf("There are %d Nodes in the LinkList!\n\n",n);
}
void main()
{
LinkList h=CreateListR1();
PrintLinkList(h);
}—————————————————————————————————
┏━★━━◆━━★━┓
♂欢|◢CSDN◣|使♂ ▲自由保存帖子,浏览,关注检测
┃迎|◥论坛助手◤|用┃ ▲完善的CSDN客户端工具
┗━☆━━◇━━━☆┛ ▲自动添加签名......让你更快,更爽,更方便地上CSDN...
http://www.csdn.net/expert/topic/573/573604.xml
http://www.chinaok.net/csdn/csdn.zip
typedef struct node
{
DataType dt;
struct node *next;
}ListNode;
typedef ListNode * LinkList;ListNode *p;
LinkList head;LinkList CreateListR1(void)
{
DataType ch;
LinkList head=(LinkList)malloc(sizeof(ListNode)); // 这儿不应该分配内存,改成 head = NULL;ListNode *s ,*r;
r=head;
while(ch=getchar()!=EOF)
{
s=(ListNode *)malloc(sizeof(ListNode));
s->dt=ch;
r->next=s; // if (head ==NULL) head = s; else r->next = s;
r=s;
}
r->next=NULL;
return head;
}
void PrintLinkList(LinkList l)
{
ListNode *p;
int n=0;
p=l;
while(p)
{
printf("%c\n",l->dt); // printf("%c\n",p->dt);
p=p->next;n++;
}
printf("There are %d Nodes in the LinkList!\n\n",n);
}
void main()
{
LinkList h=CreateListR1();
PrintLinkList(h);
}
┏━★━━◆━━★━┓
♂欢|◢CSDN◣|使♂ ▲自由保存帖子,浏览,关注检测
┃迎|◥论坛助手◤|用┃ ▲完善的CSDN客户端工具
┗━☆━━◇━━━☆┛ ▲自动添加签名......让你更快,更爽,更方便地上CSDN...
http://www.csdn.net/expert/topic/573/573604.xml
http://www.chinaok.net/csdn/csdn.zip
#include <malloc.h>
#include <stdio.h>
#include <conio.h>typedef char DataType;typedef struct node{
DataType dt;
node* next;
}ListNode;typedef ListNode* LinkList;LinkList CreateListR1(void)
{
bool mbFirst = true;
DataType ch;
LinkList head = new node; LinkList r = head;
while ((ch = getchar()) != EOF){
if (mbFirst){
mbFirst = false;
r->dt = ch;
r->next = NULL;
} else{
LinkList s = new node;
s->dt = ch;
r->next = s;
r = s;
}
getchar();
}
r->next = NULL; return head;
}void PrintLinkList(LinkList l)
{
LinkList p;
int n = 0; p = l;
while (p){
printf("node %d = %c\n", n, p->dt);
p = p->next;
n ++;
} printf("There are %d Nodes in the LinkList!\n\n",n);
}void main()
{
LinkList h = CreateListR1();
PrintLinkList(h); getch();
}
typedef struct node
{
DataType dt;
struct node *next;
}ListNode;
typedef ListNode * LinkList;ListNode *p;
LinkList head;LinkList CreateListR1(void)
{
DataType ch;
//LinkList head=(LinkList)malloc(sizeof(ListNode)); // 这儿不应该分配内存,改成 head = NULL;
head = NULL; ListNode *s ,*r;
r=head;
while( ((ch=getchar())!=EOF) && (ch != '\n'))
{
printf("%c",ch);
s=(ListNode *)malloc(sizeof(ListNode));
s->dt=ch;
//r->next=s; // if (hea1d ==NULL) head = s; else r->next = s;
if (head == NULL) head = s; else r->next = s;
r=s;
} r->next=NULL;
return head;
}void PrintLinkList(LinkList l)
{
ListNode *p;
int n=0;
p=l;
while(p)
{
//printf("%c\n",l->dt);
printf("%c\n",p->dt);
p=p->next;n++;
}
printf("There are %d Nodes in the LinkList!\n\n",n);
}void main()
{
LinkList h=CreateListR1();
PrintLinkList(h);
}// 我调试了一下,原来还有一个问题,就是
while (ch = getchar() != EOF) 这儿有问题getchar() != EOF的优先级竟然比赋值语句高:
改成
while( (ch = getchar()) != EOF)// 其余的改法都是可以的