[] Data structures and algorithms operating linear table (C ++)

  . 1 #include <stdio.h>
   2  
  . 3  #define maxSize 100   // define maxSize integer constant value of 100 
  . 4  / * definition of the structure of sequence table * / 
  . 5 typedef struct SqList {
   . 6      int Data [maxSize]; // store sequence array table element 
  . 7      int length;         // store the length of the sequence table 
  . 8 } SqList;   // sequence table type definitions 
  . 9  
10  / * a single linked list node definition * / 
. 11 typedef struct LNode {
 12 is      int Data;             // Data in data domain storage node
13 is      struct LNode * Next;    // pointer to the descendant node 
14 } LNode;   // define a single linked list node type 
15  / * doubly linked list node definition * / 
16 typedef struct DLNode {
 . 17      int Data;   // Data stored in the node data domain 
18 is      struct DLNode * Prior;     // points to the predecessor node pointer 
. 19      struct DLNode * Next;     // pointer to the descendant node 
20 is } DLNode;   // define a double linked list node type 
21 is  
22 is  / * Example 2.1 start * / 
23  / * order table lookup returns the first element is greater than to find the address of* / 
24  int findElem (SqList L, int X) {
 25      int I;
 26 is      for (I = 0 ; I <L.length; ++ I) {
 27          IF (X <L.data [I]) { // the order of elements in the table is determined one by one from small to large, see x is less than the current scan to the elements 
28              return I; // if the current position is returned is less than 
29          }
 30      }
 31 is      return I; // if the sequence table is not there is a large element ratio x, x should be inserted after the end of the table element, i mark this return 
32  }
 33  / * an important role as a reference that is a function of the parameters. Before passing arguments to functions in C language are passed by value, if there is a large block of data when the transmission parameters, the program pointer is often employed, because it eliminates all the push block data.
34 
35  But now (C ++) is added an equally efficient choice (in certain special circumstances and must be choice), is a reference. * / 
36  void insertElem (SqList L &, int X) { // Since L itself changes to occur, so use a reference type 
37 [      int P, I;
 38 is      P = findElem (L, X); // call the function findElem () to find the insert position P 
39      for (I = L.length - . 1 ; I> = P; - I) { // from right to left by one position to the right element 
40          L.data [I + . 1 ] = L.data [I];
 41 is      }
 42 is      L.data [P] = x; // the insertion position on the x P 
43 is      ++ (L.length);   //Thus a plurality of inner element long table is incremented by one 
44 is  }
 45  / * Example 2.1 End * / 
46 is  
47  / * Example 2.2 Start * / 
48  / * Remove the order list L of the subscript p elements, a successful return, otherwise 0 * / 
49  int deleteElem (SqList L &, int P, int & E) { // variables need to change with reference to type 
50      int I;
 51 is      IF (P < 0 || P> L.length- . 1 ) {
 52 is          return  0 ; // wrong position 0 is returned delete unsuccessful 
53 is      }
 54 is      E = L.data [P]; //The values assigned to the element to be deleted E 
55      for (P = I; I <L.length - . 1 ; ++ I) {
 56 is          L.data [I] = L.data [I + . 1 ];
 57 is      }
 58      - - (L.length); // table length -1 
59      return  . 1 ; // delete successful return 1 
60  }
 61 is  
62 is  / * Example 2.2 end * / 
63 is  
64  / * initialize sequence table * / 
65  
66  void initList (L & SqList ) { // L itself so changed with reference to type 
67      L.length = 0 ;
 68 }
 69  
70  / * find the position of the pointing element Algorithm * / 
71 is  int getElem in (SqList L, int P, int & E) { // To change it with a reference type 
72      IF (P < 0 || P> L.length- . 1 ) { // P values out of range error return 0 
73 is          return  0 ;
 74      }
 75      E = L.data [P];
 76      return  . 1 ;
 77  
78  }
 79  
80  
81  void showElem (SqList L) {
 82      int I;
 83     for (i = 0; i < L.length;++i) {
 84         printf("%d:%d\n",i,L.data[i]);
 85 
 86     }
 87 }
 88 void main() {
 89     SqList L;
 90     initList(L);
 91     for (int i = 0; i < 5;i++) {
 92         insertElem(L, i*10);
 93     }
 94     printf("insert over\n");
 95     showElem(L);
 96     int a = findElem(L, 10);
 97     printf("寻找10返回:%d\n", a);
 98     printf("find over\n");
 99     showElem(L);
100     insertElem(L, 35);
101     printf("insert 35 over\n");
102     showElem(L);
103     int j;
104     deleteElem(L, 2, j);
105     printf("delete second over\n");
106     showElem(L);
107 }

 

Guess you like

Origin www.cnblogs.com/dream-to-pku/p/11422946.html