0

我正在练习 LinkedList 并尝试编写函数的实现 delete_last_element()insert_after()interleave()。但是我的代码无法正常工作(即它不能正确地完成主函数中的所有操作)。我一直在 Stack Overflow 中搜索其他类似的帖子,但仍然无法弄清楚原因。任何提示或帮助将不胜感激

#include <iostream>
#include <string> 

using namespace std;

struct Node {
    int key;
    string value;
    Node* next;
}; 

// Pre-condition: The head of a linked list is provided and a key-value 
// pair to insert.
// Post-condition: The linked list now contains that element at the front.
void insert( Node*& head, int key, string value) {

    Node * temp;
    temp = new Node;
    temp->key = key;
    temp->value = value;

    temp->next = head;
    head = temp;

}

// Pre-condition: A linked list is provided.
// Post-condition: The linked list is printed to standard output.
void print( Node* head ) {
    Node* temp = head;
    while( temp != NULL ) {
    cout << "key: " << temp->key << " value: " << temp->value << endl;
        temp = temp->next;
    }
    cout<<endl;
}

// Pre-condition: The head of a linked list is provided.
// Post-condition: The last element of that linked list has been removed.

void delete_last_element( Node*& head )
{
    Node *temp = head;
    if (temp == NULL){
        cout << "The linkedList is empty, no node to delete!!" << endl;
    }

    if(temp->next == NULL){
        delete temp;
        temp = NULL;
        } 

        while(temp->next != NULL) {
            temp = temp->next;
        }
        delete temp->next;
        temp->next = NULL;          
}

// Pre-condition: The head of a linked list is provided, and a key-value
// pair to insert after the indicated key.
// Post-condition: The linked list now contains that element.
void insert_after( Node*& head, int key, int newKey, string value )
{
    Node *node_ptr = head;
    Node *nodeToInsert = new Node;

    nodeToInsert->key = newKey;
    nodeToInsert->value = value;

    while(node_ptr !=NULL){

    if (node_ptr->key == key){
        nodeToInsert->next = node_ptr->next;
        node_ptr->next = nodeToInsert;
    }
    else node_ptr = node_ptr->next;
    }   
}

// Pre-condition: Two linked lists are provided.
// Post-condition: A linked list is returned that is the result of
// interleaving the elements from each list provided (e.g. {1, 2, 3} &
// { 4, 5, 6} would return {1, 4, 2, 5, 3, 6}
Node* interleave( Node*& list1, Node*& list2 )
{
if(list1 == NULL)
    return list2;

if(list2 == NULL)
    return list1;

    Node *x=list1, *y=list2;

    while(x && y){
        Node *tmp = x->next;
        x->next = y;
        y = tmp;
        x= x->next;
    }
    return list1;

}

int main() {

    Node * list1 = NULL;
    Node * list2 = NULL;
    Node * list3 = NULL;
    Node * list4 = NULL;

    insert( list1, 1, "one");
    insert( list1, 2, "two");

    cout << "<1> Linked List 1..." << endl;
    print( list1 );

    insert( list2, 10, "ten");
    insert( list2, 9, "nine");
    insert( list2, 8, "eight");
    insert( list2, 7, "seven");
    insert( list2, 6, "six");

    cout << "<2> Linked List 2..." << endl;
    print( list2 );

    delete_last_element( list1 );
    cout << "<3> Linked List 1..." << endl;
    print( list1 );

    delete_last_element( list1 );
    cout << "<4> Linked List 1..." << endl;
    print( list1 );

    delete_last_element( list1 );
    cout << "<5> Linked List 1..." << endl;
    print( list1 );

    insert(list1, 11, "eleven");
    insert_after(list1, 11, 12, "twelve");
    cout << "<6> Linked List 1..." << endl;
    print( list1 );


    insert_after(list1, 13, 14, "fourteen");
    cout << "<7> Linked List 1..." << endl;
    print( list1 );

    list4 = interleave(list1, list2);
    cout << "<8> Linked List 4..." << endl;
    print( list4 );

    list4 = interleave(list1, list3);
    cout << "<9> Linked List 4..." << endl;
    print( list4 );

    list4 = interleave(list3, list3);
    cout << "<10> Linked List 4..." << endl;
    print( list4 );

    return 0;
}
4

2 回答 2

0

另一个问题:

在 delete_last_element 中:

if (temp == NULL){
        cout << "The linkedList is empty, no node to delete!!" << endl;
    }

if(temp->next == NULL){ ...

你应该return;if temp == null (在你 cout 之后),否则它会尝试检查temp->next == NULL,这将导致段错误。

于 2013-05-30T22:18:04.147 回答
0

我看到一个问题:

    while(temp->next != NULL) {
        temp = temp->next;
    }
    delete temp->next;
    temp->next = NULL; 

在这里,您正在删除一个 NULL 指针。

在函数 insertAfter中,如果我是正确的,您正在更改原始列表头。不要传递引用,一个普通的指针就足够了。

于 2013-05-30T20:29:52.080 回答