This is my code for an insertion-sort, sorting via the cstring member of the node. The current code only inserts before the head. The code encapsulated in comments is the sorted insertion I am trying to make work. I'm thinking I have to use a predecessor and successor pointer, but maybe it's the comparison that is confusing me. Any help would be appreciated. Thanks!
#include <iostream>
#include "groclist.h"
void insert_item(Grocery_Item_Ptr &head, int quantity, const char name[])
{
bool exists = false;
bool done = false;
Grocery_Item_Ptr temp = NULL;
Grocery_Item_Ptr current = NULL;
Grocery_Item_Ptr pred = NULL;
Grocery_Item_Ptr succ = NULL;
if (head == NULL) {
head = new Grocery_Item;
head->quantity = quantity;
strncpy(head->name, name, MAX_ITEM_NAME_LEN);
head->next = NULL;
return;
}
else {
current = head;
while (current != NULL) {
if (strncmp(current->name, name, MAX_ITEM_NAME_LEN) == 0) {
current->quantity += quantity;
exists = true;
}
current = current->next;
}
if (exists) {
current = NULL;
return;
}
else {
current = head;
}
if (!exists) {
temp = new Grocery_Item;
temp->quantity = quantity;
strncpy(temp->name, name, MAX_ITEM_NAME_LEN);
/*
while (!done || current != NULL) {
if (strncmp(current->name, name, MAX_ITEM_NAME_LEN) < 0) {
pred = current;
succ = current->next;
current->next = temp;
temp->next = succ;
done = true;
}
if (!done) {
current = current->next;
}
}
*/
temp->next = head;
head = temp;
temp = NULL;
}
}
return;
}
One thing you are missing is keeping a reference to the predecessor while searching. This is necessary in order to keep the chain intact.
Here is a draft that should work (currently untested!):
while (!done || current != NULL)
{
//If we found the place to insert
if (strncmp(current->name, name, MAX_ITEM_NAME_LEN) < 0)
{
//If the place to insert was at head
if(pred == NULL)
{
//The new node becomes the head
head = temp;
}
else
{
//Set the previous nodes next to point at this node.
pred->next = temp;
}
//Always set the node to be inserted's next
//pointing to the node we should be inserted before
temp->next = current;
done = true;
}
if (!done)
{
//No match, keep looking but keep an updated pred pointer
pred = current;
current = current->next;
}
}
It's just pseudocode, but maybe it helps:
if(head == NULl)
{
//make newnode as head
}
if(head.name == new name)
{
//update quantity
}
if(head.name <new name)
{
//insert newnode before head
//make newnode as head
}
if (new name > head.name)
{
current = head;
succ = current.next;
while (succ && new name <succ.name)
{
curent = succ;
succ = succ.next
}
if(succ = NULL)
current->next = newnode
else
if new name = succ->name
update quantity
else
curent->next = newnode
newnode->next = succ;
}
I appreciate your input guys, if only for having me think about it in different ways. However my result is quite different, and I must truly thank my whiteboard.
if (strncmp(name, head->name, MAX_ITEM_NAME_LEN) < 0) {
temp->next = head;
head = temp;
temp = NULL;
}
else {
pred = head;
current = head->next;
do {
if (strncmp(name, current->name, MAX_ITEM_NAME_LEN) < 0) {
pred->next = temp;
temp->next = current;
done = true;
}
else if (current->next == NULL) {
current->next = temp;
done = true;
}
else {
pred = current;
current = current->next;
}
if (done) {
pred = NULL;
current = NULL;
}
} while (!done && current != NULL);
}
Related
Here using the function returnReverseLinkedList I am returning the reversed linked list of the given linked list. But the problem with this approach is that i lose the original linked list. So I make another fucntion called createReversedLinkedList to make a copy of the original linked list and reverse the copy and maintain possession of both.
unfortunately createReversedLinkedList is giving Runtime error.
obviously my end goal is to check if the given linked list is palindrome or not. This issue is just a stepping stone.
Could someone tell me why?
//Check if a linked list is a palindrome
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
node(int data)
{
this->data = data;
this->next = NULL;
}
};
node *returnReverseLinkedList(node *head)
{
// Will Lose original Linked List
if (head == NULL)
return NULL;
else if (head != NULL && head->next == NULL)
return head;
node *prev = NULL;
node *curr = head;
node *tempNext = head->next;
while (tempNext != NULL)
{
curr->next = prev;
prev = curr;
curr = tempNext;
tempNext = tempNext->next;
}
curr->next = prev;
return curr;
}
node *createReversedLinkedList(node *head)
{
if (head == NULL)
return NULL;
else if (head != NULL && head->next == NULL)
return NULL;
else
{
node *temp = head;
node *newHead = NULL;
node *newTail = NULL;
while (temp != NULL)
{
node *newNode = new node(temp->data);
if (newHead == NULL)
{
newHead = newNode;
newTail = newNode;
}
else
{
newTail->next = newNode;
newTail = newNode;
}
}
return returnReverseLinkedList(newHead);
}
}
bool check_palindrome(node *head)
{
node *original = head;
node *reverse = returnReverseLinkedList(head);
while (original->next != NULL || reverse->next != NULL)
{
if (original->data != reverse->data)
return false;
cout << "debug 2" << endl;
original = original->next;
reverse = reverse->next;
}
return true;
}
// #include "solution.h"
node *takeinput()
{
int data;
cin >> data;
node *head = NULL, *tail = NULL;
while (data != -1)
{
node *newnode = new node(data);
if (head == NULL)
{
head = newnode;
tail = newnode;
}
else
{
tail->next = newnode;
tail = newnode;
}
cin >> data;
}
return head;
}
void print(node *head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main()
{
node *head = takeinput();
node *revese2 = createReversedLinkedList(head);
print(revese2);
// bool ans = check_palindrome(head);
// if (ans)
// cout << "true";
// else
// cout << "false";
// return 0;
}
As asked by the OP, building a reversed linked is simply done by building as you would a stack (e.g LIFO) rather than duplicating the same original forward chain. For example:
node *createReversedLinkedList(const node *head)
{
node *newHead = NULL;
for (; head; head = head->next)
{
node *p = new node(head->data)
p->next = newHead;
newHead = p;
}
return newHead;
}
Note we're not hanging our copied nodes on the tail of the new list; they're hanging on the head of the new list, and becoming the new head with each addition. That's it. There is no need to craft an identical list, then reverse it; you can reverse it while building the copy to begin with.
A note on the remainder of your code. You have a dreadful memory leak, even if you fix the reversal generation as I've shown above. In your check_palindrome function, you never free the dynamic reversed copy (and in fact, you can't because you discard the original pointer referring to its head after the first traversal:
bool check_palindrome(node *head)
{
node *original = head;
node *reverse = returnReverseLinkedList(head); // only reference to reversed copy
while (original->next != NULL || reverse->next != NULL)
{
if (original->data != reverse->data)
return false; // completely leaked entire reversed copy
original = original->next;
reverse = reverse->next; // lost original list head
}
return true;
}
The most obvious method for combating that dreadful leak is to remember the original list and use a different pointer to iterate, and don't leave the function until the copy is freed.
bool check_palindrome(const node *head)
{
bool result = true;
node *reverse = returnReverseLinkedList(head);
for (node *p = reverse; p; p = p->next, head = head->next)
{
if (p->data != head->data)
{
result = false;
break;
}
}
while (reverse)
{
node *tmp = reverse;
reverse = reverse->next;
delete tmp;
}
return result;
}
Following is a piece of my code for deleting a node from a BST. It is a non-recursive code.
I have applied all the possible conditions.
However when I run my code, it stops as if stuck somewhere in an infinite loop or ending up at a point where my pointer is pointing to NULL.However I am unable to identify it.
Any help would be appreciated.
template <class T>
void bst<T>::delete_node(T key1)
bst_node<T>* delNode = search(key1); //delNode is the node I wish to delete
if(delNode!=root)
{
if((delNode->left == NULL) && (delNode->right == NULL)) // node to be deleted has no children
{
delNode = NULL;
}
else if((delNode->left!=NULL) && (delNode->right == NULL)) //node to be deleted has exactly one child
{
if(delNode->parent->left == delNode)
{
delNode->parent->left = delNode->left;
delNode = NULL;
}
else if(delNode->parent->right == delNode)
{
delNode->parent->right = delNode->left;
delNode = NULL;
}
}
else if((delNode->right!= NULL) && (delNode->left == NULL))
{
if(delNode->parent->left == delNode)
{
delNode->parent->left = delNode->right;
delNode = NULL;
}
else if(delNode->parent->right == delNode)
{
delNode->parent->right = delNode->right;
delNode = NULL;
}
}
else if((delNode->right!=NULL)&&(delNode->left != NULL)) //if has two children
{
bst_node<T>* temp = delNode;
bst_node<T>* trav = delNode->right;
if((trav->right == NULL)&&(trav->left == NULL))
{
delNode->value = trav->value;
delNode->key = trav->key;
trav = NULL;
}
else
{
bst_node<T>* pred = trav;
while(trav!=NULL)
{
pred = trav; //smallest node in right subtree
trav=trav->left;
}
delNode->value = pred->value;
delNode->key = pred->key;
pred = NULL;
}
}
}
else if(delNode==root)//node to be deleted is Root
{
if((delNode->left==NULL)&&(delNode->right==NULL))
root = NULL;
else if((delNode->left!=NULL) && (delNode->right == NULL)) //root has exactly one child
{
root = delNode->left;
delNode = NULL;
}
else if((delNode->right!= NULL) && (delNode->left == NULL))
{
root = delNode->right;
delNode = NULL;
}
else if((delNode->right!=NULL)&&(delNode->left!=NULL)) {
bst_node<T>* temp = delNode;
bst_node<T>* trav = delNode->right;
if((trav->right == NULL)&&(trav->left == NULL))
{
delNode->value = trav->value;
delNode->key = trav->key;
trav = NULL;
}
else
{
bst_node<T>* pred = trav;
while(trav!=NULL)
{
pred = trav; //smallest node in right subtree
trav=trav->left;
}
delNode->value = pred->value;
delNode->key = pred->key;
pred = NULL;
}
}
}
}
In the part:
else if((delNode->right!=NULL)&&(delNode->left != NULL)) //if has two children
when if is taken:
if((trav->right == NULL)&&(trav->left == NULL))
the code copies the values from trav to delNode and sets trav to NULL:
delNode->value = trav->value;
delNode->key = trav->key;
trav = NULL;
but doesn't set delNode->right to NULL, as it should because trav didn't have any children.
the very next else statement which seeks for the most left node commits a similar error, because parent node's member left isn't set to NULL.
The next big part of the code which deals with deletion of only root node, seems to have identical problems, due to apparent copy-pasted (cough!) code.
I'm trying to delete a node in a doubly linked list, but the problem here is that when I have the list: 7-6-5-4-3-2-1, and try to delete 3, I get 7-6-5-4-3, I can't figure out the error. Please help!
void RemoveItem(int itm)
{
if (Head->item == itm) {
Head = Head -> Next;
cout<<"\nItem is at 1st node & removed\n";flag=1;
}
else if (Head->Next == NULL) {
cout<<" \n This is 1-node list & item not in it\n";
}
else {
node *current, *del = Head;
while(del->Next != NULL && del->item != itm) {
del = del->Next;
}
current = del;
current->Next = del->Next;
del->Next->previous = current;
delete(del);
del->Next = NULL;
flag = 1;
}
}
To delete a node you iterate until you reach the node of deletion del then set:
pseudocode:
del.prev.next = del.next
and
del.next.prev = del.prev
This makes no sense:
current = del;
current->Next = del->Next;
because it is the same as
del->Next = del->Next;
Try something like this
// some global int flag = 0
// some global node* Head pointing to head of list
void RemoveItem(int itm) {
if (Head->item == itm) {
node* del = Head;
Head = Head -> Next;
delete(del);
cout<<"\nItem is at 1st node & removed\n";
flag=1;
return;
}
if (Head->Next == NULL) {
cout<<" \n This is 1-node list & item not in it\n";
return;
}
node* current = Head;
node* del = Head;
while(del->Next != NULL && del->item != itm) {
del = del->Next;
}
// reached with del->Next == NULL || del->item == itm
current = del->previous;
if (del->Next == NULL) {
// at end of list
if (del->item != item) {
// item not found
// todo: signal error
return;
}
current->Next = NULL;
} else {
del->Next->previous = current;
current->Next = del->Next;
}
delete(del);
flag = 1;
}
Note: I fixed many bugs and tried to keep it within your logic. I would have written it different.
I have been stuck on this function to remove node from a list, if there are two names in the list they are both gone. If Anne and John are in the list and I want to delete Anne, then my list is empty, John is gone.
What am I missing to keep the connection in the list if I delete a node init?
bool ContactList::remove(string key)
{
NodePtr prev = NULL;
for(NodePtr temp = head; temp != NULL; temp = temp->link)
{
if(temp->data.key == key)
{
if(prev == NULL)
{
head = temp->link;
delete temp;
return true;
}
else
{
prev = temp->link;
delete temp;
return true;
}
}
}
return false;
}
You aren't keeping prev up to date in every iteration of your loop. You want something like:
prev = temp;
at the bottom of your for loop.
Try using this function
bool ContactList::remove(string key)
{
NodePtr prev = NULL;
for(NodePtr temp = head; temp != NULL; temp = temp->link)
{
if(temp->data.key == key)
{
if(prev == NULL)
{
head = temp->link;
delete temp;
return true;
}
else
{
prev->link = temp->link; // change.
delete temp;
return true;
}
}
prev = temp; // change.
}
return false;
}
I have a function and it is suppose to organize a dictionary of stemmed words. I have a function call inserted then suppose to place it in the right alphabetical order. Adding to the front and middle of the list works, but adding to the back doesn't. I've looked at several sources and I can't tell what's wrong.
void dictionary::insert(string s) {
stem* t = new stem;
t->stem = s;
t->count =0;
t->next = NULL;
if (isEmpty()) head = t;
else {
stem* temp = head;
stem* prev = NULL;
while (temp != NULL) {
if (prev == NULL && t->stem < temp ->stem) {
head = t;
head->next = temp;
}
prev = temp;
temp = temp->next;
if(t->stem > prev->stem && t->stem < temp->stem ){
prev->next =t;
t->next=temp;
}
}
if(temp == NULL && t->stem > prev->stem){
prev->next=t;
}
}
}
The statement if(temp->next=NULL) does not result in a boolean but rather an assignment. This is why the insert to the end of the list doesn't appear to work.
if (temp->next=NULL) {
prev->next = t;
}
Note the usage of a single equal. The effect of this is to set the temp->next to NULL and then evaluate if (NULL) witch will be always false. You should use ==.
This will probably do the job: (sorry, I don't have a compiler right now to test it)
#include <string>
struct node;
struct node
{
node* next;
std::string value;
};
node* head = NULL;
void insert(const std::string& word)
{
node* n = new node;
n->value = word;
node* temp = head;
node** tempp = &head;
while (true)
{
if (temp == NULL or temp->value > word)
{
n->next = temp;
*tempp = n;
return;
}
temp = temp->next;
tempp = &temp->next;
}
}