zoukankan      html  css  js  c++  java
  • C++数据结构之链表一(删除,插入,销毁,返回长度)

    1、头文件

    #pragma once
    #ifndef LINKEDLIST_
    #define LINKEDLIST_
    #include <iostream>
    class Node
    {
    public:
        Node(int a=0, int b=0,Node*p=0) :value1(a), value2(b),next(p){}
        int value1;
        int value2;
        Node *next;
    private:
    
    };
    class LinkedList
    {
    public:
        
        LinkedList():head(nullptr),tail(nullptr){}
        ~LinkedList();
    //结尾插入数据
        void insertTail(int, int);
        //按照第一个值的大小顺序排序
        void Listsort();
        //按序号索引
        Node* findIndex(int el);
        //删除第info个节点
        void deleteNode(int info);
        //销毁链表
        void ClearList(Node* ppHeadNode);
        //返回链表长度
        size_t ListLongth();
        Node *head;
        Node*tail;
    private:
        
    };
    #endif

    2、实现文件

    #include "LinkedList.h"
    LinkedList::~LinkedList()
    {
        ;
    }
    
    void LinkedList::insertTail(int a, int b)
    {
        if (head == nullptr)
        {
            head = new Node(a, b);
        }
        else
        {
            Node*Temp=head;
            while (Temp->next != nullptr)
            {
                Temp = Temp->next;
            }
            Temp->next = new Node(a, b);
            tail = Temp->next;
        }
    }
    
    void LinkedList::Listsort()
    {
        Node *p1 = head;
        Node*p2;
        
        for (p1; p1!= nullptr; p1 = p1->next)
        {
            for (p2 = p1->next; p2 != nullptr; p2 = p2->next)
            {
                
                if (p1->value1 > p2->value1)
                {
                    int temp1=p1->value1;
                    int temp2 = p1->value2;
                    p1->value1 = p2->value1;
                    p1->value2 = p2->value2;
                    p2->value1 = temp1;
                    p2->value2 = temp2;
                }
            }
        
        }
    
    }
    //索引一对儿值
    Node* LinkedList::findIndex(int el)
    {
        Node*temp = head;
        for (int i = 0; i < el; i++)
        {
            temp = temp->next;
        }
        Node*t=new Node(0);
        t->value1 = temp->value1;
        t->value2 = temp->value2;
        
        return t;
    }
    void LinkedList::deleteNode(int el)
    {
        Node*pt = head;
        Node*prev = head;
        //删除头结点,切头结点不为空
        if (el==1&&head->next!=nullptr)
        {
            head = head->next;
            delete prev;
        }
        //头结点为空
        else if (head->next == nullptr)
        {
            delete head;
        }
        //其他
        else
        { 
            for (int i=1;i<el;i++)
            {
            prev = pt;
            pt = pt->next;
            }
            //如果删除的该节点为最后节点
            if (pt->next == nullptr)
            {
                delete pt;
                tail = prev;
            }
            //删除其他节点
            else
            {
                prev->next = pt->next;
            }
        
        }
    }
    void LinkedList::ClearList(Node* Head)
    {
        Node* pListNodeTmp = nullptr;
        if ((Head) == nullptr)
        {
            return;
        }
        // 循环释放链表中的结点所占内存,清空结束后  
        while ((Head)->next != nullptr)
        {
            pListNodeTmp = Head->next;
            delete Head;
            (Head) = pListNodeTmp;
        }
        // 清除最后一个结点  
        if (Head != nullptr)
        {
            delete Head;
            Head = nullptr;
        }
        
    }
    
    //返回链表的长度
    size_t LinkedList::ListLongth()
    {
        int i = 1;
        Node*pt=head;
        while (pt->next!=nullptr)
        {
            pt = pt->next;
            i++;
        }
        return i;
    }
  • 相关阅读:
    图片点击后直接下载
    输入网址到页面呈现,以及首屏加载
    RESTful
    html语义化标签
    git 初学解决错误
    爬虫
    Scrapy安转遇到问题
    前端补充
    django-ORM
    django-web聊天
  • 原文地址:https://www.cnblogs.com/hsy1941/p/11599249.html
Copyright © 2011-2022 走看看