Previous chapters have been introduced to the order of storage, chain store
Sequential storage: initialize, insert, delete, location
Chain store: initialize, insert, delete, location
Sequential storage: Initialization
strudt student{ int ID;//ID char name [30]; // Name char sex; // Sex int class; // class int age; // Age } student={"01",“zhangsan”,"m","201","20"};
Chain store: Initialization
// create an empty list LinkList InitiateLinkList(){ LinkList head; // head pointer head = malloc (sizeof (node)); // build a dynamic node, it is the head node head->next=null; return head;
Sequential storage: Insert
void InsertSeqlist (SeqList, the DataType x, int i) { // an element x sequence prior to insertion into the table L i-th data element IF (L.length == Maxsize) Exit ( " table is full " ); IF (I < . 1 || I> + L.ength . 1 ) Exit ( " Position Error " ); for (J = L.length; J> = I; J,) // initialize L.data [J] = L.data [J- . 1 ]; // the sequentially shifted L .data [J- 1 ] = x; // element x to the subscript i-1 position L.length ++; // table length plus 1 }
Chain store: Insert
void InsertLinklist (LinkList head, the DataType x, int i) // before the i-th data element is inserted into the head node table to a new value x node { The Node * P, * Q; IF (I == . 1 ) Q = head; the else Q = GetLinklist (head, I- . 1 ); // Get the first data element i-1 node IF (Q == NULL) / / i-1 th nodes absence exit ( "not found insertion position"); the else { P = the malloc ( the sizeof (the Node)); p-> Data = X; // create a new node p-> Next = q-> Next; // new node chain domain points * q successor node q-> = P Next; // modified chain domain * q } }
I is stored sequentially inserted into first insertion movement back behind the insertion position i-1, an empty place on an insert inserted
Chain stores: the insertion node d, d tail first node b is connected to the first node, then the first node d is connected to a tail node, dropping the connection between a to b
Order Storage: Delete
. 1 void DeleteSeqList (SeqList L, int i) { 2 // delete the linear table L i-th data in the nodes . 3 IF (i < . 1 || i> L.length) // Check whether a valid location . 4 Exit ( " illegal position "); . 5 for (i = J; J <L.length; J ++) // index of i-th element. 1-i . 6 L.data [J- . 1 ] = L.data [J ]; // turn left . 7 L.length--; // table length minus 1 . 8 }
Chain store: delete
void DeleteLinklist (LinkList head, int i) // i-th node of the head drop table { The Node * Q; IF (I == . 1 ) Q = head; the else Q = GetLinklist (head, I- . 1 ); // immediate predecessor node to be deleted first find IF ! (Q == NULL && Q-> Next ! = NULL) // if the immediate predecessor node exists puncturing presence Judai { p = Q-> Next; // p points to the node to be deleted Q-> = p-Next> Next; // removed node to be deleted Free (p); // free space has been removed from the junction point p } the else Exit ( "Could not find the node you want to delete"); // node does not exist }
Data storage: Positioning
int LocateSeqlist(SeqList L, DataType x) { int I = 0 ; the while (. (I <length L) && (L.data [I] = x)!) // find the value of x in the nodes in the sequence table I ++ ; IF (I <L.length) return I + . 1 ; // if element x is found, returns the number of elements of the else return 0 ; // not found element value of x, returns 0 } // find the operation table length order table, direct output L. length to
Chain store: Positioning
int LocateLinklist (LinkList head, the Data the Type x) { // find the first head table value equal to the number of nodes x, if such a node exists, returns a value of 0 the Node * P = head; // P work pointer P = p-> Next; // initial first junction point P int I = 0 ; // ID of the representative node i, where the initial value of the while (P =! null && p-> Data = X! ) { // access the list I ++ ; p=p->next; } if(p!=null) return i+1; else return 0;}