数据结构单链表实现
继上篇顺序表,我们又实现了单链表。
//参考李春葆数据结构第5版
#include<iostream>
#include<cstdlib>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LinkNode;
//头插法
void CreateListF(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 CresteListR(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
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 InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
}
//销毁链表
void DestroyList(LinkNode *&L)
{
LinkNode *pre=L,*p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
//判空
bool ListEmpty(LinkNode *L)
{
return L->next==NULL;
}
//输出链表
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
//求长度
int ListLength(LinkNode *L)
{
int n=0;
LinkNode *p=L;
while(p->next!=NULL)
{
n++;
p=p->next;
}
return n;
}
//求线性表第i个元素的值
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 LocateElem(LinkNode *L,ElemType e)
{
int i=1;
LinkNode *p=L->next;
while(p!=NULL&&p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)return 0;
else return i;
}
//插入数据元素
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
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 LsitDelete(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if(i<=0)return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)return false;
else
{
q=p->next;
if(q==NULL)return false;
e=q->data;
p->next=q->next;
free(q);
return true;
}
}
int main()
{
LinkNode *L;
ElemType e;
int a[]={1,2,6,4,2,9,0,10,443,65};
InitList(L);
CreateListF(L,a,10);
DispList(L);
cout<<ListLength(L)<<endl;
GetElem(L,5,e);
cout<<e<<endl;
cout<<LocateElem(L,0)<<endl;
ListInsert(L,3,100);
DispList(L);
LsitDelete(L,5,e);
DispList(L);
DestroyList(L);
return 0;
}