Data structure and algorithm--linear chain-type storage structure (static linked list)

Source: Internet
Author: User

Summarize the static linked list today.

What is a static linked list?

I understand that a static list is a pseudo-linked list because it does not use pointers. A static linked list is implemented using an array , which is an array of structures, composed of data fields and pointer fields, and unlike a single linked list, this pointer field is not a pointer, but an integer that points to the next node (array subscript).

A static linked list is actually equivalent to a list of two linked lists, a linked list of actual data, and a linked list of hollow elements of an array, called an idle list or alternate list, used to hold inserted elements.

Experience: static linked list feeling than a single-linked list, two-way linked list around a lot of reading, the book in the release of the node space that piece of writing is very complex, and then I realized the discovery is not on. So, according to the static linked list of understanding, the principle of their own implementation, may be implemented in the book or on the Internet is not the same, but I think the idea and principle are the same. There is no place, I hope to point out, I also learn this piece .

Implementation code:

<span style= "Font-family:courier new;font-size:14px;"  > #include <iostream>using namespace std;const int max_size = 100;template <class t>struct staticnode {T    Data int next;};    Template <class t>class staticlinklist {public:staticlinklist ();    Staticlinklist (T a[],int N);        void Insert (int i,t x);               Insert element a T Delete (int i) at position I;                remove element I, return the deleted element value int Get (int i);                 Find the element at position I returns the subscript int NewNode () in the array;        Application node space void deletenode (int i);    Releases the node that the cursor I points to, void Printstaticlinklist ();               Traverse the linked list int getlength ();    Gets the length of the linked list Private:int front;    int tail; Staticnode<t> sarray[max_size];};         Template <class t>staticlinklist<t>::staticlinklist () {front =-1;           The head pointer of the empty list points to-1 tail = 0;  The subscript for the first element of unallocated space for (int i=0;i<max_size-1;i++) {sarray[i].next = i+1; The next field of each element points to the next element} Sarray[max_size-1] =-1; The next field of the last element is set to-1}Template <class t>staticlinklist<t>::staticlinklist (T a[],int N) {for (int i=0;i<max_size-1;i++) {    Sarray[i].next = i+1;    } sarray[max_size-1].next =-1;    for (int i=0;i<n;i++) {sarray[i].data = A[i];     } front = 0;  The head pointer points to the first starting element tail = Sarray[n-1].next;   An element that points to the first unallocated space sarray[n-1].next =-1;    Set the next field of the last element to -1}template <class t>void staticlinklist<t>::P rintstaticlinklist () {int k;    if (front!=-1)//front is not a-1 list non-null assigns the current head pointer to K k = front;        while (sarray[k].next!=-1) {cout<<sarray[k].data<< "";    K = Sarray[k].next;  } cout<<sarray[k].data; Prints the next field equal to 1 element as the last element Cout<<endl;}       Template <class t>void staticlinklist<t>::insert (int i,t x) {int k = front;       for (int j=1;j<i-1;j++) {k = Sarray[k].next;  } int p = NewNode ();       The application of a space is essentially an array of the first free space is taken out sarray[p].data = x; Sarray[p].next = Sarray[k].next Sarray[k].next = P;} /** application Space The method returns the first subscript for the element that allocates space */template <class t>int Staticlinklist<t>::newnode () {if (tail==-1) throw "null    Inadequate ";    int pos = tail;    tail = Sarray[tail].next; return POS;}    Template <class t>int staticlinklist<t>::getlength () {int count = 1;//counter int p;    Linked list non-null if (front!=-1) p = front;        while (sarray[p].next!=-1) {count++;    p = sarray[p].next; } return count;} /** gets the subscript idea in the array where the element of the specified position is located: regardless of where the element is in the array, we simply traverse to get to the previous position element then its next field points to the array subscript of the successor element */template <class T>int    Staticlinklist<t>::get (int i) {int p = front;    for (int j=0;j<i-1;j++) {p = sarray[p].next; } return p;} The space occupied by the/** release node is actually the release of the delete node into the idle list and does not use the array itself to do things or move the cursor */template <class t>void staticlinklist<t>: :D eletenode (int i) {if (i<0| | i>max_size-1| |    front==-1) throw "free space error";    Sarray[i].next = tail; tail = i;} /** Deleting a node element here is to move the cursor so that the previous node points to the next node to be deleted.Node */template <class t>t staticlinklist<t>::D elete (int i) {int p = front; for (int j=1;j<i-1;j++) {p = Sarray[p].next;//The previous node of the current node (array subscript)} int k = Sarray[p].next;    The array subscript T x = Sarray[k].data of the current node to be deleted;    Sarray[p].next = Sarray[k].next;  Deletenode (k); The release node parameter is the subscript of the array where the element is to be deleted return x;}    int main () {int a[6] = {1,5,6,8,3,0};    Staticlinklist<int> slinklist (a,6);    cout<< "Linked list length:" <<endl;    Cout<<slinklist.getlength () <<endl;    cout<< "Traversing static linked list" <<endl;    Slinklist.printstaticlinklist ();    cout<< "inserting element 4 at position 3" <<endl;    Slinklist.insert (3,4);    Slinklist.printstaticlinklist ();    cout<< "Gets the subscript of the array where the element of position 3 is located:" <<endl;    Cout<<slinklist.get (3) <<endl;    cout<< "Remove elements from position 4" <<endl;    Slinklist.delete (4);    Slinklist.printstaticlinklist ();    cout<< "inserting element 9 at position 4" <<endl;    Slinklist.insert (4,9);    Slinklist.printstaticlinklist (); return 0;} </span>


Data structure and algorithm--linear chain-type storage structure (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.