Linear table is a linked list, the basic feature is that in addition to the first element without a direct precursor, the last element has no direct successor, the other data elements have a precursor and successor. is the most basic and most commonly used linear structure.

2.1 Definition and characteristics of linear tables

by N (n>=0) data characteristics of the same element no city's finite sequence becomes a linear table, n is a linear table length, when n=0 is called an empty table.

Example: 1.26 alphabet is a linear table, the data element is a single letter.

2. Student Information table, each student is a data element, including the number, name, gender and other data items.

2.2 Case: library information Management system.

2.3 Type definitions for linear tables

Linear table is a very flexible data structure, its length can be increased or shortened as needed, that is, the data elements of the linear table can not only insert and delete operations.

Abstract data types for linear tables include: 1. Initialize 2. Value of 3. Find 4. Insert 5. Delete

2.4 Sequential representation and implementation of linear tables

Sequential storage representations of 2.4.1 linear tables

The sequential representation of a linear table is a data element that stores linear tables once with a contiguous set of storage units.

The linear table of this storage structure is the sequential table. Its characteristic is that the logical incense Ling's data elements, its physical order is also adjacent.

Its relationship can be used to represent

A linear table is a storage structure that is stored randomly, and is usually represented in a high-level language by an array of dynamically allocated one-dimensional arrays in the C language.

The type definition of the sequential table:

#define MAXSIZE //Maximum length typedef struct { elemtype *elem; The base address int length that points to the data element ; The current length of the linear table <span style= "font-family:arial, Helvetica, Sans-serif;" >}sqlist;</span><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " Implementation of basic operations in the >2.3.2 sequence table </span>

Example: basic operations for sequential tables

