双向循环列表(C 语言实现)

本文详细介绍了双向链表的各种操作,包括从尾部和头部插入节点、遍历、计算长度、搜索、删除节点、排序及销毁链表。通过具体代码实现,展示了双向链表的灵活运用。
// node.h

#ifndef NODE_H
#define NODE_H

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

// insert a node from tail
ND *createListTail();
// insert a node from head
ND *createListHead();
// insert a node
void insertList(ND *head,int data);
// traval all node using next
void travalListNext(ND *head);
// traval all node using pre
void travalListPre(ND *head);
// calculate the length of list
int lenList(ND *head);
// search a node in list from single direction
ND *searchNodeSdir(ND *head,int search);
// search a node in list from both direction
ND *searchNodeBdir(ND *head,int search);
// delete a node
void deleteNode(ND *node);
// sort list by swap data
void sortSwapData(ND *head,int n);
// sort list by swap pointer
void sortSwapPointer(ND *head,int n);
// destroy list
void destroyList(ND *head);

#endif // NODE_H
// node.c

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

// insert a node from tail
ND *createListTail()
{
    ND *head = (ND *)malloc(sizeof(ND));
    ND *tail = head;
    ND *cur = NULL;
    head->next = NULL;
    head->pre = NULL;

    int data;
    scanf("%d",&data);
    while (data)
    {
        cur = (ND *)malloc(sizeof(ND));
        cur->data = data;
        tail->next = cur;
        cur->pre = tail;
        tail = cur;

        scanf("%d",&data);
    }
    cur->next = head;
    head->pre = cur;
    return head;
}

// insert a node from head
ND *createListHead()
{
    ND *head = (ND *)malloc(sizeof(ND));
    ND *cur = NULL;
    head->next = head;
    head->pre = head;

    int data;
    scanf("%d",&data);
    while (data)
    {
        cur = (ND *)malloc(sizeof(ND));
        cur->data = data;

        cur->next = head->next;
        cur->pre = head;

        head->next = cur;
        cur->next->pre = cur;

        scanf("%d",&data);
    }
    return head;
}

// insert a node
void insertList(ND *head,int data)
{
    ND *cur;
    cur = (ND *)malloc(sizeof(ND));
    cur->data = data;

    cur->next = head->next;
    cur->pre = head;

    head->next = cur;
    cur->next->pre = cur;
}

// traval all node using next
void travalListNext(ND *head)
{
    ND *p = head->next;
    while (p != head)
    {
        printf("The data is %d\n",p->data);
        p = p->next;
    }
}

// traval all node using pre
void travalListPre(ND *head)
{
    ND *p = head->pre;
    while (p != head)
    {
        printf("The data is %d\n",p->data);
        p = p->pre;
    }
}

// calculate the length of list
int lenList(ND *head)
{
    int len = 0;
    ND *p = head->next;
    while (p != head)
    {
        len++;
        p = p->next;
    }
    return len;
}

// search a node in list from single direction
ND *searchNodeSdir(ND *head,int search)
{
    ND *p = head->next;
    while (p != head)
    {
        if(search == p->data)
            return p;
        p = p->next;
    }
    return NULL;
}

// search a node in list from both direction
ND *searchNodeBdir(ND *head,int search)
{
    ND *p = head->next;
    ND *pr = head->pre;

    while (pr != p->pre)
    {
        if(p->data == search)
            return p;
        if(pr->data == search)
            return p;
        if(p == pr)
            return NULL;
        p = p->next;
        p = p->pre;
    }
    return NULL;
}

// delete a node
void deleteNode(ND *node)
{
    if(node != NULL)
    {
        node->pre->next = node->next;
        node->next->pre = node->pre;

        free(node);
    }
}

// sort list by swap data
void sortSwapData(ND *head,int n)
{
    ND *p,*q;
    for(int i=0;i<n-1;i++)
    {
        p = head->next;
        q = p->next;
        for(int j=0;j<n-i-1;j++)
        {
            if(p->data > q->data)
            {
                p->data = p->data ^ q->data;
                q->data = p->data ^ q->data;
                p->data = p->data ^ q->data;
            }
            p = p->next;
            q = q->next;
        }
    }
}

// sort list by swap pointer
void sortSwapPointer(ND *head,int n)
{
    ND *p,*q;
    for(int i=0;i<n-1;i++)
    {
        p = head->next;
        q = p->next;
        for(int j=0;j<n-i-1;j++)
        {
            if(p->data > q->data)
            {
                p->pre->next = q;
                q->pre = p->pre;
                p->next = q->next;
                p->pre = q;
                q->next = p;
                p->next->pre = p;

                q = p->next;
            }
            else
            {
                p = p->next;
                q = q->next;
            }
        }
    }
}

// destroy list
void destroyList(ND *head)
{
    head->pre->next = NULL;
    ND *p = head;
    while (head!=NULL)
    {
        head = head->next;
        free(p);
        p = head;
    }
}
// main.c

#include <stdio.h>
#include "node.h"

int main()
{
    printf("insert a node from tail\n");
    ND *head = createListTail();

//    printf("insert a node from head\n");
//    ND *head = createListHead();

    printf("traval all node using next\n");
    travalListNext(head);

    printf("insert a node\n");
    insertList(head,10);
    travalListNext(head);

//    printf("traval all node using pre\n");
//    travalListPre(head);

    printf("calculate the length of list\n");
    int length = lenList(head);
    printf("The length of list is %d\n",length);

    printf("search a node in list from single direction\n");
    ND *pp = searchNodeSdir(head,10);
    printf("The search node is %d\n",pp->data);

//    printf("search a node in list from both direction\n");
//    pp = searchNodeBdir(head,10);
//    printf("The search node is %d\n",pp->data);

    printf("delete a node\n");
    deleteNode(pp);
    travalListNext(head);

    printf("sort list by swap data\n");
    sortSwapData(head,9);
    travalListNext(head);

//    printf("sort list by swap pointer\n");
//    sortSwapPointer(head,9);
//    travalListNext(head);

    printf("destroy list\n");
    destroyList(head);
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值