C and Pointer notes--research and improvement of the list

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.