简体   繁体   English

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

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

I have implemented a sorted doubly linked list with the help of pointers in C++.I want to display an error message on deleting the last element saying "doubly linked list is empty cannot delete any more elements" and also display a message before deleting the last element saying"the last element in the node are you sure you want to delete it"? 我已经在C ++中借助指针实现了一个已排序的双向链表。我想在删除最后一个元素时显示一条错误消息,说“双向链表为空不能删除更多元素”,并且在删除最后一个链结之前也显示一条消息元素说“您确定要删除节点中的最后一个元素”?

#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 : Defines the entry point for the console application. // 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;
    }

Since you seem to not be using dummy nodes, checking if the list is empty is the same as checking if the Head or Tail pointers are null. 由于您似乎没有使用虚拟节点,因此检查列表是否为空与检查HeadTail指针是否为空是相同的。 Checking if it's only one element is the same as checking if Head->Next is null (be sure to check that Head is not null first, of course). 检查它是否只有一个元素与检查Head->Next是否为空相同(当然,请确保首先检查Head不为空)。
Alternatively, you can maintain a size variable (incremented in insert and decremented in remove) or write a method to calculate the size by traversing the list. 另外,您可以维护一个size变量(在insert中增加,在remove中减少),或者编写一种方法来遍历列表来计算大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM