c语言链表建立

Python011

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

}

}

#include <stdio.h>

#include <stdlib.h>

typedef int elemtype

typedef struct Lnode {

elemtype data

Lnode *next

}Lnode

Lnode *CreatList(Lnode *Head) {

Head = (Lnode *)malloc(sizeof(Lnode))

Head->next = NULL

Lnode *p = Head

printf("请输入元素的个数:")

int i,n

scanf("%d", &n)

for(i = 0 i < n ++i) {

p->next = (Lnode *)malloc(sizeof(Lnode))

printf("请输入第%d个元素:",i + 1)

scanf("%d", &p->next->data)

p = p->next

}

p->next = NULL

return Head

}

void AllList(Lnode *head) {

Lnode *p = head->next

while(p) {

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

p = p->next

}

printf("\n")

}

int main() {

Lnode *head = NULL

head= CreatList(head)

AllList(head)

return 0

}

主函数这里

Linklist List

printf("输入创建链表的长度:")

scanf("%d",&num)

CreateList_H(List,num) //创建链表

改为 

LNode List

printf("输入创建链表的长度:")

scanf("%d",&num)

CreateList_H(&List,num) //创建链表

函数内在堆上分配好内存,但是 没有传递到栈上

另外 你的变量名很迷人