我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C语言实现单链表(LinkedList)-创新互联

采用Linux内核链表的实现思想,通过业务节点包含链表节点来将数据串起来。
C语言实现单链表(LinkedList)
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;
}

测试代码:

#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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:C语言实现单链表(LinkedList)-创新互联
网站地址:http://shouzuofang.com/article/jcchj.html

其他资讯