Singly linked list insert and delete data

 

  1  // Insert element according to the order of the list 
  2 #include <stdio.h>
   . 3 #include <stdlib.h>
   . 4 #include < String .h>
   . 5  
  . 6  struct the Node
   . 7  {
   . 8      int value;
   . 9      struct the Node * Next ;
 10  };
 . 11  
12 is  void insertNode ( struct the Node ** head, int value)
 13 is  {
 14      struct the Node * Previous;
 15      struct the Node * Current;
 16     struct Node *newnode;
 17 
 18     current = *head;
 19     previous = NULL;
 20     
 21     
 22     while (current != NULL && current->value < value)
 23     {
 24         int b = current->value;
 25         previous = current;        //previous记录current上一个节点的位置
 26         current = current->next;
 27     }
 28     newnode = (struct Node *)malloc(sizeof(structThe Node)); // allocate memory 
29      IF (newNode == NULL)
 30      {
 31 is          the printf ( " memory allocation failure! " );
 32          Exit ( . 1 );
 33 is      }
 34 is      newnode-> value = value;
 35      newnode-> Next = Current;
 36      IF (Previous == NULL)
 37 [      {
 38 is          * = head newNode;
 39      }
 40      the else 
41 is      {
 42 is         previous->next = newnode;
 43     }
 44     
 45 }
 46 
 47 void printNode(struct Node *head)
 48 {
 49     struct Node *current;
 50     current = head;
 51     while (current != NULL)
 52     {
 53         printf("%d ", current->value);
 54         current = current->next;
 55     }
 56     printf("\n");
 57 }
 58 
 59 void deleteNode(struct Node **head, int value)
 60 {
 61     struct Node *previous;
 62     struct Node *current;
 63 
 64     current = *head;
 65     previous = NULL;
 66 
 67     while (current != NULL && current->value != value)
 68     {
 69         previous = current;
 70         current = current->Next;
 71 is      }
 72      IF (Current == NULL)
 73 is      {
 74          the printf ( " node did not match \ n- " );
 75          return ;
 76      }
 77      the else 
78      {
 79          IF (Previous == NULL)
 80          {
 81              * = to current- head> Next;
 82          }
 83          the else 
84          {
 85              previous-> Next to current- => Next;
 86         }
 87          Free (Current);
 88      }
 89  }
 90  int main ()
 91 is  {
 92      struct the Node head * = NULL;
 93      int INPUT;
 94      the printf ( " Start test insertion integer ... \ n- " );
 95      the while ( . 1 )
 96      {
 97          the printf ( " \ n-enter an integer (-1 indicating the end): " );
 98          Scanf ( " % D " , & iNPUT);
 99         the printf ( " \ n- " );
 100          IF (INPUT == - . 1 )
 101          {
 102              BREAK ;
 103          }
 104          insertNode (& head, INPUT);
 105          printNode (head);
 106      }
 107  
108      the printf ( " Start Test Remove integer ... \ n- " );
 109      the while ( . 1 )
 110      {
 111          the printf ( " \ n-enter an integer (-1 indicating the end): " );
 112         scanf("%d", &input);
113         printf("\n");
114         if (input == -1)
115         {
116             break;
117         }
118         deleteNode(&head, input);
119         printNode(head);
120     }
121 
122     return 0;
123 }

Reprinted from: KHQ Wind River

 

Guess you like

Origin www.cnblogs.com/hsy1941/p/11505172.html