数据结构(C语言版)-单链表的实现

注:为了方便编写,本人使用C++实现,与C语言并无太大差别,代码编写不是很严谨,仅供参考;

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
#define SUCCESS 1
#define ERROR -1
int flag=0;//全局变量,判断链表是否初始化
typedef struct LinkList{
	ElemType Elem;
	LinkList* next;
}LinkList;

//1-初始化链表
void InitList(LinkList* &L){
	if(flag==0){
		L=(LinkList*)malloc(sizeof(LinkList));
		L->Elem=NULL;
		L->next=NULL;
		flag=1;//表示链表已经初始化
		cout<<"链表初始化成功!"<<endl;
	}else{
		cout<<"链表已经初始化!请先销毁!"<<endl;
	}
}
//2-销毁链表
void DestoryList(LinkList* L){
	if(flag==1){
		free(L);
		flag=0;
		cout<<"链表销毁成功!"<<endl;
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
	}
}
//3-清空链表
void	ClearList(LinkList* L){
	if(flag==1){
		L->next=NULL;
		cout<<"链表已经清空!"<<endl;
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
	}
}
//4-判断链表是否为空,为空返回true
bool ListEmpty(LinkList* L){
	//先判断链表是否初始化
	if(flag==1){
		if(L->next==NULL){
			return true;
		}else{
			return false;
		}	
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
		return true;
	}
}
//5-返回单链表的长度
int ListLength(LinkList* L){
	if(flag==1){
		int length=0;//计算链表长度
		while(true){
			//判断是否到达最后一个节点
			if(L->next==NULL){
				return length;
			}
			length++;
			L=L->next;
		}
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
		return ERROR;
	}
}
//6-用e返回L中第i个元素值
void GetElem(LinkList* L,int i,ElemType &e){
	int j;
	if(flag==1){
		if(i>0 && i<=ListLength(L)){
			//找到元素
			for(j=0;j<i;j++){
				L=L->next;
			}
			e=L->Elem;
		}else{
			cout<<"查询位置错误"<<endl;
			e=ERROR;
		}
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
		e=ERROR;
	}
}
//7-返回元素e的位置
void LocateElem(LinkList* L,int &i,ElemType e){
	//计算位置
	int count=0;
	if(flag==1){
		if(ListEmpty(L)==false){
			//循环依次查找
			while(L){
				//若找到就跳出循环
				if(L->Elem==e){
					i=count;
					break;
				}
				L=L->next;
				count++;
				i=ERROR;
			}
		}else{
			cout<<"空表无法操作!"<<endl;
		}
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
		i=ERROR;
	}
}
//8-返回元素e的直接前驱
void PriorElem(LinkList* L,ElemType cur_e,ElemType &pre_e){
	if(flag==1){
		if(ListEmpty(L)==false){
			//若链表不为空指向第一个元素
			L=L->next;
			//判断cur_e是不是第一个元素
			if(L->Elem==cur_e){
				cout<<"元素"<<cur_e<<"是第一个元素 没有直接前驱!"<<endl;
				pre_e=ERROR;
				return;
			}
			//获取直接前驱
			while(L->next){
				if(L->next->Elem==cur_e){
					pre_e=L->Elem;
					cout<<"元素"<<cur_e<<"的直接前驱是"<<pre_e<<endl;
					return;
				}
				L=L->next;
			}
			cout<<"未查询到该元素!"<<endl;
			pre_e=ERROR;
		}else{
			cout<<"空表无法操作!"<<endl;
			pre_e=ERROR;
		}
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
		pre_e=ERROR;
	}
}
//9-返回元素e的直接后继
void NextElem(LinkList* L,ElemType cur_e,ElemType &next_e){
	//定义一个指针指向L
	LinkList* p=L;
	if(flag==1){
		if(ListEmpty(L)==false){
			//判断cur_e是不是最后一个元素
			while(L->next){
				L=L->next;
			}
			if(L->Elem==cur_e){
				cout<<"元素"<<cur_e<<"是最后一个元素 没有直接后继!"<<endl;
				next_e=ERROR;
				return;
			}
			//获取直接后继
			while(p->next){
				if(p->Elem==cur_e){
					p=p->next;
					next_e=p->Elem;
					cout<<"元素"<<cur_e<<"的直接后继是"<<next_e<<endl;
					return;
				}
				p=p->next;
			}
			cout<<"未查询到该元素!"<<endl;
			next_e=ERROR;
		}else{
			cout<<"空表无法操作!"<<endl;
			next_e=ERROR;
		}
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
		next_e=ERROR;
	}
}
//10-在第i个位置插入e
void ListInsert(LinkList* L,int i,ElemType e){
	if(flag==1){
		int j;
		//判断插入位置是否正确以及当前链表是否存在
		if(i<=0 || i>ListLength(L)+1 || !L){
			cout<<"元素"<<e<<"的插入位置错误或者链表不存在!"<<endl;
			return;
		}
		//找到插入位置
		for(j=0;j<i-1;j++){
			L=L->next;
		}
		//执行插入操作
		LinkList* obj=(LinkList*)malloc(sizeof(LinkList));//申请一个新的节点来存放插入的数据
		obj->Elem=e;
		obj->next=L->next;
		L->next=obj;
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
	}
}
//11-删除第i个元素
void ListDelete(LinkList* L,int i,ElemType& e){
	if(flag==1){
		int j;
		LinkList *next_L=L,*pre_L=L;
		//判断链表是否存在以及删除位置是否正确
		if(!L || i<=0 || i>ListLength(L)){
			cout<<"删除位置错误或者链表不存在!"<<endl;
			e=ERROR;
			return;
		}

		//找到被删除元素的前一个元素
		for(j=0;j<i-1;j++){
			pre_L=pre_L->next;
		}

		//获取被删除元素,并返回给e
		L=pre_L->next;
		e=L->Elem;

		//判断当前元素是否是最后一个
		if(L->next==NULL){
			next_L=NULL;
		}else{
			//找到被删除元素的后一个元素
			for(j=0;j<i+1;j++){
				next_L=next_L->next;
			}
		}

		//执行删除操作
		pre_L->next=next_L;
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
		e=ERROR;
	}
}	
//12-遍历链表
void ListShow(LinkList* L){ 
	if(flag==1){
		cout<<endl<<"当前链表:";
		if(ListEmpty(L)==false){
			if(L->next){
				L=L->next;
			}
			while(L){
				cout<<L->Elem<<"-";
				L=L->next;
			}
			cout<<endl<<endl<<"遍历完毕!"<<endl;
		}else{
			cout<<"链表为空!"<<endl;
		}
	}else{
		cout<<"链表不存在!无法操作!"<<endl;
	}
}

