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
|
Show 6 more comments
1 Answer
Reset to default 2Your 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
head
, what happens tostart
? – BoP Commented Mar 21 at 14:11remove(head)
you delete instance of lelem at addressstart
, thus makingstart
pointer invalid. Then you try toprint
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 usinghead
temporary but do directlyremove(start);
– Ped7g Commented Mar 21 at 14:25