c++ linked list, insert strings - c++

This is my homework: Create a linked list class which stores a list of unique (no duplicates allowed) strings in alphabetic order.
enter image description here
and this is what I have so far, which is not working. can anyone find the problems for me? thank you very much. the error looks like this:
+ std::_String_alloc<std::_String_base_types<char,std::allocator<char> > > {_Mypair={_Myval2={_Bx={_Buf=0x0135f988 "Adam" _Ptr=0x6d616441 <Error reading characters of string.> ...} ...} } } std::_String_alloc<std::_String_base_types<char,std::allocator<char> > >
my code:
bool LinkedList::insert(string dataInsert) {
//remember to use find funcion to check dupcated.
if (find(dataInsert) != 0)
return false;
// build new node.
Node* newNode = new Node;
newNode->data = dataInsert;
newNode->next = NULL;
//insert into empty list
if (head == NULL)
{
head = newNode;
return true;
}
//insert before first item
if (newNode->data < head->data)
{
newNode->next = head;
head = newNode;
return true;
}
//insert somewhere after first item
Node* previous = head;
while (previous->next != NULL && previous->next->data < newNode->data)
{
previous = previous->next;
newNode->next = previous->next;
previous->next = newNode;
return true;
}
}

Your below while might have some problems because newNode was inserted every time when you found the next node has smaller data than newNode.
while (previous->next != NULL && previous->next->data < newNode->data)
{
previous = previous->next;
newNode->next = previous->next;
previous->next = newNode;
return true;
}
I think your solution is finding a position of the largest node that smaller than newNode. You can refer below code:
while (previous->next != NULL && previous->next->data < newNode->data)
{
previous = previous->next;
}
newNode->next = previous->next;
previous->next = newNode;
return true;

Related

Deleting a key from a circular linked list

void deletenode(string key) {
if (last == NULL) {
cout << "your circular linked list is an empty one" << endl;
}
else {
node* p = last->next;
node* prev = last;
do {
if (p->title == key) {
node* temp = p;
prev->next = p->next;
delete(temp);
}
else {
p = p->next;
prev = prev->next;
}
} while (p != last->next);
}}
I was trying to delete a node with key value. For instance, if node p->title is my key then I want to delete that node. However, I implemented it with other values but the code doesn't seem to work or delete a node with key value from my circular linked list. What is the mistake in the function?
circular linked list value "cat", "dog", "rat", "horse", the key to be deleted was "dog". When I traverse throughout the linked list after the deletion it still printed everything including "dog", which means deletion didn't work.
Anytime you write a "delete from the linked list" function, you have to account for the possibility that you are deleting from the "head" or whatever pointer you are referencing with the list. The common pattern is for the function to return the new head of the list if it changed, else return the current head.
Node* deletenode(Node* head, const string& key) {
// empty list
if (head == nullptr) {
return nullptr;
}
// single item list
if (head->next == nullptr || head->next == head) {
if (head->title == key) {
delete head;
head = nullptr;
}
return head;
}
// two or more item list, possibly circular
Node* prev = head;
Node* current = head->next;
Node* first = current;
while (current && current->title != key) {
prev = current;
current = current->next;
if (current == first) {
break;
}
}
if (current == nullptr || current->title != key) {
return head; // not found
}
prev->next = current->next;
if (current == head) {
head = current->next;
}
delete current;
return head;
}
I don't see the full code so I can't make a comment I tried to implement the function, hope it helps you with the comments.
void deleteNodeWithKey(node* head, string key)
{
node *curr = head;
node *last , *temp;
//Search for last node
while (curr->next != head)
{
curr = curr->next;
}
last = curr;
//If head is the desired key, make head's next new head
//and connect last node to new head
if (head->key == key)
{
temp = head->next;
delete head;
head = temp;
last->next = head;
return;
}
temp = head->next;
//Search for node with the given key
node *prev = head;
while (temp != head)
{
if (temp->key == key)
{
prev->next = temp->next;
delete temp;
return;
}
temp = temp->next;
prev = prev->next;
}
//If function gets here, key was not found
}
I made some changes to your code
void deletenode(string key) {
if (last == NULL) {
cout << "your circular linked list is an empty one" << endl;
}
else {
node* prev = last;
// If head is to be deleted
if (last->title == key) {
while (prev->next != last)
prev = (prev)->next;
prev->next = last->next;
free(last);
last = prev->next;
return;
}
node* p = last->next;
do {
if (p->next->title == key) {
node* temp = p->next;
p->next = temp->next;
delete(temp);
}
else {
p = p->next;
prev = prev->next;
}
} while (p != last->next);
}
}

C++ Delete nodes after another in Single Linked List

