1 // Code by ZZLPP && code for link_list training 2 struct Node 3 {4 int value; 5 struct Node *link; 6 }node; 7 8 // The header file contains only the node's declaration text
1#include <stdio.h>2#include <stdlib.h>3#include <malloc.h>4#include"node.h"5 6Node*creatnode (intvalue);7 voidInsertnode (Node **pointer,intvalue);8 9 voidMain ()Ten { OneNode *p=creatnode (5); ANode **pointer=&p; -Insertnode (Pointer,3); - Do the { -printf"%d\t",p->value); -P=p->link; -} while(p!=NULL); +printf"\ n"); -System"Pause"); + } ANode *creatnode (intvalue) at { -Node *Root; -root= (Node *)malloc(sizeof(Node)); - if(root==NULL) - { - Free(root); inprintf"error\n"); - exit (exit_failure); to } +Root->value=value; -root->link=NULL; the returnRoot; * } $ voidInsertnode (Node **pointer,intvalue)Panax Notoginseng { -Node *Current ; theNode *previous; + Aprevious=NULL; thecurrent=*pointer; + -Node *newnode= (node*)malloc(sizeof(Node)); $ if(newnode==NULL) $ { - Free(newnode); -printf"error\n"); the exit (exit_failure); - }WuyiNewnode->value=value; the while(Current->value<value && current->link!=NULL) - { Wuprevious=Current ; -Current=current->link; About } $ if(current->link==NULL) - { - if(current->value<value) - { Acurrent->link=NewNode; +newnode->link=NULL; the } - Else $ { thenewnode->link=Current ; the*pointer=NewNode; the } the } - Else in { thenewnode->link=Current ; theprevious->link=NewNode; About } the}
And the original book to write some of the discrepancy, in general is to consider the 3 kinds of situations, the first is to consider inserting in the middle of the list, the second is to consider the end of the check, and finally you have to consider the beginning, these three kinds of situations are indispensable, the personal feel that the treatment is more clear.
Piece name "Node.h"
C and Pointer notes--research and improvement of the list