C + + implementation of static linked list

Source: Internet
Author: User

Static linked lists are implemented using arrays to quickly insert and delete data linked list, static linked list and chain of single-linked list of the disadvantage is that the length of the list can only be initialized set, and relative to the normal sequential storage of the linked list, static linked list can not achieve fast read and write arbitrary elements.

Of course, static lists give us a way of thinking, and when we can't use pointers in certain States, we can use an alternative pointer method, a static list of cur to represent the subscript of the next node of the current node.

#pragma once#define MAXSIZE 1000template<typename eletype>class staticlist{public:typedef struct{EleType data; int cur;} Node; Staticlist (); ~staticlist (); bool Insert (const eletype& e, int index = 1), bool Delete (eletype& e, int index = 1); Vo ID Show () const;private:int newspace ();//Returns a list of available spaces subscript void Deletespace (int index);//delete the index element in list bool Empty () Const;bool full () const; Node stlist[maxsize];int Length;};

#include "StaticList.h" #include <iostream>using namespace Std;template<typename eletype>staticlist< Eletype>::staticlist (): Length (0) {for (int i = 0; i < maxsize-1;++i) {stlist[i].cur = i + 1;} stlist[maxsize-1].cur = 0;} Template<typename eletype>staticlist<eletype>::~staticlist () {}template<typename EleType>bool Staticlist<eletype>::insert (const eletype& E, int index/*= 1*/) {if (full ())//if filled, does not insert data {cout << " Can ' t insert element to a full list!\n "; return false;} if (index<1| | INDEX&GT;LENGTH+1)//If the subscript of the insertion point is not valid, return false{cout << "the invalid index!\n"; return false;} int k = NewSpace ();//Returns an index of the node that can be inserted int j = maxsize-1;if (k)//If the return subscript is not 0{stlist[k].data = e;//The data that returns the position is set to Efor (int i = 1; I <= index-1;++i)//Find the subscript of the previous node of the insertion node {j = Stlist[j].cur;} Stlist[k].cur = stlist[j].cur;//Sets the cur of the insertion node to the curstlist[j].cur of the previous node in the insertion position = k;//Sets the cur of the previous node in the insertion position to K, After the implementation of the K-node inserted into the Index-1 node, the implementation of the K-node inserted into the index position of the ++length;//list length plus a return true;} return false;} TemplaTe<typename Eletype>bool staticlist<eletype>::D elete (eletype& e, int index/*= 1*/) {if (Empty ())// If the list is empty, do not delete operation {cout << "Can ' t delete element in a empty list!\n"; return false;} if (index<1 | | index>length)//If the deleted location is not valid, return false{cout << "the invalid index!\n"; return false;} int k = Maxsize-1;int i = 1;for (; I <= index-1;++i)//Find first index-1 node k{k = stlist[k].cur;} i = stlist[k].cur;//i is the subscript for the index node stlist[k].cur = stlist[i].cur;//Sets the cur of the index-1 node to the cur of the index node, Implemented to exclude the index node from the list E = stlist[i].data;//Returns the index node of data to Edeletespace (i);//Reclaim the space--length;//of the index node List length minus one return true;} Template<typename eletype>void staticlist<eletype>::show () const{if (Empty ()) {cout << "the List is Empty!\n "; return;} int k = Stlist[maxsize-1].cur;cout << "The list is: \ n"; for (int i = 1; I <= length;++i) {cout << stlist[k ].data << ""; k = stlist[k].cur;} cout << Endl;} Template<typename Eletype>bool staticlist<eletype>::Full () const{if (Length > MAXSIZE-2)//Guaranteed stlist[0] and stlist[maxsize-1] not be inserted data overwrite {return true;} return false;} Template<typename eletype>bool Staticlist<eletype>::empty () const{return (Length = = 0);} Template<typename eletype>void staticlist<eletype>::D eletespace (int index) {stlist[index].cur = StList[0 ].cur;//the node to be removed is added to the idle node stlist[0].cur = index;//Sets the node to the first available idle node}template<typename eletype>int staticlist <eletype>::newspace () {int i = stlist[0].cur;//the first available idle sibling if (stlist[0].cur)//If the idle node is available {stlist[0].cur = stlist[ i].cur;//set the next first available idle node to the next node of the returned node}return i;//returns the subscript of the available node}

#include "StaticList.cpp" int main () {staticlist<int> testlist; Testlist.insert (12); Testlist.insert (12); Testlist.insert (34); Testlist.insert (23); Testlist.insert (12); Testlist.insert (99,4); Testlist.show (); int m = 0; Testlist.delete (m,7); cout << "____________" << m << "_______________\n"; Testlist.show (); return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + implementation of static linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.