首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinkedList、delete、insertion和insertion

LinkedList、delete、insertion和insertion
EN

Stack Overflow用户
提问于 2013-05-31 03:35:28
回答 2查看 4.6K关注 0票数 0

我正在练习使用LinkedList,并且正在尝试编写函数delete_last_element()insert_after()interleave()的实现。但是我的代码不能正常工作(例如,它不能正确地执行main函数中的所有内容)。我已经在Stack Overflow中搜索了其他类似的帖子,但仍然找不到原因。如有任何提示或帮助,我们将不胜感激

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

发布于 2013-05-31 04:29:52

我看到一个问题:

代码语言:javascript
复制
    while(temp->next != NULL) {
        temp = temp->next;
    }
    delete temp->next;
    temp->next = NULL; 

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

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

票数 0
EN

Stack Overflow用户

发布于 2013-05-31 06:18:04

另一个问题:

在delete_last_element中:

代码语言:javascript
复制
if (temp == NULL){
        cout << "The linkedList is empty, no node to delete!!" << endl;
    }

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

如果临时==为空,您应该执行return;操作(在您输出之后),否则它将尝试检查temp->next == NULL,这将导致分段错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16844492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档