Single-linked list C language implementation (lead node)

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>typedefintElemtype;typedefstructlinknode{elemtype data; structLinknode *Next;} Linknode,*linklist; Linklist Initnull (); Linklist Initlistbyheadinsert ();voidprintlist (linklist L); Linklist Initlistbytailinsert ();intLen (linklist L);intGetelem (linklist L,intIndex, Elemtype *X);intInsertnode (linklist L,intindex, elemtype data);intDeletenode (linklist L,intIndex, Elemtype *X);voidReverse (linklist L);intmain () {}//Create an empty chain listlinklist initnull () {linklist L=malloc(sizeof(Linknode)); L->next =NULL; returnL;}//setting up single-linked list by head interpolationlinklist Initlistbyheadinsert () {intdata;    Linklist temp; Linklist L=malloc(sizeof(Linknode)); L->next =NULL; printf ("Please enter a number,-1 end:"); scanf ("%d", &data);  while(Data! =-1) {Temp=malloc(sizeof(Linknode)); Temp->data =data; Temp->next = l->Next; L->next =temp; printf ("\ n Please continue to enter the number,-1 end:"); scanf ("%d", &data); }    returnL;}//setting up single-linked list by tail interpolationlinklist Initlistbytailinsert () {intdata;    Linklist temp, tail; Linklist L=malloc(sizeof(Linknode)); Tail=L; L->next =NULL; printf ("Please enter a number,-1 end:"); scanf ("%d", &data);  while(Data! =-1) {Temp=malloc(sizeof(Linknode)); Temp->data =data; Temp->next = tail->Next; Tail->next =temp; Tail=temp; printf ("\ n Please continue to enter the number,-1 end:"); scanf ("%d", &data); }    returnL;}//get the value of element I in the list, accept with X, return 1 for Operation success, 0 for failure, index in list starting from 0intGetelem (linklist L,intIndex, Elemtype *y) {linklist temp= l->Next; inti =0; if(Index <0) {printf ("Invalid index location! \ n"); return 0; }     while(Temp! = NULL && i <index) {Temp= temp->Next; I++; }    if(temp = =NULL) {printf ("Invalid index location! \ n"); return 0; }    Else    {        *x = temp->data; return 1; }}//inserts a value in the linked list, index indicates the position of the element after it was inserted, starting at 0, returning 1 indicating successful operation, 0 indicating failureintInsertnode (linklist L,intindex, elemtype data) {    if(Index <0) {printf ("The insertion position is illegal! \ n"); return 0; } linklist Ptr=L; Linklist Temp=NULL; inti =0;  while(Ptr! = NULL && i <index) {PTR= ptr->Next; I++; }        if(Ptr = =NULL) {printf ("The insertion position is illegal! \ n"); return 0; }    Else{Temp=malloc(sizeof(Linknode)); Temp->data =data; Temp->next = ptr->Next; Ptr->next =temp; }}//inserts a value from the linked list, index represents the position of the element, and indexes start at 0, 1 indicates success, 0 indicates failure, and the value of the returned element exists in XintDeletenode (linklist L,intIndex, Elemtype *y) {    if(Index <0) {printf ("Delete location Illegal! \ n"); return 0; } linklist Ptr=L; Linklist Temp=NULL; inti =0;  while(Ptr! = NULL && i <index) {PTR= ptr->Next; I++; }        if(Ptr = = NULL | | Ptr->next = =NULL) {printf ("Delete location Illegal! \ n"); return 0; }    Else{Temp= ptr->Next; *x = temp->data; Ptr->next = temp->Next;  Free(temp); }}//reverse of a single linked listvoidReverse (linklist L) {linklist CurrentNode= l->Next; Linklist NextNode=NULL; L->next =NULL;  while(CurrentNode! =NULL) {NextNode= currentnode->Next; CurrentNode->next = l->Next; L->next =CurrentNode; CurrentNode=NextNode; }}//to find the length of a linked listintLen (linklist L) {linklist temp= l->Next; intLen =0;  while(Temp! =NULL) {Len++; Temp= temp->Next; }    returnLen;}//printing elements in a linked listvoidprintlist (linklist L) {linklist temp= l->Next;  while(Temp! =NULL) {printf ("%d", temp->data); Temp= temp->Next; } printf ("\ n");}

Single-linked list C language implementation (lead node)

Related Article

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.