1. Linear table: an ordered set of n data elements.
A linear table is a common data structure. In practice, linear tables are used in the form of special linear tables such as stacks, queues, strings, and arrays. Because these special linear tables have their own characteristics, it is vital to master the characteristics of these special linear tables for data operation reliability and operation efficiency improvement. A linear table is a linear structure. It is a finite sequence Containing n or greater than 0 knots. For the nodes, there is only one starting node, there is no precursor, but there is a successor node, there is only one terminal node without any successor, but there is a precursor node. Other nodes have only one and only one successor node.
Features:
1. A unique "first element" must exist in the collection ";
2. A unique "last element" must exist in the collection ";
3. Except for the last element, each element has a unique successor (post );
4. Apart from the first element, each element has a unique precursor (front part ).
The List interface in java is a linear table. ArrayList is a sequential linear table, and linear list is a linked list linear table.
2. Sequential Representation of linear tables: ArrayList
Arrays are generally used (arrays in C language use sequential storage. That is, continuous address storage.
Advantage: Random Access elements,
Disadvantage: a large number of elements need to be moved during insertion and deletion.
C language implementation code:
// Test. cpp: Defines the entry point for the console application. // # include "stdafx. h "# include <stdio. h> # include "stdlib. h "// macro definition # define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define INFEASIBLE-1 # define OVERFLOW-2 # define LT (a, B) (a) <(B) # define N = 100 # define LIST_INIT_SIZE 100 // The initial space allocation volume of a linear table # define LISTINCREMENT 10 // incremental typedef int Status for space allocation in a linear table; typedef int ElemType; typedef struct LNod E {ElemType * elem; // The base address of the bucket int lenght; // The current length int listsize; // currently allocated storage capacity} SqList; /*** construct an empty linear table */Status initList (SqList & L, int lenght) {if (lenght = 0) lenght = LIST_INIT_SIZE; L. elem = (ElemType *) malloc (lenght * sizeof (ElemType); if (! L. elem) exit (OVERFLOW); // failed to allocate the bucket L. lenght = 0; // The initial empty table length is 0L. listsize = lenght; // The initial storage capacity is 100 return OK ;} /*************************************** * ********************************** in the I position insert e *//*********************************** * **********************************/Status insertList (SqList & L, elemType e, int I) {ElemType * p, * q; if (I <0 | I> L. lenght) return ERROR; // The I value is invalid if (L. lenght> = L. listsize) {E LemType * newbase = (ElemType *) realloc (L. elem, (L. listsize + LISTINCREMENT) * sizeof (ElemType); if (! Newbase) return OVERFLOW; // storage allocation fails. elem = newbase; // new base value L. listsize + = LISTINCREMENT; // increase storage capacity} q = & L. elem [I]; // q is the insert position for (p = & L. elem [L. lenght]; p> = q; -- p) {* p = * (p-1); // move the elements after the I element} * q = e; // insert eL. lenght + = 1; return OK ;} /*************************************** * **********************************/* quick sorting */ /*************************************** * *******************************/void sortList (SqList & L) {}/************************************* *********************************** Delete I position element, use e to return the value *//******************************** **************************************** /Status deleteListElem (SqList & L, int I, ElemType & e) {int * p, * q; if (I <0 | I> L. lenght) return ERROR; // The I value is invalid. q = & L. elem [I]; // The Position of the deleted element is I, L. elem is the array name, e = * q; // The value of the deleted element is assigned to efor (p = q; p <(L. elem + L. lenght); p ++) {// shifts the element left * p = * (p + 1);} -- L. lenght; return OK ;} /*************************************** * **********************************/* quick sorting */ /*************************************** * *******************************/int partition (SqList & L, elemType low, ElemType high) {ElemType effectkey = L. elem [low]; // pivot record keyword while (low
3. The linked list of a linear table indicates the linear list.
Linked List.
Advantage: it is convenient to add and remove new and delete operations. You do not need to move the element.
Disadvantage: it is not convenient to access elements randomly. The pointer list must move the pointer.
Code implementation:
// Test. CPP: defines the entry point for the console application. // # include "stdafx. H "# include <stdio. h> # include "stdlib. H "// macro definition # define true 1 # define false 0 # define OK 1 # define error 0 # define Infeasible-1 # define overflow-2 # define LT (A, B) (a) <(B) # define n = 100 typedef int status; typedef int elemtype; typedef struct lnode {elemtype data; struct lnode * Next;} lnode, * linklist; /********************** **************************************** * ******** // * Initialize the linked list *//********************** **************************************** * ********/status initlist (linklist & L) {/* single-chain table initialization */L = (linklist) malloc (sizeof (lnode); // apply for a header node if (! L) Exit (overflow); // failed to apply for space L-> next = NULL; // create an empty linked list with all nodes Return OK;/* pointer to be changed, therefore, the parameter must be referenced or * l :( * L) = (lnode *) malloc (sizeof (lnode); (* l)-> next = NULL; return 1; */}/************************************ *********************************** // linked list *//************************************ * **********************************/void createlist (linklist l, int N) {/* single-chain table initialization */If (! L) {initlist (l);} elemtype data; linklist p, q = L; printf ("Number of input node data % d: \ r \ n", N ); for (INT I = 0; I <n; I ++) {P = (linklist) malloc (sizeof (lnode )); // apply for a new node scanf ("% d", & data); P-> DATA = data; P-> next = Q-> next; q-> next = P; q = P ;}} /*************************************** * ********************************** in the I position insert E *//*********************************** * **********************************/status insertlist (Linklist L, elemtype E, int I) {linklist S, P = L; Int J = 0; while (P & J <I) {// find the I node P = p-> next; j ++;} If (! P | j> I) Return Error; S = (linklist) malloc (sizeof (lnode); // generate new node S-> DATA = E; s-> next = p-> next; // insert p-> next = s; Return OK ;} /*************************************** ********************************** Delete the I location element, use E to return the value *//******************************** **************************************** /status deletelistelem (linklist l, int I, elemtype & E) {linklist p, q; Int J = 0; P = L; while (P & J <I) {P = P-> next; ++ J;} If (! P-> next | j> I) Return Error; // The deletion location is incorrect. q = p-> next; P-> next = Q-> next; E = Q-> data; free (Q); // release the node Return OK ;} /*************************************** ********************************* // insert sorting */ /*************************************** * *******************************/void insertsort (linklist L) {linklist list;/* indicates the remaining node header pointer for direct insertion of sorting in the original linked list */linklist node;/* Insert node */linklist P; linklist Q; list = L-> next;/* remaining original linked list Directly Insert the sorted node linked list */L-> next = NULL;/* The ordered linked list containing only one node. */While (list! = NULL) {/* traverse the unordered linked list */node = List, q = L; while (Q & node-> DATA> q-> data) {P = Q; q = Q-> next;} If (q = L) {/* before the first node */L = node ;} else {/* P is the precursor of Q */p-> next = node;} List = List-> next; node-> next = Q; /* Complete the insert action */}}/****************************** **************************************** ** // * merge two linear tables *//***************************** **************************************** * **/void mergelist (linklist & L A, linklist & Lb, linklist & lc) {linklist Pa, Pb, PC; Pa = La-> next; Pb = LB-> next; lc = pc = La; while (PA & PA) {If (Pa-> DATA> Pb-> data) {PC-> next = Pb; Pc = Pb; Pb = Pb-> next ;} else {PC-> next = PA; Pc = PA; Pa = pa-> next ;}} PC-> next = pa? PA: Pb; free (LB );} /*************************************** * ******************************** // print list */ /*************************************** * ********************************/void printlist (linklist L) {printf ("Current Value:"); linklist P; P = L-> next; while (p) {printf ("% d", p-> data ); P = p-> next;} printf ("\ r \ n");} void main () {linklist la, LB, LC; elemtype E; int init, I; printf ("La: \ r \ n"); initlist (LA); createlist (La, 5); insertlist (La, 7, 3); printlist (LA ); deletelistelem (La, 3, e); printlist (LA); insertsort (LA); printlist (LA); printf ("LB: \ r \ n "); initlist (LB); createlist (LB, 4); insertsort (LB); printlist (LB); printf ("LC: \ r \ n"); initlist (LC ); mergelist (La, LB, LC); printlist (LC );}