Implementation and operation of "linear table" sequential storage, chained storage

Source: Internet
Author: User
Tags abstract
first, the definition of linear table:
(1) Concept definition: A finite sequence of data elements is called a linear table; the type of a data element in a linear table can be a simple type, or it can be a complex type. Many of the basic operations involved in practical application issues are very similar and should not be programmed separately for each specific application. Abstract the logical structure and basic operation (abstract data type) of generality from concrete application, then implement its storage structure and basic operation.

(2) type definition: First, the structure of the ADT List table is abstracted, and it is generally implemented by struct in C + +. The basic operations are as follows: Initlist (&L): Creating a new table, Destroylist (&l): Destroying a linear table, Clearlist (&l): Emptying the table, Listempty (L): Judging if the table is empty ; Listlength (L): The length of the table; Getelem (l,i,&e): reads the elements of the table; Locateelem (l,e): Finds the elements of the table; Priorelem (L,cur_ e,&pre _e): The precursor of the table ; Nextelem (L,cur_ e,&next_e): The successor of the table, Listinsert (&l,i,e): The front table, Listdelete (&l,i,&e): delete the table; Traverselist (L ): Traverse table;
the sequential representation and realization of linear table
(1) Concept: Sequential representations of linear tables are also known as sequential storage structures or sequential images. Sequential storage definition: A storage structure that stores logically adjacent data elements in a physically contiguous storage unit. Logically adjacent, physically also adjacent; the elements of a linear table are stored sequentially with a contiguous set of storage units, which can be implemented by array V[n].

(2) Realization: The following is the approximate implementation of the specific code;
# include <iostream> using namespace std;
# define OK 1 # define ERROR 0 # define OVERFLOW-2 # define MAXSIZE typedef int ELEMTYPE;

typedef int STATUS;
    typedef struct{ELEMTYPE *elem;
int length;

}sqlist;
    Status initlist_sq (sqlist &l) {//Create an empty order table L L.elem = new Elemtype[maxsize]; if (!
    L.elem) exit (OVERFLOW);
    l.length = 0;
