Static linked list C + + implementation

Source: Internet
Author: User

<span style= "FONT-SIZE:12PX;" > A list of linked lists, called <strong> static linked list </strong>, that is described in arrays. In the C language, the static list is represented as an array of structures, and the structure variables include data fields and cursor cur. This storage structure still needs to be pre-allocated a large space, but in the insert and delete operations as linear tables do not need to move elements, only need to modify the pointer, it is still a chain storage structure of the main advantages. Represents a storage structure for a static linked list: Two header nodes are used in the color path, and no data is stored, respectively, to record the first alternate node and the subscript of the first Data node. </span><pre name= "code" class= "CPP" ><span style= "font-size:12px;" > below gives the C + + implementation code for the static list: first give the head file: <strong>staticlist.h</strong>: #include <iostream> #include < assert.h>using namespace std; #define MAXSIZE 20//Static list (array) default length # define Elemtype int//value type class Staticlist;              Node class typedef class Staticlistnode//Static linked list node type (array element type) {friend class Staticlist;private:elemtype data;               domain int cur; Cursor (indicates the next element subscript of the current node)}staticlistnode;//static linked list class </strong></span>classStaticlist{public:staticlist () {for (int i = 2; i<maxsize-1; ++i) space[i].cur = i+1;        space[i].cur = 0;        The entire list ends Space[0].cur = 2;        space[1].cur = 0;  Data node nodding cursor is empty, no data} ~staticlist () {}//Trailing insert method void Push_back (const elemtype &x) {int i =        MALLOC_SL (); if (0 = = i)//space application failed {cout<< "is full!               "<<x<<" cannot be inserted "<<endl;        return;        } space[i].data = x;                space[i].cur = 0;        int k = 1;                while (0!=k && 0!=space[k].cur)//Find last node k = space[k].cur;             Space[k].cur = i; Link the newly applied subscript I to the last node after}//head insert void Push_front (const elemtype &x) {int i = Mal        LOC_SL (); if (0 = = i)//Ibid, space application failed {cout<< "is full!            "<<x<<" cannot be inserted "<<endl;        return; } Space[i].daTA = x;   Put X into the node just applied space[i].cur = space[1].cur;              At this point, the cursor of node I just applied points to the first data node, called the first node space[1].cur = i;        Make head node point to First Data node}//Trailing delete void Pop_back () {int i = space[1].cur;        int j = 0;     for (; 0!=space[i].cur; j = i, I = space[i].cur) {}//Find the last node and the second-lowest node space[j].cur = 0;           The cursor of the second-to-last node is assigned an empty FREE_SL (i);    The last node is freed}//head delete void Pop_front () {int i = space[1].cur;  I is the subscript of the first data node space[1].cur = space[i].cur;              The head node points to the second data node subscript FREE_SL (i); I node is freed} void Show_list () {for (int i = space[1].cur; i!=0; i = space[i].cur) cout<&        lt;space[i].data<< "";    cout<< "Over" <<endl;        }/* Static linked list from small to large ordering, insert X */void Insert_val (const elemtype &x) {int k = 1; while (0!=k && 0!=space[k].cur && space[space[k].cur].data<x) k = Space[k].cur;        After subscript k Insert if (0 = = space[k].cur)//If k points to the last node, perform the tail plug push_back (x);        else if (k = = 1)//if k points to the first node, perform head interpolation push_front (x);            else//insert x {int i = MALLOC_SL () at any place in the middle;            ASSERT (0! = i);            Space[i].data = x;   Space[i].cur = Space[k].cur;              The cursor of the I-node points to a node following the K-node space[k].cur = i;  The cursor of the K-node points to the newly opened I-node}}/* Returns the previous node of key subscript */int Find (const elemtype &key) {int i =        1;        while (0!=i && space[space[i].cur].data!=key) i = space[i].cur;            if (0 = = i) {cout<< "not found" <<key<<endl;        return-1;    } return i;        }/* Delete the given value key node, if not found return */void Delete_val (const elemtype &key) {int i = find (key);        if ( -1 = = i)//indicates that there is no element key return in the static list; else if (1 = = i)//key A node in the subscript 2 (First Data node) Pop_front ();        else if (0 = = space[i].cur)//key is in the last node Pop_back ();   else//key is in the middle anywhere {int k = space[i].cur;  Record the subscript space[i].cur = Space[k].cur for the location to be deleted;  Break out to remove the node FREE_SL (k); Remove key Node}}//* SL1 and SL2 already exist, and combine them into another list to make them in non-descending order */void Merge (Staticlist &sl1, Staticlist &A           MP;SL2) {sl1.sort ();        Sl2.sort ();        if (0==sl1.length () | | 0==sl2.length ()) return;        int p = sl1.space[1].cur;                int q = sl2.space[1].cur; while (0!=p && 0!=q) {/////Which data is small, insert the end of the data into the new list and point the cursor to the next if (Sl1.space[p].data & Lt                Sl2.space[q].data) {push_back (sl1.space[p].data);            p = sl1.space[p].cur;                } else {push_back (sl2.space[q].data);          Q = sl2.space[q].cur;  }} while (0!=p) {//Because SL1 is already in order, if SL1 has not all been inserted into the new linked list, insert all the rest into the push_back (SL1.SPACE[P].D            ATA);        p = sl1.space[p].cur;            while (0!=q) {//Because SL2 is already in order, if SL2 is not fully inserted into the new list, insert all the rest into push_back (sl2.space[q].data);        Q = sl2.space[q].cur;        }}/* If the static list is unordered, make it in non-descending order */void sort () {int s = space[1].cur;        int p = space[s].cur;        if (0 = = p) return;                space[s].cur = 0;        int k = 1;        int k1 = 0;            while (0! = p) {s = p;                        p = space[p].cur;     K = 1;                Find a location k, insert the data of the node referred to in S after K while (0!=k && Space[space[k].cur].data < Space[s].data) {                  K1 = k;        If k==0, record the last data node with k1 k = space[k].cur;                Insert after subscript k} if (0 = = k)//tail interpolation {space[k1].cur = s; Space[s].cur = 0;                } else//head Plug and middle Insert {space[s].cur = Space[k].cur;            Space[k].cur = s;        }}}//Reverse static list */void reserve () {int s = space[1].cur;        int p = space[s].cur;        if (0==p) return;        space[s].cur = 0;            while (0! = p) {s = p;                        p = space[p].cur;   Space[s].cur = Space[1].cur;              The S knuckle nods into the original linked list space[1].cur = s;            s becomes the first Data node}/* Clears the static list */void Clear () {for (int i = 2; i<maxsize-1; ++i)        Space[i].cur = i+1;                space[i].cur = 0;     Space[0].cur = 2;     Subscript 20% is the first standby node space[1].cur = 0;        The first Data node is empty}/* Returns the table length */int Length () {if (0 = = Space[1].cur) return 0;        int i = 1;        int count =-1;            do {++count; i = space[i].cur;        }while (0! = i);    return count;        }/* Returns the next node of the node labeled K Subscript in the static list is not useful */int next (const int k) {if (0==k | | 1==k) return-1;    return space[k].cur;        }/* Returns the previous node subscript for the node labeled K */int prio (const int k) {if (0==k | | 1==k) return-1;        int p = 1;        while (0!=p && space[p].cur!=k) p = space[p].cur;    return p;   } protected:/* used to request a space, return the subscript for that node */int MALLOC_SL () {int i = space[0].cur;  0 The subscript cursor points to the first standby node if (space[0].cur) space[0].cur = space[i].cur;    Modify the head node to save the first standby point subscript return i;    }/* Releases the node labeled K */void Free_sl (int k) {space[k].cur = Space[0].cur;               The cursor of the node points to the first standby node space[0].cur = k; The node is called the first standby node} private:staticlistnode space[maxsize];}; </span><span style= "FONT-SIZE:12PX;" > Below is the test code: Main.cpp </span><pre name= "code" class= "CPP" ><span style= "font-size:12px; " > #include "StaticList.h" void Main () {staticlist SL;    Staticlist SL1; Test merge () Staticlist SL2; Sl1.push_back (1); Sl1.push_back (9); Sl1.push_back (0); Sl1.push_back (6); Sl1.push_back (999); Sl2.push_back (5); Sl2.push_back (8); Sl2.push_back (100); Elemtype Item = 0;int select = 1;while (select) {cout<< "********************************************" << endl;cout<< "*[1" push_back [2] Push_front * "<<endl;cout<<" *[3] show_list [4] Pop              _back * "<<endl;cout<<" *[5] pop_front [6] insert_val * "<<endl;cout<<" *[7] Length [8] Find * "<<endl;cout<<" *[9] merge [ten] Delete_ val * "<<endl;cout&l t;< "*[11" sort [[] reserve * "<<endl;cout<<" *[13] Next [] prio *                              "<<endl;cout<<" *[15] clear [+] Destroy * "<<endl;cout<<" *[0] Quit_sys * "<<endl;cout<< "********************************************" <<endl;cout<< "Please select:" ";cin>> Select;switch (Select) {case 1:cout<< "input data to be interpolated: (-1 end) >"; while (Cin>>item && 1! = Item) Sl.push_ Back (Item); break;case 2:cout<< "Enter data to be interpolated: (-1 end) >"; while (Cin>>item && 1! = Item) Sl.push_front (Item); break;case 3:sl.show_list (); break;case 4:sl.pop_back (); break;case 5:sl.pop_front (); Break;case 6:cout< < "Enter the data to be inserted:>";cin>>item; Sl.insert_val (Item); break;case 7:cout<< "linked list length:" <<sl.length () <<endl;break;case 8:cout<< " Enter the data you want to find:> ";cin>>item; Sl.find (Item); Break;case 9:sl.merge (SL1, SL2); break;case 10:cout<< "Enter data to be deleted:>";cin>>item; Sl.delete_val (Item); break;case 11:sl.sort (); break;case 12:sl.reserve (); Break;case 13:sl.next (0); Break;case 14: Sl.prio (0); break;case 15:sl.clear (); break;case 16:sl.~staticlist (); break;default:break;}} </span>





here is the test:



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

Static linked list C + + implementation

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.