C Language: Linked list

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>
structlnode{intdata; structLnode *next;};voidCreatestructLnode *l);//Create a linked listvoidClearlist (structLnode *l);//Clear Linked listvoidPrintstructLnode *l);//Print linked listvoidSearchstructLnode *l,intLOC);//FindvoidDeldata (structLnode *l,intLOC);//DeletevoidInsert (structLnode *l,intLocintdata);//InsertvoidSort_bubble (structLnode *L);voidSort_insert (structLnode *L);intMain () {intLoc; intdata; structLnode *linklist;//Define a head pointer (a description of the list)Create (linklist); Print (linklist); //clearlist (linklist); //print (linklist);/*printf ("Find the first few nodes:"); scanf ("%d", &loc); Search (Linklist,loc); printf ("Delete the first few nodes:"); scanf ("%d", &loc); Deldata (Linklist,loc); Print (linklist); printf ("INSERT into the number of nodes and values:"); scanf ("%d%d", &loc,&data); Insert (Linklist,loc,data); Print (linklist); */ //sort_bubble (linklist); //printf ("After sorting: \ n"); //print (linklist);}/*Direct Insert Sort*/voidSort_insert (structLnode *m) { structLnode *p,*temp; P= l->Next; while(p) {if(P->next->data < p->data) {Temp= p->Next; } }}/*Bubble sort is not a standard bubble sort, because he is not connected to the exchange once the P cycle determines a minimum value*/voidSort_bubble (structLnode *L) { /*p = l->next; Q = p->next; while (p) {while (q) {if (P->data >= q->data) {temp = p->data; P->data = q->data; Q->data = temp; } q = q->next; } p = p->next; if (P==null) return 0; Q = p->next; } */}/*insert data into the LOC location----p is located on the ' Loc-1 ' node*/voidInsert (structLnode *l,intLocintdata) { structLnode *p,*Q; intj =1; P=L; while(P && (J<loc)) {//P is positioned as the ' loc-1 ' data nodep = p->Next; J++; } if(!p | | (j>Loc)) {printf ("out of range \ n"); return-1; } q= (structLnode *) malloc (sizeof(structLnode)); Q->data =data; Q->next = p->next;//the original Loc received the Q backP->next = q;//Q receives ' loc-1 ' behind and becomes Loc return 0;}/*Delete the LOC node:------p is positioned as the ' loc-1 ' data node*/voidDeldata (structLnode *l,intLoc) { structLnode *p,*Q; intj =1; P= L;//p point to head node while(P && (J<loc)) {//P is positioned as the ' loc-1 ' data nodeP=p->Next; J++; } if(!p | | (j>Loc)) {printf ("find out of range \ n"); return-1; } q= p->next;//Q locates the first LOC data nodeP->next = q->next;//Loc+1 received the back of Loc-1.Free (q);//Delete Loc return 0;}/*find the value of the LOC node-----p locates the "LOC" Data node*/voidSearchstructLnode *l,intLoc) { structLnode *p; intj=1; P= l->next;//head Pointer to the first data node from the top point while(P && (j<Loc)) {P= p->Next; J++; } if(!p | | (j>Loc)) {printf ("find out of range \ n"); return-1; } printf ("the value of the%d node is:%d\n",loc,p->data);}/*create a linked header pointer to the head node, with the contents of the header nodes being the length of the linked list*/voidCreatestructLnode *L) { structLnode *rear,*p; intdata; intLength =0; Rear=L; printf ("Enter the value of the element and end with \ " -1\": \ n"); scanf ("%d",&data); while(data+1) {p= (structlnode*) malloc (sizeof(structLnode));//new NodeP->data =data; Rear->next =p; Rear=p; Length++;//linked list length plus 1scanf"%d",&data); } Rear->next =NULL; L->data =length;}/*clear the contents of a linked list*/voidClearlist (structLnode *L) { structLnode *p,*Q; P= l->Next; while(p) {Q= p->next;//Save Next nodeFree (p);//Delete current nodep =Q; } L->next =NULL; return 0;}/*Print the contents of a linked list*/voidPrintstructLnode *L) { structLnode *p; printf ("the contents of the linked list are:"); P= l->next;//head Pointer to the first data node from the top point while(p) {printf ("%d",p->data); P= p->Next; } printf ("the length of the list is:%d\n",l->data); return 0;}

C Language: Linked 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.