线性表的链式存储API

    xiaoxiao2026-09-26  12

    函数接口实现如下: </pre><pre name="code" class="cpp"> #include <iostream> #include <cstdlib> #include <cstring> #include "linklist.h" typedef struct _LinkList { int length; LinkListNode header; }TLinkList; //ps:让业务节点包含自己写的链表 LinkList* LinkList_Create() { TLinkList *ret = (TLinkList*)malloc(sizeof(TLinkList)); if (ret == NULL) { return NULL; } memset(ret, 0, sizeof(TLinkList)); ret->header.next = NULL; ret->length = 0; } void LinkList_Destroy(LinkList* list) { if (list == NULL) { return; } free(list); list = NULL; return; } void LinkList_Clear(LinkList* list) { if (list == NULL) { return; } TLinkList * q_tmp = NULL; q_tmp = (TLinkList *)list; q_tmp->length = 0; q_tmp->header.next = NULL; return; } int LinkList_Length(LinkList* list) { int tmp; TLinkList * q_tmp = NULL; if (list == NULL) { return -1; } q_tmp = (TLinkList *)list; tmp = q_tmp->length; return tmp; } //向一个链表LinkList的pos位置处插入新元素node int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) { <span style="white-space:pre"> </span>int i = 0; <span style="white-space:pre"> </span>TLinkList *<span style="white-space:pre"> </span>q_tmp = NULL; <span style="white-space:pre"> </span>q_tmp = (TLinkList *)list; <span style="white-space:pre"> </span>LinkListNode *current = NULL;  <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>if (list == NULL||node == NULL) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>return -1; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>current = &q_tmp->header; <span style="white-space:pre"> </span>//current = (LinkListNode *)q_tmp; <span style="white-space:pre"> </span>for (i = 0; (i < pos) && (current->next != NULL); i++) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>current = current->next; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>node->next = current->next; <span style="white-space:pre"> </span>current->next = node; <span style="white-space:pre"> </span>q_tmp->length++; <span style="white-space:pre"> </span>return 0; } LinkListNode* LinkList_Get(LinkList* list, int pos) { int i = 0; TLinkList * q_tmp = NULL; if (list == NULL) { return NULL; } q_tmp = (TLinkList *)list; LinkListNode *current = NULL; LinkListNode *ret = NULL; if (pos < 0 || pos >= q_tmp->length) { return NULL; } current = &q_tmp->header; for (i = 0; (i < pos) && (current->next != NULL); i++) { current = current->next; } ret = current->next; return ret; } LinkListNode* LinkList_Delete(LinkList* list, int pos) { int i = 0; TLinkList * q_tmp = NULL; if (list == NULL) { return NULL; } q_tmp = (TLinkList *)list; LinkListNode *current = NULL; LinkListNode *ret = NULL; if (pos < 0 || pos >= q_tmp->length) { return NULL; } current = &q_tmp->header; for (i = 0; i < pos && (current->next != NULL); i++) { current = current->next; } ret = current->next; current->next = ret->next; q_tmp->length--; return ret; }
    转载请注明原文地址: https://ju.6miu.com/read-1312333.html
    最新回复(0)