数据结构之单链表——C++模板类实现

来源:互联网 发布:印刷排版用什么软件 编辑:程序博客网 时间:2024/06/06 02:11

单链表定义

#ifndef SinglyLinkedListEDLIST_H_INCLUDED#define SinglyLinkedListEDLIST_H_INCLUDED#include <bits/stdc++.h>using namespace std;template<class T>class LinkNode {public:        LinkNode(LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by default way!" << endl; link = ptr;}        LinkNode(const T& item, LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by defined way!" << endl; data = item, link = ptr;}        ~LinkNode() {cout << "Desctructing a LineNode!" << endl; delete link;}        T data;                    // Data Scope        LinkNode<T> *link;         // Point Scope, Point to next Node};// SinglyLinkedListList with head nodetemplate <class T>class SinglyLinkedList : public LinkNode<T> {public:        SinglyLinkedList() {cout << "Constructing a SinglyLinkedListList by default way" << endl; first = new LinkNode<T>;}        SinglyLinkedList(const T&x);        SinglyLinkedList(SinglyLinkedList<T>& L);       // Deep copy constructor function        ~SinglyLinkedList() {cout << "Destructing a SinglyLinkedListNode by default way" << endl; delete first;}        void makeEmpty();           // Delete all nodes in the SinglyLinkedList        int Length() const;         // Get the length of the SinlyLinkedList        LinkNode<T> *GetHead() const {return first;} // Return first        LinkNode<T> *Search(T x);   // x can only be a variable while T& x        LinkNode<T> *Locate(int i); // Get the i-th node's address        bool GetData(int i, T& x);        // Get the data in i-th node and let x = data        void SetData(int i, T& x);              // Make the data in i-th node equals x        bool Insert(int i, T& x);               // Insert a node in i-th with its' data be x        bool Remove(int i, T& x);               // Delete the i-th node and make x = node[i]->data        bool IsEmpty() const {return first->link == NULL ? true : false;}        bool IsFull() const {return false;}        void Sort();                            // Sort all datas in the SinglyLinkedList by non-decreasing way        void Input();                           // Input all elements        void Output();                          // Out all elements        SinglyLinkedList<T>& operator=(SinglyLinkedList<T>& L); // Overloading operator =protected:        LinkNode<T> *first;     // Point to Head node};#endif // SinglyLinkedListEDLIST_H_INCLUDED

单链表实现

