温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言实现单链表(LinkedList)

发布时间:2020-10-09 11:16:59 来源:网络 阅读:5770 作者:暮色伊人 栏目:编程语言

采用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