链表的表示和实现

本文详细介绍了链表的概念,以及如何在编程中实现链表数据结构,包括单链表、双链表等,适合初学者理解链表的内部工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人使用的模板

#pragma once
#include<iostream>
#define ElemType int
#define OK 1
#define ERROR 0
//单链表
typedef struct
{
	ElemType data;
	LNode* next;
}LNode,*LinkList;

//双向链表
typedef struct
{
	ElemType data;
	DoubleNode* next, * prior;

}DoubleNode, * DoubleList;


//创建头节点
int InitList(LinkList& L)
{
	L = new LNode;
	//L=(LinkList)mallor(sizeof(LNode))
	L->next = NULL;
	return OK;
}

int IsEmpty(LinkList L)
{
	if (L->next)//非空表返回0
		return 0;
	return 1; //空表返回0
}

//销毁链表
int DestroyList(LinkList& L)
{
	LNode* p;
	while (L)
	{
		p = L;
		L = L->next;
		delete p;
	}
	return OK;
}

//清空链表
int ClearList(LinkList& L)
{
	LNode* p, * q;
	p = L->next;
	while (p)
	{
		q = p->next;
		delete p;
		p = q;
	}
	L->next = NULL;
	return OK;
}

//计算表长
int LengthList(LinkList L)
{
	LNode* p;
	p = L->next;
	int cnt = 0;
	while (p)
	{
		p = p->next;
		cnt++;
	}
	return cnt;
}

//获取第i个位置的元素
int GetElem(LinkList L, int pos, ElemType& e)
{
	LNode* p = L->next;
	int cnt = 1;
	while (p && cnt < pos)
	{
		p = p->next;
		cnt++;
	}
	if (!p || cnt > pos)return ERROR;//pos为负数或者超过元素个数
	e = p->data;
	return OK;
}

//查找元素e的地址
LNode * LocateElem(LinkList L, ElemType e)
{
	LNode* p = L->next;
	while (p && p->data != e)
		p = p->next;
	return p;
}

//查找元素e的位置
int PositionElem(LinkList L, ElemType e)
{
	LNode* p = L->next;
	int cnt = 1;
	while (p && p->data != e)
	{
		p = p->next;
		cnt++;
	}
	if (p) return cnt;//未超过表长
	return 0;//超出表长
}

int InsertList(LinkList& L, int pos, ElemType e)
{
	LNode* p = L;
	int cnt = 0;
	while (p && cnt < pos - 1) //找第pos-1个位置
	{
		p = p->next;
		cnt++;
	}
	if (!p || cnt > pos - 1) return ERROR;//大于表长或者pos小于1

	LNode* s = new LNode;//为元素e创建一个LNode的空间
	s->data = e;
	s->next = p->next;
	p->next = s;
	return OK;
}

int DeleteList(LinkList& L, int pos, ElemType e)
{
	LNode* p = L;
	int cnt = 0;
	while (p && cnt < pos - 1)//找到pos-1的位置
	{
		p = p->next;
		cnt++;
	}
	if (!(p->next) || cnt > pos - 1) return ERROR;
	LNode* q = p->next;
	p->next = q->next;
	e = q->data;
	delete q;
	return OK;
}

//头插法
int InsertFront(LinkList& L,int n)
{
	//创建一个头节点
	L = new LNode;//L=(LinkList)mallor(sizeof(LNode));
	L->next = NULL;

	//倒叙插入
	for (int i = n; i >= 1; --i)
	{
		LNode* p = new LNode;
		std::cin >> p->data;
		p->next = L->next;
		L->next = p;
	}
}

int InsertBack(LinkList& L, int n)
{
	L = new LNode;
	L->next = NULL;
	
	LNode* r = L;
	//正序插入
	for (int i = 1; i <= n; ++i)
	{
		LNode* p = new LNode;
		std::cin >> p->data;
		p->next = NULL;
		r->next = p;
		r = p;
	}
	return OK;
}

//循环链表合并,参数为尾指针
LinkList Connet(LinkList Ta, LinkList Tb)
{
	LNode* p = Ta->next;
	Ta->next = Tb->next->next;
	delete Tb->next;
	Tb->next = p;
	return Tb;
}

//双向链表插入到pos和pos-1之间
int InsertDoubleList(DoubleList& L, int pos, ElemType e)
{
	DoubleNode* p = L->next;
	int cnt = 1;
	while (p && cnt < pos)
	{
		p = p->next;
		cnt++;
	}
	DoubleNode* s = new DoubleNode;
	s->data = e;
	s->prior = p->prior;
	p->prior->next = s;
	s->next = p;
	p->prior = s;
	return OK;
}

//双向链表删除
int DeleteDoubleList(DoubleNode& L, int pos)
{
	DoubleNode* p = L.next;
	int cnt = 1;
	while (p && cnt < pos)
	{
		p = p->next;
		cnt++;
	}
	p->prior->next = p->next;
	p->next->prior = p->prior;
	delete p;
	return OK;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LoveCover

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值