91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言實現單鏈表(LinkedList)

發布時間:2020-10-09 11:16:59 來源:網絡 閱讀:5771 作者:暮色伊人 欄目:編程語言

采用Linux內核鏈表的實現思想,通過業務節點包含鏈表節點來將數據串起來。
C語言實現單鏈表(LinkedList)
linkedList.h

#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>

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;
}

測試代碼:

#include<stdio.h>
#include<stdlib.h>
#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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

蕲春县| 工布江达县| 昭觉县| 吐鲁番市| 山阴县| 德州市| 麻城市| 农安县| 大厂| 垦利县| 中宁县| 泰宁县| 轮台县| 泰州市| 绵竹市| 阿克苏市| 苍南县| 宁强县| 石楼县| 福鼎市| 江油市| 府谷县| 卓尼县| 乌兰县| 特克斯县| 邯郸县| 新竹市| 碌曲县| 荥经县| 若尔盖县| 桂阳县| 福清市| 金山区| 隆化县| 雅江县| 永新县| 榆林市| 定陶县| 泾川县| 台州市| 米脂县|