數據結構(線性表)
阿新 • • 發佈:2018-09-23
new t 模板 返回 listt tlist fin 執行 efi included
大二學C++都快忘沒了,寫點數據結構來復習一下,寫的不好,不喜勿噴。
直接上代碼,這是模板類的寫法,必須全部寫在頭文件裏。因為編譯器不知道你會使用什麽類型的數據,所以無法確定要分配的存儲空間大小。
編譯器CodeBlocks
#ifndef LINEAR_LIST_H_INCLUDED #define LINEAR_LIST_H_INCLUDED #include <cstdio> template <class T> class List { private: int s; //線性表空間大小 int length; //線性表元素個數 T* head; //空間首地址 public: List<T>() { } List<T>(int size) : s(size), head(NULL) { } ~List<T>() { DestroyList(); } void InitList(); //初始化線性表 void DestroyList(); //銷毀線性表 void ClearList(); //線性表置空 bool ListEmpty(); //判斷線性表是否存在 int ListLength(); //返回線性表長度 bool GetElem(int i, T &e); //通過e得到第i個元素 int LocateElem(T e, bool (*compare)(T list_e, T e)); //通過compare函數進行比較,得到第一個符合的元素 bool PriorElem(T cur_e, T &pre_e); //通過pre_e得到cur_e的前驅 bool NextElem(T cur_e, T &next_e); //通過next_e得到cur_e的後繼 bool ListInsert(int i, T e); //在第i個位置前插入新元素e bool ListDelete(int i, T &e); //刪除第i個元素,用e返回它的值 bool Insert(T); //向尾部插入一個元素 bool ListTraverse(bool (*visit)(T e)); //依次對每個元素執行visit()操作 }; template <class T> void List<T>::InitList() { if(head == NULL) { head = new T[s]; length = 0; } } template <class T> void List<T>::DestroyList() { if(head != NULL) { delete head; head = NULL; } } template <class T> void List<T>::ClearList() { length = 0; } template <class T> bool List<T>::ListEmpty() { if(head == NULL) return true; else return false; } template <class T> int List<T>::ListLength() { if(head == NULL) return -1; return length; } template <class T> bool List<T>::GetElem(int i, T &e) { if(head == NULL || length == 0) return false; e = head[i]; return true; } template <class T> int List<T>::LocateElem(T e, bool (*compare)(T list_e, T e)) { if(head == NULL || length == 0) return -1; for(int i = 0; i < length; i++) if(compare(head[i], e)) return i; return -1; } template <class T> bool List<T>::PriorElem(T cur_e, T &pre_e) { if(head == NULL || length == 0 || head[0] == cur_e) return false; for(int i = 0; i < length; i++) { if(head[i] == cur_e) { pre_e = head[i-1]; return true; } } return false; } template <class T> bool List<T>::NextElem(T cur_e, T &next_e) { if(head == NULL || length == 0 || head[length] == cur_e) return false; for(int i = 0; i < length; i++) { if(head[i] == cur_e) { next_e = head[i+1]; return true; } } return false; } template <class T> bool List<T>::ListInsert(int i, T e) { if(head == NULL || length == 0 || length-1 < i || length == s) return false; for(int j = length-1; j >= i; j--) head[j+1] = head[j]; head[i] = e; length++; return true; } template <class T> bool List<T>::ListDelete(int i, T &e) { if(head == NULL || length == 0 || length-1 < i) return false; if(i == length-1) { length--; e = head[i]; return true; } e = head[i]; length--; for(int j = i; j < length; j++) head[j] = head[j+1]; return true; } template <class T> bool List<T>::Insert(T e) { if(head == NULL || length == s) return false; head[length] = e; length++; return false; } template <class T> bool List<T>::ListTraverse(bool (*visit)(T e)) { if(head == NULL || length == 0) return false; for(int i = 0; i < length; i++) if(!visit(head[i])) return false; return true; } #endif // LINEAR_LIST_H_INCLUDED
數據結構(線性表)