#include <iostream> #include <fstream>//file#include<string> #include <iomanip>//io Manipulator using namespace std; #define OK 1#define ERROR 0#define overflow-2typedef int statues;typedef int elemtype; #de Fine MAXSIZE 100struct Book {string id; Isbnstring name;double Price;}; typedef struct {book *elem; Base addressint length;} Sqlist;//initstatues initlist_sq (sqlist &l) {L.elem = new book[maxsize];//application memory space★if (! L.elem) exit (OVERFLOW); l.length = 0;} Getstatues Getelem (sqlist L, int i, book &e) {if (I < 1 | | i > l.length) return overflow;e = L.elem[i-1];retu RN OK;} Locatestatues locateelem_sq (SqList L, double e) {for (int i = 0; i < l.length; i++) if (L.elem[i].price = = e) return I + 1;return 0;} Insertstatues listinsert_sq (sqlist &l, int i, book e) {if ((I < 1) | | (i > L.length+1)) Return Error;if (l.length = = MAXSIZE) return error;for (int j = l.length-1; J >= i-1; j--) L.elem[j + 1] = L.elem[j]; L.eleM[i-1] = E;++l.length;return OK;} Deletestatues listdelete_sq (sqlist &l, int i) {if (i<1) | | (I>l.length)) return error;for (int j = i; J <= L.length; j + +) l.elem[j-1] = L.elem[j];--l.length;return OK;} int main () {sqlist L;int i = 0, temp, a, C, choose;double Price; Book E;string head_1, Head_2, Head_3;cout << "1. Create \ n"; cout << "2. Enter \ n"; cout << "3. Value \ n"; cout << "4 Find \ n "; cout <<" 5. Insert \ n "; cout <<" 6. Delete \ n "; cout <<" 7. Output \ n "; cout <<" 8. Write \ n "; cout <<" 0. Exit \ n "; choose = -1;while (Choose! = 0) {cout <<" please select: "; Cin >> Choose;switch (choose) {Case 1:if (INITLIST_SQ (L)) cout << "Create the list successful!\n\n"; Elsecout << "Create the list failed.\n\n"; Break;case 2: {//scanf//involves File operation brackets cannot save i = 0; L.elem = new Book[maxsize];if (! L.elem) exit (OVERFLOW); L.length = 0;fstream File;file.open ("Book.txt"), if (!file) {cout << "can not found the file." << Endl;exit (ERR OR);} File >> head_1 >> HeAd_2 >> Head_3;while (!file.eof ()) {file >> l.elem[i].id >> l.elem[i].name >> l.elem[i].price;i+ +;} cout << "scanf book.txt done." << Endl; L.length = I;file.close ();} Break;case 3:cout << "Please enter the pos\n"; c >> i;temp = Getelem (L, I, E); if (temp! = 0) {cout << "fin D successful! "<< endl;cout <<" The information of book "<< I << ' is:\n ';/*cout << left <& Lt SETW (left--align setw--word count*/cout << left << setw (+) << e.id << "\ t" << left < < SETW << e.name << "\ t" << left << SETW (5) << e.price << Endl << Endl;} Elsecout << "Find failed." << endl;break;case 4://query Price//weakness:query The first book of this price. cout << "Please enter the price." << endl;cin >> price;temp = locateelem_sq (L, price); if (temp! = 0) {Co UT << "Find successful!" << endl;cout << "the NAMe is "<< l.elem[temp-1].name << Endl; } elsecout << "Find failed!" << endl;break;case 5://insertcout << "Please enter the Insertpos ID name PR Ice of the book "<< endl;cin >> a;cin >> e.id >> e.name >> e.price;if (listinsert_sq (L, A, E )) cout << "Find successful!" <<endl;elsecout << "Find failed!" << endl;break;case 6://delete a bookcout << "Please enter the P Os "<< endl;cin >> c;if (listdelete_sq (L, c)) cout <<" Delete successful! "<< endl;elsecout << ; "Delete failed!" << endl;break;case 7://travelcout << "There is the information:\n"; for (i = 0; i < L.len gth;i++) cout << left << setw (All) << l.elem[i].id << "\ t" << left<< SETW (+) << L . Elem[i].name << "T" << left<< SETW (5) << l.elem[i].price << endl;cout << endl;case 8 : {//enter to the Filefstream file;file.open ("Book1.txt"if (!file) {cout << "can not found the file." << endl;exit (ERROR);} for (i = 0; i < l.length; i++) file << left << setw (All) << l.elem[i].id << "\ t" << left< ;< setw << l.elem[i].name << "\ t" << left<< SETW (5) << l.elem[i].price << Endl; cout << "Write done." << Endl; File.close ();} Default:break;}} return 0;}

2.5 chain representation and implementation of linear tables

2.5.1 definition and representation of a single linked list

For data element Ai, in addition to storing information of its own, it is necessary to store a message indicating its direct successor. These two pieces of information make up the storage image of the data element AI, called the node.

It consists of two domains, where the domain where data element information is stored is called the data domain, and the domain where the direct successor storage is stored is called the pointer field.

