数据结构学习三:链表(基本操作)

前提:

在这里插入图片描述

链表

  • 线性链式存储的特点:
    动态存储分配
    结点总不顺序存放(与可变长数组的差别)

  • 基本概念
    结点(Node)
    一个链表由若干个结点组成
    每个结点包含两个域:数据域和指针域
    数据域:存放数据
    指针域:用于存放指向该链表的下一个结点的指针

单链表:

它指通过一组任意的存储单元来存储线性表中的数据元素。
头结点:
在链表的第一个结点之前附加一个结点(不算在数组索引中,数据域为空,指针域指向开始的结点)。
区分头结点和头指针:
不管带不带头结点,头指针始终指向链表的第一个结点,而头结点是带头结点链表中的第一个结点,结点内通常不存储信息。
在这里插入图片描述

循环链表

特点:表中最后一个结点的指针域指向头结点,整个链表形成一个环。
在这里插入图片描述

双向链表

双向链表的结点中有两个指针域,其一指向直接后继,另一指向直接前驱。
在这里插入图片描述

静态链表(无“指针”)

在这里插入图片描述
静态链表:第一个位置为头节点
0 1(指向的下一个元素为1号)
1 4(指向4号)
2 3(指向3号)
3 0(指向0号,当前链表走到最后)
4 2(指向2号)

链表的基本操作

插入操作

插入类型
在这里插入图片描述
插入操作分析
在这里插入图片描述
将值为x的新结点插入到单链表的第i个位置上

  1. 先检查插入位置的合法性
  2. 找到待插入位置的前驱结点,即第i-1个结点
  3. 插入结点
bool List::ListInsert(int i, Node *pNode)//在第i位插入结点
{
	if (i<0 || i>m_iLength)//判断插入是否合法;i不能小于0,i不能大于链表长度
	{
		return false;
	}
	Node *currentNode = m_pList;//currentNode保存头结点
	for (int k = 0; k < i; k++)
	{
		currentNode = currentNode->next;
	}//直到找到第i个
	Node *newNode = new Node;//声明newNode
	if (newNode == NULL) //判断申请的结点内存是否为空
	{
		return false;
	}
	else
	{
		newNode->data = pNode->data;//pNode数据域赋值给newNode数据域
		newNode->next = currentNode->next;//新结点指向原来结点的下一个结点
		currentNode->next = newNode;//原来的结点指向新结点
		m_iLength++;//插入链表长度+1
		return true;
	}
}
删除操作

删除操作分析:
在这里插入图片描述

bool List::ListDelete(int i, Node *pNode)
{
	if (i<0 || i>=m_iLength)//判断i是否合法,注意此处的i取值
	{
		return false;
	}
	Node *currentNode = m_pList;//currentNode保存头结点
	Node *currentNodeBefore = NULL;//头结点前一个结点置空
	for (int k = 0; k <= i; k++)//找到<=i
	{
		currentNodeBefore = currentNode;//赋值
		currentNode = currentNode->next;
	}
	currentNodeBefore->next = currentNode->next;
	pNode->data = currentNode->data;//pNode数据域接收当前结点的数据域
	delete currentNode;//删除结点
	currentNode = NULL;//赋予null值
	m_iLength--;//删除链表长度-1
	return true;
}

源码:
头文件:Node.h

#include<iostream>
using namespace std;
class Node
{
public:
	int data;//数据域
	Node *next;//指针域
	void printNode();
};

#endif 

Node.cpp

#include"Node.h"

void Node::printNode()
{
	cout << data << endl;
}

头文件:list.h

#include"Node.h"
#ifndef LIST_H
#define LIST_H
#include<iostream>
using namespace std;
class List
{
public:
	List();//构造函数
	~List();//析构函数
	void ClearList();//清空链表
	bool ListEmpty();//链表判空
	int ListLength();//链表长度
	bool GetElem(int i, Node *pNode);//获取指定索引的元素
	int LocateElem(Node *pNode);//获取指定元素的索引
	bool PriorElem(Node *pCurrentNode, Node *pPreNode);//获取某个元素前驱结点
	bool NextElem(Node *pCurrentNode, Node *pNextNode);//获取某个元素后继结点
	void ListTraverse();//遍历
	bool ListInsert(int i,Node *pNode);//插入元素
	bool ListDelete(int i, Node *pNode);//删除元素
	bool ListInsertHead(Node *pNode);//在头结点后面插入元素
	bool ListInsertTail(Node *pNode);//在尾结点后面插入元素
private:
	Node * m_pList;
	int m_iLength;
};

#endif 

list.cpp

#include"List.h"

List::List()
{
	m_pList = new Node;
	m_pList->data = 0;
	m_pList->next= NULL;
	m_iLength = 0;
}

List::~List()//析构函数
{
	ClearList();
	delete m_pList;
	m_pList = NULL;
}

void List::ClearList()
{
	Node *currentNode = m_pList->next;
	while (currentNode != NULL)
	{
		Node *temp = currentNode->next;
		delete currentNode;
		currentNode = temp;
	}
	m_pList->next = NULL;
}

bool List::ListEmpty()
{
	return 0 == m_iLength ? true : false;
}

