"C Language-data structure" linear chain-type storage structure

Source: Internet
Author: User

Direct Sticker Code

Header file

#ifndef __linklist_h__#define__linklist_h__typedefvoidLinklist;typedefstruct_tag_linklistnode{linklist*Next;} Linklistnode; Linklist*linklist_create ();voidLinklist_destroy (linklist*pstlist);voidLinklist_clear (linklist*pstlist);intLinklist_lenght (linklist*pstlist);intLinklist_insert (linklist* pstlist, linklistnode* Pstnode,intIPos); Linklistnode* Linklist_get (linklist* pstlist,intIPos); Linklistnode* Linklist_delete (linklist* pstlist,intIPos);#endif/* #ifndef __linklist_h__ */

function implementation

/** * Chained storage for linear tables*/#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include"LinkList.h"typedefstruct_tag_linklist{Linklistnode Header; intlength;} Tlinklist;/**************************************************************************** function Name: LinkList_Create** function: Create a chain-stored linear table * * Function parameter: void** return value: linklist* * * * * Date: February 14, 2017 * * Author: rookie************************************* **************************************/linklist*linklist_create () {tlinklist* Pstret = (tlinklist *)malloc(sizeof(tlinklist)); if(NULL! =Pstret) {Pstret->length =0; Pstret->header.next =NULL; }    returnPstret;}/**************************************************************************** function Name: LinkList_Destroy** function: Destroys a chain-stored linear table * * Function parameter: linklist*** return value: void**** Date: February 14, 2017 * * Author: rookie************************************** *************************************/voidLinklist_destroy (linklist*pstlist) {     Free(pstlist);}/**************************************************************************** function Name: LinkList_Clear** function: Empties a chain-stored linear table * * Function parameter: linklist*** return value: void**** Date: February 14, 2017 * * Author: rookie************************************** *************************************/voidLinklist_clear (linklist*pstlist) {Tlinklist* Pstret = (Tlinklist *) Pstlist; if(NULL! =Pstret) {Pstret->length =0; Pstret->header.next =NULL; }    return;}/**************************************************************************** function Name: LinkList_Lenght** function: Get the length of a chain-stored linear table * * Function parameter: linklist*** return value: int**** Date: February 14, 2017 * * Author: rookie************************************ ***************************************/intLinklist_lenght (linklist*pstlist) {Tlinklist* Pstret = (Tlinklist *) Pstlist; intIRet =-1; if(NULL! =Pstret) {IRet= pstret->length; }    returnIRet;}/**************************************************************************** function Name: LinkList_Insert** function:    Inserting nodes at a specified location in a chain-stored linear table * * Function parameter: linklist* pstlist** linklistnode* pstnode** int ipos** return value: int**** Day Issue: February 14, 2017 * * Author: rookie***************************************************************************/intLinklist_insert (linklist* pstlist, linklistnode* Pstnode,intIPos) {Tlinklist* Psttemp = (Tlinklist *) Pstlist; Linklistnode*pstnodecurrent; intIRet = ((NULL! = psttemp) && (IPos >=0) && (NULL! =Pstnode)); intILoop; if(iRet) {pstnodecurrent= (linklistnode*) Psttemp;  for(ILoop =0; (ILoop < IPos) && (pstnodecurrent->next! = NULL); iloop++) {pstnodecurrent= pstnodecurrent->Next; } Pstnode->next = pstnodecurrent->Next; Pstnodecurrent->next =Pstnode; Psttemp->length++; }    returnIRet;}/**************************************************************************** function Name: LinkList_Get** function:  Get a chain-stored linear table with the specified position node * * Function parameter: linklist* pstlist** int ipos** return value: linklistnode***** Date: February 14, 2017 * * Author: rookie***************************************************************************/Linklistnode* Linklist_get (linklist* pstlist,intIPos) {Tlinklist* Psttemp = (Tlinklist *) Pstlist; Linklistnode* Pstnoderet =NULL; Linklistnode* Pstnodecur =NULL; intILoop; if((NULL! = psttemp) && (IPos >=0) && (IPos <= psttemp->length)) {Pstnodecur= (linklistnode*) Psttemp;  for(ILoop =0; ILoop < IPos; iloop++) {Pstnodecur= pstnodecur->Next; } Pstnoderet= pstnodecur->Next; }    returnPstnoderet;}/**************************************************************************** function Name: LinkList_Delete** function:  Deletes the specified position node of a chain-stored linear table * * Function parameter: linklist* pstlist** int ipos** return value: linklistnode***** Date: February 14, 2017 * * Author: rookie***************************************************************************/Linklistnode* Linklist_delete (linklist* pstlist,intIPos) {Tlinklist* Psttemp = (Tlinklist *) Pstlist; Linklistnode* Pstnoderet =NULL; Linklistnode* Pstnodecur =NULL; intILoop; if((NULL! = psttemp) && (IPos >=0) && (IPos < psttemp->length)) {Pstnodecur= (linklistnode*) Psttemp;  for(ILoop =0; ILoop < IPos; iloop++) {Pstnodecur= pstnodecur->Next; } Pstnoderet= pstnodecur->Next; Pstnodecur->next = pstnoderet->Next; Psttemp->length--; }    returnPstnoderet; }

"C Language-data structure" linear chain-type storage structure

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.