如何从链表中删除这些节点?

时间:2017-02-10 05:01:16

标签: c++ linked-list nodes

当我运行代码时,它不会像我想要的那样删除节点。他们留在名单上。我认为.cpp和头文件可能存在问题,但我不确定。这是我第一次搞乱链接列表,所以任何帮助将不胜感激!代码:

			<p>Click on the shape you would like to play as</p>
			<div class="XO">
				<span id="X" class="X" onclick="userChoiceX()">X</span><span id="O" class="O" onclick="userChoiceO()">O</span>
			</div>

NumberList.cpp:

#include <iostream>
#include "NumberList.cpp"

using namespace std;

int node(){
    int i,j;
    char answer;
    NumberList list; //create a new linked list

    bool MainMenu = true;

    while (MainMenu){
    cout<<"Please make a selection:\n1.Add a starting point\n2.Remove a starting point.\n3.Quit"<<endl;
    cin>>answer;

    if(answer == '1'){
        bool keepGoing = true;
        while (keepGoing){
        cout<<"Enter the row: ";
        cin>>i;
        cout<<"Enter the column: ";
        cin>>j;
        list.add(i,j);
        cout<<"You added "<<i<<"and "<<j<<endl;
        //list.add(4,5);
        //list.add(3,6);
        cout<<"Here is the list: "<<endl;
        list.displayList();
        cout<<"Would you like to add another point? y/n"<<endl;
        cin>>answer;
            if (answer == 'y')
            {
                keepGoing=true;
            }
            else if (answer == 'n')
            {
                keepGoing=false;
            }//end else
        }
    }
    else if (answer == '2'){
        int x,y;
        cout<<"Here are the points chosen: "<<endl;
        list.displayList();
        cout<<"Which point do you want to remove?\nRow: "<<endl;
        cin>>x;
        cout<<"Column: "<<endl;
        cin>>y;
        list.remove(x,y);
        cout<<"Updated list: "<<endl;
        list.displayList();
    }
    else if (answer == '3'){
        MainMenu = false;
    }//end else if
    else{
        cout<<"Unable to process"<<endl;
        }
    }//end keepGoing
}

int main()
{
    node();
}

NumberList.h:

#include "NumberList.h"
using namespace std;

void NumberList::add(int i, int j)
{
    if (head == NULL)
        head = new ListNode(i,j);
    else
    {
        //The node is not empty. Use nodePtr to traverse the list
        ListNode *nodePtr = head;
        while (nodePtr->next != NULL)
            nodePtr = nodePtr->next;

        //nodePtr->next is NULL so nodePtr points to the last node.
        //Create a new node and put it after the last node
        nodePtr->next = new ListNode(i,j);
    }
}

void NumberList::displayList()
{
    ListNode *nodePtr = head; //Start at head of the list
    while(nodePtr)
    {
        //Print the value in the current node
        cout<<"("<<nodePtr->i<<","<<nodePtr->j<<")";
        //Move onto the next node
        nodePtr = nodePtr -> next;
    }
}

void NumberList::remove(int x, int y)
{
    ListNode *nodePtr, *previousNodePtr;

    //if the list is empty, do nothing;
    if (!head) {
        cout<<"The list is empty"<<endl;
        return;
    }
    //int i,j;
    int j;
    //Determine if the first node is the one to delete.
    if (head-> i&&j == x&&y)

    {
        nodePtr = head;
        head = head ->next;
        delete nodePtr;
    }
    else
    {
        //Initialize notePtr to the head of the list;
        nodePtr = head;
        //Skip the nodes whose value member is not number
        while(nodePtr != NULL && nodePtr-> i&&j != x&&y)
            {
                    previousNodePtr = nodePtr;
                    nodePtr = nodePtr->next;
            }
        //Link the previous node to the next node after
        // nodePtr, then delete nodePtr
        if (nodePtr)
        {
            previousNodePtr -> next = nodePtr->next;
            delete nodePtr;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

需要在和符号的两侧添加带有结构指针运算符的方法调用。而不是:

if (head-> i&&j == x&&y)

这样做了:

if (head-> i==x && head->j==y)
相关问题