回 帖 发 新 帖 刷新版面

主题:单链表头插法的一个问题

void CreateFromTail(LinkList L) 

Node *r, *s; 
int c; 
int flag =1; /*设置一个标志,初值为1,当输入"$"时,flag为0,建表结束*/ 
r=L; /*r指针动态指向链表的当前表尾,以便于做尾插入,其初值指向头结点*/ 
while(flag) /*循环输入表中元素值,将建立新结点s插入表尾*/ 

c=getchar(); 
if(c!='$') 

s=(Node*)malloc(sizeof(Node)); 
s->data=c; 
r->next=s; 
r=s; 

else 

flag=0; 
r->next=NULL; /*将最后一个结点的next链域置为空,表示链表的结束*/ 




一般定义都是以字符来结束,但如果题目要求是用-100来结束应该怎样改?我将c定义为整形后修改c!='-100'发现‘ ’内的只能是字符,超过2位数都可以编译可以运行但输入‘ ’内的数字后没反应

回复列表 (共3个回复)

沙发

参考下面的一个程序,不知道你是否能理解?
#include "stdio.h"
#include "malloc.h"
#define  LEN sizeof (struct student )
struct student
{int num;
 int score;
 struct student *next;
 };
struct student *creat()
{struct student *head,*p1,*p2;
    int n=0;
    head=NULL;
    p1=(struct student *)malloc(LEN);
    scanf("%d%d",&p1->num,&p1->score);
    while (p1->num!=0)
    {n++;
     if(n==1) head=p1;
     else     p2->next=p1;         /*结点的链接的语句*/
                 p2=p1;
     p1=(struct student *)malloc(LEN);
     scanf("%d%d",&p1->num,&p1->score);
    }
          p2->next=NULL;                  
         return(head);   
}

板凳


将if(c!='$')改为if(c!=-100)就行了。

3 楼


将if(c!='$')改为if(c!=-100)就行了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册