zoukankan      html  css  js  c++  java
  • list的基本操作实现

    有关list的相关实现,主函数没有写很多,每个部分目前没发现有问题;

    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    typedef int ElemType;
    typedef int Status;
    #define OK    1
    #define ERROR 0
    
    struct Node
    {
        ElemType Data;
        Node* Prior;
        Node* Next;
    } ;
    
    struct List
    {
        int length;
        Node* head;
        Node* tail;
        List(int l=0, Node* h=0, Node* t=0):length(l),head(h),tail(t) {}
    } ;
    
    void Pop_Front(List &L);///pop_front() 删除第一个元素
    void Pop_Front(List &L);///pop_front() 删除第一个元素
    void Push_back(List &L, int x);///push_back() 在list的末尾添加一个元素
    void Push_front(List &L, int x);///push_front() 在list的头部添加一个元素
    bool Empty(List L);///empty() 如果list是空的则返回true
    int Back(List L);///back() 返回最后一个元素
    int Front(List L);///front() 返回第一个元素
    void Clear(List &L); ///clear() 删除所有元素
    void Erase(List &L, int x);///erase() 删除一个元素
    
    int main()
    {
        List L;
    
        Push_back(L, 1);
        Push_front(L, 2);
        Push_back(L, 3);/// 2 1 3
        //Push_back(L, 3);/// 2 1 3
        //Push_front(L, 4);/// 2 1 3
    
        int ans = Back(L);
        printf("%d
    ", ans);
    
        int ans1 = Front(L);
        printf("%d
    ", ans1);
    
        Erase(L, 3);
    
        //Pop_back(L);
        //Pop_back(L);
    
        //Clear(L);
    
        ans = Back(L);
        printf("%d
    ", ans);
    
        return 0;
    }
    bool Empty(List L)///empty() 如果list是空的则返回true
    {
        return (L.length == 0);
    }
    
    int Back(List L)///back() 返回最后一个元素
    {
        if(Empty(L))
        {
            printf("链表为空
    ");
            return -1;
        }
        return L.tail->Data;
    }
    int Front(List L)///front() 返回第一个元素
    {
        if( Empty(L) )
        {
            printf("链表为空
    ");
            return -1;
        }
        return L.head->Data;
    }
    void Clear(List &L) ///clear() 删除所有元素
    {
        while(!Empty(L))
        {
            Node* s = L.head;
            L.head = L.head->Next;
            L.length --;
            delete s;
        }
    }
    
    void Pop_back(List &L)///pop_back() 删除最后一个元素
    {
        if(Empty(L))
            return;
        Node* s = L.tail;
    
        L.tail = s->Prior;
        L.tail->Next = NULL;
    
        L.length --;
    
        delete s;
    }
    
    void Pop_Front(List &L)///pop_front() 删除第一个元素
    {
        if(Empty(L))
            return;
        Node* s = L.head;
    
        L.head = s->Next;
        L.head->Next = NULL;
    
        L.length --;
    
        delete s;
    }
    
    void Erase(List &L, int x)///erase() 删除一个元素
    {
        Node* p = L.head;
    
        while(p)
        {
            //printf("%d
    ", p->Data);
            if(p->Data == x)
            {
                if(p->Prior == NULL)
                    Pop_Front(L);
                else if(p->Next == NULL)
                    Pop_back(L);
                else
                {
                    Node* s = p;
                    p->Prior->Next = p->Next;
                    p->Next->Prior = p->Prior;
                    delete s;
                }
            }
            p = p->Next;
        }
    
    }
    void Push_back(List &L, int x)///push_back() 在list的末尾添加一个元素
    {
        Node* s = new Node;
        s->Data = x;
    
        if(Empty(L))
        {
            L.head = s;
            L.tail = s;
        }
        else
        {
            L.tail->Next = s;
            s->Prior = L.tail;
            L.tail = s;
        }
        L.length ++;
    }
    void Push_front(List &L, int x)///push_front() 在list的头部添加一个元素
    {
        Node* s = new Node;
    
        s->Data = x;
        if(Empty(L))
        {
            L.head = s;
            L.tail = s;
        }
        else
        {
            s->Next = L.head;
            L.head->Prior = s;
            L.head = s;
        }
        L.length ++;
    }
    View Code
  • 相关阅读:
    java 自定义线程池
    java 锁
    volatile相关内容
    消息驱动式微服务:Spring Cloud Stream & RabbitMQ
    JVM中的本机内存跟踪
    性能监控: SPF4J介绍
    Spring Batch 入门级示例教程
    使用 Spring Boot Actuator 构建 RESTful Web 应用
    回调“地狱”和反应模式
    Java动态规划
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/6641428.html
Copyright © 2011-2022 走看看