采用Linux內(nèi)核鏈表的實(shí)現(xiàn)思想,通過(guò)業(yè)務(wù)節(jié)點(diǎn)包含鏈表節(jié)點(diǎn)來(lái)將數(shù)據(jù)串起來(lái)。
linkedList.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include
#include
#include
typedef struct LinkedListNodeStruct {
struct LinkedListNodeStruct *next;
}LinkedListNode;
typedef struct LinkedListStruct {
LinkedListNode header;
int length;
}LinkedList;
LinkedList* LinkedList_Create();
void LinkedList_Destory(LinkedList* list);
void LinkedList_Clear(LinkedList* list);
int LinkedList_Length(LinkedList* list);
int LinkedList_Insert(LinkedList* list, LinkedListNode* node, int pos);
LinkedListNode* LinkedList_Get(LinkedList* list, int pos);
LinkedListNode* LinkedList_Delete(LinkedList* list, int pos);
#endif // !LINKED_LIST_H
linkedList.c
#include"linkedList.h"
LinkedList* LinkedList_Create() {
LinkedList *list = malloc(sizeof(LinkedList));
memset(list, 0, sizeof(LinkedList));
list->length = 0;
list->header.next = NULL;
return list;
}
void LinkedList_Destory(LinkedList* list) {
if (list != NULL) {
free(list);
list = NULL;
}
}
void LinkedList_Clear(LinkedList* list) {
if (list != NULL) {
list->length = 0;
list->header.next = NULL;
}
}
int LinkedList_Length(LinkedList* list) {
if (list != NULL) {
return list->length;
}
}
int LinkedList_Insert(LinkedList* list, LinkedListNode* node, int pos) {
if (list == NULL || pos < 0 || node == NULL) {
return -1;
fprintf(stderr,"Wrong argument.\n");
}
LinkedListNode* cur = &list->header;
int i = 0;
while (i < pos && cur->next != NULL) {
cur = cur->next;
i++;
}
node->next = cur->next;
cur->next = node;
list->length++;
return 0;
}
LinkedListNode* LinkedList_Get(LinkedList* list, int pos) {
if (list == NULL || pos < 0) {
return NULL;
fprintf(stderr, "Wrong argument.\n");
}
LinkedListNode* cur = &list->header;
int i = 0;
while (i < pos && cur->next != NULL) {
cur = cur->next;
i++;
}
return cur->next;
}
LinkedListNode* LinkedList_Delete(LinkedList* list, int pos) {
if (list == NULL || pos < 0) {
return -1;
fprintf(stderr, "Wrong argument.\n");
}
LinkedListNode* cur = &list->header;
int i = 0;
while (i < pos && cur->next != NULL) {
cur = cur->next;
i++;
}
LinkedListNode* r = cur->next;
cur->next = cur->next->next;
list->length--;
return r;
}
測(cè)試代碼:
#include
#include
#include"linkedList.h"
typedef struct BusinessNodeStruct {
LinkedListNode node;
int id;
char name[30];
}BusinessNode;
int main() {
LinkedList* list = LinkedList_Create();
BusinessNode t1 = {NULL,21,"Beijing1"};
BusinessNode t2 = { NULL,31,"Beijing2" };
BusinessNode t3 = { NULL,41,"Beijing3" };
BusinessNode t4 = { NULL,51,"Beijing4" };
LinkedList_Insert(list, (LinkedListNode*)&t2, 0);
LinkedList_Insert(list, &t3.node, 0);
LinkedList_Insert(list, (LinkedListNode*)&t4, 0);
for (int i = 0; i < list->length; i++) {
BusinessNode* bNode = (BusinessNode*)LinkedList_Get(list,i);
printf("bNode->name:%s,bNode->id:%d\n",bNode->name,bNode->id);
}
LinkedList_Delete(list, 1);
printf("-----------after delete------------");
for (int i = 0; i < list->length; i++) {
BusinessNode* bNode = (BusinessNode*)LinkedList_Get(list, i);
printf("bNode->name:%s,bNode->id:%d\n", bNode->name, bNode->id);
}
system("pause");
return 0;
}
linkedList.c
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。