注:轉載請標明原文出處鏈接:https://xiongyiming.blog.csdn.net/article/details/100848417
1 線性表簡介
線性表(linear list)是最基本、最簡單、也是最常用的一種數據結構。
一個線性表是n個具有相同特性的數據元素的有限序列。
線性表中數據元素之間的關係是一對一的關係,即除了第一個和最後一個數據元素之外,其它數據元素都是首尾相接的(注意,這句話只適用大部分線性表,而不是全部。比如,循環鏈表邏輯層次上也是一種線性表(存儲層次上屬於鏈式存儲),但是把最後一個數據元素的尾指針指向了首位結點)。
(以上均來自百度百科)
線性表分爲順序表和鏈表,如下圖所示:
鏈表有:靜態鏈表,單鏈表,循環鏈表 和 雙向鏈表。
2 順序表的基本操作
(1) 順序表——插入, 刪除, 清空, 遍歷等操作
要求:
線性表——順序表
3 5 7 2 9 1 8
前驅 後繼
- 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(); //遍歷線性表
List.h
#pragma once
class List
{
public:
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(); //遍歷線性表
private:
int *m_pList;
int m_iSize;
int m_iLength;
};
List.cpp
#include<iostream>
#include"List.h"
using namespace std;
List::List(int size)
{
m_iSize = size;
m_pList = new int[m_iSize];
m_iLength = 0;
}
List::~List()
{
delete[]m_pList;
m_pList = NULL;
}
//清空線性表
void List::ClearList()
{
m_iLength = 0;
}
//判斷線性表是否爲空
bool List::ListEmpty()
{
if (m_iLength==0)
{
return true;
}
else
{
return false;
}
}
//獲取線性表長度
int List::ListLength()
{
return m_iLength;
}
//獲取指定元素
bool List::GetElem(int i, int *e)
{
if (i<0 || i>m_iSize)
{
return false;
}
*e = m_pList[i];
return true;
}
//尋找第一個滿足e的數據元素的位序
int List::LocateElem(int *e)
{
for (int i = 0; i < m_iLength; i++)
{
if (m_pList[i] == *e)
{
return i;
}
}
return -1;
}
//獲取指定元素的前驅
bool List::PriorElem(int *currentElem, int *preElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == 0)
{
return false;
}
else
{
*preElem=m_pList[temp - 1];
return true;
}
}
}
//獲取指定元素的後繼
bool List::NextElem(int *currentElem, int *nextElem)
{
int temp = LocateElem(currentElem);
if (temp == -1)
{
return false;
}
else
{
if (temp == m_iLength-1)
{
return false;
}
else
{
*nextElem = m_pList[temp + 1];
return true;
}
}
}
//在第i個位置插入元素
bool List::ListInsert(int i, int *e)
{
if (i < 0 || i > m_iLength)
{
return false;
}
for (int k = m_iLength-1; k >=i; k--)
{
m_pList[k + 1] = m_pList[k];
}
m_pList[i] = *e;
m_iLength++;
return true;
}
//刪除第i個位置的元素
bool List::ListDelete(int i, int *e)
{
*e = m_pList[i];
if (i<0||i>=m_iLength)
{
return false;
}
for (int k = i+ 1; k < m_iLength; k++)
{
m_pList[k - 1] = m_pList[k];
}
m_iLength--;
return true;
}
//遍歷線性表
void List::ListTraverse()
{
for (int i=0; i < m_iLength; i++)
{
cout<<m_pList[i] << endl;
}
}
main.cpp
#include<iostream>
#include"List.h"
using namespace std;
/**********************************************************************
線性表——順序表
3 5 7 2 9 1 8
前驅 後繼
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()
{
int e1 = 3;
int e2 = 5;
int e3 = 7;
int e4 = 2;
int e5 = 9;
int e6 = 1;
int e7 = 8;
int temp = 0;
List *list1 = new List(10);
//插入元素 3 5 7 2 9 1 8
list1->ListInsert(0, &e1);
list1->ListInsert(1, &e2);
list1->ListInsert(2, &e3);
list1->ListInsert(3, &e4);
list1->ListInsert(4, &e5);
list1->ListInsert(5, &e6);
list1->ListInsert(6, &e7);
//遍歷
list1->ListTraverse();
cout << "--------------------" << endl;
//刪除元素
list1->ListDelete(0, &temp);//刪除第一個元素
//遍歷
list1->ListTraverse();
cout << "刪除的元素爲:" << temp << endl;
cout << "--------------------" << endl;
//判斷線性表是否爲空
if (!list1->ListEmpty())
{
cout << "not empty" << endl;
}
cout << "--------------------" << endl;
//清空線性表
list1->ClearList();//清空線性表
//遍歷
list1->ListTraverse();
//判斷線性表是否爲空
if (list1->ListEmpty())
{
cout << "empty" << endl;
}
cout << "--------------------" << endl;
delete list1;
cin.get();
return 0;
}
運行結果
(2) 順序表——指定元素的前驅 和 後繼
main.cpp文件出現改動,其他文件不做改變
main.cpp
#include<iostream>
#include"List.h"
using namespace std;
/**********************************************************************
線性表——順序表
3 5 7 2 9 1 8
前驅 後繼
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()
{
int e1 = 3;
int e2 = 5;
int e3 = 7;
int e4 = 2;
int e5 = 9;
int e6 = 1;
int e7 = 8;
int temp = 0;
List *list1 = new List(10);
//插入元素 3 5 7 2 9 1 8
list1->ListInsert(0, &e1);
list1->ListInsert(1, &e2);
list1->ListInsert(2, &e3);
list1->ListInsert(3, &e4);
list1->ListInsert(4, &e5);
list1->ListInsert(5, &e6);
list1->ListInsert(6, &e7);
//遍歷
list1->ListTraverse();
cout << "--------------------" << endl;
//獲取指定元素的值
list1->GetElem(0, &temp);
cout << "temp的值: " << temp << endl;
//獲取指定元素位置
cout << "temp的位置: " << list1->LocateElem(&temp) << endl;
cout << "--------------------" << endl;
//指定元素的前驅
list1->PriorElem(&e4, &temp);
cout << "e4的前驅值: " << temp << endl;
//指定元素的後繼
list1->NextElem(&e4, &temp);
cout << "e4的後繼值: " << temp << endl;
delete list1;
cin.get();
return 0;
}
運行結果
3 鏈表的基本操作
4 鏈表應用——通訊錄
參考資料
[1] 數據結構探險之線性表篇