I've created a Hash-table and I want to remove a node from the linked-list. The code works for removing the first node but not for removing others.
void intHashTable::remove(int num){
int location = ((unsigned)num) % size;
Node * runner = table[location];
int checker;
if(runner->next == NULL){
if(num == table[location]->num){
table[location] = NULL;
}
}else{
if(table[location]->num == num){
table[location] = table[location]->next;
}else{
//This part doesn't seem to be working.
Node *temp = runner->next;
while(temp != NULL){
if(temp->num == num){
runner->next = temp->next;
delete(temp);
break;
}
}
}
}
}
You haven't updated temp to point to the next item within the loop:
temp = temp->next;
You also appear to represent an empty row with a NULL pointer in your table, but you don't handle this case properly in your code - if runner is NULL then you'll crash when you try to access runner->next in the first check. Also, you're failing to delete the node in some cases.
To fix these issues, you can update your code to something like this:
void intHashTable::remove(int num)
{
int location = ((unsigned)num) % size;
Node * runner = table[location];
if (runner != NULL) {
if (runner->num == num) {
delete runner;
table[location] = NULL;
} else {
while (runner->next != NULL) {
if (runner->next->num == num) {
Node *temp = runner->next;
runner->next = runner->next->next;
delete temp;
break;
}
runner = runner->next;
}
}
}
}
Also note that I've removed the brackets from delete, which is a C++ keyword and not a function.
If you use doubly-linked lists (i.e. with a previous pointer as well as a next) then you can simplify this code a little, although for something like a hash table where you only tend to iterate through in one direction it's probably not worth the expense of the extra pointer (an extra 8 bytes per item on a 64-bit system).
You didn't updated temp and runner variables inside loop:
while(temp != NULL)
{
if(temp->num == num)
{
runner->next = temp->next;
delete temp;
break;
}
runner = temp; // Keep previous element to change its next pointer when num found
temp = temp->next; // Advance current pointer to next element
}
Related
So I'm trying to insert nodes into linked list in descending order, but I struggle when I'm getting duplicate numbers and cant find a good solution for the problem. I either encounter missing numbers / program crash or program lists only 1 number infinite times.
Here is my code that I think works up to the "else" statement, it's the part that I cant figure out and im just leaving my last version, which doesnt work obviously
void Link::insert(int number) {
Node *news = new Node;
news->number = number;
if(first == NULL) {
first = news;
}
if(news->number > first->number) {
Node *temp = first;
first = news;
news->next = temp;
} else {
Node *temp = first;
while (temp->next || news->number < temp->number) {
temp=temp->next;
}
temp->next = news;
news->next = temp->next;
}
}
If the other functions are needed or my main.cpp please let me know.
Maybe
void Link::insert(int number){
Node *news = new Node;
news->number = number;
if(first == NULL){
first = news;
return;
}
for(Node *i=first, *pred=NULL;!i;i=i->next){
if(i->number<number){
if(i==first) {
news->next=first;
first=news;
} else {
pred->next=news;
news->next=i;
}
break;
}
pred=i;
}
}
When you are first inserting, it goes into your first if condition and then sets first=news, but after that its again checking news->number > first->number which will be false, so going into the else condition unnecessarily. So either add return; in first if block or put others in else block.
keep track of previous element
else{
Node *temp=first,*prev=null;
while (temp && (temp->next || news->number < temp->number)){
prev=temp;
temp=temp->next;
}
if(prev==null){
news->next=first;first=news;
}
else{
prev->next=news;news->next=temp;
}
}
You should swap your 2 last lines, else you have news->next = news, creating a cycle.
Anyway, I suggest to split the function in 2 (private) parts: One which found the Node* where to insert after (or nullptr for first position), and the method for the insertion (and it is so easier to debug).
Node* Link::upper_bound(int value) const
{
if (first == nullptr || first->number <= value) {
return nullptr;
}
Node* node = first;
Node* next = first->next;
while (next && value < next->number) {
node = next;
next = node->next;
}
return node; // we have: node->number < value && (next == nullptr || value <= next->number)
}
void Link::insert_after(Node* node, int value)
{
Node* new_node = new Node(value);
if (node == nullptr) {
new_node->next = first;
first = new_node;
} else {
new_node->next = node->next;
node->next = new_node;
}
}
and finally:
void Link::insert(int number) {
insert_after(upper_bound(number), number);
}
I am trying to go through a singly linked list and delete all nodes that contain a certain char. I have been trying to fix a pointer error (it said glibc detected: double free or corruption) by looking up online and trying those suggestions but now I seem to be stuck in a loop after rewriting my original code. I think the I am at least stuck in the 3rd while loop, I have tried using and if/else statement but the same thing happens. I also get a segmentation fault sometimes.
Code:
int MonsterList::removeMonsterType(char monster){
if(!isalpha(monster)) return 0;
if(first == 0) return 0;
int count = 0;
char key = toupper(monster);
MonsterNode *prev = first; //first is the ptr in the list this function is called on
if(prev->next == 0){
while(prev->id == key || prev->id == key+32){
first = 0;
delete prev; prev = 0;
count++;
return count;
}
return count;
}
MonsterNode *temp = prev->next;
while(temp != NULL){
while(prev->id == key || prev->id == key+32){
first = temp;
delete prev;
prev = temp;
temp = temp->next;
}
while(temp->id == key || temp->id == key+32){
prev->next = temp->next;
delete temp;
temp = prev->next;
count++;
}
prev = temp;
temp = temp->next;
}
return count;
}
Thanks in advance for your help.
No wonder you're having such trouble. You only need a single for loop. Allow me to simplify your code [please pardon some gratuitous style edits]. Note this is untested and may not be perfect:
int
MonsterList::removeMonsterType(char monster)
{
if (!isalpha(monster))
return 0;
// first is the ptr in the list this function is called on
if (first == 0)
return 0;
int count = 0;
char key = toupper(monster);
MonsterNode *prev;
MonsterNode *cur;
MonsterNode *next;
prev = NULL;
for (cur = first; cur != NULL; cur = next) {
// grab this _first_ so we have it before cur may be deleted (e.g. if
// cur gets deleted cur->next is immediately invalid, but this will
// remain)
next = cur->next;
if ((cur->id == key) || (cur->id == (key + 32))) {
// link previous item to next item
if (prev != NULL)
prev->next = next;
// replace first item in list
else
first = next;
// delete the node and advance the count
delete cur
count++;
continue;
}
// remember previous item
prev = cur;
}
return count;
}
I'm trying to delete the n'th element, which is a random number from 1 to n.
My code does this fine (correct element is deleted and surrounding elemnts are connected) but when it comes to being efficient, it is crashing when I un-comment the line delete (nodeToRemove); and I'm not sure why. Does anyone have any insight?
Assuming my struct looks like :
struct Node {
int data; // The data being stored at the node
Node *next; // Pointer to the next node
};
//------------------------------------------------------------------------------
void deleteNthElement (Node * & head, Node * &temp, int random)
{
temp = head;
Node *nodeToRemove;
if (random == 1)
{
nodeToRemove = temp;
head = head->next;
}
else
{
for (int i = 1; i < random - 1; i++)
temp = temp->next;
nodeToRemove = temp->next;
temp->next = temp->next->next;
}
// delete (nodeToRemove); <----- uncommenting this leads to crash,
}//end deleteNthElement()
//------------------------------------------
int main()
{
Node *head = NULL;
Node *temp;
Node *listarray[n[i]];
int n[] = {1000, 5000, 9000, 105000, 400000, 500000, 505000, 800000, 995000, 1000000};
for (int j = 0; j < n[i]; j++)
listarray[j] = new (Node);
//start timer
begin = clock();
//fill it
for (int j = 0; j < n[i]; j++)
{
listarray[j]->data = (rand() % n[i] + 1);
insertNodeInOrder (head, temp, listarray[j]);
}
//delete it
for (int j = 0; j < n[i]; j++)
deleteNthElement (head, temp, (rand() % (n[i] - j) + 1));
//deallocate
for (int j = 0; j < n[i]; j++)
delete listarray[j];
delete *listarray;
//end timer
}
You're picking the wrong Node, you want:
...
nodeToRemove = temp;
...
At least, you have to check for the end of the list, that is, you need to avoid access to null pointers (i really hope you set next to 0 at the end of the list). If you add the allocation parts, I will extend my answer.
void deleteNthElement (Node * & head, Node * &temp, int random)
{
temp = head;
Node *nodeToRemove;
if (random == 1)
{
nodeToRemove = temp;
if(head != 0)
head = head->next;
} else {
for (int i = 1; i < random - 1; i++)
if(temp != 0)
temp = temp->next;
else throw 1; // no such index; throw something more verbose :)
if(temp == 0)
throw 1; // same situation as above
nodeToRemove = temp->next;
if(nodeToRemove == 0)
throw 1; // same situation as above
temp->next = temp->next->next;
}
if(nodeToRemove == 0)
throw 1; //no such index; ... as the above 3
delete nodeToRemove;
}//end deleteNthElement()
Some clean-up first:
Why pass in two pointers here? do you need the value of temp out? If so why not return it?
Also why the node * & temp? (I can see why it is done for head).
int random should probably be called something like index as it describes the functionality better (as far as the function is concerned, there is nothing random about it).
The same with temp.
I propose:
void delete_element (Node* &head, int index)
{
Node* parent_node = head;
Node* node_to_remove;
//...
We don't need temp if we are removing the head. Also, we generally 0 index things, so we should reflect that too. Thus it becomes:
if (index== 0)
{
node_to_remove= head;
head = head->next;
}
Now we get to the main bit. The loop is just there to step through to the parent node of the node to delete, so we can simplify it a little and add checks to make sure we can't 'fall off' the end of the list.
We then have to make sure there is a node to remove, (so another check). We don't need to check for a node beyond as assigning nullptr isn't a problem (I am assuming that an invalid pointer is set to nullptr here):
{
while(--index && parent_node->next){ //pre-decrement means we stop before the one we want (parent)
parent_node = parent_node->next;}
if (parent_node->next){node_to_remove= parent_node->next;}
else {return;} //no point deleting it if it doesnt exist
parent_node->next = node_to_remove->next;//less indirection is always good. Ok if this is nullptr
}
Incidentally, this fixes a probable off by one error. Which is probably your problem (did it crash every time? only when deleting the last element? next to last?
Now we just need to delete it.
Putting it all together:
void delete_element (Node* &head, int index)
{
Node* parent_node = head;
Node* node_to_remove;
if (index== 0)
{
node_to_remove= head;
head = head->next;
}
else
{
while(--index && parent_node->next){ //pre-decrement means we stop before the one we want (parent)
parent_node = parent_node->next;}
if (parent_node->next){node_to_remove= parent_node->next;}
else {return;} //no point deleting it if it doesnt exist
parent_node->next = node_to_remove->next;//less indirection is always good. Ok if this is nullptr
}
delete node_to_remove;
return;
}
And that should work fine. The checks will prevent us dereferencing null pointers which was (probably) what caused you to crash. Can't tell without full code.
I can't delete my first node and it will crash when I insert the number that no in list. Please help.
Here is my declare:
struct node
{
char student_name[40];
char student_gender[40];
char student_course[40];
int student_number;
struct node *next;
struct node *prev;
}*newnode, *studentlist,*temp;
char name[40];
char gender[40];
char course[40];
int number = 0;
char selection[4];
int s;
int a;
char student_number[20];
Here is my code:
void delete_student(){
temp=studentlist;
printf("Enter the number of student to delete\n");
fgets(student_number,20,stdin);
a = atoi (student_number);
if (studentlist == NULL)
printf("list is empty\n");
else if (a==studentlist->student_number)
studentlist=studentlist->next;
else
{
temp=studentlist;
while(a != temp->student_number)
{
temp->prev=temp;
temp=temp->next;
}
temp->prev->next=temp->prev->next->next;
}
printf("Delete success\n");
printf("Press any key to go back main menu\n");
getch();
}
Another problem is when I make a deletion, my show list from back will also bug. It continues non stop loop. How can i fix it too?
void show_list_from_last(){
temp = studentlist;
if (studentlist == NULL)
printf("List is empty\n");
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
do
{
printf("student name = %s", temp->student_name);
printf("student gender = %s", temp->student_gender);
printf("student course = %s", temp->student_course);
printf("Student Number = %d\n\n", temp->student_number);
temp = temp->prev;
} while (temp != NULL);
}
printf("\n\nPress any key to go back main menu\n");
getch();
}
Issues that I see:
You have:
temp=studentlist;
I didn't see the type specifier for temp.
You have:
else if (a==studentlist->student_number)
studentlist=studentlist->next;
Here, you are leaving temp still in the list. temp->next still points to studentlst and studentlist->prev still points to temp.
You have:
while(a != temp->student_number)
{
temp->prev=temp;
temp=temp->next;
}
Why do you need the line temp->prev=temp;? You are just looking for the node that contains the item. There is no need to modify the list yet. Plus I haven't clearly thought through the side effects of that statement. You certainly don't need it. In addition, you are not taking care of the case where the item is not found in your list. In other words, you are not checking whether you've reached the end of the list.
When you get out of the above while loop, you are assuming that you have found the item. That is a bad assumption. You have to check whether you found the item before you try to change links using temp.
Here's a modified version of the function that I came up with. You will have to refine it since I don't know what type temp needs to be and whether you can use delete temp;.
void delete_student()
{
// What's the type of 'temp'? Node?
temp=studentlist;
printf("Enter the number of student to delete\n");
fgets(student_number,20,stdin);
a = atoi (student_number);
if (studentlist == NULL)
printf("list is empty\n");
else if (a==studentlist->student_number)
{
studentlist=studentlist->next;
studentlist->prev = NULL;
// Need to deallocate temp.
delete temp;
}
else
{
temp=studentlist;
while( a != NULL && a != temp->student_number)
{
// Why do you need to set temp->prev ???
// temp->prev=temp;
temp=temp->next;
}
// What if we did not find the item?
// Delete the item only if we find it.
if ( temp )
{
// Unlink the node from the rest of the list.
Node* prev = temp->prev;
Node* next = temp->next;
temp->prev = NULL;
temp->next = NULL;
if ( prev )
{
prev->next = next;
}
if ( next )
{
next->prev = prev;
}
// Delete the node that contains the item.
delete temp;
}
}
printf("Delete success\n");
printf("Press any key to go back main menu\n");
getch();
}
I'm trying to find a way of deleting a linked list without recursion, because a stack overflow isn't really something nice.
I have a struct as follows:
typedef struct _my_Item
{
_my_Item(const std::string& name)
{
m_name = name;
}
~my_Item()
{
delete next; // this recursively deletes the "tail" of the list
next = NULL;
}
struct _my_Item *next;
std::string m_name;
// ... More members here...
}
In some piece of code (not relevant here) I'm constructing a list from a data file using the above structure. I keep the pointer to the head of the list in a variable and can work with it. All fine.
When I finally call the destructor on the head of the list, the destructor gets called and the delete next; causes a recursion to delete the "tail" of the list (which is the entire list without the first element). Now since the list is quite long, I see a stack overflow sometimes.
Is there a nice way to get around this problem?
~my_Item()
{
while (next)
{
_my_Item* item = next;
next = item->next;
item->next = NULL; // this prevents the recursion
delete item;
}
}
Create a class representing the list itself that will encapsulate nodes deletion in its destructor via a for/while loop. Doing it the way you do leaves the possibility to delete part of the list and leave dangling pointer.
One suggestion would be to remove the delete code from the destructor and use a pointer to delete the list.
struct _my_Item * nodeToDelete = NULL;
while(firstNode != NULL)
{
nodeToDelete = firstNode;
firstNode = firstNode->next;
delete nodeToDelete;
}
// I wrote this java code to delete a node from BST
// I only used one recursion call to remove successor
public Boolean delete(int data){
if(isEmpty() || !search(data))
return false;
return delete(null,root,data);
}
public Boolean delete(Node parent,Node temp,int data) {
while(true){
if(data == temp.getData()) {
break;
} else if(data < temp.getData()) {
parent = temp;
temp = temp.getLeft();
} else {
parent = temp;
temp = temp.getRight();
}
}
if(parent == null && (temp.getLeft() == null || temp.getRight() == null)){
if(temp.getLeft() == null)
root = temp.getRight();
else
root = temp.getLeft();
} else if(temp.getLeft() == null || temp.getRight() == null) {
if (parent.getLeft() == temp) {
if (temp.getLeft() == null)
parent.setLeft(temp.getRight());
else
parent.setLeft(temp.getLeft());
} else if (parent.getRight() == temp) {
if (temp.getLeft() == null)
parent.setRight(temp.getRight());
else
parent.setRight(temp.getLeft());
}
}else{
int min = findMin(temp.getRight());
temp.setData(min);
delete(temp,temp.getRight(),min);
}
return true;
}