数据结构 3 双链表

数据结构 3 双链表

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

#define TRUE 1
#define FALSE 0 

typedef struct Node{
    int data;
    struct Node* pre;
    struct Node* next;
}Node;

Node* InitaNode()
{
    Node* Head = (Node*)malloc(sizeof(Node));
    Head->data = 0;
    Head->next = NULL;
    Head->pre = NULL;
    return Head;
}

void HeadInsert(Node* list, int data)
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->pre = list;   
    node->next = list->next;
    list->data++;
    if(list->next)    
    {
        list->next->pre = node;
    }
    list->next = node;
}

void TailInsert(Node* list, int data)
{
    Node* CurrentNode = list->next;
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    while(CurrentNode->next != NULL)
    {
        CurrentNode = CurrentNode->next;
    }
    node->next = CurrentNode->next;
    CurrentNode->next = node;
    node->pre = CurrentNode;
    
    list->data++;

}

int DeleteNum(Node* list, int data)
{
    Node* CurentNode = list->next;
    while(CurentNode)
    {
        if(CurentNode->data == data)
        {
            CurentNode->pre->next = CurentNode->next;
            if(CurentNode->next)
                 CurentNode->next->pre = CurentNode->pre;
            list->data--;
            free(CurentNode);
            return TRUE;
        }
        CurentNode = CurentNode->next;
    }
    return FALSE;
}

void PrintList(Node* list)
{
    Node* node = list->next;
    while(node)
    {
        printf("%d->", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

void main(void)
{
    Node* HeadNode = InitaNode();
    for(int i = 0; i < 6; i++)
    {
        HeadInsert(HeadNode, i);
    }
    for(int i = 1; i < 6; i++)
    {
        TailInsert(HeadNode, i);
    }

    DeleteNum(HeadNode,1);
    DeleteNum(HeadNode,0);
    PrintList(HeadNode); 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值