//主菜单
void menu(LinkList* L){
	int i,n,pre_e,cur_e,next_e;
	ElemType e;
	cout<<"***********************************************"<<endl;
	cout<<"**************1、初始化顺序表******************"<<endl;
	cout<<"**************2、销毁顺序表********************"<<endl;
	cout<<"**************3、清空顺序表********************"<<endl;
	cout<<"**************4、判断顺序表是否为空************"<<endl;
	cout<<"**************5、返回顺序表的长度**************"<<endl;
	cout<<"**************6、返回第i个元素的值*************"<<endl;
	cout<<"**************7、返回元素e的位置***************"<<endl;
	cout<<"**************8、返回元素e的直接前驱***********"<<endl;
	cout<<"**************9、返回元素e的直接后继***********"<<endl;
	cout<<"**************10、在第i个位置插入元素e*********"<<endl;
	cout<<"**************11、删除第i个元素****************"<<endl;
	cout<<"**************12、遍历顺序表*******************"<<endl;
	cout<<"**************13、退出*************************"<<endl;
	cout<<"***********************************************"<<endl;
	cout<<"请选择:";
	cin>>n;
	switch(n){
	case 1:
		InitList(L);
		menu(L);
	case 2:
		DestoryList(L);
		menu(L);
	case 3:
		ClearList(L);
		menu(L);
	case 4:
		if(ListEmpty(L)==true){
			cout<<"当前链表为空!"<<endl;
		}else{
			cout<<"当前链表不为空"<<endl;
		}
		menu(L);
	case 5:
		cout<<"当前顺序表长度:"<<ListLength(L)<<endl;
		menu(L);
	case 6:
		cout<<"请输入位置:";
		cin>>i;
		GetElem(L,i,e);
		cout<<"位置"<<i<<"上的元素是:"<<e<<endl;
		menu(L);
	case 7:
		cout<<"输入数据e:";
		cin>>e;
		LocateElem(L,i,e);
		if(i>0){
			cout<<"元素 "<<e<<" 的位置是 "<<i<<endl;
		}else{
			cout<<"未查询到!"<<endl;
		}
		menu(L);
	case 8:
		cout<<"请输入元素e:";
		cin>>cur_e;
		PriorElem(L,cur_e,pre_e);
		menu(L);
	case 9:
		cout<<"请输入元素e:";
		cin>>cur_e;
		NextElem(L,cur_e,next_e);
		menu(L);
	case 10:
		cout<<"请输入数据e:";
		cin>>e;
		cout<<"请输入插入位置:";
		cin>>i;
		ListInsert(L,i,e);
		menu(L);
	case 11:
		cout<<"请输入要删除的位置i:";
		cin>>i;
		ListDelete(L,i,e);
		cout<<"元素 "<<e<<"删除成功!"<<endl;
		menu(L);
	case 12:
		ListShow(L);
		menu(L);
	case 13:
		exit(0);
	}
	
}

void main(){
	LinkList L;
	menu(&L);
	/*
	int e;
	InitList(L);
	ListInsert(L,1,1);
	ListInsert(L,2,2);
	ListInsert(L,3,3);
	cout<<"当前链表长度:"<<ListLength(L);
	ListShow(L);
	ListDelete(L,5,e);
	cout<<"当前链表长度:"<<ListLength(L);
	ListShow(L);*/
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值