C语言双向链表

包含双向链表的创建,插入,遍历,查找,排序,删除,销毁。

#include <stdio.h>

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

Node *creatList()
{
    Node *head = (Node*)malloc(sizeof(Node));
    head->next = head;
    head->pre = head;
    return head;
}

void inseartList(Node *head,int data)
{
    Node *cur = (Node*)malloc(sizeof(Node));
    cur->data = data;
    cur->next = head->next;
    head->next = cur;
    cur->pre = head;
    cur->next->pre = cur;
}

void traverlist(Node *head)
{
    Node *phead = head->next;
    while (phead != head)
    {
        printf("%d\n",phead->data);
        phead = phead->next;
    }
}
int strlenlist(Node *head)
{
    int len = 0;
    Node *phead = head->next;
    while(phead != head)
    {
        len++;
        phead = phead->next;
    }
    return len;
}

Node *searchlist(Node *head, int find)
{
    Node *phead = head->next;
    while(phead != head)
    {
        if(phead->data == find)
            return phead;
        phead = phead->next;
    }
    return NULL;
}

Node *doublesearchlist(Node *head,int data)
{
    Node *pdowm = head->next;
    Node *pup = head->pre;
    while(pdowm->pre != pup)
    {
        if(pdowm->data == data)
            return pdowm;
        if(pup->data == data)
            return pup;
        if(pdowm == pup)
            return NULL;
        pdowm = pdowm->next;
        pup = pup->pre;
    }
    return NULL;
}
void deletelist(Node *pfind)
{
    if(pfind == NULL)
        return ;
    else
    {
        pfind->next->pre = pfind->pre;
        pfind->pre->next = pfind->next;
        free(pfind);
    }
}

void destrylist(Node *head)
{
    head->pre->next = NULL;
    Node *pre = head;
    while(head)
    {
        head = head->next;
        free(pre);
        pre = head;
    }
}

void sqrtlist(Node *head,int n)
{
    Node *q;
    Node *p;
    for(int i=0;i<n-1;i++)
    {
        q = head->next;
        p = q->next;
        for(int j=0;j<n-1-i;j++)
        {
            if(p->data < q->data)
            {
                p->data ^= q->data;
                q->data ^= p->data;
                p->data ^= q->data;
            }
            q = q->next;
            p = p->next;
        }
    }
}

int main(int argc, char *argv[])
{
    Node *head = creatList();
    for(int i= 0;i<10;i++)
    {
        inseartList(head,i);
    }
    sqrtlist(head,10);
    traverlist(head);
    printf("---------------------------\n");
    printf("%d\n",strlenlist(head));
    Node *pfind = doublesearchlist(head,5);
    printf("%d\n",pfind->data);
    deletelist(pfind);
    traverlist(head);
    destrylist(head);
    printf("Hello World!\n");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值