线性表链式存储

    xiaoxiao2021-03-25  127

    传智课程学习笔记。

    #include <stdlib.h> #include <string.h> #include <stdio.h> #include "linklist.h" typedef struct _tag_LinkList { int length; LinkListNode header; }TlinkList; LinkList* LinkList_Create() { TLinkList* ret = NULL; ret = (TLinkList *)malloc( sizeof(TLinkList)); memset( ret, 0, sizeof(TLinkList)); ret->length = 0; ret->header.next = NULL; return ret; } void LinkList_Destroy( LinkList* list) { if( list != NULL) { free( list); list = NULL; } return ; } void LinkList_Clear( LinkList* list) { TLinkList* tList = NULL; if( list == NULL ) { return ; } tList = ( TLinkList *)list; tList->length = 0; tList->header.next = NULL; return ; } int LinkList_Length( LinkList *list) { TLinkList *tList = NULL; if( list == NULL ) { return -1; } tList = (TlinkList *)list; return tList->length; } int LinkList_Insert( LinkList* list, LinkListNode* node, int pos) { int ret = 0, i=0; LinkListNode *current = NULL; if( list == NULL || node == NULL || pos<0 ) { ret = 0; printf("func LinkList_Insert() err:%d\n", ret); return ret; } tList = ( TLinkList *)list; current = &( tList->header); for( i=0; i<pos&¤t->next!=NULL; i++) { current = current->next; } node->next = currentt->next; current->next = node; tList->length++; return 0; } LinkListNode* LinkList_Get( LinkList* list, int pos) { int ret = 0 ,i=0; LinkListNode *current = NULL; TLinkList *tList = NULL; if( list == NULL || pos<0) { ret = 0; printf("func linklist insert() err:%\n", ret); } tList = (TLinkList *)list; current = &(tList->header); for( i=0; i<pos&&( current->next!=NULL); i++) { current = current->next; } return current->next; } LinkListNode* LinkList_Delete( LinkList* list, int pos) { int i=0; LinkListNode *current = NULL; LinkListNode *ret = NULL; TLinkList *tList = NULL; if( list == NULL || pos<0) { printf("func LinkList_Insert() err:%d\n", ret); return NULL; } tList = (TLinkList *)list; current = &(tList->header); for( i=0; i<pos&&(current->next!=NULL); i++) { current = current->next; } ret = current->next; current->next = ret->next; return ret; }

    转载请注明原文地址: https://ju.6miu.com/read-5113.html

    最新回复(0)