The basic operation of a single list - create lists, insert or delete nodes, etc.

Preface

In this paper include:

Create (1) a single list

(2) create junction

(3) Printing node

(4) is inserted into the list] [interpolation head

(5) delete the list of specified location [Delete]

An initial understanding of the basic operation for novice learning a single list

First, the code:

#include <stdio.h> 
#include <stdlib.h> 
#include < String .h>
 // structure composed ---- node pointer field + data field 
struct the Node {
     int Data; // data field 
    struct the Node Next *; // pointer field 
};
 // Create a list (the header) 
struct the Node * createList () {
     struct the Node HeadNode * = ( struct the Node *) the malloc ( the sizeof ( struct the Node));
     // HeadNode become structure variable
     // before variables must be initialized 
     // headNode-> = Data. 1; //Usually no initialization data 
    headNode-> Next = NULL;
     return HeadNode;      
}
// Create the node 
struct the Node * the createNode ( int Data) {
     struct the Node * the newNode = ( struct the Node *) the malloc ( the sizeof ( struct the Node));
     // initialize new node 
    newnode-> Data = Data;
    newNode->next=NULL;
    return newNode;
 } 
 // print the node (node traversal) 
 void printList ( struct the Node * HeadNode) {
      struct the Node * = headNode- pMove> Next; // print head pointer to the next node of a node 
     the while (pMove) {
         printf("%d\t",pMove->data);
         pMove=pMove->next;    
     }        
     printf("\n");
 }

// list insertion: --- the insertion node is inserted into the linked list, the insertion node number data is 
  void insertNodeByHead ( struct the Node HeadNode *, int Data) {
   // . 1, create an insertion node 
  struct the Node * = the createNode insertNode (Data); // call createNode method creates a new node 
    insertNode-> Next = headNode-> Next;
    headNode->next=insertNode;
} 
// delete the list: delete the specified location --- delete the list, the number of deleted data is 
void deleteNodeByAppoin ( struct the Node * HeadNode, int POSDATA) {
     struct the Node * posNode headNode- => the Next;
     struct the Node * posNodeFront = HeadNode ;
     IF (posNode == NULL)
        the printf ( " list is empty! " ); 
     the else {
         the while (! posNode-> Data = POSDATA) {
            posNodeFront=posNode;
            posNode=posNodeFront->next;
            if(posNode==NULL){
                printf ( " Unable to find the specified location " );
                 return ;
            }            
        }
        posNodeFront->next=posNode->next;
        free(posNode);
    }    
} 
int main () {
     struct the Node * = createList list (); // Create a list called list 
    the printf ( " inserted into the front of the list data: \ n- " );
    printList(list);
    the printf ( " data is inserted in the list: \ n- " );
    insertNodeByHead (list, . 3 ); // the list inserted list data -3 
    insertNodeByHead (list, 2 ); // data to the list -2 list 
    insertNodeByHead (list, . 1 ); // insert data into the list -1 list 
    printList (list );          // Print the list List 
    printf ( " data in a linked list after deleting: \ the n- " );
    deleteNodeByAppoin (List, 2 ); // delete the list of data 2 
    printList (list);
    system("pause");
    return 0; 
}

 

 

Second, the results:

Guess you like

Origin www.cnblogs.com/it1997/p/12054157.html