Data structure and algorithm--linear table-type storage (single-loop linked list)

Source: Internet
Author: User

Summary of today's circular single linked list

What is a single-loop linked list?

The pointer field of a single-linked list terminal node is pointed to null, and if it points to the head node, so that a ring is formed, then the end-to loop is sufficient to form a single-loop linked list.

We are all represented by a head pointer in a single-linked list, but in a single-loop link, with the tail pointer (pointing to the last node). Why this, because if the first pointer, then when the last element, then the loop traversal, time complexity of O (n), if the tail pointer, the time complexity of O (1), and because it is a loop, so the head pointer is easy to represent the Rear->next, time complexity is O (1)

There are two points to note in a single-loop list:

    • The head pointer uses the rear->next representation of the
    • Determine if a node is a tail node condition:rear->next==rear instead of p->next==null in a single-linked list

Icon (Baidu image interception):

Note: While writing the link list, the pointer is sometimes wrapped around, but I find that it is important to remember that the pointer holds the address, and this address usually represents a node. For example, Rear->next represents the head pointer, and the head pointer is stored in the address is the head node, that is, pointing to the head node, in fact, represents the head node. Pointer This piece needs to understand well, try to figure out, I think rather slowly, want to understand, also faster, confused effect better.

Specific code implementation:

<span style= "Font-family:courier new;font-size:14px;"    > #include <iostream>using namespace std;template <class t>struct node{T data; struct node<t> *next;        Pointer field};template <class t>class clinklist {public://Initialize empty circular single-linked list Clinklist () {rear = new node<t>;    Rear->next = rear;  } clinklist (T a[],int N);     Initializes a list of ~clinklist ();  destructor int getlength ();  Gets the length of the single-linked list T get (int i);  Gets the element int Locate (T x) on the first position of the linear table; Finds an element in a linear table whose value is x returns its position void Insert (int i,t x); Insert element x to position I on T Delete (int i); Deletes the element on position I and returns the deleted element to void Printlinklist ();   Looping through the various elements of the circular single-linked list private:node<t> *rear;     The tail-pointer};/**-tail interpolation method initializes the circular single-link list with array elements: 1. Create a new node, and the tail pointer points to this head node 2. Assigning the header address to the head node pointer field constitutes an empty single-loop list of 3.for loops beginning the insertion node. 3.1 Create a new node, assign the array element to the data field of the node 3.2 to make the pointer field of the new node point to the head node's pointer field to form a loop 3.3 to point the current node to the new node 3.4 tail pointer move backward one */template <class t>clinkl    Ist<t>::clinklist (T a[],int n) {rear = new node<t>; Rear->next = Rear;        Use the tail interpolation method for (int i=0;i<n;i++) {node<t> *s= new node<t>;        S->data = A[i];        S->next = Rear->next;        Rear->next = s; Rear = rear->next; The tail pointer moves backwards}}template <class t>int clinklist<t>::getlength () {int count = 0;//counter node<t> *p =    rear->next;        while (p!=rear) {count++;    p = p->next; } return count;} The/** destructor is used to release the node */template <class t>clinklist<t>::~clinklist () {node<t> *p = rear->next;//Initial Chemical  Make pointer node<t> *q;        Temporary node while (p!=rear) {q = p;        p = p->next;    Delete q; }}template <class t>t clinklist<t>::get (int i) {if (I>getlength () | | | i<1) return-1;    Determine if the location of the lookup is reasonable node<t> *p = rear->next;     for (int j=0;j<i;j++) {p = p->next; Get the node of position i} return p->data;} Template <class t>int clinklist<t>::locate (T x) {node<t> *p = rear-&Gt;next;    int j=0;        while (p!=rear) {if (p->data==x) return J;        j + +;    p = p->next; } return-1;}    Template<class t>void clinklist<t>::insert (int i,t x) {node<t> *p = rear->next;    Gets the previous node for (int j=0;j<i-1;j++) {p = p->next;    } node<t> *s = new node<t>;    S->data = x;    S->next = p->next; P->next = s;}  Template <class t>void clinklist<t>::P rintlinklist () {node<t> *p = rear->next; Here is the head node for (int i=0;i<getlength (); i++) {cout<<p->next->data<< "";    data field p = p->next; of the next node } Cout<<endl;}    Template<class t>t clinklist<t>::D elete (int i) {node<t> *p = rear->next;    for (int j=0;j<i-1;j++) {p = p->next; } node<t> *q = p->next;    node to delete P->next = q->next;    T x = q->data;    Delete q; return x;}    int main () {int a[6] = {2,4,6,1,7,9}; ClInklist<int> list (a,6);    cout<< "traversing elements of a single linked list" <<endl; List.    Printlinklist ();    cout<< "Circular single-linked list length:" <<endl; Cout<<list.    GetLength () <<endl;    cout<< "Get the element of position 2:" <<endl; Cout<<list.    Get (2) <<endl;    cout<< "Get the location of element 6" <<endl; Cout<<list.    Locate (2) <<endl;    cout<< "Insert element 8 to position 3" <<endl; List.    Insert (3,8); List.    Printlinklist ();    cout<< "pin out elements on position 4" <<endl; List.    Delete (4); List.    Printlinklist (); return 0;} </span>
Results:


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