数据结构单循环链表
#include <stdio.h>
#include <stdlib.h>

typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode;

LNode *initList()
{
    LNode *list = (LNode *)malloc(sizeof(LNode));
    if(NULL == list)
    {
        printf("malloc list node failed\n");
        return NULL;
    }
    list->data = 0;
    list->next = list;
    return list;
}

void headInsert(LNode *list, int data)
{
    LNode *node = (LNode *)malloc(sizeof(LNode));
    node->data = data;
    node->next = list->next;
    list->next = node;
    return;
}

void tailInsert(LNode *list, int data)
{
    LNode *tail = list;
    LNode *node = (LNode *)malloc(sizeof(LNode));
    node->data = data;

    while(tail->next != list)
    {
        tail = tail->next;
    }
    node->next = list;
    tail->next = node;
    return ;
}

int deleteNode(LNode *list, int data)
{
    LNode *pre = list;
    LNode *node = list->next;
    while (node != list)
    {
        if(node->data == data)
        {
            //delete data node
            pre->next = node->next;
        }
        pre = node;
        node = node->next;
    }

}

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

}

int main()
{
    LNode *list = initList();
    headInsert(list, 0);
    headInsert(list, 1);
    headInsert(list, 2);
    headInsert(list, 3);
    headInsert(list, 4);
    tailInsert(list, 5);
    tailInsert(list, 6);
    printList(list);
    deleteNode(list, 2);
    deleteNode(list, 5);
    printList(list);

}

评论

  1. 博主
    Windows Chrome 120.0.0.0
    已编辑
    2 年前
    2024-1-01 23:30:08

    1

发送评论 编辑评论


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