#include <bits/stdc++.h>#include "SinglyLinkedList.h"using namespace std;template<class T>SinglyLinkedList<T>::SinglyLinkedList(const T&x) {        first = new LinkNode<T>;        if(first == NULL) {                cerr << "Invalid allocation " << endl;        }        first->link->data = x;}template<class T>SinglyLinkedList<T>::SinglyLinkedList(SinglyLinkedList<T>& L) {        // Deep copy constructor function        LinkNode<T> *destptr = first = new LinkNode<T>;        LinkNode<T> *srcptr = L.GetHead();        while(srcptr->link != NULL){                destptr->link = srcptr->link;                destptr->link->data = srcptr->link->data;                destptr = destptr->link;                srcptr = srcptr->link;        }}template<class T>void SinglyLinkedList<T>::makeEmpty() {        // Cannot delete first due to is the head in order to make some operation like insert more easy        LinkNode<T> *current;        while(first->link != NULL) {               current = first->link;               first->link = current->link;               delete current;        }}template<class T>int SinglyLinkedList<T>::Length() const {        int cnt = 0;        LinkNode<T> *current = first;        while(current->link != NULL) {                cnt++;                current = current->link;        }        return cnt;}template<class T>LinkNode<T>* SinglyLinkedList<T>::Search(T x) {      // x can only be a variable while T& x        LinkNode<T> *current = first;        while(current->link != NULL) {                if(current->link->data == x) {                        return current;                }                current = current->link;        }        return NULL;    // Not found}template<class T>LinkNode<T>* SinglyLinkedList<T>::Locate(int i) {        // Return the address of i-th node        if(i < 0) {                cerr << "Invalid Location of " << i << endl;                return NULL;        }        LinkNode<T> *current = first;        int k = 0;        while(current != NULL && k < i) {                current = current->link;                k++;        }        return current;         // Return NULL while i is too large}template<class T>bool SinglyLinkedList<T>::GetData(int i, T& x) {        if(i <= 0) {                cerr << "Invalid index of " << i << endl;                return false;        }        LinkNode<T> *current = Locate(i);        if(current == NULL) {                return false;        }        x = current->data;        return true;}template<class T>void SinglyLinkedList<T>::SetData(int i, T& x) {        if(i <= 0) {                cerr << "Invalid index of " << i << endl;                return ;        }        LinkNode<T> *current = Locate(i);        if(current == NULL) return ;        current->data = x;}template<class T>bool SinglyLinkedList<T>::Insert(int i, T& x) {        // Insert a node behind i-th node        if(i <= 0) {                cerr << "Invalid index of " << i << endl;                return false;        }        LinkNode<T> *current = Locate(i-1);        if(current == NULL) {                return false;        }        LinkNode<T> *newLinkNode = new LinkNode<T>(x);        if(newLinkNode == NULL) {                cerr << "Invalid allocation " << endl;                return false;        }        newLinkNode->link = current->link;        current->link = newLinkNode;        return true;}template<class T>bool SinglyLinkedList<T>::Remove(int i, T& x) {        // Remove the i-th node is the SinglyLinkedList        if(i <= 0) {                cerr << "Invalid position " << endl;        }        LinkNode<T> *current = Locate(i-1);        if(current == NULL || current->link == NULL) {                return false;   // The SinglyLinkedList is too short        }        LinkNode<T> *del = current->link;        x = del->data;        current->link = del->link;        delete del;        return true;}template<class T>void SinglyLinkedList<T>::Sort() {        // Sort all datas in the SinglyLinkedList by non-decreasing way        LinkNode<T> *p = first->link;        LinkNode<T> *q;        T value;        while(p->link != NULL) {                q = p->link;                while(q != NULL) {                        if(q->data < p->data) {                                value = p->data;                                p->data = q->data;                                q->data = value;                        }                        q = q->link;                }                p = p->link;        }}template<class T>void SinglyLinkedList<T>::Input() {        int cnt;        cout << "Please enter the total nodes of the SinglyLinkedListedList" << endl;        while(true) {                cin >> cnt;                if(cnt <= 0) {                        cerr << "Invalid length of SinglyLinkedListedList, the length must be a positive interger " << endl;                }                else break;        }        LinkNode<T> *current = first;        T val;        for(int i = 1; i <= cnt; ++i) {                cin >> val;                current->link = new LinkNode<T>(val);                current = current->link;        }}template<class T>void SinglyLinkedList<T>::Output() {        LinkNode<T> *current = first;        int cnt = 1;        while(current->link != NULL) {                cout << "#No. " << cnt++ << "  "  << current->link->data << endl;                current = current->link;        }}template<class T>SinglyLinkedList<T>& SinglyLinkedList<T>::operator=(SinglyLinkedList<T>& L) {        LinkNode<T> *srcptr, *destptr;        destptr = first = new LinkNode<T>;        srcptr = L.GetHead();        while(srcptr->link != NULL) {                destptr->link = srcptr->link;                destptr->link->data = srcptr->link->data;                destptr = destptr->link;                srcptr = srcptr->link;        }        destptr->link = NULL;        return *this;}int main(){        int a = 10;        SinglyLinkedList<int> SinglyLinkedListList;        if(SinglyLinkedListList.IsEmpty()) {                cout << "Jesus Christ, is empty!" << endl;        }        SinglyLinkedListList.Input();        cout << "Length: " << SinglyLinkedListList.Length() << endl;        SinglyLinkedListList.Sort();        SinglyLinkedListList.Output();        int num = 284;        SinglyLinkedListList.Insert(1, num);        SinglyLinkedListList.Insert(2, num);        SinglyLinkedListList.Insert(3, num);        SinglyLinkedListList.Output();        cout << "\n----------------------------\n";        SinglyLinkedListList.Sort();        SinglyLinkedListList.Output();        SinglyLinkedListList.GetData(1, a);        cout << "a: " << a << endl;        cout << "Length before remove the first element is:   " << SinglyLinkedListList.Length() << endl;        SinglyLinkedListList.Remove(1, a);        cout << "a: " << a << endl;        cout << "Length after remove the first element is:   " << SinglyLinkedListList.Length() << endl;        SinglyLinkedListList.Output();        SinglyLinkedListList.makeEmpty();        if(SinglyLinkedListList.IsEmpty()) {                cout << "Jesus Christ, is Empty!" << endl;        }        return 0;}


0 0
原创粉丝点击