转载部分数据结构C语言

通用单链表(C语言)

2019-06-18  本文已影响5人  PersisThd

1、头文件c_linklist.h

#include <stdio.h>


typedef struct Node
{
    struct Node* next;
}Node;

typedef struct ListNode
{
    Node node;
    void* data;
}ListNode;

typedef struct LINKLIST
{
    int length;
    ListNode header;
}LINKLIST;

void InitList(LINKLIST*);
int ListEmpty(LINKLIST*);
int ListLength(LINKLIST*);
void GetElem(LINKLIST*, int, void **);
void ListInsert(LINKLIST*, int, void*);
void ListDel(LINKLIST*, int, void **);
void ClearList(LINKLIST*);
void DestroyList(LINKLIST*);

2、操作链表文件c_linklist.c

#include <stdio.h>
#include <stdlib.h>
#include "c_linklist.h"

void InitList(LINKLIST *L)
{
    L->length = 0;
    L->header.node.next = NULL;
}

int ListEmpty(LINKLIST *L)
{
    if(L->length == 1)
        return 1;

    return 0;
}

int ListLength(LINKLIST* L)
{
    return L->length;
}

void GetElem(LINKLIST* L, int pos, void **e)
{
    if(pos < 0 || pos >= L->length)
        return;
    if(L->length == 0)
        return;

    ListNode* pCur = &L->header;

    int i = 0;
    for(i = 0; i < pos+1; i++)
    {
        //pCur->node.next返回一个node类型,因为其首地址与ListNode的首地址重合,强制类型转换后其取的内存区域大小占ListNode
        pCur = (ListNode*)pCur->node.next;
    }
    *e = pCur->data;
}

void ListInsert(LINKLIST* L, int pos, void *e)
{
    if(pos < 0 || pos > L->length)
        return;

    ListNode* pCur = &L->header;
    int i = 0;
    for(i = 0; i < pos; i++)
    {
        pCur = (ListNode*)pCur->node.next;
    }

    ListNode* pNew = (ListNode*)malloc(sizeof(ListNode));
    pNew->node.next = pCur->node.next;
    pNew->data = e;
    pCur->node.next = &pNew->node;

    L->length++;
}

void ListDel(LINKLIST* L, int pos, void **e)
{
    if(pos < 0 || pos >= L->length)
        return;
    if(L->length == 0)
        return;

    ListNode* pCur = &L->header;
    int i = 0;
    for(i = 0; i < pos; i++)
    {
        pCur = (ListNode*)pCur->node.next;
    }

    ListNode* pDel;
    pDel = (ListNode*)pCur->node.next;
    *e = pDel->data;

    pCur->node.next = pDel->node.next;

    free(pDel);
    L->length--;
}

void ClearList(LINKLIST* L)
{
    while(L->length)
    {
        void * tmp;
        ListDel(L, 0, &tmp);
    }
}

void DestroyList(LINKLIST* L)
{
    ClearList(L);
}

3、测试主函数文件main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "c_linklist.h"

int main()
{
    LINKLIST ls;
    InitList(&ls);

    typedef struct student
    {
        char id[20];
        int age;
        char name[50];
    }student;

    student s[2];

    strcpy(s[0].id, "2019888888");
    s[0].age = 20;
    strcpy(s[0].name, "Mike");

    strcpy(s[1].id, "2019999999");
    s[1].age = 22;
    strcpy(s[1].name, "John");

    int i = 0;
    for(i = 0; i < 2; i++)
    {
        ListInsert(&ls, i, (void*)&s[i]);
    }

    for(i = 0; i < 2; i++)
    {
        student* tmp = (student*)malloc(sizeof(student));
        GetElem(&ls, i, (void**)&tmp);

        printf("the num_%d id is %s\n", i, tmp->id);
        printf("the num_%d age is %d\n", i, tmp->age);
        printf("the num_%d id is %s\n", i, tmp->name);
    }

    system("Pause");

    return 0;
}
注意:上述三个文件在同一项目下
上一篇下一篇

猜你喜欢

热点阅读