如何显示消息说双向链表为空,无法从中删除元素?

How to display a message saying doubly linked list is empty,cannot delete element from it?

我已经在 C++ 指针的帮助下实现了一个排序双向链表。我想在删除最后一个元素时显示一条错误消息说 "doubly linked list is empty cannot delete any more elements" 并且在删除最后一个元素之前显示一条消息说"the last element in the node are you sure you want to delete it"?

#include "stdafx.h"
#include "DoublyLinkedList.h"

#include<iostream>
using namespace std;

DoublyLinkedList::DoublyLinkedList():Head(nullptr),Tail(nullptr) {

}

DoublyLinkedList::~DoublyLinkedList() {

    Node *current = Head;
    while (current != NULL)
    {
    Node* next = current->Next;   //The objects pointed to by head and tail at the beginning of the destructor are deleted         
        Node* prev = current->Prev;     //  through the current pointer. Then they are deleted again at the                                                               end of the destructor.
        delete current;
        current = next;
        current = prev;
    }



}
void DoublyLinkedList::SortedInsert(const int& new_element) {


    if(new_element !=0){
        Node* np = new Node(new_element);
    if (!Head)
    {
        Head = np;
        Tail = np;
    }
    else if (new_element < Head->Element)
    {
        np->Next = Head;
        Head->Prev = np;
        Head = np;
    }
    else
    {
        Node *cur = Head->Next;
        while ((cur) && (new_element >= cur->Element))
            cur = cur->Next;

        if (cur)
        {
            np->Prev = cur->Prev;
            np->Next = cur;
            cur->Prev->Next = np;
            cur->Prev = np;
        }
        else
        {
            Tail->Next = np;
            np->Prev = Tail;
            Tail = np;
        }
        }
    }
}

void DoublyLinkedList::Delete(const int& del_element)
{
    Node *cur = Head;
    while (cur)
    {
        if (cur->Element == del_element)
        {
            if (cur->Prev)
                cur->Prev->Next = cur->Next;
            if (cur->Next)
                cur->Next->Prev = cur->Prev;
            if (cur == Head)
                Head = cur->Next;
            if (cur == Tail)
                Tail = cur->Prev;
            delete cur;
            break;
        }
        cur = cur->Next;



    }
}

void DoublyLinkedList::traverse_head() {
    Node *t = Head;
    while (t != NULL)
    {
        cout << t->Element << "\t";

        t = t->Next;
    }

    cout << endl;
}

void DoublyLinkedList::traverse_tail() {


    Node *temp = Tail;
    while (temp != NULL) {
        cout << temp->Element << "\t";
        temp = temp->Prev;
    }
}
main.cpp

// Question1.cpp : 定义控制台应用程序的入口点。 //

#include "stdafx.h"
#include"DoublyLinkedList.h"
#include<iostream>

using namespace std;


    int main()
    {

        DoublyLinkedList intlist;

        int i = 0, x=0,delete_elm=0;

        //intlist.SortedInsert(3);
        //intlist.SortedInsert(6);
        //intlist.SortedInsert(7);
        //intlist.SortedInsert(10);
        //intlist.SortedInsert(-1);

        //intlist.traverse_head();

        do
        {
            cout << "\nEnter value of node.Press 0 to stop\n";
            cin >> x;


            if ((!cin) || cin.peek() != '\n') {

                cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(), '\n');
                    cout << "Invalid input, no string allowed!!" << endl;
            }
            else {
                intlist.SortedInsert(x);
                i++;
            }
        } while (x != 0);
        intlist.traverse_head();

        cout << "\nTraversing Doubly Linked List head first\n";
        intlist.traverse_head();
        cout << "\nTraversing Doubly Linked List tail first\n";
        intlist.traverse_tail();
        cout << endl;
        do {
            cout << "Which element do you want to delete? 0 to stop delete operation" << endl;
            cin >> delete_elm;
            cout << endl;
            if ((!cin) || cin.peek() != '\n' || x < 0) {

                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Invalid input, no string allowed!!" << endl;
            }
            else {

                intlist.Delete(delete_elm);
                cout << "\nTraversing Doubly Linked List head first\n";
                intlist.traverse_head();
                cout << "\nTraversing Doubly Linked List tail first\n";
                intlist.traverse_tail();
                cout << endl;
            }
        } while (delete_elm != 0);

        system("pause");
        return 0;
    }

由于您似乎没有使用虚拟节点,因此检查列表是否为空与检查 HeadTail 指针是否为空相同。检查它是否只有一个元素与检查 Head->Next 是否为 null 相同(当然,请务必先检查 Head 是否不为 null)。
或者,您可以维护一个 size 变量(在插入时递增,在删除时递减)或者编写一个方法来通过遍历列表来计算大小。