#include <iostream> #include <fstream>//file#include<string> #include <iomanip>//io Manipulator using namespace std; #define OK 1#define ERROR 0#define overflow-2typedef int statues;typedef int ELEMTYPE;STR UCT book {string id; Isbnstring name;double Price;}; typedef struct LNODE {book data;struct lnode *next;} Lnode, *linklist;string head_1, head_2, Head_3;int length;//initstatues initlist_l (linklist &L) {L = new LNode; L->next = Null;return OK;} Get the no.i elementstatues getelem_l (linklist L, int i, book &e) {int J; linklist p;p = L->next;j = 1;while (J < i && p) {p = p->next;++j;} if (!p | | j>i && p) {return ERROR;} e = P->data;return OK;} Locate the Pricelnode *locateelem_l (linklist L, int e) {linklist p;p = L->next;while (P && p->data.price! = e) p = P->next;return p;} Statues listinsert_l (linklist &l, int i, book &e) {int J; Linklist p, s;p = L;j = 0;while (P && J < i-1) {p = P->next;++j;} if (!p | | J > i-1) return error;s = new Lnode;s->data = E;s->next = P->next;p->next = S;++length;return OK ;} Statues listdelete_l (linklist &l, int i) {linklist p, q;int j;p = L;j = 0;while ((p->next) && (j<i-1)) {p = p->next;++j;} if (!p | | J > i-1) return error;q = P->next;p->next = Q->next;delete Q;--length;return OK;} Pre-inserted void Createlist_h (linklist &l, int n) {linklist p; L = new Lnode; L->next = Null;length = 0;fstream File;file.open ("Book.txt"), if (!file) {cout << "No related file found, cannot open!" "<< Endl;exit (ERROR);} File >> head_1 >> head_2 >> head_3;while (!file.eof ()) {p = new Lnode;file >> p->data.id >&G T P->data.name >> P->data.price;p->next = l->next; L->next = p;length++;} File.close ();} void Createlist_r (linklist &l, int n) {///Algorithm 2.12 Post-interpolation creates a single-link list//positive-order input N-element value, establishes a single-linked table with a header node L linklist p, R; L = new Lnode; L->next = NULL; First set up a leading node of the empty list r = L; End Pointer R points to head node length = 0;fstream file; Open the file for read-write Operation File.Open ("Book.txt"); if (!file) {cout << "No related file found, cannot open! "<< Endl;exit (ERROR);} File >> head_1 >> head_2 >> head_3;while (!file.eof ()) {//To insert the information in the files into the linked list using the post-interpolation method p = new lnode;//to generate a node file & gt;> p->data.id >> p->data.name >> p->data.price;//INPUT element value assigned to new node *p data field P->next = null;r-> Next = p;//The new node *p into the tail node *r after r = p;//r points to the new tail node *plength++; At the same time, the list length is statistically}file.close ();} Createlist_lint Main () {int A, n, choose;double Price; Book E; Linklist L, p;initlist_l (l); cout << "1. Build \ n "; cout <<" 2. Enter \ n "; cout <<" 3. Value \ n "; cout <<" 4. Find \ n "; cout <<" 5. Insert \ n "; cout <<" 6. Delete \ n "; cout <<" 7. Output \ n "; cout <<" 0. Exit \ n \ choose = -1;while (Choose! = 0) {cout << "please select:"; Cin >> Choose;switch (choose) {Case 1://Create a single-linked list if (in itlist_l (L)) cout << "successfully set up a linked list!\n\n"; Break;case 2: Create a single-linked list Createlist_r (l, length) using the post-interpolation method cout << "Input book.txt Message complete \ n "; Break;case 3://single-linked list by ordinal value cout << "Please enter a location to use for value:"; Cin >> A;if (getelem_l (L, A, E)) {cout << "find successful \ n"; cout << "First" << a << ; "The information of this book is: \ n"; cout << left << setw (All) << e.id << "\ t" << left << setw (+) << e.na Me << "\ t" << left << SETW (5) << e.price << endl<< Endl;} Elsecout << "Find failed \ n"; Break;case 4://single-linked list by value lookup cout << "Please enter the price you want to find:"; Cin >> Price;if (locateelem_l (L, Price) = NULL) {cout << "find succeeded \ n"; cout << "The title of the prices corresponds to:" << locateelem_l (L, *)->data.name<< Endl << Endl;} Elsecout << "Find failed! Pricing "<< price <<" not found \ n "; Break;case 5://single-linked list insert cout <<" Please enter the location and the information of the book, including: numbered title price (separated by spaces): "; Cin >&G T A;cin >> e.id >> e.name >> e.price;if (listinsert_l (L, A, E)) cout << "Insert succeeded. \ n"; Elsecout << "Insert failed!\n\n"; Break;case 6://Delete single-linked list cout << "Please enter the location of the book you want to delete:"; Cin >> A;if (listdelete_l (L, a)) cout << "Delete Success!\n\n "; elsecout << "Delete failed!\n\n"; Break;case 7://output of single-linked list cout << "Current book System Information (linked list) read: \ n";p = L->next;while (p) {cout < ;< left << setw () << p->data.id << "\ t" << left << setw (<<) p->data.name << "\ t" << left << SETW (5) << p->data.price << endl;p = P->next;} cout << Endl;break;}} return 0;}