return OK; } void Destorylist_sq (SqList &l) {//Destroy linear table L if (l.elem) delete[]l.elem;//free storage Space} void Clearlist_sq (sqlist

&AMP;L) {//Empty linear table L l.length = 0;}

int getlength_sq (sqlist &l) {//To find the length of the linear table L return (l.length);}
    int isempty_sq (sqlist L) {//Judgment linear table is empty if (L.length = = 0) return 1;
else return 0;
        } Status getelem_sq (sqlist l,int i,elemtype &e) {//Get element if (i<1 | | i>l.length)//Determine if I value is reasonable, if unreasonable, return error  
    return ERROR;       e = l.elem[i-1];
The unit of section I-1 stores the first Data return OK; } int locateelem_sq (SqList l,elemtype e) {//Find element for (int i=0; i<l.length; i++) {if (l.elem[i] = = e) return i+1;
The found element is actually in the first i+1 position} return 0;     } Status insertelem_sq (sqlist &l,int i,elemtype e) {if (i<1 | | i>l.length+1) return ERROR;     The I value of the insertion position is not valid if (L.length = = MAXSIZE) return ERROR;    The current storage space is full for (int j=l.length-1; j>=i-1; j--) {l.elem[j+1] = l.elem[j];            Insert position and subsequent element move} L.elem[i-1] = e;                 The new element e is placed in the first position ++l.length;
Increase the table length by 1 return OK;              } Status deleteelem_sq (sqlist &l,int i) {if (i<1 | | i>l.length) return ERROR;   Delete location illegal for (int j=i;j<=l.length-1;j++) {l.elem[j-1] = l.elem[j];                    The element that is deleted after the element is moved forward}--l.length;
Table length reduced by 1 return OK;
    } void Print (SqList L) {for (int i=0;i<l.length;i++) {cout<<l.elem[i]<<endl;
    }} int main () {sqlist lst;
    INITLIST_SQ (LST);
    int i,n; 
    Elemtype e; cout<< "Please enter the length of the sequential table:" <<endl;
    cin>>n;
        for (i=1;i<=n;i++) {cout<< "Please enter the section" <<i<< "Elements:" <<endl;
        cin>>e;
    INSERTELEM_SQ (lst,i,e);
    } print (LST);
    cout<< "Would you like to delete the first few elements:" <<endl;
    cin>>i;
    DELETEELEM_SQ (Lst,i);
    Print (LST); cout<< "To insert elements in the first place.
    "<<endl;
    cin>>i; cout<< "The element that needs to be inserted is.
    "<<endl;
    cin>>e;
    INSERTELEM_SQ (lst,i,e);
    Print (LST); cout<< "I need to take the first few elements.
    "<<endl;
    cin>>i;
    GETELEM_SQ (lst,i,e);
    cout<< "First" <<i<< "element is" <<e<<endl<<endl; cout<< "which element to query.
    "<<endl;
    cin>>e;
    n = locateelem_sq (lst,e);
    if (n!=0) cout<< "successfully found element in" <<n<< "Location" <<endl;
    else printf ("The element was not found \ n"); cout<< "Now empties the linear table.
    "<<endl;
    CLEARLIST_SQ (LST);
    printf ("Linear table Length:%d\n", GETLENGTH_SQ (LST));
return 0;
 }
the chain expression and realization of linear table
(1) Concept: Chain storage structure, node in the memory position is arbitrary, that is logically adjacent data elements are not physically adjacent, and the above mentioned in the order of storage, the chain stored in the computer memory of the representation is random, it is only in the logic is adjacent, the physical is not adjacent The linear table is also referred to as a non-sequential image or chain image, and the linked list is implemented by pointers;

(2) linked lists are related terms: Linked lists are implemented by connecting each node together, so each node consists of two domains. data field: storing element numeric data; Second, pointer field: stores the location of direct successor nodes, so a table consisting of many nodes is called a linked list. The list also has the head pointer, the head node, the first element node. The head pointer is a pointer to the first node in the list, and the first node is the node in the list that stores the first data element A1, and the Head node is a node that is attached before the first node of the list, and the data field is only emptied of information such as the table flag and the length of the table.

(3) The classification of the linked list: nodes have only one pointer-domain linked list, called a single-linked list or a linear list, there are two pointer-field linked lists, called doubly linked lists, and end-to-end linked lists are called circular lists;
Iv. definition and implementation of single linked list # #
(1) Concept: Single linked list is determined by the table header, so the single linked list can be named by the name of the head pointer, if the head pointer name is L, the linked list is called Table L;

(2) The following code describes the basic operation and implementation of a single-linked list:
# include <stdio.h> # include <stdlib.h> # include <iostream> using namespace std;
# define OK 1 # define ERROR 0 typedef int ELEMTYPE;

typedef int STATUS;      typedef struct lnode{elemtype data; Data domain struct Lnode *next;       Pointer field}lnode,*linklist;
    *linklist for Lnode type pointer Status initlist_l (linklist &l) {//Constructs an empty table L = new Lnode;
    L->next = NULL;
return OK;
    } Status destroylist_l (linklist &l) {//Destroy table linklist p;  
        while (L) {p=l;
        l=l->next;  
    Delete p;
} return OK;
    } Status clearlist_l (linklist &l) {//empty table linklist p,q;   p=l->next; 
        P points to the first node while (p)//not to the end of the table {q=p->next;     
        Delete p;   
     p=q;   } l->next=null;
The head node pointer field is empty return OK;
    } int listlength_l (linklist l) {//returns the number of data elements in L linklist p;  p=l->next;        P points to the first node int i=0;
        Set the Count variable i while (p) {//Traverse single-linked list, statistic node-count i++; P=p->Next
} return i;
    } int isempty_l (linklist L) {//Determines whether the table is empty, if return 1, non-null returns 0 if (L->next) return 0;
 else return 1;
    } Status getelem_l (linklist l,int i,elemtype &e) {//Gets the contents of the I data element in the linear table L linklist p;
    p=l->next;       int j=1; 
        Initialize while (p&&j<i) {//backwards scan until p points to element I or P is empty p=p->next; 
    ++j;  } if (!p | | j>i) return ERROR;         The first element does not exist e=p->data; 
Take the first element I return OK;
    } int locateelem_l (linklist &l,elemtype e) {//returns the position ordinal of the data element with the value E in L, the lookup fails to return 0 linklist p; 
    p=l->next;
    int j=1;
            while (P! = NULL) {if (P->data! = e) {++j;
        p=p->next;
    } else break;
} return j--;
    } Status listinsert_l (linklist &l,int i,elemtype e) {//insert e element linklist p,s in L-table;
    P=l;
    int j=0;   
        while (P && j<i-1) {//Find i-1 node P = p->next;
    ++j; } if (!p | | j>i-1) return ERROR;     S=new Lnode;       Generate new node S s->data=e; Place the data field of node S to e s->next=p->next; 
    Insert the junction s into the L p->next=s;. 
return OK;
    } Status listdelete_l (linklist &l,int i,elemtype &e) {////Removes the data element I in the linear table L from linklist p,q;
    P=l; 
    int j=0; 
        while (P->next && j<i-1) {//search for the first node and point P to its predecessor p=p->next; 
    ++j; } if (! ( P->next) | |       J&GT;I-1) return ERROR;              Delete location unreasonable q=p->next;        Temporarily save the address of the deleted node for release p->next=q->next;              Changing the pointer domain of the node's predecessor node e=q->data;               Save the deleted node data field delete Q; 
Release the space to delete the node return OK;
    } Status Print (Linklist L) {linklist p;
    p = l->next;
        while (P! = NULL) {cout<<p->data<<endl;
    p = p->next;
} return OK;
    } int main () {int i,n;
    Elemtype e;
    Linklist L;
    initlist_l (L);
    cout<< "Please enter the length of the list:" <<endl;
    cin>>n; for (i=1;
        i<=n;i++) {cout<< "Please enter the" <<i<< "elements:" <<endl;
        cin>>e;
    listinsert_l (l,i,e);
    } n = listlength_l (L);
    cout<< "Insert complete, the length of the table is:" <<n<<endl;
    printf ("\ n");
    if (isempty_l (L) = = 0) cout<< "table is not empty, the content is:" <<endl;
    Print (L);
    cout<< "Please enter the element you are looking for:" <<endl;
    cin>>e;
    n = locateelem_l (l,e);
    if (n! = 0) cout<< "Find succeeded, element in section" <<n<< "Locations" <<endl;
    Else cout<< "Find failed, Element not found" <<endl;
    printf ("\ n");
    cout<< "Would you like to delete the first few elements:" <<endl;
    cin>>i;
    Listdelete_l (l,i,e); cout<< "element deletion succeeded.
    "<<endl;
    Print (L);
    printf ("\ n"); cout<< "Where would you like to get the element?"
    "<<endl;
    cin>>i;
    getelem_l (l,i,e);
    cout<< "Get successful, the element of the" <<i<< "position is:" <<e<<endl; cout<< "Operation ends and now empties the list.
    "<<endl;
    clearlist_l (L);
    n = listlength_l (L); cout<< "emptied, the list length is:" &Lt;<n<<endl;

    destroylist_l (L);
        /* Lookup: Because the linear list can only be accessed sequentially, that is, when looking for the pointer from the beginning, the time to find the complexity of O (n).
        Insert, Delete: Because the linear list does not need to move elements, as long as the pointer is modified, generally the time complexity is O (1).
    However, if you want to make a pre-insert or delete operation in a single-linked list, the time complexity is O (n) because you want to find the precursor node from scratch.
*/return 0; }

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.