Chapter 1 about Data Structure 1. What is the research on data structure? (The objects processed by a computer are composed of numeric values> non-numeric values.) A large number of complex problems (non-numeric problems) in real life are identified by a specific data type (logical structure) and a specific storage structure (physical structure) stored in the primary storage, and on this basis to achieve a function (delete, sort) corresponding operations. 2. logical Structure of data: 3. storage Structure (physical structure): 1) sequential storage structure (with the help of the relative location of elements in the memory) 2) chained storage structure (with the help of the element storage address pointer) 4. ABSTRACT Data Type ADT: [cpp] ADT abstract data type name {Data Object: <data object definition> Data Relationship: <definition of the relationship between data objects> basic operations: <Definition of basic operations>} 5. Time Complexity: Take the number of repetitions of decisive statements. Chapter 2 linear table 1. basic Features of a linear structure: 1) a unique "first element" must exist in the Set; 2) a unique "last element" must exist in the Set; 3) except for the last element, each has a unique successor. 4) except for the first element, each has a unique precursor. 2.ADT [cpp] ADT List {Data Object: D = {a1, a2, a3 ,... an}; Data Relationship: R ={< a1, a2 >,< a2, a3 >,< a3, a4>... <An-1, an >}; basic operation: InitList (& L); // operation result: Create a linear table; DestroyList (& L); // Operation Result: destroy a linear table; listEmpty (L); // Operation Result: if L is empty, TRUE is returned; otherwise, FALSE is returned; ListLength (L); // Operation Result: returns the number of elements in L; priorElem (L, cur_e, & pre_e) // Operation Result: cur_e is an element of L, but it is not the first //. Then, use pre_e to return its precursor. If the operation fails, pre_e does not define NextElem (L, cur_e, & next_e) // Operation Result: cur_e is an element of L, but not the last one, // use next_e to return its successor. If the operation fails, next_e does not define the GetElem (L, I, & e) // 1 <= I <= LengthList (L) operation result: use e to return the value of the I-th element in L. LocateElem (L, e, compare () // compare () is an element judgment function. Returns the first element order in L that matches compare () with e. // If such an element does not exist, the return value is 0. ListTraverse (L, visit () // calls the visit () function for each element of L in sequence (). once visit () fails, the operation fails to ClearList (& L) // The operation result resets L to an empty table. PutElem (L, I, & e) // 1 <= I <= LengthList (L) Result: element I in L is assigned to e ListInsert (& L, I, e) // 1 <= I <= LengthList (L) + 1 Result: Insert a new element e before the I element of L, L length increases by 1 ListDelete (& L, I, & e) // 1 <= I <= LengthList (L) result: the I-th element of L is deleted, return the value with e, and the length of L minus 1} ADT List 3. sequential implementation 1) Storage Structure: [cpp] # define LIST_INIT_SIZE 10 # define INCREAMENT 2 struct SqList {ElemType * elem; int length; int listsize ;}; 2) basic operation [cpp] void InitList (SqList & L) {L. elem = (ElemType *) Malloc (LIST_INIT_SIZE * sizeof (ElemType); L. length = 0; L. listsize = LIST_INIT_SIZE;} void DestroyList (SqList & L) {free (L. elem); L. elem = NULL; L. length = 0; L. listsize = 0;} void ClearList (SqList & L) {L. length = 0;} Status ListEmpty (SqList L) {if (L. length! = 0) return FALSE; else return TRUE;} int ListLength (SqList L) {return L. length;} Status GetElem (SqList L, int I, ElemType & e) {if (I <1 | I> L. length) return ERROR; e = * (L. elem + I-1); return OK;} int LocateElem (SqList L, ElemType e, Status (* compare) (ElemType, ElemType) {ElemType * p; p = L. elem; int I = 1; while (I <L. length &&! (Compare (e, * p) {I ++; p ++;} if (I <L. length) return I; else return 0;} Status PriorElem (SqList L, ElemType cur_e, ElemType & pre_e) {ElemType * p; p = L. elem + 1; // cooperation between p and I int I = 2; while (I <L. length & * p! = Cur_e) {I ++; p ++;} if (I <L. length) {pre_e = * (-- p); return OK;} else return INFEASIBLE;} Status NextElem (SqList L, ElemType cur_e, ElemType & next_e) {// initial condition: the ordered linear table L already exists // Operation Result: If cur_e is a data element of L and is not the last one, use next_e to return its successor. // otherwise, the operation fails, next_e does not define int I = 1; ElemType * p = L. elem; while (I <L. length & * p! = Cur_e) {I ++; p ++;} if (I = L. length) return INFEASIBLE; // operation failed else {next_e = * ++ p; return OK ;}} Status ListInsert (SqList & L, int I, ElemType e) {if (I <1 | I> L. length + 1) return ERROR; ElemType * newbase; if (L. length> = L. listsize) {newbase = (ElemType *) realloc (L. elem, (L. listsize + INCREAMENT) * sizeof (ElemType); L. listsize = L. listsize + INCREAMENT; if (! Newbase) exit (OVERFLOW); L. elem = newbase;} ElemType * p, * q; p = L. elem + L. length-1; q = L. elem + I-1; for (p = L. elem + L. length-1; p> = q; -- p) {* (p + 1) = * p;} * q = e; L. length = L. length + 1; return OK;} Status ListDelete (SqList & L, int I, ElemType & e) // algorithm 2.5 {// initial condition: the ordered linear table L already exists, 1 ≤ I ≤ ListLength (L) // Operation Result: Delete the I-th data element of L and return its value with e. The length of L is reduced by 1 ElemType * p, * q; if (I <1 | I> L. length) // The I value is invalid. return ERRO R; p = L. elem + I-1; // p is the location of the deleted Element e = * p; // The value of the deleted element is assigned to e q = L. elem + L. length-1; // The Position of the element at the end of the table for (+ + p; p <= q; + + p) // shifts left the element after the element is deleted * (p-1) = * p; L. length --; // The table length minus 1 return OK;} void ListTraverse (SqList L, void (* vi) (ElemType &) {// initial condition: the ordered linear table L already exists // operation result: Call the form of the vi () // vi () function for each data element of L in sequence to participate in '&', it indicates that ElemType * p; int I; p = L can be changed by calling vi. elem; for (I = 1; I <= L. length; I ++) vi (* p ++); printf ("\ n");} 4. chain Implementation 1) Storage Structure: [cpp] struc T LNode {ElemType data; LNode * next;}; typedef LNode * LinkList; 2) Basic operation: [cpp] void InitList (LinkList & L) {// operation result: construct an empty linear table L = (LinkList) malloc (sizeof (LNode); // generate a header node and point L to this header node if (! L) // storage allocation failure exit (OVERFLOW); L-> next = NULL; // the pointer field is empty} void DestroyList (LinkList & L) {// initial condition: the linear table L already exists. Operation Result: destroy the linear table L LinkList q; while (L) {q = L-> next; free (L); L = q ;}} void ClearList (LinkList L) // do not change the L {// initial condition: the linear table L already exists. Operation Result: reset L to the empty table LinkList p, q; p = L-> next; // p points to the first node while (p) // {q = p-> next; free (p); p = q;} L-> next = NULL; // The header node pointer field is null} Status ListEmpty (LinkList L) {// initial condition: the linear table L already exists. Operation Result: if L is an empty table, TRUE is returned. Otherwise, FALSE if (L-> next) is returned. // non-empty return FALSE; else return TRUE ;} int ListLength (LinkList L) {// initial condition: the linear table L already exists. Operation Result: return the number of data elements in L int I = 0; LinkList p = L-> next; // p points to the first node while (p) // {I ++; p = p-> next;} return I;} Status GetElem (LinkList L, int I, ElemType & e) // algorithm 2.8 {// L is the header pointer of the single-chain table of the leading node. When the I-th element exists, the value is assigned to e and OK is returned; otherwise, ERROR int j = 1 is returned; // j is the counter LinkList p = L-> next; // p points to the first node while (p & j <I) // returns the cursor for Backward Search, until p points to the I element or p is null {p = p-> next; j ++;} if (! P | j> I) // return ERROR does not exist for element I; e = p-> data; // return OK for element I ;} int LocateElem (LinkList L, ElemType e, Status (* compare) (ElemType, ElemType) {// initial condition: Linear table L already exists, compare () is the data element determination function (satisfied as 1, otherwise 0)/* operation result: returns the order of the 1st data elements in L that meet the compare () Relationship with e. If such a data element does not exist, the returned value is 0 */int I = 0; LinkList p = L-> next; while (p) {I ++; if (compare (p-> data, e) // find the data element return I; p = p-> next;} return 0;} Status PriorElem (LinkList L, elemType cur_e, ElemType & pre_e) {// initial condition: Linear table L already exists/* operation result: If cur_e is a data element of L and is not the first, then, use pre_e to return its precursor and Return OK; otherwise, the operation fails. If pre_e is not defined, INFEASIBLE */LinkList q, p = L-> next is returned; // p points to the first node while (p-> next) // p indicates that the node has a successor {q = p-> next; // if (q-> data = cur_e) after q is p) {Pre_e = p-> data; return OK;} p = q; // p moves back} return INFEASIBLE;} Status NextElem (LinkList L, ElemType cur_e, ElemType & next_e) {// initial condition: the linear table L already exists./* operation result: If cur_e is a data element of L and is not the last one, use next_e to return its successor and Return OK; otherwise, the operation fails. next_e is not defined. INFEASIBLE */LinkList p = L-> next; // p is returned to point to the first node while (p-> next) // p indicates that the node has a successor {if (p-> data = cur_e) {next_e = p-> next-> data; return OK ;} p = p-> next;} return INFEASIBLE;} Status ListInsert (Link List L, int I, ElemType e) // algorithm 2.9. Do not change L {// Insert the Element e int j = 0 before position I in the single-chain linear table L of the leading node; LinkList p = L, s; while (p & j <i-1) // find the I-1 node {p = p-> next; j ++;} if (! P | j> i-1) // I less than 1 or greater than the table length return ERROR; s = (LinkList) malloc (sizeof (LNode )); // generate new node s-> data = e; // insert s-> next = p-> next; p-> next = s; return OK ;} status ListDelete (LinkList L, int I, ElemType & e) // algorithm 2.10. Do not change L {// In the leader node's single-chain linear table L, delete the I-th element, and e returns its value int j = 0; LinkList p = L, q; while (p-> next & j <i-1) // find the I-th node and point p to its precursor {p = p-> next; j ++ ;} if (! P-> next | j> i-1) // return ERROR where the deletion location is unreasonable; q = p-> next; // Delete and release the node p-> next = q-> next; e = q-> data; free (q); return OK;} void ListTraverse (LinkList L, void (* vi) (ElemType) // The vi parameter type is ElemType, which is different from the ElemType of the corresponding function in the bo2-1.cpp. {// The initial condition is that the linear table L already exists. Operation Result: Call the function vi () LinkList p = L-> next for each data element of L in sequence; while (p) {vi (p-> data ); p = p-> next;} printf ("\ n ");}