数据结构——链表

1.单链表

要点
一、第一个结点->next=&第二个结点
二、有头链表和无头链表区别
有头:第一个结点里不存放数据(数据插在表头后面,表头不变)
无头:第一个结点里存放数据(数据插在第一个结点前,表头一直在变)
下面我创建的都是无头的

#include<iostream>
#include<stdlib.h>
using namespace std;
#define Elemtype int

typedef struct LNode{
	Elemtype data;
	struct LNode *next;
}LNode,* Linklist;
//初始化单链表
struct LNode* InitList()
{
	struct LNode* L = (LNode*)malloc(sizeof(LNode));//将指针变成变量
	L->next = NULL;
	return L;
}
//头插法创建链表(后插)
struct LNode* list_HeadInsert(Linklist &L)
{
	LNode* s;
	Elemtype temp;
	int count;
	cout << "请输入你想插入的结点数" << endl;
	cin >> count;
	for (int j = 0; j < count; j++)
	{
		s = (LNode*)malloc(sizeof(LNode));
		cout << "第" << j + 1 << "个数字为" << endl;
		cin >> temp;
		s->data = temp;
		s->next = L->next;
		L->next = s;
	}
	return L;
}
//尾插法建立链表(要借助尾指针)
struct LNode* list_TailInsert(Linklist &L)
{
	Linklist s;
	Elemtype temp;
	int count;
	cout << "请输入你想插入的结点数" << endl;
	cin >> count;
	Linklist trail;
	trail = L;
	for (int j = 0; j < count; j++)
	{
		cout << "第" << j + 1 << "个结点为" << endl;
		cin >> temp;
		s = (LNode*)malloc(sizeof(LNode));
		trail->next = s;
		s->next = NULL;
		s->data = temp;
		trail = s;
	}
	trail->next = NULL;
	return L;
}
//按序号查找结点值
bool GetElem(Linklist &L, int i,Elemtype &e)
{
	Linklist Pmove = L;
	if (i < 0)
	{
		return false;
	}
	for (int j = 1; j <= i; j++)
	{
		Pmove = Pmove->next;
		if (!Pmove)
		{
			return false;
		}
	}
	e = Pmove->data;
	return true;
}
//在其前插入
bool List_insert(Linklist &L, int i, Elemtype e)
{
	Linklist Pmove=L->next;
	for (int j = 1; j < i-1; j++)
	{
		if (!Pmove)
		{
			return false;
		}
		Pmove = Pmove->next;
	}
	Linklist newNode = (LNode*)malloc(sizeof(LNode));
	newNode->next = Pmove->next;
	Pmove->next = newNode;
	newNode->data = e;
	return true;
}
//删除结点
bool Delete_list(Linklist &L, int i, Elemtype &e)
{
	Linklist Pmove = L;
	for (int j = 1; j <i; j++)
	{
		Pmove = Pmove->next;
		if (!Pmove)
		{
			return false;
		}
	}
	Linklist q = Pmove->next;
	e = q->data;
	Pmove->next = q->next;
	delete q;
	return true;
}
//打印链表
void Printlist(Linklist L)
{
	Linklist Pmove=L->next;
	while (Pmove)
	{
		cout << Pmove->data << "\t";
		Pmove = Pmove->next;
	}
	cout << endl;
}
//释放链表
bool Clear_linklist(Linklist &L)
{
	Linklist Pmove=L->next;
	while (Pmove)
	{
		L->next = Pmove->next;
		delete Pmove;
		Pmove = L->next;
	}
	delete L;
	return true;
}
//将单链表逆置
bool Convert_list(Linklist &L)
{
	Linklist p, q;
	p = L->next;
	while (p->next)
	{		
		q = p->next;
		p->next = q->next;
		q->next = L->next;
		L->next = q;
	}
	return true;
}
int main()
{
	Linklist mylist= InitList();
	list_TailInsert(mylist);
	Printlist(mylist);
	Elemtype e = 0;
	return 0;
}

2.双链表

