当前位置:首页 » 编程语言 » c语言实现的链表

c语言实现的链表

发布时间: 2022-07-11 03:04:34

① 如何实现c语言链表

链表的创建:

#include “stdlib.h”
#include “stdio.h”
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};

int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf(“%ld,%f”,&p1->num,&p1->score);
head=NULL;
while(p1->num != 0)
{
n=n+1;
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf(“%ld,%f”,&p1->num,&p1->score);
}
p2->next = NULL;
return(head);
}

void main()
{
creat();
}

这样便可创建链表

② 用C语言实现对单链表的基本操作

#include<stdio.h>
#include<stdlib.h>

typedefintDataType;

typedefstructnode{
DataTypemember;
structnode*next;
}*LinkList,*pNode;

//初始化链表
LinkListGetEmptyList(){
LinkListhead=(pNode)malloc(sizeof(structnode));
head->member=0;
head->next=NULL;
returnhead;
}


//在非增链表中插入结点
voidInsertNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p->next!=NULL;p=p->next){
if(p->next->member<=x){
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
return;
}
}
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
}

//新结点插入为首结点
voidPushNode(LinkListhead,DataTypex){
pNodep=(pNode)malloc(sizeof(structnode));
p->member=x;
p->next=head->next;
head->next=p;
}

//删除结点
intDeleteNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p!=NULL;p=p->next){
if(p->next->member==x){
q=p->next;
p->next=q->next;
free(q);
return1;//成功删除member(第一个)为x的结点
}
}
return0;//没有找到member为x的结点
}

//查找结点
intFindNode(LinkListhead,DataTypex){
pNodep;
for(p=head->next;p!=NULL;p=p->next){
if(p->member==x)return1;//找到了
}
return0;//没有找到
}

//销毁链表
voidDestroyList(LinkListhead){
pNodeq,p=head;
while(p){
q=p;
p=q->next;
free(q);
}
head=NULL;
}

//遍历链表
voidShowList(LinkListhead){
pNodep=head->next;
while(p!=NULL){
printf("%d",p->member);
p=p->next;
}
printf(" ");
}

intmain(){
DataTypex,res;
LinkListhead=GetEmptyList();
printf("输入一个整数('q'toquit):");
while(scanf("%d",&x)==1){
InsertNode(head,x);//创建非增链表
printf("输入一个整数('q'toquit):");
}
fflush(stdin);
ShowList(head);
printf("输入待查找的整数:");
scanf("%d",&x);
res=FindNode(head,x);
if(res)printf("找到了。 ");
elseprintf("没找到! ");
printf("输入待删除的整数:");
scanf("%d",&x);
res=DeleteNode(head,x);
if(res)printf("成功删除。 ");
elseprintf("没找到数据为:%d的结点! ",x);
ShowList(head);
DestroyList(head);
return0;
}

③ 用c语言写一个简单的链表,具体要怎么用代码实现

struct
stu
{
char
a[10];
char
e[4];
char
c[14];
char
d[30];
struct
stu*b;
};
struct
stu
*creat(struct
stu*head,char
c)
//功能1:创建链表
{
while(c-'y'==0||c-'y'==0)
{
printf("请输入姓名,性别,电话号码,e-mail地址\n");
head=insert(head);
printf("是否继续输入y/n\n");getchar();
scanf("%c",&c);system("cls");
}
if(c-'n'==0||c-'n'==0)
return
head;
else
{
printf("是否继续输入y/n\n");
getchar();
scanf("%c",&c);
system("cls");
return
(creat(head,c));
}
}

④ 用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;
}
看看。

⑤ 实现链表(C语言)

链表应该没有很高效的查找算法吧,只有一个一个按顺序查找啊

不过可以设置两个指针变量,
link* p1,p2;
p1用来遍历
p2用来记录与p2相差K-1个节点
当p1遍历到完链表时,p2就是了

⑥ 如何用C语言编写一个链表

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

struct Node
{
int data;//数据域
struct Node * next;//指针域
};

