#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define F -1
typedef int ElemType;
typedef int Status;
typedef struct Lnode//单链表的定义
{
ElemType data;//数据域
struct Lnode *next;//指针域
}Lnode,*linkList;//linklist为指向结构体Lode的指针类型
linkList L;//定义链表L
//初始化链表
Status InitList(linkList &L)//要改变链表的形状,所以用引用类型
{
L=new Lnode;//生成一个新结点,用头指针指向头结点
L->next=NULL;//将新结点的指针域置空
return OK;
}
//判断链表为空
int ListEmpty(linkList L)
{
if(L->next)//非空,则不是空链表,返回ERROR
return 0;
else return 1;
}
//销毁单链表
int DestoryList(linkList &L)
{
Lnode *p;
while(L)//当链表不为空时,一直释放结点
{
p=L;//将p指向头结点,而头指针存放头结点的地址
L=L->next;
delete p;
}
return OK;
}
//清空链表
//链表仍存在,但链表中无元素,成为空链表(头指针和头结点依旧在)
//算法思路
//依次释放所有结点,并将头指针结点置为空
Status clearList(linkList &L)
{
linkList p,q;
p=L->next;//使p指向首元结点
while(p)//当p指向的结点为NULL时,结束循环
{
q=p->next;
delete p;
p=q;
}
L->next=NULL;
return OK;
}
//返回单链表的长度
int ListLength(linkList L)//返回L中数据元素的个数
{
Lnode *p;
p=L->next;//p指向首元结点
int i=0;
while(p)//当p为空时,循环结束
{
i++;//记录结点的个数
p=p->next;//指向下一个结点
}
return i;
}
//取单链表第i个元素的内容
int GetElem(linkList L,int i,ElemType &e)
{
Lnode *p;
p=L->next; int j=1;//初始化
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p&&j>i)//j>i,i的值可能是负数或者是0,j>i用来判断i的值是否具有有效性
return ERROR;
e=p->data;
return OK;
}
//按值查找,根据指定数据的值查找它的位置信号
int LocateElem(linkList L,ElemType e)
{
Lnode *p;
p=L->next;int j=1;
while(p&&p->data!=e)
{
p=p->next;
j++;
}
if(p)
return j;
else
return F;
}
//插入,在第i个结点,插入值为e的结点
//算法思路
//首先找到i-1的存储位置p
//生成一个数据域为e的新结点s
//插入新结点,新节点的指针域指向i,结点i-1的指针域指向新节点
Status ListInsert(linkList &L,int i,ElemType e)
{
Lnode *p,*s;
p=L;int j=0;
while(p&&j<i-1)//寻找第i-1个结点,p指向第i-1个结点
{
p=p->next;
j++;
}
if(!p||j>i-1)//i大于表长+1或者小于1,插入位置非法
return ERROR;
s=new Lnode;//生成新结点
s->data=e;//将结点s的数据与置为e
s->next=p->next;
p->next=s;
return OK;
}
//单链表的删除,删除第i个结点
Status ListDelete(linkList &L,int i,ElemType &e)
{
Lnode *p,*q;
p=L; int j=0;//初始化
while(p->next&&j<i-1)//可以找到第i-1的数据元素,p指向i-1,p->next指向i,即i必须存在
{
p=p->next;
j++;
}
if(!(p->next)||j>i-1)//删除位置不合理
return ERROR;
q=p->next;//临时保存被删节点的地址以备释放
p->next=q->next;//改变删除结点前驱结点的指针域
e=q->data;
delete p;
return OK;
}
//尾插法
int CreateList(linkList &L,int n)
{
L=new Lnode;
L->next=NULL;
Lnode *p,*r;
r=L;//尾指针指向头结点
for(int i=0;i<n;++i)
{
p=new Lnode;//生成新结点
scanf("%d",&(p->data));//输入元素的值
p->next=NULL;
r->next=p;//将结点插入到表尾
r=p;//r指向新的尾结点
}
return true;
}
//主函数测试
int main()
{
ElemType e;int n;
//测试InitList函数
if(InitList(L))
printf("InitList success!\n");
else
{
printf("InitList error\n");
}
//测试listEmpty函数
if(ListEmpty(L))
printf("ListEmpty success!\n");
else
{
printf("ListEmpty error\n");
}
//测试Creatlist函数
printf("please enter the number:\n");
scanf("%d",&n);
if(CreateList(L,n))
printf("Createlist success\n");
//测试listlength函数
printf("Linklist length is %d\n",ListLength(L));
//测试Getelem函数
if(GetElem(L,1,e))
printf("the first element is %d\n",e);
else
{
printf("the first element is not exist\n");
}
//测试locateElem_L函数
int a2;
printf("please enter the number of you want to find it,if success,return a position sequence for the element\n");
scanf("%d",&a2);
int f=LocateElem(L,a2);
if(f)
printf("%d is located at the %d\n",a2,f);
else
printf("%d is not located at the Linklist\n",a2);
//测试listInsert函数
printf("please enter the insertion element position sequence number and the value of the number\n");
int c,a;
scanf("%d %d",&c,&a);
if(ListInsert(L,c,a))
printf("listInsert success!\n");
//测试listDelete函数
printf("please enter the location number of the element you want to delete\n");
int d;
scanf("%d",&d);
if(ListDelete(L,d,e))
printf(" the deleted element is %d\n",e);
else
{
printf("Delete error\n");
}
//测试clearlist函数
if(clearList(L))
printf("clearList success\n");
else
{
printf("clearlist error\n");
}
//测试DestoryList函数
if(DestoryList(L))
printf("DestoryList success\n");
else
printf("Destorylist error\n");
return 0;
}
数据结构之线性表的实现
最新推荐文章于 2024-05-29 17:32:37 发布