The basic operation of the dual linked list

  . 1 #include <stdio.h>
   2 #include <stdlib.h>
   . 3 typedef struct Line {
   . 4      struct Line * Prior;
   . 5      int Data;
   . 6      struct Line * Next;
   . 7  } Line;
   . 8  // create a doubly-linked list 
  . 9 Line initLine * (* Line head);
 10  // doubly linked list insert elements, add showing the insertion position 
. 11 Line insertLine * (* Line head, int Data, int the Add);
 12 is  // doubly linked list to delete the specified element 
13 is Line delLine * (Line head *, intData);
 14  // doubly linked list to find the specified element 
15  int selectElem (Line head *, int elem);
 16  // doubly linked list to change the specified position stored in the node data, add the position change represents 
. 17 Line amendElem * (* Line P, int the Add, int newElem);
 18 is  // output function doubly-linked list implementation 
. 19  void the display (* Line head);
 20 is  int main () {
 21 is      Line head * = NULL;
 22 is      // create a doubly linked list 
23 is      head = initLine (head);
 24      the display (head);
 25      //In Table 3, the position of the insertion element. 7 
26 is      head = insertLine (head, . 7 , 3 );
 27      the display (head);
 28      // Table delete elements 2 
29      head = delLine (head, 2 );
 30      the display (head );
 31 is  
32      the printf ( " position of the element 3 is: D% \ n- " , selectElem (head, 3 ));
 33 is      // data table third storage node 6 to 
34 is      head = amendElem (head, . 3 , . 6 );
 35      the display (head);
 36      return  0 ;
 37 [ }
 38 line* initLine(line * head){
 39     head=(line*)malloc(sizeof(line));
 40     head->prior=NULL;
 41     head->next=NULL;
 42     head->data=1;
 43     line * list=head;
 44     for (int i=2; i<=5; i++) {
 45         line * body=(line*)malloc(sizeof(line));
 46         body->prior=NULL;
 47         body->next=NULL;
 48         body->data=i;
 49 
 50         list->next=body;
 51         body->prior=list;
 52         list=list->next;
 53     }
 54     return head;
 55 }
 56 line * insertLine(line * head,int data,int add){
 57     //新建数据域为data的结点
 58     line * temp=(line*)malloc(sizeof(line));
 59     temp->data=data;
 60     temp-> = Prior NULL;
 61 is      temp-> Next = NULL;
 62 is      // inserted into the head of the list, special consideration to 
63 is      IF (the Add == . 1 ) {
 64          temp-> Next = head;
 65          head-> Prior = TEMP ;
 66          head = TEMP;
 67      } the else {
 68          Line * = body head;
 69          // find a node to be inserted before the position 
70          for ( int I = . 1 ; I <add- . 1 ; I ++ ) {
 71 is              body = body ->Next;
 72          }
 73 is          // determination condition is true, indicating that the insertion position of the end of the list 
74          IF (body-> Next == NULL) {
 75              body-> Next = TEMP;
 76              temp-> Prior = body;
 77          } the else {
 78              body-> next-> Prior = TEMP;
 79              temp-> Next = body-> Next;
 80              body-> Next = TEMP;
 81              temp-> Prior = body;
 82          }
 83      }
 84      return head;
85  }
 86 Line delLine * (* Line head, int data) {
 87      Line * TEMP = head;
 88      // traverse the list 
89      the while (TEMP) {
 90          // determines whether the current node and the data in the data fields are equal, if equal , removal of the node 
91 is          IF (temp-> == Data Data) {
 92              temp-> prior-> Next = temp-> Next;
 93              temp-> next-> Prior = temp-> Prior;
 94              Free (TEMP) ;
 95              return head;
 96          }
 97          TEMP = temp-> Next;
 98     }
 99      the printf ( " the list without the data element " );
 100      return head;
 101  }
 102  // head of the original double-linked list, elem represents a lookup element 
103  int selectElem (Line head *, int elem) {
 104  // New a pointer t, the head pointer is initialized to head 
105      Line T * = head;
 106      int I = . 1 ;
 107      the while (T) {
 108          IF (T-> Data == elem) {
 109              return I;
 110          }
 111          I ++;
 112          T = T-> Next;
 113      }
 114      // program execution to here, represent the lookup fails 
115      return - . 1 ;
 1 16  }
 117  // update function, wherein, the Add node represents a change in position of the doubly linked list, newElem the value of new data 
1 18 Line * amendElem (Line * P, int the Add, int newElem) {
 119      Line * TEMP = P;
 120      // traverse to the deleted node 
121      for ( int I = . 1 ; I <the Add; I ++ ) {
 122          TEMP = temp-> Next;
 123      }
124      temp-> Data = newElem;
 125      return P;
 126  }
 127  // output performance function list 
128  void the display (* Line head) {
 129      Line * TEMP = head;
 130.      the while (TEMP) {
 131 is          IF (temp-> == Next NULL) {
 132              the printf ( " % D \ n- " , temp-> Data);
 133          } the else {
 134              the printf ( " % D-> " , temp-> Data);
 135         }
136         temp=temp->next;
137     }
138 }

 

Guess you like

Origin www.cnblogs.com/wy0526/p/11788493.html