/*************************************************************************************
*函数名称:Create
*函数功能:创建链表.
*输入:各节点的data
*返回值:指针head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函数名称:insert
*函数功能:在链表中插入元素.
*输入:head 链表头指针,p新元素插入位置,x 新元素中的数据域内容
*返回值:无
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函数名称:del
*函数功能:删除链表中的元素
*输入:head 链表头指针,p 被删除元素位置
*返回值:被删除元素中的数据域.如果删除失败返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函数名称:print
*函数功能:打印链表中的元素
*输入:head 链表头指针
*返回值:无
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函数名称:main
*函数功能:主函数创建链表并打印链表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}

⑦ C语言里面的链表是什么

C语言里面的链表是一种数据结构
是一种线形的存储结构
链表和数组一样,也是将一组同类型的数据组织在一起的一种数据结构
不同的是
数组采用的是顺序存储,依靠数组的首地址和元素的相对地址(下标)来实现访问
优点是访问方便快捷,而缺点是数组是静态的,不利于实现元素的动态增减。
而链表采用的是离散存储,依靠节点间的指向下一个节点的指针来实现访问。
其优缺点和数组相反

⑧ 用C语言实现链表的算法

这个是我们数据结构上机实验的链表问题,
#include<stdio.h>
#include<malloc.h>
#define
LEN
sizeof(LinkNode)
typedef
int
Datatype;
typedef
int
Status;
typedef
struct
LinkNode{
Datatype
data;
struct
LinkNode
*next;
}
LinkNode,*LinkList;
typedef
struct
OrderedList
{
LinkNode
*head,*tail;
int
Listsize;
}
OrderedList;//有序循环链表的头节点head,尾接接节点
tail及长度Listsize
Status
InitList(OrderedList
*List)//生成循环链表头节点
{
List->tail=List->head=(LinkList)malloc(LEN);
if(List->head==NULL)
return
0;
else
{
List->head->next=List->tail;
List->tail->next=List->head;
List->Listsize=0;
return
1;
}
}
void
OrderedInsert(OrderedList
*List,Datatype
data)//每调用一次有序插入data形成有序的(从小到大)的链表
{
LinkNode
*p
,*q;
if(List->head==List->tail->next)
{
p=(LinkNode*)malloc(LEN);
p->data
=
data;
List->head->next=p;
p->next=List->tail;
List->Listsize++;
}
else
{
p=List->head->next;
q
=
List->head;
while(p->data<data&&p!=List->tail)
{
q
=
p;
p=p->next;
}
if(p->data==data)
{printf("YOu
have
input
the
same
datas
%d\n\t
YOu
should
input
another
data
\n",data);
scanf("%d",&data);
OrderedInsert(List,data);
}
else
{
p=(LinkNode*)malloc(LEN);
p->data
=
data;
p->next
=
q->next;
q->next
=
p;
List->Listsize++;
}
}
}
void
Creatset(OrderedList
*List)//多次调用OrderedInsert()生成有序链表即集合List
{
Datatype
data;
int
setsize
,
i=0;
printf("Please
input
the
setsize
you
want
to
creat:\n");
scanf("%d",&setsize);
InitList(List);
if(setsize==0)
printf("You
needen't
input
any
data\n");
else
if(setsize==1)
printf("Please
input
a
single
data\n");
else
printf("Please
input
%d
different
datas;\n",setsize);
while(i<setsize||setsize>List->Listsize)//当循环次数i小于setsize或者集合内实际元素数List.Listsize小于setsize时一直循环下去
{
scanf("%d",&data);
OrderedInsert(List,data);
i++;
}
}
void
Append(OrderedList
*List,Datatype
data)//在循环链表的最后面追加
一个data
{
LinkNode
*p;
p=(LinkNode*)malloc(LEN);
p->data=data;
List->tail=List->tail->next=p;
List->tail->next=List->head;
List->Listsize+=1;
}
void
MergeList(OrderedList
La,OrderedList
Lb,OrderedList
*Lc)//有序循环链表ListLa,ListLb求并集生成ListLc
{
LinkList
Pa,Pb;
Pa=La.head->next;Pb=Lb.head->next;
while(Pa!=La.tail&&Pb!=Lb.tail)
{
if(Pa->data<=Pb->data)
{
Append(Lc,Pa->data);
Pa=Pa->next;
}
else
{
Append(Lc,Pb->data);Pb=Pb->next;
}
}
while(Pa!=La.tail)
{
Append(
Lc,Pa->data);Pa=Pa->next;}
while(Pb!=Lb.tail)
{
Append(Lc,Pb->data);Pb=Pb->next;}
}
void
Print(OrderedList
List)
{
LinkNode
*p;
p=List.head->next;
if(p->next==List.head)
printf("No
Elem\n");
while(p!=List.head)
{
printf("%5d",p->data);p=p->next;
}
printf("\n");
}
void
main()
{
OrderedList
ListLa,ListLb,ListLc;
Creatset(&ListLa);
Creatset(&ListLb);
InitList(&ListLc);
MergeList(ListLa,ListLb,&ListLc);
printf("The
orgnial
list
ListLa,ListLb:\n");
Print(ListLa);
Print(ListLb);
printf("The
Merge
list
ListLc;\n");
Print(ListLc);
}

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:432
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:146
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:233
java驼峰 发布:2025-02-02 09:13:26 浏览:651
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:532
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726