int List::ListLength()
{
	return m_iLength;
}

bool List::ListInsertHead(Node *pNode)//头部插入结点
{
	Node *temp = m_pList->next;//保留头结点在temp
	Node *newNode = new Node;//从堆申请内存保留新的结点
	if (newNode == NULL) //判断申请的结点内存是否为空
	{
		return false;
	}
	else
	{
		newNode->data = pNode->data;
		m_pList->next = newNode;//头结点的next指向新申请的结点
		newNode->next = temp; //指向保留头结点的temp
		m_iLength++;
		return true;
	}
}

bool List::ListInsertTail(Node *pNode)//尾部插入结点
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)//判断当前结点next是否指向null
	{
		currentNode = currentNode->next;//相当于来到下一个结点
	}
	Node *newNode = new Node;//从堆中分配内存
	if (newNode == NULL)
	{
		return false;
	}
	else
	{
		newNode->data = pNode->data;//赋值
		newNode->next = NULL;//指针域指向null
		currentNode->next = newNode;//
		m_iLength++;//插入++
		return true;
	}
}

bool List::ListInsert(int i, Node *pNode)//在第i位插入节点
{
	if (i<0 || i>m_iLength)//判断插入是否合法m_ilength每次插入都+1,每次删减都-1
	{
		return false;
	}
	Node *currentNode = m_pList;//头结点
	for (int k = 0; k < i; k++)//找到第i节点
	{
		currentNode = currentNode->next;
	}//直到找到第i个
	Node *newNode = new Node;
	if (newNode == NULL) //判断申请的结点内存是否为空
	{
		return false;
	}
	else
	{
		newNode->data = pNode->data;
		newNode->next = currentNode->next;//currentnode下一个节点变成newnode的下一个节点
		currentNode->next = newNode;
		m_iLength++;
		return true;
	}
}

bool List::ListDelete(int i, Node *pNode)
{
	if (i<0 || i>=m_iLength)//判断是否合法
	{
		return false;
	}
	Node *currentNode = m_pList;//找到头结点
	Node *currentNodeBefore = NULL;
	for (int k = 0; k <= i; k++)//找到<=第i个位置
	{
		currentNodeBefore = currentNode;
		currentNode = currentNode->next;
	}
	currentNodeBefore->next = currentNode->next;//将currennode分离出来
	pNode->data = currentNode->data;//赋值
	delete currentNode;//删除
	currentNode = NULL;//赋予null值
	m_iLength--;//删除节点-1
	return true;
}

bool List::GetElem(int i, Node *pNode)
{
	if (i<0 || i >= m_iLength)
	{
		return false;
	}
	Node *currentNode = m_pList;
	Node *currentNodeBefore = NULL;
	for (int k = 0; k <= i; k++)
	{
		currentNodeBefore = currentNode;
		currentNode = currentNode->next;
	}
	pNode->data = currentNode->data;
	return true;
}

int List::LocateElem(Node *pNode)
{
	Node *currentNode = m_pList;
	int count = 0;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
		if (currentNode->data == pNode->data)
		{
			return count;
		}
		count++;
	}
	return -1;
}

bool List::PriorElem(Node *pCurrentNode, Node *pPreNode)
{
	Node *currentNode = m_pList;
	Node *tempNode = NULL;
	int count = 0;
	while (currentNode->next != NULL)
	{
		tempNode = currentNode;
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data)
		{
			if (tempNode == m_pList) //tempnode是否是头结点
			{
				return false;
			}
			pPreNode->data = tempNode->data;
			return true;
		}
	}
	return false;
}

bool List::NextElem(Node *pCurrentNode, Node *pNextNode)//寻找后继
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)//遍历
	{
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data)
		{
			if (currentNode->next == NULL)//最后一个结点
			{
				return false;
			}
			pNextNode->data = currentNode->next->data;
			return true;
		}
	}
}

void List::ListTraverse()//遍历
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
		currentNode->printNode();
	}
}

main.cpp

#include"List.h"
#include "Node.h"
int main(void)
{
	Node node1;
	node1.data = 1;
	Node node2;
	node2.data = 2;
	Node node3;
	node3.data = 3;
	Node node4;
	node4.data = 4;

	Node node5;
	node5.data = 0;

	Node temp;

	List *pList = new List();
	//pList->ListInsertHead(&node1);
	//pList->ListInsertHead(&node2);
	//pList->ListInsertHead(&node3);
	//pList->ListInsertHead(&node4);//头部插入


	pList->ListInsertTail(&node1);
	pList->ListInsertTail(&node2);
	pList->ListInsertTail(&node3);
	pList->ListInsertTail(&node4);//尾部插入

	pList->ListInsert(1,&node5);
	pList->ListDelete(1, &temp);
	//pList->NextElem(&node5,&temp);
	pList->ListTraverse();
	cout << "temp=" << temp.data << endl;
	delete pList;
	pList = NULL;
	system("pause");
	return 0;
}

资料来源:慕课网

今天的分享就到这里。欢迎大家来指教和一起学习,我是爱吃肉的yyyloki,如果你觉得不错的话,那就给我点个👍吧!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值