当前位置:首页 » 编程语言 » c语言单链表基础知识
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言单链表基础知识

发布时间: 2022-06-06 21:01:22

1. c语言如何创建单链表

C语言创建单链表如下:

#include"stdio.h"

#include"stdlib.h"

#include"malloc.h"

#include "iostream.h"

typedef struct node

{

intdata;

node * next;

}node , * List;

void create(int n)

{

int c;

List s,L;

L=(List)malloc(sizeof(node));

L->next=NULL;

printf("请输入第1个数据:");

scanf("%d",&c);

L->data=c;

for(int i=2;i<=n;i++)

{

s=(List)malloc(sizeof(node));

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

scanf("%d",&c);

s->data=c;

s->next=L;

L->next =s;

}

printf("链表创建成功!");

}

void main()

{

int n;

printf("请你输入链表的个数:");

scanf("%d",&n);

create(n);

}

2. C语言中有关链表的基础知识

链表的关键就在这里:
struct
node
*
pnext;
你要遍历的时候,可以通过pnext获得下一个struct
node的地址。
比如struct
node
a,它的下一个是struct
node
b,这样,a->pnext
=
&b

3. 用C语言编程实现单链表的基本操作

第二个显示为什么?为什么什么东西都没有、
#include<stdio.h>
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//创建单链表,头插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n");
head->next=NULL;
head->ch
=
'\0';
int
i=
0;
printf("请一次输入A-Z:\n");
for(i
=
0
;
i
<
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf("aply
error\n");
scanf("%c",&p->ch);
p->next
=
head->next;
head->next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next;
while(p->next)
{
p
=
p->next;
}
int
i
=
0
;
for(i
=
0
;
i
<
3;
i++)
{
p->next
=
r;
p
=
p->next;
q
=
q->next;
r
=
q;
}
p->next
=NULL;
head->next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//链表初始化
ListPrint(head);
//打印单链表数据
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。

4. c语言数据结构单链表是什么

head=(LNode *)malloc(sizeof(LNode));

这一句不要,没啥用处,除非你head指向的节点也就是第一个节点的data不需要数据

head->next=NULL;这里修改为head=NULL;
让head先指向NULL,也就是没有节点
其实这个可以不要,再主函数中,先让链表是空链表即可(即让head=NULL)

head->data=data;
head->next=p->next;
head->next=p;
关键在这里
你仔细考虑一下,一般来说头插法的head只是一个指针,不要对head指向的那个节点操作,对p操作完成后,让head指过去即可
所以修改为
p->data=data; //赋值过去,因为你现在申请了p的内存空间
p->next=head; //把head指向的那个节点连接到p的后面,这样完成头插
// 这是head没有用了,p成为链表的头指针
head=p; //head再指向这个链表的头部,也就是p指向的节点,为下一次循环做准备

head=Createlist(head);//链表初始化
主函数中这样不太好,建议不要重名

5. 我是c语言的初学者(自学)学到链表是看不太懂了,现求简单一点的链表的入门知识

先把指针看明白了,
不论单链表还是双向链表都是一指针为基础的.指针在c语言中很重要,也是最难明白的,好象现在流行的其它高级语言都不支持指针了.只要能把指针弄明白了,链表就简单了.