2.5.3 Cycle Chain List

Point the tail pointer to the first NODE element

Example: Huffman coding

#include "Cstdio" #include "stdlib.h" #include <iostream>using namespace std; #define S sizeof (struct node) struct Node{int num;struct node *next;}; typedef struct node node; node *createlinklist (int n) {node *head, *p, *q;int i = 1;head = P = (struct node*) malloc (sizeof (struct NODE));p->num = I;for (i = 2; I <= n; i++) {q = (struct node*) malloc (sizeof (struct node)); if (q = = 0) return (0);p->next = Q;p = q;p-& Gt;num = i;} P->next = head; /* Make the tail point of the chain list to form a circular list */return head;} void Printlinklist (NODE *p, int n) {int i; NODE *q = p;if (NULL = = Q->next) {printf ("The list is null!"); return;} printf ("Information list for all players: \ n"); for (i = 1; I <= n; i++) {if (NULL = = q) {printf ("The list is null!"); return;} printf ("%d", p->num);p = P->next;} printf ("\ n");} void Joseph (NODE *p, int n, int m) {int I, J; NODE *q;for (i = 1; i<n; i++) {for (j = 1; J <= M-1; j + +) {p = P->next;} Q = P->next;p->next = q->next;printf ("%d", q->num); free (q);} printf ("\ nthe last remaining number is%d. \ n", P->num);p->next = NULL;} void Main () {NODE *head;int N, m;printf ("Please enter the number of people n:\n"), scanf_s ("%d", &n);p rintf ("Input k:\n"), scanf_s ("%d", &m); Head = Createlinklist (n);p rintlinklist (head, N);p rintf ("The selected is: \ n"); Joseph (Head, N, M);

2.5.4 doubly linked list

A pointer is built on a single-linked list.

