c语言建立链表并输出

Python016

c语言建立链表并输出,第1张

我帮你稍微改了一下,其他你自己改吧,我也刚学c数据结构,给你个例子你可以选择性参考//我自己写的。。

linklist *Creatlist(linklist*L){

L=(linklist*)malloc(sizeof(linklist))

L->next=NULL

return L

}

int Judge(linklist *L){

if(L->next==NULL)

{

printf("建表成功...\n")

}

else

printf("建表失败.\n")

return 0

}

int Input(linklist *L,int n){

int i=1,x

linklist *p,*q,*r

r=L

q=L

if(L->next==NULL)

{

for(i=1i<=ni++)

{

printf("请输入%d个数据:",i)

scanf("%d",&x)

p=(linklist*)malloc(sizeof(linklist))

p->data=x

p->next=NULL

r->next=p

r=r->next

}

printf("输入的数据为:")

for(i=1i<=ni++)

{

q=q->next

printf("%d ",q->data)

}

printf("\n")

}

else

printf("输入失败.\n")

return 0

}

需求有点不清晰,你要从文件里取什么东西出来?

我改了从txt取每一行的字符串出来,记录在你的链表,你参考一下

#include

"stdafx.h"

#include

"stdlib.h"

int

main()

{

struct

fac

{

//int

data

char

data[256]

//不知道你要取什么数据,这里用个字符串数组代替

struct

fac

*next

}*phead

int

i

FILE

*fp=fopen("d:\\text.txt","rb")

//一个有内容的txt文本,自己替换

struct

fac

*p

struct

fac

*ptemp

phead=(struct

fac*)malloc(sizeof(struct

fac))

phead->next=NULL

ptemp=phead

//fread(p,sizeof(struct

fac),1,fp)

while(fgets(

ptemp->data,256,fp

)!=NULL)//改用fgets取一行的数据

{

printf("%s\n",ptemp->data)

p=(struct

fac*)malloc(sizeof(struct

fac))

ptemp->next=p

ptemp

=

ptemp->next

}

//后面还应该有个释放链表的操作,这里程序结束会回收,就不写了。

}

#include <cstdio>

#include <cstdlib>

 

typedef struct poi

{

    char num[10]

    char name[20]

    int age

    struct poi *next

}pointer

 

pointer* head,* tail

 

pointer* newnode()

{

    pointer* u=(pointer*) malloc(sizeof(pointer))//分配一个动态地址。这个函数要记下里。同时要开cstdlib头文件 

    u->next=NULL

}

 

int main()

{

    head=newnode()//创建一个新的指针。 

    tail=head

    for (int i=1i<=5i++)

        {

            tail->next=newnode()

            tail=tail->next

            //你可以输入数据然后存入指针中。比如scanf("%d",&tail->age)然后给tail->num什么的赋值。 

        }

    pointer* u=head->next

    while (u!=NULL)

        {

            //输出什么东西。。。比如printf("%d\n",u->age) 

            u=u->next

        }

    return 0

}