Data structure (4) -- chained representation and implementation of linear tables

Source: Internet
Author: User
2.3.1 Linear Linked List

The chain storage structure of the Linear Linked List is specific to the use of a groupAnyA storage unit stores the data elements of a linear table (this set of data storage units can be continuous or discontinuous ).

Nodes include data domains and pointer domains.
A linear linked list that contains only one pointer field is also called a single-chain table.

The main operation code for a single-chain table is as follows:

# Include <iostream> # include <string> using namespace STD; // defines the element type typedef int elemtype; // defines the storage structure typedef struct lnode {elemtype data of a single-chain table of a linear table; struct lnode * Next;} lnode, * linklist; // function: Get the value on node I // input parameter: l single-chain table of the leading node // input parameter: I position I // input parameter: e value at position I // return: error not found, OK find string getelembyindex (linklist L, int I, elemtype & E) {// L is the single-chain table with the lead node. // when the I-th element exists, the value is assigned to E and OK is returned. Otherwise, error linklist P = L-> next is returned; int J = 1; while (P & J <I) {P = p-> next; j ++;} If (! P | j> I) {return "error";} e = p-> data; Return "OK";} // function: insert the value e before position I // input parameter: l single-chain table // input parameter: I position I // input parameter: e. The value at position I to be inserted. // return: OK, insert succeeded. Error: insertion failed. String insertlinklist (linklist & L, int I, elemtype E) {// linklist P = L-> next; Int J = 1; // find the I-1 node while (P & J <I-1) {P = p-> next; j ++;} If (! P | j> I-1) {return "error";} linklist S = (linklist) malloc (sizeof (lnode); s-> DATA = E; s-> next = p-> next; P-> next = s; Return "OK";} // function: delete the value at position I of the single-link table of the lead node // input parameter: I node I // return: error: deletion failed. OK: String dellinklist (linklist & L, int I) {linklist P = L-> next; Int J = 1; while (P & J <I-1) {P = p-> next; j ++;} If (! P | j> I-1) {return "error";} linklist q = p-> next; P-> next = Q-> next; free (Q ); return "OK";} // function: create a single-chain table // input parameter: linklist & L single-chain table // input parameter: length of the int n single-chain table // return value: void createlinklist (linklist & L, int N) {// create a header node L = (linklist) malloc (sizeof (lnode); L-> next = NULL; linklist P; int I; for (I = 0; I <= N; I ++) {P = (linklist) malloc (sizeof (lnode); P-> DATA = I; p-> next = L-> next; L-> next = P ;}// function: traverse the output single-chain table // input parameter: single-chain table of the lead node // return value: void display (linklist L) {linklist P = L-> next; cout <"the result of traversing a single-chain table is as follows:" <Endl; while (p) {cout <p-> data <"; P = p-> next;} cout <Endl ;}// main function void main () {// The linklist l single-chain table of the leading node; // create a single-chain table createlinklist (L, 10) with 10 nodes; // traverse the output single-chain table display (L ); // obtain the element elemtype e at position I; string status = getelembyindex (L, 12, E); cout <status <", "<e <Endl; // Insert the value E string insertstatus = insertlinklist (L, 4, 90) before position I; cout <insertstatus <Endl; display (l); // Delete the node string delstatus = dellinklist (L, 5); cout <delstatus <Endl; display (l );}

 

Data structure (4) -- chained representation and implementation of linear tables

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.