双向链表基本操作

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

typedef struct Node
{
    int value;
    struct Node* prev;
    struct Node* next;
}Node;

Node* CreateNode(int data)
{
    Node* newNode = (Node*)calloc(1, sizeof(Node));
    assert(newNode);
    newNode->value = data;
    return newNode;
}

typedef struct List
{
    struct Node* frontNode;
    struct Node* tailNode;
    int size;
}List;

List* CreateList()
{
    List* list = (List*)calloc(1, sizeof(List));
    if (list == NULL)exit(EXIT_FAILURE);
    return list;
}


void PushFront(List* list, int val)
{
    if (!list)return;
    Node* newnode = CreateNode(val);
    if (list->size == 0)
    {
        list->tailNode = newnode;
    }
    else
    {
        newnode->next = list->frontNode;
        list->frontNode->prev = newnode;
    }
    list->frontNode = newnode;
    list->size++;
}

void PushBack(List* list, int val)
{
    if (!list)return;
    Node* node = CreateNode(val);
    if (list->size == 0)
    {
        list->tailNode = node;
    }
    else
    {
        node->prev = list->tailNode;
        list->tailNode->next = node;
    }

    list->tailNode = node;
    list->size++;
}

void InsertIndex(List* list, int pos, int val)
{
    if (list->size == 0 || (!list))
        return;
    Node* cur = list->frontNode;
    Node* prenode = NULL;
    Node* node = CreateNode(val);
    pos--;
    while (pos != 0)
    {
        prenode = cur;
        cur = cur->next;
        pos--;
    }
    if (cur == NULL)return;
    else if (cur == list->frontNode)
        PushFront(list, val);
    else
    {
        /*prenode->next = node;
        node->next = cur;
        cur->prev = node;
        node->prev = prenode
        */
        cur->prev = node;
        node->next = cur;
        prenode->next = node;
        node->prev = prenode;
    }
}

void PopFront(List* list)
{
    if (!list || list->size == 0)return;
    Node* nextnode = list->frontNode->next;
    free(list->frontNode);
    list->frontNode = nextnode;
    nextnode ? (nextnode->prev = NULL) : (list->tailNode = NULL);
    list->size--;
}

void PopBack(List* list)
{
    if (!list || list->size == 0)return;
    Node* prenode = list->tailNode->prev;
    if (!prenode)
    {
        free(list->tailNode);
        list->tailNode = NULL;
        list->frontNode = NULL;
        list->size--;
    }
    free(list->tailNode);
    list->tailNode = prenode;
    list->size--;
    list->tailNode->next = NULL;
}

void EraseValue(List* list, int val)
{
    if (!list)return;
    Node* cur = list->frontNode;
    Node* prenode = NULL;
    while (cur)
    {
        prenode = cur->prev;
        if ((cur->value == val) && (cur == list->frontNode))
        {
            list->frontNode = list->frontNode->next;
            Node* temp = cur;
            free(temp);
            temp = NULL;
            cur->prev = NULL;
            cur = cur->next;
            list->size--;
        }
        else if (cur->value == val && cur == list->tailNode)
        {
            list->tailNode = list->tailNode->prev;
            free(cur);
            cur = NULL;
            list->tailNode->next = NULL;
            list->size--;
            break;
        }
        else if (cur->value == val)
        {
            Node* temp = cur;
            prenode->next = cur->next;
            cur->next->prev = prenode;
            cur = cur->next;
            free(temp);
            temp = NULL;
            list->size--;
        }
        cur = cur->next;
    }
}


void PrintList(List* list)
{
    if (!list)return;
    Node* cur = list->frontNode;
    while (cur)
    {
        printf("%d\t", cur->value);
        cur = cur->next;
    }
    printf("\n");
}


void test()
{
    List* list = CreateList();
    PushFront(list, 3);
    PushFront(list, 2);
    PushFront(list, 1);

    PushBack(list, 4);
    PushBack(list, 5);

    PrintList(list);

    //PopFront(list);
    //PopBack(list);
    //InsertIndex(list, 5, 88);
   // EraseValue(list,1);
   // EraseValue(list,5);
    EraseValue(list, 3);
    PrintList(list);
}

int main()
{
    test();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值