算法與數據結構(11): 線性表(2)——鏈表

在這裏插入圖片描述

注:轉載請標明原文出處鏈接:https://xiongyiming.blog.csdn.net/article/details/100855604


3 鏈表的基本操作

3.1 鏈表簡介

鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。
鏈表由一系列結點(鏈表中每一個元素稱爲結點)組成,結點可以在運行時動態生成。每個結點包括兩個部分:一個是存儲數據元素的數據域,另一個是存儲下一個結點地址的指針域
相比於線性表順序結構,操作複雜。由於不必須按順序存儲,鏈表在插入的時候可以達到O(1)的複雜度,比另一種線性表順序錶快得多,但是查找一個節點或者訪問特定編號的節點則需要O(n)的時間,而線性表和順序表相應的時間複雜度分別是O(logn)和O(1)。
(以上均來自百度百科)


在這裏插入圖片描述


順序表優點:遍歷和尋址非常快
順序表缺點:往順序表中插入一個元素,順序表中的元素都要往後移動;順序表中刪除一個元素, 順序表中的元素都要往前移動。這樣的效率很低。

因此,針對順序表的缺點,引出鏈表


在這裏插入圖片描述


在這裏插入圖片描述


在這裏插入圖片描述


在這裏插入圖片描述



3.2 代碼示例

要求

線性表——單鏈表

  1. List();//創建線性表——構造函數
  2. ~List(); //銷燬線性表——析構函數
  3. void ClearList();//清空線性表
  4. bool ListEmpty();//判斷線性表是否爲空
  5. int ListLength(); //獲取線性表長度
  6. bool GetElem(int i, Node *pNode); //獲取指定元素
  7. int LocateElem(Node *pNode); //尋找第一個滿足e的數據元素的位序
  8. bool PriorElem(Node *pCurrentNode, Node *pPreNode); //獲取指定元素的前驅
  9. bool NextElem(Node *pCurrentNode, Node *pNextNode); //獲取指定元素的後繼
  10. bool ListInsert(int i, Node *pNode); //在第i個位置插入元素
  11. bool ListDelete( int i, Node *pNode); //刪除第i個位置的元素
  12. void ListTraverse(); //遍歷線性表
  13. bool ListInsertHead(Node *pNode);//從頭插入節點
  14. bool ListInsertTail(Node *pNode);//從尾插入節點

Node.h

#pragma once

class Node
{
public:
	int data;//數據域
	Node *next;//指針域

	void printNode();//打印節點

};

Node.cpp

#include<iostream>
#include"Node.h"

using namespace std;

//打印節點
void Node::printNode()
{
	cout << data << endl;
}

List.h

#pragma once
#include"Node.h"

class List
{
public:
	List();//創建線性表——構造函數
	~List();      //銷燬線性表——析構函數
	void ClearList();//清空線性表
	bool ListEmpty();//判斷線性表是否爲空
	int ListLength(); //獲取線性表長度
	bool GetElem(int i, Node *pNode); //獲取指定元素
	int LocateElem(Node *pNode); //尋找第一個滿足e的數據元素的位序
	bool PriorElem(Node *pCurrentNode, Node *pPreNode); //獲取指定元素的前驅
	bool NextElem(Node *pCurrentNode, Node *pNextNode); //獲取指定元素的後繼
	bool ListInsert(int i, Node *pNode); //在第i個位置插入元素
	bool ListDelete( int i, Node *pNode); //刪除第i個位置的元素
	void ListTraverse(); //遍歷線性表

	bool ListInsertHead(Node *pNode);//從頭插入節點
	bool ListInsertTail(Node *pNode);//從尾插入節點

private:
	Node *m_pList;
	int m_iLength;

};

List.cpp

#include<iostream>
#include"List.h"

using namespace std;

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()
{
	if (m_iLength==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//獲取線性表長度
int List::ListLength()
{
	return m_iLength;
}

//獲取指定元素
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;
}


//尋找第一個滿足e的數據元素的位序
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)
			{
				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;
		}
	}

	return false;

}

//在第i個位置插入元素
bool List::ListInsert(int i, Node *pNode)
{
	if (i<0 || i>m_iLength)
	{
		return false;
	}
	Node *currentNode = m_pList;
	for (int k = 0; k < i; k++)
	{
		currentNode = currentNode->next;
	}

	Node *newNode = new Node;
	if (newNode == NULL)
	{
		return false;
	}

	newNode->data = pNode->data;
	newNode->next = currentNode->next;
	currentNode->next = newNode;

	return true;
}

//刪除第i個位置的元素
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++)
	{
		 currentNodeBefore= currentNode;
		 currentNode = currentNode->next;
	}

	currentNodeBefore->next = currentNode->next;
	pNode->data = currentNode->data;

	delete currentNode;
	currentNode = NULL;
	m_iLength--;

	return true;
}

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


//從頭插入節點
bool List::ListInsertHead(Node *pNode)
{
	Node *temp = m_pList->next;
	Node *newNode = new Node;
	if (newNode == NULL)
	{
		return false;
	}

	newNode->data = pNode->data;
	m_pList->next = newNode;
	newNode->next = temp;
	m_iLength++;

	return true;
}


//從尾插入節點
bool List::ListInsertTail(Node *pNode)
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
	}

	Node *newNode = new Node;
	if (newNode == NULL)
	{
		return false;
	}
	currentNode->data = pNode->data;
	newNode->next = NULL;
	currentNode->next = newNode;
	m_iLength++;

	return true;

}

main.cpp

#include<iostream>
#include"List.h"

using namespace std;

/**********************************************************************
線性表——單鏈表


	List(int size);//創建線性表——構造函數
	~List();      //銷燬線性表——析構函數
	void ClearList();//清空線性表
	bool ListEmpty();//判斷線性表是否爲空
	int ListLength(); //獲取線性表長度
	bool GetElem(int i, int *e); //獲取指定元素
	int LocateElem(int *e); //尋找第一個滿足e的數據元素的位序
	bool PriorElem(int *currentElem, int *preElem); //獲取指定元素的前驅
	bool NextElem(int *currentElem, int *nextElem); //獲取指定元素的後繼
	bool ListInsert(int i, int *e); //在第i個位置插入元素
	bool ListDelete( int i, int *e); //刪除第i個位置的元素
	void ListTraverse(); //遍歷線性表

**********************************************************************/


int main()
{
	Node node1;
	node1.data = 3;
	Node node2;
	node2.data = 4;
	Node node3;
	node3.data = 5;
	Node node4;
	node4.data = 6;

	List *pList = new List();

	pList->ListInsertHead(&node1);
	pList->ListInsertHead(&node2);
	pList->ListInsertHead(&node3);
	pList->ListInsertHead(&node4);
	pList->ListTraverse();
	
	cout << "______________" << endl;

	//從中間插入元素
	Node node5;
	node5.data = 7;
	pList->ListInsert(0, &node5);
	pList->ListTraverse();
	cout << "______________" << endl;


	

	delete pList;
	pList = NULL;

	cin.get();
	return 0;
}

運行結果

在這裏插入圖片描述



4 鏈表應用——通訊錄

等待更新…….




參考資料

[1] 數據結構探險之線性表篇

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章