C++通過類實現(xiàn)線性表
更新時間:2020年05月25日 10:36:03 作者:wwxy261
這篇文章主要為大家詳細介紹了C++通過類實現(xiàn)線性表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++類實現(xiàn)線性表的具體代碼,供大家參考,具體內(nèi)容如下
下圖是標準C語言實現(xiàn)的函數(shù)定義
下面可以用C++實現(xiàn),第一個參數(shù)就是this的指針
list.h函數(shù)
#pragma once typedef int Elem; class List { public: List(int size); ~List(); void ClearList(); // 將數(shù)組長度設(shè)為0 bool ListEmpty(); // 判斷數(shù)組是否為空 int ListLength(); // 獲取數(shù)組長度 bool GetElem(int i, Elem *e); // 查找指定下標元素 int LocateElem(Elem *e); // 查找指定元素 bool PriorElem(Elem *currentElem, Elem *preElem); // 查找元素的前驅(qū)元素 bool NextElem(Elem *currentElem, Elem *nextElem); // 查找元素的后繼元素 void ListTraverse(); // 遍歷線性表,輸出元素 bool ListInsert(int i, Elem *e); // 在指定位置插入一個元素 bool ListDelete(int i, Elem *e); // 刪除指定位置元素 private: int *m_pList; // 指向一塊內(nèi)存 int m_iSize; // 內(nèi)存的大小 int m_iLength; // 數(shù)組的長度 };
類的實現(xiàn),list.cpp
#include<iostream> #include "List.h" using namespace std; List::List(int size) { m_iSize = size; m_pList = new Elem[m_iSize]; m_iLength = 0; } List::~List() { delete[] m_pList; // 釋放數(shù)組內(nèi)存 m_pList = NULL; } void List::ClearList() { m_iLength = 0; } bool List::ListEmpty() { return m_iLength == 0 ? true : false; } int List::ListLength() { return m_iLength; } bool List::GetElem(int i, Elem *e) { if (i < 0 || i >= m_iSize) { return false; } *e = m_pList[i]; return true; } int List::LocateElem(Elem *e) { for (int i = 0; i < m_iLength; i++) { if (m_pList[i] == *e) { return i; } } return -1; } bool List::PriorElem(Elem *currentElem, Elem *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(Elem *currentElem, Elem *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; } } void List::ListTraverse() { for (int i = 0; i < m_iLength; i++) { cout << m_pList[i] << endl; } } bool List::ListInsert(int i, Elem *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; } bool List::ListDelete(int i, Elem *e) { if (i<0 || i>m_iLength) return false; *e = m_pList[i]; for (int k = i + 1; k < m_iLength; k++) { m_pList[k - 1] = m_pList[k]; } m_iLength--; return true; }
測試主程序
#include<iostream> #include "List.h" using namespace std; int main() { Elem temp; Elem arry[11] = { 3,5,7,2,9,1,8 }; List *list1 = new List(10); cout << "length:" << list1->ListLength() << endl; for (int i = 0; i < 7; i++) { list1->ListInsert(i, &arry[i]); } cout << "length:" << list1->ListLength() << endl; // 刪除第一個元素 list1->ListDelete(0, &temp); cout << temp << endl; // 搜索前驅(qū)元素 list1->PriorElem(&arry[4], &temp); cout << temp << endl; list1->NextElem(&arry[4], &temp); cout << temp << endl; list1->ListTraverse(); delete list1; return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實現(xiàn)輸入一個字符串后打印出該字符串中字符的所有排列
這篇文章主要介紹了C語言實現(xiàn)輸入一個字符串后打印出該字符串中字符的所有排列的方法,是數(shù)學(xué)中非常實用的排列算法,需要的朋友可以參考下2014-09-09