链接列表,删除,插入和交错

时间:2013-05-30 19:35:28

标签: c++ linked-list

我正在练习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;
}

2 个答案:

答案 0 :(得分:0)

我看到一个问题:

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

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

在功能insertAfter中,如果我是正确的,您将更改原始列表头。不要传递引用,只需一个普通的指针即可。

答案 1 :(得分:0)

另一个问题:

在delete_last_element中:

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

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

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