双链表

#ifndef BOTHWAYLIST
#define BOTHWAYLIST
#include <stdio.h>
#include <stdlib.h>

typedef struct NODE
{

    struct NODE* last; //指向前一个节点
    void* data;
    struct NODE* next;//指向后一个节点

}Node;

typedef struct LIST
{
    Node* head; //链表的头部
    Node* tail; //链表的尾部
    int len;
}List;

typedef void(PRINT)(void* data);

Node* InitList(); //初始化链表

void InsertList(List* list, void* data, int pos); //插入链表

void RemoveList(List* list, int pos); //移除pos位置的数据

void PrintList(List* list,PRINT print); //打印数据

void FreeList(List* list); //释放链表

#endif

 

#include "bothwaylist.h"

Node* InitList()
{

    List* list = (List*)malloc(sizeof(List));
    list->head = (Node*)malloc(sizeof(Node));
    list->tail = (Node*)malloc(sizeof(Node));
    list->head->next = list->tail;
    list->head->last = NULL;
    list->head->data = NULL;
    list->tail->last = list->head;
    list->tail->next = NULL;
    list->len = 0;
    return list;

}

void InsertList(List* list, void* data, int pos)
{
    if ( list == NULL || data == NULL )
    {
        printf("链表为空");
        return;
    }
    if ( pos < 0 || pos > list->len )
    {
        printf("插入下标错误\n");
        return;
    }
    Node* newnode = (Node*)malloc(sizeof(Node));
    newnode->data = data;
    Node* pCurrent = list->head;
    for (int i = 0; i < pos; i++)
    {
        pCurrent = pCurrent->next;
    }
    newnode->last = pCurrent;
    pCurrent->next->last = newnode;
    newnode->next = pCurrent->next;
    pCurrent->next = newnode;
    
    list->len++;
    //printf("%d", list->len);
}

void RemoveList(List* list, int pos)
{
    if ( list == NULL )
    {
        printf("链表为空");
        return;
    }
    if (pos < 0 || pos > list->len)
    {
        printf("下标错误\n");
        return;
    }
    Node* pCurrent = list->head;
    for (int i = 0; i < pos; i++)
    {
        pCurrent = pCurrent->next;
    }
    Node* pDel = pCurrent->next;

    pCurrent->next = pCurrent->next->next;
    
    free(pDel);
    list->len--;
}

void PrintList(List* list,PRINT* print)
{
    Node* pCurrent = list->head->next;
    for (int i = 0; i < list->len; i++)
    {
        print(pCurrent->data);
        pCurrent = pCurrent->next;
    }
    pCurrent = list->tail->last;
    putchar('\n');
    while (pCurrent != list->head)
    {
        
        print(pCurrent->data);
        pCurrent = pCurrent->last;
    }
}

void FreeList(List* list)
{
    if (list == NULL)
    {
        printf("链表为空");
        return;
    }
    Node* pCurrent = list->head;
    
    for (int i = 0; i < list->len; i++)
    {
        Node* p = pCurrent;
        pCurrent = pCurrent->next;
        free(p);
    }
}
 

 

#include "bothwaylist.h"

typedef struct MYDATA
{
    int n;
}mydata;
void Print(void* data)
{
    mydata* newdata = (mydata*)data;
    printf("%d    ", newdata->n);
}

int main()
{

    mydata data1;
    mydata data2;
    mydata data3;
    mydata data4;
    mydata data5;

    data1.n = 1;
    data2.n = 2;
    data3.n = 3;
    data4.n = 4;
    data5.n = 5;

    List* list = InitList();
    InsertList(list, &data1, 0);
    InsertList(list, &data2, 1);
    InsertList(list, &data3, 2);
    InsertList(list, &data4, 3);
    InsertList(list, &data5, 4);
    //RemoveList(list, 0);
    PrintList(list, Print);
    //FreeList(list);

    system("pause");
    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值