首页 > 编程知识 正文

结构体c语言链表,c语言数据结构链表

时间:2024-03-25 09:50:10 阅读:332979 作者:GOPA

本文目录一览:

C语言中,结构体与链表是什么关系?

可以用结构体来实现链表啊。结构体相当于一种数据类型。链表是数据结构的一种,可以用结构体来实现链表。

希望采纳

c语言结构体链表

结构体定义指针应该是这样的: node *p;或者struct Node *p;

在定义/声明函数时,void as(struct node *p);这样是不对的。应该是这样:

void as(struct Node *p);

或者

void as(node *p);

函数调用的时候不用指针直接放入结构体该是这样调用的:

node stnod;

as(stnod);

它和

node stnod,*p_stnod;

p_stnod=stnod;

as(p_stnod);

作用是一样的。

C语言链表概念

简单说来,就是通过指针指向,把两个结构体连接起来。比如定义下面这个结构体

struct node

{

int data;

struct node *next;

}

可以看到结构体里面定义了一个自身类型的指针,通过让指针指向另外一个结构体,我们就能通过结构体里面的next变量访问下个结构体里面的内容,而通过下一个结构体,同样可以通过下一个结构体的next指向,找到下一个这种类型的结构体,这样就形成了所谓的链表。

C语言 怎么把结构体数组写入链表?

1.用头插法。因为数据追加和删除比较多,追加的话,头插法可以直接插,用尾插降低了时间效率,删除用两个一样。

2./*结构体定义*/

struct client{

char account[14];

char name[10];

char identity[20];

char address[15];

long int money;

};

/*链表结点定义*/

struct node{

struct client band_inf;

struct node *next;

};

应该把结构体结点定义成链表的成员,这样链表才对。如果像你那样定义的话,完全不用定义结构体,链表就搞定了。因为你在链表里面把结构的成员都又定义了。

3.

1),定义结点:p1=(struct node*)malloc(sizeof(struct node)) ;表示定义一个node型的结点指针

使用,这个要看具体怎么用了。比如说删除searchp,priorp是searchp的前面一个结点,这样写

priorp-next=searchp-next;

delete searchp; 即可

插入newnode在searchp的后面,可以这样:

newnode-next=searchp-next;

C语言中怎样用链表保存结构体数据(动态数据结构)

链表有多种形式,如:单向链表,双向链表,单向循环链表,双向循环链表。将链表结构定义为list_t,则该类型中一定(至少)存在一个指向下一节点的指针list_t

*next;除了这个指针,list_t

中可以包含其它类型的数据,包括结构体变量。比如:typedef

struct

{

struct

usr_struct

data;

list_t

*next;

}

list_t;

C语言链表简介和举例!

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,链表比较方便插入和删除操作。

/*

*对链表的综合操作

*功能有建立,排序,插入,删除,输出

*/

#includestdio.h

#includemalloc.h

typedef int ElemType;

typedef struct NodeType

{

ElemType data;

struct NodeType *next;

} NodeType,*LinkType;

LinkType create()

{//建立链表,返回链表的首地址,头结点没有数据

LinkType head,p1,p2;

head=(LinkType)malloc(sizeof(NodeType));

p1=head;

while(p1-data!=0)//当data=0时链表结束

{

p2=p1;

p1=(LinkType)malloc(sizeof(NodeType));

printf("Enter student's information:ndata=");

scanf("%d",p1-data);

p2-next=p1;

}

p2-next=NULL;

free(p1);

return(head);

}

void output(LinkType head)

{//链表的输出,接收链表的首地址

head=head-next;

while(head!=NULL)

{

printf("data=%dn",head-data);

head=head-next;

}

}

LinkType sort(LinkType head)

{//链表排序,接收链表首地址,返回链表首地址

LinkType ph,p1;

ElemType temp;

ph=head-next;

p1=head-next;

while(p1-next!=NULL)//冒泡法

{

ph=head;

while(ph-next!=NULL)

{

if(ph-dataph-next-data)//按data由小到大排序

{

temp=ph-data;

ph-data=ph-next-data;

ph-next-data=temp;

}

ph=ph-next;

}

p1=p1-next;

}

return(head);

}

LinkType del(LinkType head)

{//删除结点,接收链表的首地址,返回链表的首地址

ElemType DelData;

LinkType ph,p;

ph=head-next;

printf("Enter the data you want to del:nDelData=");

scanf("%d",DelData);

while(ph!=NULL ph-data!=DelData)//寻找要删除的结点

{

p=ph;

ph=ph-next;

}

if(ph==NULL)//没有找到要删除的结点

{

printf("Enter error!n");

return(head);

}

else

{

if(ph==head-next)//删除头结点

{

head-next=ph-next;

}

else//删除其它结点

{

p-next=ph-next;

}

}

free(ph);

return(head);

}

LinkType insert(LinkType head)

{//插入结点,接收链表首地址,返回链表首地址

LinkType ph,p,insert,temp;

insert=(LinkType)malloc(sizeof(NodeType));

printf("Enter the data you want to insert:ndata=");

scanf("%d",insert-data);

ph=head-next;

while(ph!=NULL ph-data insert-data)//寻找插入的位置

{

p=ph;

ph=ph-next;

}

if(head-next-data insert-data)//插入头部

{

temp=head-next;

head-next=insert;

insert-next=temp;

}

else//插入到其它地方

{

p-next=insert;

insert-next=ph;

}

return(head);

}

void main()

{

LinkType head;

head=create();

output(head);

printf("nn");

head=sort(head);

output(head);

printf("nn");

head=del(head);

output(head);

printf("nn");

head=insert(head);

output(head);

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。