Data structure of C + + implementation of the linear table of the chain storage structure and single linked list inversion

Source: Internet
Author: User
Tags bool rand

In order to represent the logical relationship between each data element AI and its immediate successor element Ai+1, the data AI, in addition to storing its own information, also needs to store a message indicating its immediate successor (i.e., the immediate successor storage location). These two pieces of information form the storage image of the data element AI, known as node (node). n node chains form a linked list, which is a chain-type storage structure for the linear table (A1,a2,..., an), because each node of the list contains only one pointer field, so it is called a single linked list.

We call the storage location of the first node in the list as the head pointer, and in order to make it easier to manipulate the linked list, such as deleting the first node of the special case (the first node has no precursor, and to remove a node need to first find its precursor to do the removal operation), often in a single linked list of the first node before a node, Called the head node, so the head pointer points to the head node.

The advantages and disadvantages of a single linked list and an array are basically the opposite, that is, single linked list inserts delete efficiency, while the search needs to traverse, the efficiency is low.

Sample program: (Adapted from the "Big Talk data Structure", added a list inversion, etc.)

#include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
    
typedef int ELEMTYPE;
    typedef struct NODE {elemtype data;
struct Node *next;
    
Node;
    
typedef Node *NODEPTR; BOOL Initlist (nodeptr *npp) {*npp = (nodeptr) malloc (sizeof (Node));/* Generate header node and cause *NPP to point to this head node */if (!) ( *NPP)) return false;
Allocation failure (*NPP)->next = null;/* pointer field is NULL/return true;
    BOOL Listempty (Nodeptr Np) {if (Np->next = NULL) return true;
else return false;
    BOOL Clearlist (nodeptr *npp) {cout << "clear List ..." << Endl;
    Nodeptr p = *npp; if (!) (
    P->next)) return true;
        while (p->next)/* Not to the end of the table * * Nodeptr q = p->next;
        P->next = q->next;
    Free (q);
return true;
    int Listlength (nodeptr Np) {cout << "List ' s length:";
    
  Nodeptr p = np->next;/* p points to the first node/int i = 0;  while (p) {p = p->next;
    ++i;
return i; }/* Operation result: Returns the value of the POS data element in Np with PTR * * bool Getelem (nodeptr Np, int pos, elemtype *ptr) {cout << "get Item from Pos
    "<< Pos <<": ";
    Nodeptr p = np->next;
    int i = 1;
        /* p is not empty or the counter I is not equal to POS, the loop continues */while (P && i < pos) {p = p->next;
    ++i;
    } if (!p) return false;
*ptr = p->data;/* to fetch the data of the POS element/return true; }/* Returns the sequence of the 1th data element in NP that satisfies the relationship with Elem. * * * If such data elements do not exist, the return value is 0/int Locateelem (nodeptr Np, Elemtype Elem) {cout << "Item" << Elem << "
    ' s pos: ';
    Nodeptr p = np->next;
    
    int i = 1;
        while (P && p->data!= Elem) {p = p->next;
    ++i;
    } if (!p) return 0;
return i; /* Operation result: Insert new data element before POS position in NP ELEM,NP length plus 1/bool Listinsert (nodeptr *npp, int pos, elemtype Elem) {cout << " Insert List Pos "<<POS << "Item" << Elem << Endl;
    Nodeptr p = *npp;
    
    int i = 1;
        while (P && i < pos) {p = p->next;
    ++i;
    
    } if (!p) return false;
    Nodeptr in = (nodeptr) malloc (sizeof (Node));
    In->data = Elem;  In->next = p->next;/* Assigns a subsequent node of P to the successor/P->next = in;
/* will be assigned to P's successor/return true; /* Delete Npp pos data element and return its value with PTR, Npp length minus 1/bool Listdelete (nodeptr *npp, int pos, elemtype *ptr) {cout << "Dele
    Te List Item in pos "<< pos << Endl;
    Nodeptr p = *npp;
    
    int i = 1;
        while (P && i < pos) {p = p->next;
    ++i;
    
    } if (!p) return false;
    Nodeptr q = p->next;
    *ptr = q->data;
    
    P->next = q->next;/* assigns the successor of Q to P's successor/free (q);
return true;
    BOOL Listtraverse (nodeptr Np) {cout << "List ' s Items:";
    Nodeptr p = np->next; while (p) {cout << p->data << ';
    p = p->next;
    } cout << Endl;
return true;  }//* Randomly produces the value of n elements, establish a single chain linear table with header node Npp (head interpolation)/void Createlisthead (nodeptr *npp, int num) {cout << "create List from
    Head ... "<< Endl;
    if (*npp!= NULL) free (*NPP);
    *NPP = (nodeptr) malloc (sizeof (Node));
    
    (*NPP)->next = null;/* First set up a single linked list of the leading node/Srand (Time (NULL));
        for (int i = 0; i < num i++) {nodeptr p = (nodeptr) malloc (sizeof (Node)); P->data = rand ()% 100 + 1;
        Random number P->next = (*NPP)->next; (*NPP)->next = p;/* inserted into the header * */}}//* Randomly generating the value of n elements, establishing a single chain linear table with a header node Npp (trailing interpolation)/void Createlisttail (Nodeptr *npp, in
    T num) {cout << "Create List from Tail ..." << Endl;
    if (*npp!= NULL) free (*NPP);
    *NPP = (nodeptr) malloc (sizeof (Node));
    (*NPP)->next = NULL;
    
    Srand (Time (NULL)); Nodeptr tail = *NPP;
    /* Tail to point to the tail of the node * *for (int i = 0; i < num i++) {nodeptr p = (nodeptr) malloc (sizeof (Node));
        P->data = rand ()% 100 + 1;  Tail->next = p; * * to point the end of the terminal node pointer to the new node * * * tail = p;
/* The current new node is defined as the footer terminal node/} Tail->next = NULL;
    /* Reverse single linked list/Nodeptr reverselist (nodeptr head) {cout << "Reverse List ..." << Endl; if (NULL = = Head->next | |
    NULL = = Head->next->next) return head;
    Nodeptr p;
    NODEPTR Q;
    Nodeptr R;
    p = head->next;
    Q = p->next;
    Head->next->next = NULL;
        while (q) {r = q->next;//q->next = p; p = q; Q = r;
    } head->next = P;
return head;
    int main (void) {nodeptr Np;//Head pointer initlist (&NP);
    
    for (int i = 1; i < 5; i++) Listinsert (&NP, I, I); if (!
    Listempty (NP)) cout << Listlength (NP) << Endl;
    
    Listtraverse (NP); cout << Locateelem (Np, 3) << Endl;
    int get;
    Getelem (Np, 2, &get);
    
    cout << get << Endl;
    Clearlist (&NP);
    
    cout << listlength (Np) << Endl;
    Createlisthead (&NP, 10);
    Listtraverse (NP);
    int result;
    Listdelete (&NP, 5, &result);
    
    Listtraverse (NP);
    Clearlist (&NP);
    Createlisttail (&NP, 10);
    Listtraverse (NP);
    
    Listtraverse (Reverselist (Np));
    
    Clearlist (&NP);
return 0; }

Related Article

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.