#include<iostream>
#include<stdlib.h>
using namespace std;
#define Elemtype int
typedef struct LNode {
	Elemtype data;
	struct LNode *prior;
	struct LNode *next;
}LNode, *Linklist;

//初始化双链表
struct LNode* InitList()
{
	struct LNode* L = (LNode*)malloc(sizeof(LNode));//将指针变成变量
	L->prior = NULL;
	L->next = NULL;
	L->data = 0;//头结点数据为0时双链表为空
	return L;
}
//头插法创建链表(后插)
struct LNode* list_HeadInsert(Linklist &L)
{
	LNode* s;
	Elemtype temp;
	int count;
	cout << "请输入你想插入的结点数" << endl;
	cin >> count;
	for (int j = 0; j < count; j++)
	{
		s = (LNode*)malloc(sizeof(LNode));
		cout << "第" << j + 1 << "个数字为" << endl;
		cin >> temp;
		s->data = temp;//先赋上数据
		if (L->next == NULL)//如果链表为空
		{
			s->next = L->next;
			s->prior = L;
			L->next = s;
		}
		else
		{
			s->next = L->next;
			L->next->prior = s;
			s->prior = L;
			L->next = s;
		}
	}
	return L;
}
//尾插法建立链表(要借助尾指针)
struct LNode* list_TailInsert(Linklist &L)
{
	Linklist s;
	Elemtype temp;
	int count;
	cout << "请输入你想插入的结点数" << endl;
	cin >> count;
	LNode *tail;
	tail = L;
	while (tail->next!=NULL)//找到尾结点
	{
		tail = tail->next;
	}
	for (int j = 0; j < count; j++)
	{
		cout << "第" << j + 1 << "个结点为" << endl;
		cin >> temp;
		s = (LNode*)malloc(sizeof(LNode));
		s->data = temp;
		s->next = NULL;
		s->prior = tail;
		tail->next = s;
		tail = s;
	}
	return L;
}
//打印链表
void Printlist(Linklist L)
{
	Linklist Pmove = L->next;
	while (Pmove)
	{
		cout << Pmove->data << "\t";
		Pmove = Pmove->next;
	}
	cout << endl;
}

//在其前插入
bool List_insert(Linklist &L, int i, Elemtype e)
{
	LNode *Pmove = L;
	for (int j = 1; j < i; j++)
	{
		if (!Pmove)
		{
			return false;
		}
		Pmove = Pmove->next;
	}
	Linklist newNode = (LNode*)malloc(sizeof(LNode));
	newNode->data = e;
	newNode->next = Pmove->next;
	newNode->prior = Pmove;
	Pmove->next->prior = newNode;
	Pmove->next = newNode;
	return true;
}
//删除结点
//单链表中必须找到第i-1个结点才能把第i个结点跳过去
//双向不需要
bool Delete_list(Linklist &L, int i, Elemtype &e)
{
	LNode *Pmove = L->next;
	for (int j = 1; j <i; j++)
	{
		Pmove = Pmove->next;
		if (!Pmove)
		{
			return false;
		}
	}
	if (Pmove->next == NULL)
	{
		Pmove->prior->next = NULL;
	}
	else
	{
		Pmove->prior->next = Pmove->next;
		Pmove->next->prior = Pmove->prior;
	}
	delete Pmove;
	return true;
}
//释放链表
bool Clear_linklist(Linklist &L)
{
	Linklist Pmove = L->next;
	while (Pmove)
	{
		L->next = Pmove->next;
		delete Pmove;
		Pmove = L->next;
	}
	delete L;
	return true;
}
bool isEmpty(Linklist L)
{
	if (L->next == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	Linklist mylist = InitList();
	list_TailInsert(mylist);
	Printlist(mylist);
	Elemtype e = 100;
	List_insert(mylist, 5, e);
	Printlist(mylist);
	Delete_list(mylist, 6, e);
	Printlist(mylist);
	if (Clear_linklist(mylist))
	{
		cout << "删除完成" << endl;
	}
	else
	{
		cout << "删除不成功" << endl;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值