带头双向链表实现

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

带头双向链表
typedef struct DoublyNode {
    DoublyNode*next;
    DoublyNode*prior;
    int data;
}DoublyNode;

头插创建
DoublyNode* HeadCreateLinkList(){

    DoublyNode*head = (DoublyNode*)malloc(sizeof(DoublyNode));
    head->next = NULL;
    head->prior = NULL;

    for (int i=0;i<10;i++)
    {
        DoublyNode*p = (DoublyNode*)malloc(sizeof(DoublyNode));
        p->data = i;
        p->next = head->next;

        if (head->next!=NULL)
        {
            head->next->prior = p;
        }

        p->prior = head;
        head->next = p;
    }

    return head;
}

尾插创建
DoublyNode* TailCreateLinkList() {

    DoublyNode*head = (DoublyNode*)malloc(sizeof(DoublyNode));
    head->next = NULL;
    head->prior = NULL;

    DoublyNode*s=head;

    for (int i=0;i<10;i++)
    {
        DoublyNode*p = (DoublyNode*)malloc(sizeof(DoublyNode));
        p->data = i;
        p->next =NULL;
        p->prior = s;
        s->next = p;
        s=p;
    }

    return head;
}


插入指定节点前
void InsertNodeLinkList(DoublyNode*head,int elem,int val)
{
    if (head==NULL||head->next==NULL)
    {
        return ;
    }

    DoublyNode*p=head->next;

    while(p!=NULL)
    {
        if (p->data==elem){
            DoublyNode*q = (DoublyNode*)malloc(sizeof(DoublyNode));
            q->data = val;
            q->next  = p;
            q->prior = p->prior;
            if (p->prior!=NULL)
            {
                p->prior->next = q;
            }

            p->prior=q;
        }

        p=p->next;
    }
}

打印
void PrintLinkList(DoublyNode*head){

    if (head==NULL||head->next==NULL)
    {
        return;
    }

    DoublyNode*p=head->next;
    while(p!=NULL)
    {
        printf("%d ",p->data );
        p=p->next;
    }
    printf("\n");
}

长度
int GetLinkListLenth(DoublyNode*head)
{
    if (head==NULL||head->next==NULL){
        return 0;
    }
    DoublyNode*p=head->next;
    int count = 0;
    while(p!=NULL){
        count++;
        p=p->next;
    }

    return count;
}

删除指定节点
void DeleteLinkList(DoublyNode*head,int elem){

    if (head==NULL||head->next==NULL){
        return ;
    }

    DoublyNode*p=head->next;
    while(p!=NULL)
    {
        if (p->data == elem)
        {
            DoublyNode*q = p;
            if (p->prior!=NULL)
            {
                p->prior->next = p->next;
            }

            if (p->next!=NULL)
            {
                p->next->prior = p->prior;
            }

            free(q);
        }

        p=p->next;
    }
}

释放
void DestoryLinkList(DoublyNode*head)
{
    if (head==NULL||head->next==NULL){
        return ;
    }

    DoublyNode*p=head->next;
    while(p!=NULL)
    {
        DoublyNode*q = p;

        head->next=p->next;
        if (p->next!=NULL)
        {
            p->next->prior=p->prior;
        }

        free(q);
        p=p->next;
    }

    //free(head); 释放头
}

//逆置  从第二个开始逆置
void ReverseLinkList(DoublyNode*head)
{
    if (head==NULL||head->next==NULL||head->next->next==NULL){
        return ;
    }

    DoublyNode*p=head->next;
    DoublyNode*q=p->next;

    while(q!=NULL)
    {
        p->next=q->next;
        if (q->next!=NULL)
        {
           q->next->prior=p;
        }

        q->next= head->next;
        q->prior = head;
        if (head->next!=NULL)
        {
            head->next->prior = q;
        }
        head->next = q;
        q=p->next;
    }
}


/*
 //从第一个开始逆置
void ReverseLinkList(DoublyNode*head)
{
    if (head==NULL||head->next==NULL||head->next->next==NULL){
        return ;
    }

    DoublyNode*p=head->next;
    DoublyNode*q;

    while(p!=NULL)
    {
        q=p;
        p->prior->next = p->next;
        if (p->next!=NULL)
        {
           p->next->prior=p->prior;
        }

        p=p->next;

        q->next= head->next;
        q->prior = head;
        head->next->prior = q;
        head->next = q;

    }
}

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值