What I am trying to do is check if node after value is specific number and if is, then delete previoues node.
Something like:
1,2,3,4,5,4
if next node number is 4 then delete this node.
1,2,3,4,5,4 -> 1,2,4,4
node* temp = head;
while (head != NULL) {
if (head->next->number == 4) {
delete temp;
}
head = head->next;
}
Struggling at this moment as compiler crashes.
You delete the head before you advance it:
node *temp = NULL;
while (head->next != NULL) {
if (head->next->number == 4) {
temp = head;
}
if ( temp == NULL ){
head = head->next;
}
else{
head = head->next->next;
delete temp;
temp= NULL:
}
}

Order Link List

I have a base class called linkList and two derived class. One order link list class and unorder link list class. My order link list class is not working and i have no idea why.When i insert 3 numbers into the list it work but when i insert more than 4 values it messes up. It will also say the length of the list is larger than what it actually is. [enter image description here][1]
void insert(const T& newItem){
nodeType<T> *current;
nodeType<T> *trailCurrent=nullptr;
nodeType<T> *newNode;
bool found;
newNode = new nodeType<T>;
newNode->info = newItem;
newNode->next = nullptr;
//if the list is empty add to empty list
if (this->first == nullptr){
this->first = newNode;
this->last = newNode;
this->count++;
}
else {
current = this->first;
found = false;
while (current != nullptr && !found){
if (current->info >= newItem)
found = true;
else{
trailCurrent = current;
current = current->next;
}
//if current value is first item into the list insert into the front
if (current == this->first){
newNode->next = this->first;
this->first=newNode;
this->count++;
}
else{
trailCurrent->next = newNode;
newNode->next=current;
if (current == nullptr)
this->last = newNode;
this->count++;
}
}
}
}

Removing a node in a circular doubly linked list

I'm trying to write some code to remove a node from a circular doubly linked list. I've written the following function, which mostly works:
bool Circular::remove(int index)
{
if (head == NULL)
return false;
Node* current;
current = head;
if (index > 0)
{
for (int i = 0; i < index; i++)
{
current = current->next;
}
}
else if (index < 0)
{
for (int i = 0; i < abs(index); i++)
{
current = current->prev;
}
}
if (current == head)
{
head = current->next;
}
else if (current == tail)
{
tail = current->prev;
}
current->prev->next = current->next;
current->next->prev = current->prev;
return true;
}
The only problem I have is that it won't remove the correct value when I pass the number 1 into the index number. Instead it always deletes the tail. If you think there's something wrong with my code somewhere else then I'll also look into it.
I think I've figured it out. Mostly I used functions to remove the head...
Node* temp = head;
head = head->next;
head->prev = tail;
tail->next = head;
delete temp;
return true;
...and to remove the tail:
Node* temp = tail;
tail = tail->prev;
tail->next = head;
head->prev = tail;
delete temp;
return true;
Apparently just moving around the head and tail wasn't enough to actually delete those nodes.

how to insert a linked list after a particular node inside head? c++

I wrote the following code to insert an integer to a linked list, but in a sorting approach.
I commented where my problem is and explained it below:
void LLL::insertSorted(int r) {
node * temp = NULL;
node * current = NULL;
if (head == NULL) {
head = new node;
head->data = r;
head->next = NULL;
} else {
temp = new node;
temp->data = r;
current = head;
while (temp->data > current->data && current != NULL) {
current = current->next;
}
temp->next = current;
/*
* Assume that I have head points to this list: { 3 -> 5 -> 8 -> NULL }
* And I want to insert {6} (temp) to the list just after 5; then what
* I've done so far on my previous code I made temp = {6 -> 8 -> NULL}.
* NOW!! How can correctly insert temp to ((head)) just after {5}??!
*/
}
}
void LLL::insertSorted(int r) {
node * cur = head;
node * prev = NULL;
while((cur != NULL) && (r > cur->data)){//find the location to insert
prev = cur;
cur = cur->next;
}
node *new_node = new node;
new_node->data = r;
new_node->next = cur;
if(prev == NULL){//new first one
head = new_node;
}else{
prev->next = new_node;
}
}
You need to remember which node you are inserting after, and have that node link to the new one.
For example, in your loop to find the node that comes after temp, current, have another variable, say prev, and do pre = current just before current = current->next.
you need a new node when insert
I think just make a new node after the previous one you want insert, like 5 in your comment
This is the final result: (it works!)
It handles the following cases:
case 1: head is empty.
case 2: the new element is the smallest in the list.
case 3: the new element is somewhere in the middle of the list.
case 4: the new element is the largest.
void LLL::insertSorted(int r) {
node * temp = NULL;
node * current = NULL;
node * previous = NULL;
if (head == NULL) {
head = new node;
head->data = r;
head->next = NULL;
} else {
temp = new node;
temp->data = r;
temp->next = NULL;
current = head;
if (temp->data < current->data) {
temp->next = head;
head = temp;
} else {
while (current != NULL && temp->data >= current->data) {
previous = current;
current = current->next;
}
temp->next = current;
previous->next = temp;
}
}
}