实验3-单链表(优化版/王道版)

单链表结构体

cpp 复制代码
typedef int ElemType;
typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode,* LinkList;

单链表的初始化

cpp 复制代码
void InitList(LinkList &L)
{
	L=new LNode;
	L->next=NULL;
}

打印单链表

cpp 复制代码
void visit(LinkList L)
{
	L=L->next;
	while(L){
		cout<<L->data<<" ";
		L=L->next;
	}
	cout<<endl;
}

头插法建表

cpp 复制代码
void HeadInsert(LinkList &L)
{
	InitList(L);
	LinkList p=NULL;
	ElemType e;
	cin>>e;
	while(e!=9999){
		p=new LNode;
		p->data=e;
		p->next=L->next;
		L->next=p;
		cin>>e;
	}
}

尾插法建表

cpp 复制代码
void TailInsert(LinkList &L)
{
	InitList(L);
	LinkList p;
	LinkList r=L;
	ElemType e;
	cin>>e;
	while(e!=9999){
		p=new LNode;
		p->data=e;
		r->next=p;
		r=p;
		cin>>e;
	}
	r->next=NULL;
}

求表长操作

cpp 复制代码
int Length(LinkList L)
{
	int len=0;
	L=L->next;
	while(L){
		len++;
		L=L->next;
	}
	return len;
}

按位查找结点

cpp 复制代码
LinkList GetElem(LinkList L,int i)
{
	if(i<0) return NULL;
	int j=0;
	while(L && j<i){
		L=L->next;
		j++;
	}
	return L;
}

按值查找结点

cpp 复制代码
LinkList LocateElem(LinkList L,ElemType e)
{
	L=L->next;
	while(L && L->data!=e) L=L->next;
	return L;
}

插入结点

cpp 复制代码
bool ListInsert(LinkList &L,int i,ElemType e)
{
	LinkList p=GetElem(L,i-1);
	if(p==NULL) return false;
	LinkList s=new LNode;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}

删除结点

cpp 复制代码
bool ListDelete(LinkList &L,int i,ElemType &e)
{
	LinkList p=GetElem(L,i-1);
	if(p==NULL || p->next==NULL) return false;
	LinkList s=p->next;
	e=s->data;
	p->next=s->next;
	delete s;
	return true;
}

main函数

cpp 复制代码
int main()
{
	LinkList L;
	ElemType e;
	HeadInsert(L);//5 4 3 2 1 9999
//	TailInsert(L);//1 2 3 4 5 9999
	visit(L);
	cout<<"len:"<<Length(L)<<endl;
	cout<<"elem:"<<GetElem(L,5)->data<<endl;
	cout<<"pos:"<<LocateElem(L,5)<<endl;
	ListInsert(L,6,6);
	visit(L);
	ListDelete(L,6,e);
	visit(L);
	return 0;
}

完整代码

cpp 复制代码
	#include <iostream>
	using namespace std;
	typedef int ElemType;
	typedef struct LNode{
		ElemType data;
		struct LNode *next;
	}LNode,* LinkList;
	void InitList(LinkList &L)
	{
		L=new LNode;
		L->next=NULL;
	}
	void visit(LinkList L)
	{
		L=L->next;
		while(L){
			cout<<L->data<<" ";
			L=L->next;
		}
		cout<<endl;
	}
	void HeadInsert(LinkList &L)
	{
		InitList(L);
		LinkList p=NULL;
		ElemType e;
		cin>>e;
		while(e!=9999){
			p=new LNode;
			p->data=e;
			p->next=L->next;
			L->next=p;
			cin>>e;
		}
	}
	void TailInsert(LinkList &L)
	{
		InitList(L);
		LinkList p;
		LinkList r=L;
		ElemType e;
		cin>>e;
		while(e!=9999){
			p=new LNode;
			p->data=e;
			r->next=p;
			r=p;
			cin>>e;
		}
		r->next=NULL;
	}
	int Length(LinkList L)
	{
		int len=0;
		L=L->next;
		while(L){
			len++;
			L=L->next;
		}
		return len;
	}
	LinkList GetElem(LinkList L,int i)
	{
		if(i<0) return NULL;
		int j=0;
		while(L && j<i){
			L=L->next;
			j++;
		}
		return L;
	}
	
	LinkList LocateElem(LinkList L,ElemType e)
	{
		L=L->next;
		while(L && L->data!=e) L=L->next;
		return L;
	}
	bool ListInsert(LinkList &L,int i,ElemType e)
	{
		LinkList p=GetElem(L,i-1);
		if(p==NULL) return false;
		LinkList s=new LNode;
		s->data=e;
		s->next=p->next;
		p->next=s;
		return true;
	}
	bool ListDelete(LinkList &L,int i,ElemType &e)
	{
		LinkList p=GetElem(L,i-1);
		if(p==NULL || p->next==NULL) return false;
		LinkList s=p->next;
		e=s->data;
		p->next=s->next;
		delete s;
		return true;
	}
	int main()
	{
		LinkList L;
		ElemType e;
		HeadInsert(L);//5 4 3 2 1 9999
	//	TailInsert(L);//1 2 3 4 5 9999
		visit(L);
		cout<<"len:"<<Length(L)<<endl;
		cout<<"elem:"<<GetElem(L,5)->data<<endl;
		cout<<"pos:"<<LocateElem(L,5)<<endl;
		ListInsert(L,6,6);
		visit(L);
		ListDelete(L,6,e);
		visit(L);
		return 0;
	}

输出示例

bash 复制代码
5 4 3 2 1 9999
1 2 3 4 5
len:5
elem:5
pos:0xc56a20
1 2 3 4 5 6
1 2 3 4 5
相关推荐
冻柠檬飞冰走茶1 天前
PTA基础编程题目集 7-35有理数均值(C++语言实现)
开发语言·数据结构·c++·算法·均值算法
民乐团扒谱机1 天前
【微实验】倒谱算法(Cepstrum)深度解析:原理、数学推导与代码实现
人工智能·算法·语音识别
Nil2081 天前
leetcode 189轮转数组
数据结构·算法·leetcode
-dzk-1 天前
【动态规划】LC 118.杨辉三角
算法·动态规划
恣逍信点1 天前
《凌微经》静态自悖——变化之自因
人工智能·科技·算法·机器学习·业界资讯·拓扑学·哲学
吃着火锅x唱着歌1 天前
LeetCode 3885.设计事件管理器
算法·leetcode·职场和发展
土司大王1 天前
LeetCode hto100——字母异位词分组
java·算法·leetcode
Eloudy1 天前
预词力:LLM 的唯一形式化能力
人工智能·算法·agent
依然鸣1 天前
PTA团体程序设计天梯赛L1真题讲解L1-085-088
数据结构·c++·经验分享·算法·深度优先·pat考试
听取WA声一片(无恶意)1 天前
CSP-J/S 初赛图论完全讲义
算法·csp-s初赛·csp-j初赛