C language Simple implementation of single-linked list of general operations

Source: Internet
Author: User

/** * Author:soarhu verison:1.0 date:2016/05/01 Description: "This is a linkedlist operation ..."*/#include"stdio.h"#include"stdlib.h"typedefstructnode{intvalue; structNode *Pnext;} Node,*Pnode; Pnode Createlinkedlist (void);voidtranverse (Pnode phead);intgetlength (Pnode phead);intInsertnode (Pnode Phead,intPositon,intvalue);intDeletenode (Pnode Phead,intposition);voidAppendlist (Pnode Phead,intvalue);voidGetelem (Pnode Phead,intposition);voidPriorelem (Pnode Phead,intposition);voidNextelem (Pnode phead);intMainintargcChar Const*argv[]) {Pnode Phead= NULL;//define root pointer to point Headnode.Phead =createlinkedlist ();    Tranverse (Phead); if(Insertlinkedlist (Phead,3, the) {printf ("Insert success!");    Tranverse (Phead); }Else{printf ("%s\n","Insert Error"); } printf ("What are wanna to deleted value is%d\n", Deletenode (Phead,4));        Tranverse (Phead);  Free(Phead); return 0;}/** * [createlinkedlist:create the LinkedList] * @return [the pointer to the Headnode]*/Pnode createlinkedlist (void){    intLength =0;//length of list.    intvalue; Pnode Phead= NULL;//The root pointer.Pnode ptail = NULL;//The tail pointer or understand as the current node pointer.Pnode pnew = NULL;//The pointer to point new node.printf ("%s\n","Please input the length of your wanna to create the LinkedList:"); scanf ("%d",&length); Phead=malloc(sizeof(Node)); Phead->pnext =NULL; Ptail=Phead; if(null==phead) {printf ("%s\n","Out of memory"); Exit (-1); } printf ("%s\n","Please input the value of every node");  for(inti =0; i < length; ++i) {scanf ("%d",&value); Pnew=malloc(sizeof(Node)); if(null==pnew) {printf ("%s\n","Out of memory"); Exit (-1); }Else{pnew->value = value;//Set value to the current node.Ptail->pnext = pnew;//In order to previous node point next node.Ptail = pnew;//In order to the tail pointer point new node.Ptail->pnext = NULL;//If is the last node,set the pointer null.        }    }    returnPhead;}/** * [tranverse:tranverse the LinkedList] * @param phead [the pointer to the headnode* (the root pointer)]*/voidtranverse (Pnode phead) {if(NULL = =phead) {printf ("%s\n","The LinkedList is empty"); } pnode pcurrent= phead->Pnext;  while(null!=pcurrent) {printf ("%d", pcurrent->value); Pcurrent= pcurrent->Pnext; } printf ("%s\n","tranverse over!");}/** * [getlength:get length of list] * @param phead [the root pointer] * @return [the length]*/intgetlength (Pnode phead) {intLength =0; if(NULL = = Phead | | null== phead->Pnext) {        return 0; }Else{pnode pcurrent= phead->Pnext;  while(null!=pcurrent) {Length++; Pcurrent= pcurrent->Pnext; }        returnlength; }    };/** * [Insertlinkedlise:insert the value to the LinkedList] * @param phead [the root pointer] * @param positon [the New node wanna to insert Positon] * @param value [the value wanna to insert] * @return [If success return 1,ot Her return 0]*/intInsertnode (Pnode Phead,intPositionintvalue) {    if(position<=0|| Position>GetLength (Phead)) {        return 0; } pnode pcurrent=Phead; Pnode pprevious=NULL; Pnode pnew=NULL;  for(inti =0; i < position; ++i) {pprevious=pcurrent; Pcurrent= pcurrent->Pnext; }    /*Now the pprevious have point to the previous node of position node and the current pointer point to the Positon No    De. */pnew= (Pnode)malloc(sizeof(Node)); Pnew->value =value; Pnew->pnext = pprevious->Pnext; Pprevious->pnext =pnew; return 1;}/** * [deletenode:d elete the node which Locaiton has given] * @param phead [root pointer] * @param position [the P Osition wanna to delete] * @return [return the value of which you deleted]*/intDeletenode (Pnode Phead,intposition) {    intValue =0; if(position<=0|| Position>GetLength (Phead)) {Exit (-1); }Else{pnode pcurrent=Phead; Pnode pprevious=NULL;  for(inti =0; i < position; ++i) {pprevious=pcurrent; Pcurrent= pcurrent->Pnext; } Value= pcurrent->value; Pprevious->pnext = pcurrent->Pnext; Pcurrent=NULL; returnvalue; }}

C language Simple implementation of single-linked list of general operations

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.