提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一、单链表概念
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素 + 指针。
二、单链表接口实现
1.自定义单链表:struct LNode
代码如下(示例):
typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode*next; }LinkNode;
2. 初始化单链表:InitList
代码如
下(示例):
void InitList(LinkNode *&L) { L=(LinkNode *)malloc(sizeof(LinkNode)); L->next=NULL; }
3.头插法创建单链表: CreateListF
头插法就是在头结点后面,首节点前面插入。
代码如下(示例):
void CreatListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
4.尾插法创建单链表: CreateListR
代码如下(示例):
void CreatListR(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
r=L;
for(int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;//表尾结点next的值域为空
}
5.判断单链表是否为空: ListEmpty
代码如下(示例):
bool ListEmpty(LinkNode *L)
{
return (L->next==NULL);
}
6.求单链表长度:ListLength
代码如下(示例):
int ListLenght(LinkNode *L)
{
int n=0;
LinkNode *p=L;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
7.插入数据:ListInsert
代码如下(示例):
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *s,*p=L;
if(i<=0) //插入位置错误
return false;
while(j<i-1&&p!=NULL) //找到第i-1个结点
{
j++;
p=p->next;
}
if(p==NULL) //没找到第i-1个结点
return false;
else
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
8.输出单链表:DispList
代码如下(示例):
void DispList(LinkNode *L)
{
LinkNode *p;
p=L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
9.删除数据元素:ListDelete
代码如下(示例):
bool DeleteList(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *s,*p=L;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=p->next;//指向第i个结点
if(s==NULL)
return false;
e=s->data;
p->next=s->next;
free(s);
return true;
}
}
10.取指定位置数据元素:GetElem
代码如下(示例):
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L;
if(i<=0)
return false;
while(j<i&&p!=NULL) //找到第i个结点
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else{
e=p->data;
return true;
}
}
11.销毁单链表:DestroyList
代码如下(示例):
void DestroyList(LinkNode *L)
{
LinkNode *pre=L,*p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
12.单链表完整代码
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode*next;
}LinkNode;
void CreatListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
void CreatListR(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
r=L;
for(int i=0;i<n;i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void DispList(LinkNode *L)
{
LinkNode *p;
p=L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
}
bool ListEmpty(LinkNode *L)
{
return (L->next==NULL);
}
int ListLenght(LinkNode *L)
{
int n=0;
LinkNode *p=L;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
void DestroyList(LinkNode *L)
{
LinkNode *pre=L,*p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *s,*p=L;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool DeleteList(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *s,*p=L;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=p->next;//指向第i节点
if(s==NULL)
return false;
e=s->data;
p->next=s->next;
free(s);
return true;
}
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L;
if(i<=0)
return false;
while(j<i&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else{
e=p->data;
return true;
}
}
int main()
{
LinkNode *L1,*L2,*L;
ElemType e;
int a[]={1,3,5,7,9};
CreatListF(L1,a,5);
CreatListR(L2,a,5);
DispList(L1);
DispList(L2);
printf("***********************\n");
InitList(L);
printf("%d\n",ListEmpty(L));
printf("%d\n",ListLenght(L));
printf("***********************\n");
InsertList(L,1,4);
InsertList(L,2,5);
InsertList(L,1,7);
DeleteList(L,2,e);
DispList(L);
printf("%d\n",e);
printf("***********************\n");
GetElem(L1,2,e);
printf("%d\n",e);
DestroyList(L);
DestroyList(L1);
DestroyList(L2);
return 0;
}
总结
以上就是数据结构单链表的内容,本文仅仅简单介绍了单链表接口的实现,如果以上有问题,请大神指教,和同学们一起学习进步。