链接列表插入问题

时间:2015-03-04 18:22:17

标签: c++ linked-list singly-linked-list

我正在尝试使用Linked List,我的insertNode1函数有点问题。该功能应该由用户在输入的节点之后插入新节点。结果没有显示我想要显示的内容。我希望数字5在3之后,但它没有显示我想要它。

#include <iostream>
using namespace std;

class List {
    struct node {
        float data;
        node* next;
    };
    node* head;
public:
    void appendNode(float);
    void insertNode(float);
    void insertNode1(float,float);
    void deleteNode(float);
    void displayList();
    List(){

        head = NULL;
    }
    ~List(){
        node *nodePtr, *nextNode;
        nodePtr = head;
        while (nodePtr != NULL){
        nextNode = nodePtr->next;
        delete nodePtr;
        nodePtr = nextNode; 
        }
    }
};


void List::appendNode(float d)
{
    node *newNode, *nodePtr;
    newNode = new node; 
    newNode->data = d;
    newNode->next = NULL;

    if (!head){
        head = newNode;
    }
    else {
        nodePtr = head;
        while (nodePtr->next){
            nodePtr = nodePtr->next;
        }
        nodePtr->next = newNode;
    }

}

void List::insertNode(float d){
    node *newNode, *nodePtr, *prevNode=NULL;
    newNode = new node;
    newNode->data = d;
    if (head== NULL)
    {
        head = newNode;
        newNode->next = NULL;
    }
    else {
        nodePtr = head;
        while (nodePtr != NULL && nodePtr->data < d){
            prevNode = nodePtr;
            nodePtr = nodePtr->next;
        }
        if (prevNode == NULL){
            head = newNode;
            newNode->next = nodePtr;
        }
        else {
            prevNode->next = newNode;
            newNode->next = nodePtr;
        }
    }
}

void List::insertNode1(float p, float d){
    node *newNode, *nodePtr, *selectedNode=NULL;
    newNode = new node;
    newNode->data = d;
    nodePtr = head;
    selectedNode = nodePtr;
    selectedNode->data = p;

    if (head != NULL){
        head = newNode;
        newNode->next = NULL;
    }
    else {
        while (nodePtr->next != NULL && nodePtr->data != p){
            nodePtr = nodePtr->next;
        }
        if (selectedNode == NULL){
            head = newNode;
            selectedNode->next = NULL;
        }
        else {

            selectedNode->next = newNode;

        }
    }
}

void List::deleteNode(float da){
    node *nodePtr, *previousNode=NULL;
    if (head == NULL){
        return;
    }
    if (head ->data == da){
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }
    else{
        nodePtr = head;
        while (nodePtr != NULL && nodePtr->data != da){
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }
        previousNode->next = nodePtr->next;
        delete nodePtr;
    }

}

void List::displayList(){
    node *nodePtr;
    nodePtr = head;
    while (nodePtr!= NULL){
        cout << nodePtr->data << endl;
        nodePtr = nodePtr->next;
    }
}


int main(){
    List lobj;
    lobj.appendNode(3);
    lobj.appendNode(6.4);
    lobj.appendNode(4.5);
    lobj.appendNode(7.8);
    lobj.insertNode(1.5);
    lobj.displayList();
    cout << "after deleting and adding a new node" << endl;
    lobj.deleteNode(6.4);
    lobj.displayList();
    lobj.insertNode1(3, 5);
    lobj.displayList();
    system("pause");
}

2 个答案:

答案 0 :(得分:0)

void List::insertNode1(float p, float d)函数的开头:

nodePtr = head;
selectedNode = nodePtr;
selectedNode->data = p;

此处,selectedNode指向列表的head,您似乎用值p覆盖该节点的数据,我不禁感到不是什么你打算这样做。

答案 1 :(得分:0)

List::insertNode1内有一些问题。

nodePtr = head;
selectedNode = nodePtr;
selectedNode->data = p;

此处selectedNode指向head,但您将第一个数据值设置为p。

if (head != NULL){
    head = newNode;
    newNode->next = NULL;
}

此部分仅在head不为空时执行。这意味着每次运行时都会删除整个列表。这就是为什么最后一次调用list::displayList只打印5

if (selectedNode == NULL){
    head = newNode;
    selectedNode->next = NULL;
}

此处selectedNodeNULL。首先,您将head设置为新节点,但这不会更改selectedNode。如果您在空列表上运行此方法,您的程序将在此处进行分段。您应该在此处设置headnewNode的下一个到NULL,而不是selectedNode

else {
    selectedNode->next = newNode;
}

此处selectedNode仍然指向head。您更改了nodePtr,但这也不会更改selectedNode