#include <iostream> #include <string> #include <fstream> #include <iomanip>using namespace std; #define OK 1#define ERROR 0#define overflow-2typedef int Status; Status is the function return value type whose value is the function result status code. typedef int ELEMTYPE; Elemtype is a definable data type, set to int type struct Book {string id;//isbnstring name;//title double price;//pricing};typedef struct Dulnode { Book data; Data domain struct Dulnode *prior; Direct precursor struct Dulnode *next; Direct successor} Dulnode, *dulinklist;string head_1, head_2, head_3;int length; Status initdulist_l (dulinklist &l) {//constructs an empty doubly-linked list LL = new Dulnode;//Generate a new node as a head node, with the head pointer L point to the head node L->next = NULL; The pointer field of the head node is empty l->prior = Null;return OK;} Dulnode *getelemp_dul (dulinklist l, int i) {//Find the first element in the doubly linked list L of the lead node, return the address of the node int j;dulinklist p;p = l->next;j = 1;//Initialize, p means To the first node, J is the counter while (J < i && p) {//cis-scan backwards until p points to element I or P = p->next;++j;} if (!p | | j > i) return NULL; The first element does not exist return p;} Getelemp_dulstatus Listinsert_dul (dulinklist &l, int i, book e) {//algorithm 2.13 bidirectional linked list insertion//bidirectional linked list in the lead nodeThe legal value of the Insert element e,i before the I position in L is 1<=i<= table length +1dulinklist s, p;if (! p = Getelemp_dul (L, i))//The position pointer of element I is determined in L Preturn ERROR; When P is null, the I element does not exist if (i = = 1) {//Insert S = new Dulnode on the first element of the doubly-linked list,////Generate a node Ss->data = e;//place node s data at edulinklist p = L->ne xt L->next = S;s->prior = L;s->next = p;//node *s is inserted into L p->prior = s;++length;} else if (i = = length) {//Insert S = new Dulnode on the last element of the doubly-linked list,/////////Generate node Ss->data = e;//place node s data at edulinklist p = l;while (p-> Next) p = p->next;//linklist p points to the end of the doubly linked list P->next = S;s->prior = p;//Inserts the node *s into the back of P, inserts into l s->next = null;++length;} else {s = new Dulnode;//generation of nodes *ss->data = e;//node *s data field is set to Es->prior = p->prior;//node *s is inserted in L, this step corresponds to diagram 2.20①p->prior ->next = s; corresponding figure 2.20②s->next = p; corresponding figure 2.20③p->prior = s; corresponding figure 2.20④++length;} return OK;} Listinsert_dulstatus Listdelete_dul (dulinklist &l, int i) {//Algorithm 2.14 Two-way list Delete//delete lead node doubly linked list L position before insert element e,i legal value is 1 <=i<= table length dulinklist p;if (! p = Getelemp_dul (L, i))//The position pointer of element I is determined in L Preturn ERROR; P isNULL, the I element does not exist if (i = = 1)//delete the first element of the doubly linked list L = L->next;else if (i = = length) {//delete the last element of the doubly linked list P->prior->next = Null;de Lete P;--length;return OK;} else {P->prior->next = p->next;//Modify the successor of the predecessor node of the deleted node, corresponding to Figure 2.21①p->next->prior = p->prior;// The precursor pointer of the successor node of the deleted node is modified, and the corresponding figure 2.21②delete p; Release the deleted node space--length;return OK;}} Listdelete_dulvoid createdulist_l (dulinklist &l) {//positive-order input N-element values, establish a doubly linked list L with a header node, and establish precursor pointer dulinklist R, p; L = new Dulnode; L->next = NULL; First set up a lead node of the empty list R = l;//Tail pointer r points to the head node length = 0;fstream file;file.open ("Book.txt"); if (!file) {cout << "No related file found, cannot open!" "<< Endl;exit (ERROR);} File >> head_1 >> head_2 >> head_3;while (!file.eof ()) {p = new Dulnode;//Generate new node file >> p->data . ID >> p->data.name >> p->data.price; INPUT element Value P->next = Null;r->next = P; Insert to the end of the table R = p; R points to the new tail node P->prior = l->prior; Insert to table header L->prior = p;length++;} File.close ();} Createdulist_lint Main () {int A, choose; Book E;dulinklist L, P;couT << "1. Build \ n "; cout <<" 2. Enter \ n "; cout <<" 3. Insert \ n "; cout <<" 4. Delete \ n "; cout <<" 5. Output \ n "; cout <<" 0. Exit \ n \ choose = -1;while (Choose! = 0) {cout << "please select:"; Cin >> Choose;switch (choose) {Case 1://Create a doubly linked list if (I nitdulist_l (L)) cout << "successfully set up two-way linked list!\n\n"; Break;case 2://Use the post-interpolation method to create a doubly linked list createdulist_l (l); cout << input Book.txt Information complete \ n \ break;case 3://Insert cout << "Please enter two numbers for the location and value of the insertion (information of the book: Number & Title & price):"; Cin >> a;cin > > e.id >> e.name >> e.price;if (Listinsert_dul (L, A, E)) cout << "Insert succeeded. \ n"; elsecout << "Insert failed!\ N\n "; Break;case 4://delete doubly linked list cout <<" Please enter the location of the book you want to delete: "; Cin >> A;if (Listdelete_dul (L, a)) cout <<" Delete succeeded!\ N\n "; elsecout <<" Delete failed!\n\n "; Break;case 5://output of the doubly linked list cout <<" Current book system Information read: \ n ";p = L->next;while (p) {cout &L t;< left << setw () << p->data.id << "\ t" << left << setw (<<) P->data.nam e << "\ t" << lEFT << SETW (5) << p->data.price << endl;p = P->next;} cout << Endl;break;}} return 0;}

2.6 Comparison of sequential tables and linked lists

Not to be continued ...

Linear table of data structure notes