c++ - Head gets "lost" after removing an element from my linked list - Stack Overflow

This is my uni homework but I am currently stuck. We have to incorporate several different functions in

This is my uni homework but I am currently stuck. We have to incorporate several different functions in our code in order to learn about lists in C++. We didn't use so many libraries yet, that's why we have to keep it simple. The code ran well until I wanted to delete the first element of my list. I think the mistake is that head gets 'lost' after running the remove function and I am helpless. When I change the function to deleting the second element of the list, everything works fine, just the first element just won't work. Maybe you can help me finding the mistake.

#include <iostream>
using namespace std;

struct lelem
{
    int data;
    lelem* next;
};

void print (lelem* head)
{
    if (head == nullptr)
    {
        cout << "Liste ist leer." << endl;
        return;
    }
    
    while (head)
    {
        cout << head->data << " ,";
        head = head->next;
    }
    return;
}

int size (lelem* head)
{
    int size = 0;
    
    while (head)
    {
        size ++;
        head = head->next;
    }
    return size;
}

void insert_back(lelem*& head, int e)
{
    if (!head)
    {
        head = new lelem;
        head->data = e;
        head->next = nullptr;
        return;
    }
    lelem* tmp = head;
    
    while (tmp->next)
    {
        tmp = tmp->next;
    }
    
    lelem* neu = new lelem;
    neu->data = e;
    neu->next = nullptr;
    tmp->next = neu;
    return;
}

void insert_sort(lelem* &head, int d)
{
    lelem* neu = new lelem;
    neu->data = d;
    
    if (!head || d < head->data)
    {
        neu->next = head;
        head = neu;
        return;
    }
    
    lelem* tmp = head;
    
    while (tmp->next)
    {
        if (d < tmp->next->data)
        {
            neu->next = tmp->next;
            tmp->next = neu;
            return;
        }
        tmp = tmp->next;
    }
}

void remove (lelem* &head)
{
    lelem* loesch = head;
    head = head->next;
    delete loesch;
    return;
}

int main ()
{
    lelem* const start = new lelem;
    lelem* head = start;
    
    int* anz = new int;
    
    cout << "Anzahl der Listenelemtete eigeben: ";
    cin >> *anz;
    
    for (int i=0; i<*anz; i++)
    {
        head->data = i+1;
        if(i<*anz-1)
        {
            head->next = new lelem;
            head = head->next;
        }
        else
        {
            head->next = nullptr;
        }
    }
    
    head = start;
    print (head);
    
    head = start;
    cout << "Liste hat " << size(head) << " Elemente." << endl;
    
    head = start;
    cout << "Sortiertes einfügen des Wertes 5" << endl;

    insert_sort(head, 5);
    
    head = start;
    print (head);
    
    head=start;
    remove(head);
    
    head = start;
    print (head);
    
    return 0;
}

This is my uni homework but I am currently stuck. We have to incorporate several different functions in our code in order to learn about lists in C++. We didn't use so many libraries yet, that's why we have to keep it simple. The code ran well until I wanted to delete the first element of my list. I think the mistake is that head gets 'lost' after running the remove function and I am helpless. When I change the function to deleting the second element of the list, everything works fine, just the first element just won't work. Maybe you can help me finding the mistake.

#include <iostream>
using namespace std;

struct lelem
{
    int data;
    lelem* next;
};

void print (lelem* head)
{
    if (head == nullptr)
    {
        cout << "Liste ist leer." << endl;
        return;
    }
    
    while (head)
    {
        cout << head->data << " ,";
        head = head->next;
    }
    return;
}

int size (lelem* head)
{
    int size = 0;
    
    while (head)
    {
        size ++;
        head = head->next;
    }
    return size;
}

void insert_back(lelem*& head, int e)
{
    if (!head)
    {
        head = new lelem;
        head->data = e;
        head->next = nullptr;
        return;
    }
    lelem* tmp = head;
    
    while (tmp->next)
    {
        tmp = tmp->next;
    }
    
    lelem* neu = new lelem;
    neu->data = e;
    neu->next = nullptr;
    tmp->next = neu;
    return;
}

void insert_sort(lelem* &head, int d)
{
    lelem* neu = new lelem;
    neu->data = d;
    
    if (!head || d < head->data)
    {
        neu->next = head;
        head = neu;
        return;
    }
    
    lelem* tmp = head;
    
    while (tmp->next)
    {
        if (d < tmp->next->data)
        {
            neu->next = tmp->next;
            tmp->next = neu;
            return;
        }
        tmp = tmp->next;
    }
}

void remove (lelem* &head)
{
    lelem* loesch = head;
    head = head->next;
    delete loesch;
    return;
}

int main ()
{
    lelem* const start = new lelem;
    lelem* head = start;
    
    int* anz = new int;
    
    cout << "Anzahl der Listenelemtete eigeben: ";
    cin >> *anz;
    
    for (int i=0; i<*anz; i++)
    {
        head->data = i+1;
        if(i<*anz-1)
        {
            head->next = new lelem;
            head = head->next;
        }
        else
        {
            head->next = nullptr;
        }
    }
    
    head = start;
    print (head);
    
    head = start;
    cout << "Liste hat " << size(head) << " Elemente." << endl;
    
    head = start;
    cout << "Sortiertes einfügen des Wertes 5" << endl;

    insert_sort(head, 5);
    
    head = start;
    print (head);
    
    head=start;
    remove(head);
    
    head = start;
    print (head);
    
    return 0;
}
Share Improve this question edited Mar 21 at 17:04 JaMiT 17.3k5 gold badges18 silver badges39 bronze badges asked Mar 21 at 14:06 JadeTateJadeTate 1 11
  • What do you expect to happen when you delete the first element of your list? – Friedrich Commented Mar 21 at 14:10
  • 1 When you update head, what happens to start? – BoP Commented Mar 21 at 14:11
  • @Friedrich I expect that I can print the list, just like before. But when I want to run print after removing the first element, the program crashes. – JadeTate Commented Mar 21 at 14:19
  • the problem is about the start thing, but I just can't figure out how to solve this @BoP. Do I need to set start as a const in the beginning or can I delete this whole part? I guess if I only use head and don't use start at all, it might work – JadeTate Commented Mar 21 at 14:20
  • 1 in remove(head) you delete instance of lelem at address start, thus making start pointer invalid. Then you try to print from there, which fails (luckily for you, it could have silently do something less obvious and you wouldn't maybe notice). In this case you are removing first element, so you need to update also your start to have new start from second element. You can achieve that by not using head temporary but do directly remove(start); – Ped7g Commented Mar 21 at 14:25
 |  Show 6 more comments

1 Answer 1

Reset to default 2

Your head becomes the next element of the linked list through head->next, but in main your start still points to the original head, but this head has been deleted.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744347897a4569798.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信