数据结构双链表
#include <stdio.h>
#include <stdlib.h>

typedef struct double_list
{
    int data;
    struct double_list *pre;
    struct double_list *next;
}List;

List *initList()
{
    List *node = (List *)malloc(sizeof(List));
    node->data = 0;
    node->pre = NULL;
    node->next = NULL;
    return node;
}

void headInsert(List *l, int data)
{
    List *node = (List *)malloc(sizeof(List));
    node->data = data;
    node->next = l->next;
    node->pre = l;
    if(l->next)
    {
        l->next->pre = node;
        l->next = node;
    }
    else
    {
        l->next = node;
    }

}

void tailInsert(List *l, int data)
{
    List *node = l;
    List *new = (List *)malloc(sizeof(List));
    new->data = data;
    while (node->next)
    {
        node = node->next;
    }
    new->next = node->next;
    node->next = new;
    new->pre = node;

}

void deleteNode(List *l, int data)
{
    List *node = l->next;
    while(node)
    {
        if(node->data == data)
        {
            node->pre->next = node->next;
            if(node->next)
            {
                node->next->pre = node->pre;
            }

            free(node);
            break;
        }
        node = node->next;
    }
}

void printList(List *l)
{
    List *node = l->next;
    while (node)
    {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main()
{
    List *l = initList();
    headInsert(l, 0);
    headInsert(l, 1);
    headInsert(l, 2);
    headInsert(l, 3);
    tailInsert(l, 4);
    tailInsert(l, 5);
    printList(l);
    deleteNode(l, 2);
    deleteNode(l, 5);
    printList(l);
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇
error: Content is protected !!内容保护!!