c语言链表建立

c语言链表建立,第1张

#include

#include

struct

chain

{

int

value

struct

chain

*next

}

struct

chain

*create()

{

struct

chain

*head,*tail,*p

int

x

head

=

tail

=

null

while(scanf("%d",&x)==1)

{

p=(struct

chain*)malloc(sizeof(struct

chain))

p->value=x

p->next=null

if(head==null)

head

=

tail

=

p

else

tail=tail->next=p

}

return

head

}

struct

chain

*inlink(struct

chain

*head,int

a,int

b)

//int

a代表要插入的节缺好点,int

b代表创建节点的数据域

{

struct

chain

*p,*q,*s

s

=

(struct

chain

*)malloc(sizeof(struct

chain))

s->value=b

if(head==null)

{

head

=

s

head->next

=

null

}

if(head->value

==

a)

{

s->next=head

head

=

s

}

else

{

p=head

while((p->value!=a)&&(p->next!=null))

{

q=p

p=p->next

}

if(p->value

==

a)

{

q->next

=

s

s->next

=

p

}

else

{

p->next=s

s->next=null

}

}

return

(head)

}

struct

chain

*dellink(struct

chain

*head,int

a)

//int

a代表要删除的节点

{

struct

chain

*q,*p

if(head

==

null)

printf("找不到节点!\姿好n")

else

if(head->value

==

a)

{

p

=

head

head

=

head->next

}

else

{

p=head

while((p->value!=a)&&(p->next!=null))

{

q=p

p=p->next

}

if(p->value

!=

a)

printf("链表不存在此节点!\n")

else

{

q->next

=

p->next

free(p)

}

}

return

(head)

}

void

main()

{

struct

chain

*p,*q

q=create()

//链表的创建;

//q=inlink(create(),3,1)

//链表的插入;

//q=dellink(create(),2)

//链表的删除;

while(q){

//输出链表;

printf("%d\n"伏册铅,q->value)

p=q->next

free(q)

q=p

}

}

typedef

struct

tagLinkList{

int

val

struct

tagLinkList

*next

}LinkList,*pLinkList

void

main()

{

LinkList

*l,*h

l=(pLinkList)malloc(sizeof(LinkList))

scanf("宽稿%d",&l->val)

h=l

l=(pLinkList)malloc(sizeof(LinkList))

scanf("%d",&l->val)

h->next=l

printf("1=%d\n",h->val)

l=h->next

printf("2=%d\n",l->val)

}闲局碰的桐巧谈无聊,给你写个测试的,只是引导作用,可以参考


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12523985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存