C-Language Linked list section

Source: Internet
Author: User


⒂ linked list ① data to Chain list (stack to heap) #include<stdlib.h>structnode{intdata; structnode*next;};voidMain () {structNode*n1 = (structnode*)malloc(sizeof(structnode)); structNODE*N2 = (structnode*)malloc(sizeof(structnode)); structNode*n3 = (structnode*)malloc(sizeof(structnode)); structnode*p=N1; N1->data= -; N1->next=N2; N2->data= -; N2->next=N3; N3->data= +; N3->next=NULL; while(p!=NULL) {printf ("%d\n",p->data); P=p->Next; }}② traversal # include<stdlib.h>typedefstruct //struct named node{ intdata; structnode*Next;} node;voidPrintall (Node*head)//Head knot Point{node*p=Head; while(p!=NULL) {printf ("%d\n",p->data); P=p->Next; }}voidMain () {node*n1 = (node*)malloc(sizeof(node)); Node*N2 = (node*)malloc(sizeof(node)); Node*n3 = (node*)malloc(sizeof(node)); N1->data= -; N1->next=N2; N2->data= -; N2->next=N3; N3->data= +; N3->next=NULL; Printall (n1);} ③ array to link List # include<stdlib.h>typedefstruct //struct named node{ intdata; structnode*Next;} Node;voidPrintall (Node*head)//Head knot Point{Node*p=Head; while(p!=NULL) {printf ("%d\n",p->data); P=p->Next; }}node*arrtolist (int*arr,intLen//The first address and length of the array, the return value is a pointer to the first address int arr[]{Node*head=NULL; inti; for(i=0; i<len;i++) {printf ("%d\n", Arr[i]); } returnhead;}voidMain () {intmyarr[]={3,4,6,8,0,9,1}; intmylen=sizeof(Myarr)/sizeof(myarr[0]);//Find Array LengthNode*myhead =arrtolist (Myarr,mylen);//The node pointer receives the head node pointer.}④ Increase, delete, change, search # # # # #<stdlib.h>typedefstructNode//structure naming{ intdata; structnode*Next;} Node;voidPrintall (Node*head)//Head knot Point{Node*p=Head; while(p!=NULL) {printf ("%d\n",p->data); P= p->Next; }}node*arrtolist (int* Arr,intLen) {Node*head=NULL; Node*tail=null;//record the last node for easy insertionNode*newnode=NULL; inti; for(i=0; i<len;i++)//looping through an array of elements encapsulates each element into a single node{NewNode= (node*)malloc(sizeof(Node));//Create a nodenewnode->data=arr[i];//assigns a node element to an element in the corresponding position in the group//fill in the datanewnode->next=NULL; if(i==0)//First time creation, first node{Head=NewNode; Tail=NewNode; Continue;//prevent the generation of circular linked lists} tail->next=newnode;//If this node is not placed after the tail nodeTail=newnode;//to create a new tail node } returnHead//return to the linked list header}voidAddNode (Node*head,intPoslval,intVal//linked list to be inserted (to the end of the list), position (inserted after that value), node value to insert{Node*p=Head; Node*newnode= (node*)malloc(sizeof(Node)); while(p!=null&&p->data!=poslval) {P=p->next;}if(p==NULL) {printf ("No, you're looking for a location!\n");return;} NewNode->data=Val;newnode->next=Null;newnode->next =p->next;//new node points to the next node of the original nodeP->next =newnode;//after the new node is placed in P}voidDelnode (Node*head,intDelVal)//that linked list, delete that node{Node*p=Head; Node*q=NULL; while(P->next!=null&&p->next->data!=delval) {P=p->Next; } if(p->next==NULL) {printf ("What did you delete?"); return; } q=p->Next; P->next=p->next->next;//p->next=q->next; Free(q); Q=NULL;}voidUpdatenode (node* Head,intOldval,intnewval) {Node*p=head; while(p!=null&&p->data!=oldval) {P=p->next;}if(p==NULL) {printf ("The node to be modified was not found \ n");return;} P->data=newval;}intQuerynode (Node*head,intVal//0 means no, 1 means there is{Node*p=head; while(p!=NULL) { if(P->data=val) { return 1; } P=p->next;}return 0;}voidMain () {intmyarr[]={3,4,6,8,0,9,1}; intmylen=sizeof(Myarr)/sizeof(myarr[0]); Node*myhead =arrtolist (Myarr,mylen); Printall (Myhead); AddNode (Myhead,6,888);//IncreaseDelnode (Myhead,Ten);//No nodes to be deleted and nodes to be deleted;printf"__________\n"); Updatenode (Myhead,6,999);//Changeprintall (myhead);}/*if (Querynode (myhead,4)) printf ("Exists \ n"), Else printf ("does not exist \ n");}*/Check

C-Language Linked list section

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.