List full resolution (C language)

First, in the node list storage

       Left node list stored in the data portion, the right portion of the address is a pointer to the successor node. C language definition of a structure type generally to a storage node, as follows:

struct Node 
{ 
    int Data; 
    struce Node * Next; next // node type is a struct node, so the type of the subsequent pointer must also be Node * struct
 };

Second, let's not even get up to the junction

       Want to string together one node, requires three pointer of type struct node *: head (head pointer to the beginning of the list, from the beginning to facilitate traverse the entire list), p (temporary pointer that is not yet connected to the junction point), q (current pointer to the most current string node).

  When the list has not been established, the head pointer is null head.

struct Node * head; 
head = NULL;   // initial head pointer is null

  Now let's create the first node, and points to the node with a temporary pointer p.

struct Node * p; 
p = ( struct Node *) the malloc ( the sizeof ( struct Node));   // the new node dynamically apply a spatial and temporary junction point p to point to this new address 
Scanf ( " % D " , & A) ;   // read data 
p-> data = a; // data stored in the data field of the current node 
P -> Next = NULL;   // set the successor node pointer points to a null current, i.e. the current node the next node is null

  The addition of the new node into the linked list of strings. If the node is the first node created, then the head pointer to this node then the pointer to the current node; if the node is not the first, then the node a pointer to the successor and then modifying the current node pointer to the new node.

IF (head == NULL) 
    head = P;
 the else 
    q -> Next = P; 
q = P; // a final pointer also points to the current node q

Third, establish a linked list traversal and output the complete code

 

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
 };
 int main()
 {
     struct node *head,*p,*q,*t;
     int i,n,a;
     scanf("%d",&n);
     head=NULL;
     for(i=0;i<n;i++){
         p=(struct node *)malloc(sizeof(struct node));
         scanf("%d",&a);
         p->data=a;
         p->next=NULL;
         if(head==NULL)
             head=p;
         else
             q->next=p;
         q=p;
     }
     //输出链表
     t=head;
      while(t!=NULL){
          printf("%d ",t->data);
          t=t->next;
      }
      return 0;
 }

 

 

Fourth, the insertion node

 

Guess you like

Origin www.cnblogs.com/littleLittleTiger/p/11487235.html