双向链表

双向链表

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

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

    //创建双向链表
    Node * creatList() 
    { 
        Node * head = (Node*)malloc(sizeof(Node)); 
        Node * phead = head;
        Node * cur = NULL; 
        phead->next = NULL;
        phead->pre = NULL;
        int data; 
        scanf("%d",&data); 
        while(data) 
        { 
            cur = (Node*)malloc(sizeof(Node)); 
            cur->data = data;
            phead->next = cur; 
            cur->pre = phead;
            phead = cur;
            scanf("%d",&data);
        } //完成回环
        cur->next = head; 
        head->pre = cur;
        return head;
    }
    //打印链表
    void traverseList(Node *head) 
    { 
        Node *phead = head->next; 
        while (phead != head)
        { 
            printf("%d ",phead->data); 
            phead = phead->next;
        } 
    }
    //尾部打印

    void traverseList1(Node *head) 
    { 
        Node *phead = head->pre; 
        while (phead != head)
        { 
            printf("%d ",phead->data); 
            phead = phead->pre;
        } 
    }

    //尾插法
    Node*endinsert(Node *head,int data) 
    { 
            Node * cur = NULL; 
            cur = (Node*)malloc(sizeof(Node)); 
            cur->data = data;
            Node * phead = head;
            while (phead->next != head)
            {  
                phead = phead->next;
            } 
            phead->next = cur; 
            cur->pre = phead;
            phead = cur;
            cur->next = head; 
            head->pre = cur;    
            return head;

    }

    //头插法
    Node * HeadInsert(Node * head,int data) 
    { 

        Node * cur = NULL; 
        cur = (Node*)malloc(sizeof(Node));
        cur->data = data;
        cur->next = head->next; 
        cur->pre = head;
        head->next = cur;
        cur->next->pre = cur;


        return head;
    }


    //单向查找
    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 * searchList(Node * head,int find) 
{ 
    Node * pClock = head->next; 
    Node * pAntiClock = head->pre;
    while (pAntiClock != pClock->pre)
     { 
         if(pClock->data == find) 
             return pClock;
              if(pAntiClock->data == find)  
              return pAntiClock;
        if(pClock == pAntiClock) 
            return NULL;
        pClock = pClock->next;  
        pAntiClock = pAntiClock->pre;
    } 
    return NULL;
}   



    //根据值位置插入
    Node * insertList(Node * head,int data,int fdata) 
    { 

        Node *phead = searchList(head,fdata);
        if(phead==NULL)
        {
            return NULL;
        }
        Node * cur; 
        cur = (Node*)malloc(sizeof(Node));
        cur->data = data;
        cur->next = phead->next; 
        cur->pre = phead;
        phead->next = cur; 
        cur->next->pre = cur;


        return head;
    }

    //删除
    Node *  deleteList(Node * head,int data) 
    { 
        Node*pfind = searchList(head,data);
        if(pfind == NULL) 
            return NULL;

        else 
        { 
            pfind->pre->next = pfind->next;
            pfind->next->pre = pfind->pre;
            free(pfind);
        }
        return head;
    }



    int main()
    {
        Node*head = creatList();
        head = endinsert(head,100);
        head = endinsert(head,200);
        //head = HeadInsert(head,3);
        //head = insertList(head,99,2);
        //head = deleteList(head,100);
        traverseList1(head);



        system("pause");
        return 0;
    }

双向链表的优点:
优点:双向链表在单链表的基础上增加了指向前驱的指针
功能上双向链表可以完全取代单链表的使用
双向链表的Next,Pre和Current操作可以高效的遍历链表中的所有元素

双向链表的缺点:代码复杂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值