Linear list data structure

  1 #include<stdio.h> 
  2 #include<stdlib.h>
  3  
  4 #define OK 1
  5 #define ERROR 0
  6 #define OVERFLOW -1
  7 
  8 typedef int Elemtype;
  9 
 10 typedef struct LNode//线性链表 
 11 {
 12     Elemtype data;
 13     struct LNode *next;
 14     
 15 } LNode,*Linklist;
 16 
 17 int init_Linklist(Linklist &l)
 18 {
 . 19      L = (Linklist) the malloc ( the sizeof (LNode)); // build the first node and the first node to the initial value element 
20 is      
21 is      IF (! L)
 22 is          Exit (the OVERFLOW);    
 23 is      
24      L-> Next = NULL;
 25      L-> Data = 0 ;
 26 is          
27      return the OK;
 28  }
 29  
30  int input_Linklist (Linklist & L)
 31 is  {
 32      int n-;
 33 is      the printf ( " \ n-number of inputs: \ n- " );
34 is      Scanf ( " % D " , & n-);
 35      the printf ( " \ n-input data: \ n- " );
 36      
37 [      Linklist tail = L; // Create a temporary tail node 
38 is      
39      for ( int I = 0 ; I <n-; I ++ )
 40      {
 41 is          Linklist P = (Linklist) the malloc ( the sizeof (LNode)); // Create a node as the current node 
42 is          Scanf ( " % D " , & p-> Data);
 43 is          
44 is         p-> Next = tail-> Next;
 45          tail-> Next = P; // End connection      
46 is          
47          tail tail- => Next; // kept tail node, so that the normal order of the list to create 
48      }
 49      return the OK;
 50  }
 51 is  
52 is  int get_Elem (Linklist L, int i, & elemType e)
 53 is  {
 54 is      // L is the first node single list head pointer
 55      // when the i-th element is present, and the value assigned to e return OK, otherwise ERROR 
56 is      Linklist = L-P> Next;
 57 is      int J = . 1 ;
 58      the while(P && J < I)
 59      {
 60          P = p-> Next;
 61 is          J ++ ;
 62 is      }
 63 is      
64      IF (P || J>! I)
 65          return ERROR;
 66      
67      E = p-> Data;
 68      return the OK;
 69  }
 70  
71 is  int insert_Linklist (Linklist l &, int i, elemType e)
 72  {
 73 is      // insert elements e i-th position before the lead node of a single linked list l 
74      Linklist P = l;
 75      int= J 0 ;
 76      
77      the while (J && P <I- 1 ) // find the node i-1, (j = 0 the first node / j = 1 the first node) 
78      {
 79          P = p-> Next;
 80          J ++ ;
 81      }
 82      
83      IF (! J || P> I- . 1 ) // P empty / i input error 
84          return eRROR;
 85      
86      Linklist S = (Linklist) the malloc ( the sizeof (LNode));
 87      
88      S-> Data = E;
 89      S-> Next = p-> Next;
90      p-> Next = S;
 91 is      
92      return the OK;
 93  }
 94  
95  int delete_Linklist (Linklist l &, int i, elemType & E)
 96  {
 97      // at the First Node single list l, the i-th element deleted , e and returns its value 
98      Linklist p = L;
 99      int J = 0 ;
 100      
101      the while (p-> Next && J <I- . 1 ) // find the i-th node, and to make its precursor p at node 
102      {
 103          = p-P> Next;
 104          J ++ ; 
105      }
 106      
107      IF ((p-> Next) || J> I-! . 1 )
 108          return ERROR; // delete location unreasonable 
109      
110      Linklist = p-Q> Next;
 111      p-> Next Q- => Next; // list by deleting the node q 
112      E = Q-> data; // record data 
113      Free (q); // release q node 
114      
115      return the OK; 
 1 16  }
 117  
1 18  int output_Linklist (Linklist & L)
 119  {
 120      the printf ( "\nLinklist:\n");
121     
122     Linklist p=l;
123     
124     while(p->next!=NULL)
125     {
126         printf("%5d",p->next->data);
127         p=p->next;
128     }
129     printf("\n");
130     
131     return OK;
132 }
133 
134 int free_Linklist(Linklist &l)
135 {
136     while(L-> Next =! NULL)
 137      {
 138          Linklist L-Q => Next;
 139          L-> Next = a Q-> Next; // after deleting the header of a node 
140          
141 is          Free (Q); // releasing the node 
142      }
 143      Free (L); // first node also releases 
144      
145      return the OK;
 146  }
 147   
148  int main ()
 149  {
 150      Linklist La;
 151      
152      init_Linklist (La);
 153      
154      the printf ( "\ n Enter linear list A: \ n " );
 155      input_Linklist (La);
 156      
157      
158      the printf ( " \ n linear list A is: \ n " );
 159      
160.      output_Linklist (La);
 161      
162      free_Linklist (La) ; 
 163      
164 is      return  0 ;
 165 }

 

 

PS: this month did not send a blog, do not know doing to go, and thought it a blog or water, this is the inside of the data structure class linear list,

There are list creation, input and output, insert, delete, release and other operations, for reference ......

Guess you like

Origin www.cnblogs.com/xwl3109377858/p/11766994.html