• 双向链表


    #include<stdio.h>
    #include<list>
    typedef int ElemType;
    
    enum Status
    {
        OK        = 1,
        NOT_FOUND = 2,
        ERROR     = 3
    };
    
    struct LinkListNode
    {
        LinkListNode():pre(NULL), next(NULL) {}
        ElemType data;
        LinkListNode* pre;
        LinkListNode* next;
    };
    
    struct LinkList
    {
        LinkList():length(0), head(NULL), tail(NULL) {}
        int length;
        LinkListNode* head;
        LinkListNode* tail;
    };
    
    Status Empty(LinkList ls)
    {
        if(ls.length==0)
            return OK;
        return ERROR;
    }
    
    Status LinkList_push_back(LinkList& ls, const ElemType& e)
    {
        LinkListNode* node=new LinkListNode();
        node->data=e;
        ls.length++;
        if(ls.length==1)
        {
            ls.head=node;
            ls.tail=node;
    
            return OK;
        }
    
        node->pre=ls.tail;
        ls.tail->next=node;
        ls.tail=node;
    
        return OK;
    }
    
    Status LinkList_push_front(LinkList& ls,const ElemType& e)
    {
        LinkListNode* node = new LinkListNode();
        node->data = e;
        ls.length ++;
        if( ls.length == 0)
        {
            ls.tail = node;
            ls.head = node;
            return OK;
        }
    
        node->next = ls.head;
        ls.head->pre = node;
        ls.head = node;
    
        return OK;
    }
    
    Status LinkList_insert(LinkList& ls, int i, const ElemType& e)
    {
        LinkListNode* p = new LinkListNode();
        int j=1;
        p=ls.head;
        if(Empty(ls)==1)
        {
            LinkList_push_back(ls, e);
            return OK;
        }
        while(p && j<i)
        {
            p=p->next ;
            j++;
        }
        if(!p)return ERROR;
        LinkListNode* node=new LinkListNode();
        if(i==0)
        {
            LinkList_push_front(ls, e);
            return OK;
        }
        if(i==ls.length)
        {
            LinkList_push_back(ls, e);
            return OK;
        }
        node->data=e;
        node->next=p->next;
        node->pre=p;
        p->next=node;
        p->next->pre=node;
        ls.length++;
        return OK;
    }
    
    Status LinkList_Erase(LinkList& ls, int i, ElemType& e)
    {
        LinkListNode* p = new LinkListNode();
        int j=1;
        p=ls.head;
        if(Empty(ls)==1)return ERROR;
        while(p && j<i)
        {
            p=p->next ;
            j++;
        }
        if(!p)return ERROR;
        LinkListNode* node=new LinkListNode();
        if(i==0)
        {
            node=p;
            e=node->data;
            ls.length--;
            ls.head=node->next;
            delete node;
            return OK;
        }
        node=p->next;
        e=node->data;
        p->next=node->next;
        if(i==ls.length-1)
        {
            ls.tail=node->pre;
        }
        else
            node->next->pre=p;
        ls.length--;
        delete node;
        return OK;
    }
    
    Status LinkList_Find(LinkList ls, int i, ElemType& e)
    {
        LinkListNode* node = new LinkListNode();
        int j=0;
        node=ls.head;
        while(node && j<i)
        {
            node=node->next ;
            j++;
        }
    
        if(!node)return NOT_FOUND;
        e=node->data;
        return OK;
    }
    
    Status LinkList_back(LinkList& ls, ElemType& e)
    {
        if(ls.length==0)
            return NOT_FOUND;
        e=ls.tail->data;
        return OK;
    }
    
    Status LinkList_front(LinkList& ls, ElemType& e)
    {
        if(ls.length==0)
            return NOT_FOUND;
        e=ls.head->data;
        return OK;
    }
    
    Status LinkList_pop_front(LinkList& ls, ElemType& e)
    {
        if(ls.length==0)
            return ERROR;
        LinkListNode* node = new LinkListNode();
        node=ls.head;
        e=node->data;
        ls.head=node->next;
        ls.length--;
        delete node;
        return OK;
    }
    
    Status LinkList_pop_back(LinkList& ls, ElemType& e)
    {
        if(ls.length==0)
            return ERROR;
    
        LinkListNode* node = new LinkListNode();
        node=ls.tail;
        e=node->data;
        ls.tail=node->next;
        ls.length--;
        delete node;
        return OK;
    }
    
    int LinkList_size(LinkList ls)
    {
        return ls.length;
    }
    
    Status LinkList_clear(LinkList &ls)
    {
        LinkListNode* node = new LinkListNode();
    
        while(ls.length)
        {
            ls.length--;
            node=ls.head;
            ls.head=node->next;
            delete node;
        }
        return OK;
    }
    void TraverseList(LinkList ls)
    {
        LinkListNode* node = new LinkListNode();
        node=ls.head;
        while(node)
        {
            printf("%d ", node->data);
            node=node->next;
        }
        printf("
    ");
    }
    
    int main()
    {
        LinkList list;
        int n, num, e;
        int Insert_index, Delete_index, Find_index;
    
        printf("请输入链表中的元素个数:");
        scanf("%d", &n);
        printf("请输入链表中的元素值:");
        for(int i=0; i<n; i++)
        {
            scanf("%d", &num);
            LinkList_push_back(list, num);
        }
        TraverseList(list);
        printf("输出链表的长度:%d
    ", LinkList_size(list));
    
        printf("请输入插入下标和元素:");
        scanf("%d%d", &Insert_index, &e);
        if(LinkList_insert(list, Insert_index, e)==1)
        {
            printf("插入成功,输出改变后的链表:");
            TraverseList(list);
            printf("
    ");
        }
        else
            printf("插入失败
    ");
    
    
        printf("请输入删除元素的下标:");
        scanf("%d", &Delete_index);
        if(LinkList_Erase(list, Delete_index, e)==1)
        {
            printf("删除成功,输出被删除的元素:%d
    ", e);
            printf("输出改变后的链表:");
            TraverseList(list);
            printf("
    ");
        }
        else
            printf("删除失败
    ");
    
        printf("请输入查找元素的下标:");
        scanf("%d", &Find_index);
        if(LinkList_Find(list, Find_index, e)==1)
        {
            printf("查找成功!输出查找到的元素:%d
    ", e);
        }
        else
        {
            printf("查找失败!
    ");
        }
    
        printf("请输出首元素:");
        if(LinkList_front(list, e)==2)
            printf("not_found
    ");
        else
            printf("%d
    ", e);
    
        printf("请输出尾元素:");
        if(LinkList_back(list, e)==2)
            printf("not_found
    ");
        else
            printf("%d
    ", e);
    
        LinkList_clear(list);
        printf("输出链表的长度:%d
    ", LinkList_size(list));
        return 0;
    }
    
    /*
    5
    11 12 23 34 45
    3 9
    2
    3
    */
  • 相关阅读:
    如何添加“写字板”打开方式
    UML类图聚集与组合的区别
    系统调用跟驱动程序中相应函数的参数对应关系
    PHP 判断数据类型
    PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
    java 中的内部类总结
    cross-env使用笔记
    MySQL——约束(constraint)详解
    MySQL数据库--外键约束及外键使用
    Java中Lambda表达式的使用
  • 原文地址:https://www.cnblogs.com/w-y-1/p/6652491.html
Copyright © 2020-2023  润新知