單鏈表基本實現(c++)
阿新 • • 發佈:2019-01-08
因為找實習,公司工作人員讓我實現了一下單鏈表的基本演算法。
單鏈表其實是一種比較簡單的資料結構,不同於陣列,單鏈表的節點在記憶體中並不是連續存放的,而是靠節點中的指標來連線起來。所以單鏈表不如陣列那樣可以隨機存取,但是,在其他方面,比如增加元素,插入元素,刪除元素方面,單鏈表要比陣列效率高。
下面是我實現的程式碼:
標頭檔案 linkedlist.h
#ifndef LINKEDLIST_H #define LINKEDLIST_H typedef struct Node { int data; struct Node *next; }Node; class LinkedList { private: Node *head; int length; public: LinkedList(); ~LinkedList(); void insert(int i, int value); void deleteElem(int e); void create(int *a, int n); bool isEmpty(); void insertsort(); void quicksort(); int getlength(); void display(); private: void quicksort2(Node *head, Node *tail); Node * partion(Node *head, Node *tail); // implement create, insert, remove and sort methods. }; #endif
實現檔案 linkedlist.cpp
#include<iostream> #include<assert.h> using namespace std; #include"LinkedList.h" //單鏈表建構函式 LinkedList::LinkedList() { head = NULL; length = 0; } //單鏈表解構函式——回收單鏈表每個節點的儲存空間 LinkedList::~LinkedList() { Node *pNode, *qNode; pNode = NULL; qNode = head; while(qNode) { pNode = qNode; qNode = qNode->next; delete pNode; } head = NULL; } // 在第i個節點後面插入資料域為value的節點 //i=0表示在最前面位置插入 void LinkedList::insert(int i, int value) { int j = 1; Node *p = head; Node *ins; if(i < 0 || i > length) { cout<<"out range! fails to insert!"<<endl; return; } //超出有效範圍,插入失敗,返回0 ins = new Node; assert(ins != NULL); ins->data = value; if(i == 0) //i==0,在最前面插入 { ins->next = head; head = ins; length++; } else //i!=0,在第i個元素後面進行插入 { while(p && j < i) //遍歷到第i個元素,在其後進行插入 { j++; p = p->next; } ins->next = p->next; p->next = ins; length++; } } bool LinkedList::isEmpty() { if(head == NULL) return true; else return false; } void LinkedList::deleteElem(int e) { Node *p = head; Node *pre = NULL; while(p && p->data != e) { pre=p; p = p->next; } if(p == NULL) { cout<<"no such node whose value equals e"<<endl; return; } if(pre == NULL) { head = head ->next; length--; } else { pre->next = p->next; length--; } free(p); return; } int LinkedList::getlength() { return length; } void LinkedList::quicksort2(Node *head, Node *tail) { if(head != tail) { Node *mid = partion(head, tail); // display(); quicksort2(head, mid); quicksort2(mid->next, tail); } } void LinkedList::quicksort() { Node *tail = head; while(tail) tail = tail->next; quicksort2(head,tail); } Node * LinkedList::partion(Node *head, Node *tail) { Node *p, *mid; int key, temp; mid = head; p = mid->next; key =head->data; while(p != tail) { if(p->data <= key) { mid = mid->next; // 交換資料域,p->data 《——》 mid->data temp = p->data; p->data = mid->data; mid->data = temp; } p = p->next; } temp = mid->data; mid->data = head->data; head->data = temp; return mid;//返回劃分出來的中間點。 } void LinkedList::insertsort() { Node *p = head; Node *q, *r, *prer; if(!p || !p->next) return; //只有一個元素或為空 ,不用排序;直接返回。 q = p->next; while(q) { if(p->data > q->data) { r = head; prer = NULL; while(r->data < q->data) { prer = r; r = r->next; } if(prer == NULL) { p->next = q->next; q->next = r; head = q; q = p->next; } else { p->next = q->next; prer->next = q; q->next = r; q = p->next; } } else { p = p->next; q = q->next; } } } void LinkedList::create(int a[], int n) { int i = 0; Node *p; for(i = n-1; i >=0; i--) { p = new Node; assert(p!=NULL); p->data = a[i]; p->next = head; head = p; length++; } } void LinkedList::display() { Node *p = head; if(isEmpty()) cout<<"there is nothing! It is empty!"<<endl; else { // cout<<"the list has "<<getlength()<<" members: "<<endl; while(p) { cout<<p->data<<" "; p = p->next; } } cout<<endl; }
上面連結串列的排序演算法出來插入排序,還實現了單鏈表的快速排序,快速排序的時間複雜度要比插入的時間複雜度低。
總的來說,單鏈表實現起來比較簡單,但是能力是在一點一滴的積累中鍛煉出來的。