Single-linked list initialization, whole table creation, single element insertion, single element deletion, whole table deletion, etc.

Source: Internet
Author: User

Early study of the data structure, put a long time, so that some of the operations are somewhat forgotten, so again to review the data structure, and collated a little notes, put here and everyone to share, my code comments have been very detailed, for error prone places I also have marked, welcome everyone to communicate.

#include"stdafx.h"#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<time.h>#defineOK 1#defineERROR 0//#definetypedefintStatus;//the status value type returned by the functiontypedefintElemtype;typedefstructnode{elemtype data; structNode *Next;} Node;//define a single-linked table storage structuretypedefstructNode *linklist;//defines a linear table that defines a pointer to a node struct//Create a single-chain linear table with n elements, created with a head-interpolation method, and note that the initialization operation is already included//Initialize the list, when the function call is complete, L will point to an empty list, which will change the value of the pointer, so use *L//*l A pointer to a struct-body pointerStatus list_link_create (linklist *l,intN//Head Insertion Method{linklist p; *l = (linklist)malloc(sizeof(Node));//generate a Head node, and make *l point to the head node, so *l is a head pointer, the head pointer is a list of necessary elements    if(L = =NULL)returnERROR; (*l)->next = NULL;//makes the head pointer point to a null, creating a single linked list with a lead nodeprintf"please output n randomly generated numbers:");  for(inti =0; I < n; i++) {p= (linklist)malloc(sizeof(Node));//To generate a new nodeP->data = rand ()% -+1;//randomly generate a number within 100printf"%d", p->data); printf (" ");//spaces between characters and charactersP->next = (*l)Next; (*l)->next = p;//Insert to table header} printf ("\ n");//line Break    returnOK;} Status list_link_length (linklist L)//finding the length of a single linked list{    intj =0;    Linklist p; P=L;  while(P! =NULL) {P= p->Next; J++; } printf ("the current length of the single-linked list =%d", J); returnJ;}//Status List_link_ini (linklist L)//initialize a linear single-linked list//{//L = (linklist) malloc (sizeof (Node));//generate a Head node and make L point to the head node, so L is a head pointer, the head pointer is the necessary element of the list//if (L = = NULL)//return ERROR;//l->next = NULL;//makes the pointer field of the head node empty.//return OK;//}//destroy the linked list L, release the linked list L The requested memory, so that the value of L becomes null again, so it will change the value of L, with *lStatus List_link_destory (linklist *L)    {linklist p,q; P= (*l)Next;  while(p) {Q= p->Next;  Free(P); P=Q; }    (*l)->next = NULL;//the pointer field of the head node is empty    returnOK;} Status List_link_insert (linklist*l,intI, Elemtype e)//inserting a new data element before the I element in L{    intJ;    Linklist p, S; P= *m; J=1;  while(P&&j<i)//Traverse looking for node I{p= p->Next; ++J; }    if(!p | | J >i)returnERROR;//The first element does not exists = (linklist)malloc(sizeof(Node));//generate a new nodeS->data =e; S->next = p->next;//assign the successor of P to the successor of SP->next =s; printf ("The value of the element that inserts the node is:%d\n", E); returnOK;//Insert Successful}//delete the I data element of L and return its value with E//Note that this is *e, not E, which is different from the variable e in the insert.Status List_link_delete (linklist *l,intI, Elemtype *D) {    intj=1;    Linklist p, q; P= *L;  while(P->next&&j<i)//Traverse to find the first element of I{p= p->Next; ++J; }    if(! (P->next) | | J >i)returnERROR;//The first element does not existQ= p->next;//Q Indicates the node of the element that is about to be deleted*e = q->data; P->next = q->next;//the subsequent assignment of Q to the successor of P     Free(q);//Q is released after the use is completedprintf"the element value for removing%d nodes is:%d\n", I, *e); returnOK;} Status List_link_getelem (linklist L,intI, Elemtype *e)//Remove the first element of the single-linked list L, and return via *e{    intJ;    Linklist p; P= l->next;//find the first node in a single-linked list Lj =1;  while(P&&j <i) {p= p->Next; ++J; }    if(!p | | J >i)returnERROR;//The first element does not exist*e = p->data;//Take out the data field of element I and pass the value to *eprintf"The value of the element being removed is:"); printf ("%d\n", *e); returnOK;}intMain ()//test function{linklist L1;    Elemtype f,h; List_link_create (&AMP;L1,6); List_link_getelem (L1,3, &f); List_link_insert (&AMP;L1,3, the);//Insert an element before the 3rd node in the listList_link_delete (&AMP;L1,3, &h);//Delete the element of the 3rd node of the linked list and return to the H output    returnOK;}/*1. For linklist L:l is a pointer to the defined node struct, you can access the struct member by using the-e operator, which is L->elem, and (*l) is a node-type struct that can be accessed with the dot operator, i.e. (*l). Elem;2. For linklist *l:l is a pointer to a defined node struct pointer, so (*L) is a pointer to the node struct body, which can be accessed with the operator, which is (*l)->elem, of course (**L) is a node struct, so you can use the dot operator to access struct members, i.e. (**l). Elem;3. In the chain list operation, we often use the chain table variable crop function parameters, at this time, with linklist L or linklist *l is worth considering, one with bad, The function will have a logic error, the guideline is: if the function will change the value of the pointer L, and you want the function to save the value of L after the call, then you have to use linklist *l, so that the function is passed the pointer to the address, after the end of the call, it is natural to change the value of the pointer; If the function only modifies what the pointer points to, and does not change the value of the pointer, then the linklist L is used .*/


Here are the results of my test in vs2013:

Please output n randomly generated numbers: 42 68 35 1 70 25
The value of the element being removed is: 1
The value of the element that inserts the node is: 15
The element value for removing the 3rd node is: 15
Please press any key to continue ...

Single-linked list initialization, whole table creation, single element insertion, single element deletion, whole table deletion, etc.

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.