数据结构-线性结构-离散存储-动态链表
C语言实现:
头文件:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
结构体构造类型-链表节点类型:
typedef struct node
{
struct node *next;
int data;
}Node;
函数接口:
- 创建链表(头插法)
Node *Creat_List()
{
Node *phead=(Node *)malloc(sizeof(Node));
assert(phead!=NULL);
phead=NULL;
Node *pnew=NULL;
int temp;
while(1)
{
printf("Input value:\n");
scanf("%d",&temp);
if(0==temp)
break;
pnew=(Node *)malloc(sizeof(Node));
assert(NULL!=pnew);
pnew->data=temp;
pnew->next=phead->next;
phead=pnew;
}
return phead;
}
- 创建链表(尾插法)
Node *Creast_List()
{
Node *phead=(Node *)malloc(sizeof(Node));
assert(NULL!=phead);
phead->next=NULL;
Node *ptemp=phead,*pnew=NULL;
int data;
printf("Input value:\n");
scanf("%d",&data);
while(data)
{
pnew=(Node *)malloc(sizeof(Node));
pnew->data=data;
pnew->next=NULL;
ptemp->next=pnew;
ptemp=ptemp->next;
printf("Input value:\n");
scanf("%d",&data);
}
return phead;
}
- 显示链表
void Show_List(Node *phead)
{
phead=phead->next;
while(NULL!=phead)
{
printf("%d->",phead->data);
phead=phead->next;
}
}
- 求链表长度
int Lenth_List(Node *phead)
{
int lenth=0;
phead=phead->next;
while(NULL!=phead)
{
lenth++;
phead=phead->next;
}
return lenth;
}
- 头插法(通过参数传地址修改头结点指向)
void Push_Front(Node **phead,int data)
{
Node *pnew=(Node *)malloc(sizeof(Node));
assert(NULL!=pnew);
pnew->data=data;
pnew->next=(*phead)->next;
(*phead)->next=pnew;
}
- 头插法(通过返回值修改头结点指向)
Node * Push_Front(Node *phead,int data)
{
Node *pnew=(Node *)malloc(sizeof(Node));
assert(NULL!=pnew);
pnew->data=data;
pnew->next=phead->next;
phead->next=pnew;
return phead;
}
- 尾插法
void Push_Back(Node *phead,int data)
{
Node *pend=phead;
while(NULL!=pend->next)
{
pend=pend->next;
}
Node *pnew=(Node *)malloc(sizeof(Node));
pnew->data=data;
pend->next=pnew;
pnew->next=NULL;
}
- 按值查找
int Find_Data(Node *phead,int data)
{
int count=0;
phead=phead->next;
while(NULL!=phead)
{
count++;
if(phead->data==data)
{
return count;
}
phead=phead->next;
}
return -1;
}
- 按位置查找
int Find_Pos(Node *phead,int pos)
{
while(pos--)
{
phead=phead->next;
}
return phead->data;
}
- 头删法
void Del_Front(Node *phead)
{
phead->next=phead->next->next;
}
- 尾删法
void Del_Back(Node *phead)
{
Node *pend=phead;
while(NULL!=pend->next->next)
{
pend=pend->next;
}
pend->next=NULL;
}
- 按位置删除
void Del_Pos(Node *phead,int pos)
{
while(--pos-1)
{
phead=phead->next;
}
phead->next=phead->next->next;
}
- 排序
void Sort_List(Node *phead)
{
int lenth=Lenth_List(phead);
Node *ptemp=NULL;
for(int i=1;i<lenth;++i)
{
ptemp=phead->next;
for(int j=0;j<=lenth-i;++j)
{
if(ptemp->data>ptemp->next->data)
{
int temp=ptemp->data;
ptemp->data=ptemp->next->data;
ptemp->next->data=temp;
}
ptemp=ptemp->next;
}
}
}
- 逆置
Node *Reverse_List(Node *phead)
{
Node *ptemp=phead->next;
Node *pend=NULL;
phead->next=NULL;
while(NULL!=ptemp)
{
pend=ptemp;
ptemp=ptemp->next;
pend->next=phead->next;
phead->next=pend;
}
}
- 摧毁
void Destroy_List(Node *phead)
{
Node *ptemp=phead->next;
while(NULL!=ptemp)
{
phead=phead->next;
free(ptemp);
ptemp=phead;
}
}

本文介绍使用C语言实现动态链表的各种基本操作,包括创建、显示、求长度、插入、查找、删除及排序等,并提供了完整的代码示例。
1738

被折叠的 条评论
为什么被折叠?



