void RemoveDuplicates(Slist& l)
{
if (l.head == NULL) {
return;
}
Node* cur = l.head;
while (cur != NULL && cur->next != NULL) {
Node* prev = cur;
Node* temp = cur->next;
while (temp != NULL) {
if (temp->data == cur->data) {
prev->next = temp->next;
cur->next = prev->next;
temp = prev->next;
}
else {
prev = prev->next;
temp = temp->next;
}
}
cur = cur->next;
}
}
Hi, I want to remove duplicates from linked list (0 is NULL)
input: 1->2->2->4->2->6->0
outPut: 1->2->4->6->0
Result after I run my program is:
1->2->6
Where am I wrong? Please help me
Here is my solution for your problem:
bool alreadyExist(Node head)
{
Node cur = head;
while(cur.next != nullptr)
{
if(cur.next->data == head.data) {
return true;
}
cur = *cur.next;
}
return false;
}
void RemoveDuplicates(Slist& l)
{
if (l.head == nullptr) {
return;
}
Node* head = l.head;
Node* curPtr = l.head->next;
while(curPtr != nullptr)
{
if(alreadyExist(*curPtr) == false)
{
head->next = curPtr;
head->next->prev = head;
head = head->next;
curPtr = curPtr->next;
}
else
{
Node* backup = curPtr;
curPtr = curPtr->next;
// delete duplicate elements from the heap,
// if the nodes were allocated with new, malloc or something else
// to avoid memory leak. Remove this, if no memory was allocated
delete backup;
}
}
}
Important: The destructor of the Node-object is NOT allowed to delete the linked object behind the next and prev pointer.
It results, for your input-example, in the output 1->4->2->6->0. Its not totally exact the order, you want as output, but each number exist only one time within the output. It only add the last time of a duplicate number.
I don't really know, if you use C or C++, but because I prefer C++, I replaced the NULL with nullptr in the code. The delete can be removed, if the objects are not on the HEAP create with malloc or new.
Related
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);
}
}
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;
}
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:
}
}
I am trying to delete from a linked list and return true if it was successfully deleted and return false if the number in the liked list is not in the list. The problem is that I don't know how I would return false if the item is not in the list. I don't even know how i would check if it is in the list. Please help.
bool list::deleteElement(int element)
{
node *curr, *prev;
prev = NULL;
if(head!=NULL)
{
for(curr=head; curr->item!=element&&curr!=NULL;curr= curr->next)
{
prev=curr;
}
if(curr!=NULL)
{
if(prev==NULL)
{
head = head->next;
delete curr;
numberofelements--;
return true;
}
else
{
prev->next = curr ->next;
delete curr;
return true;
}
}
return false;
}
}//end of function
Your code is pretty close to being correct.
You don't need to explicitly check if head is NULL. Your logic can handle that case implicitly. Once you eliminate that test, then the return false; will be the last line of your function.
You need to reverse your check in the for loop so that you don't try to dereference a NULL pointer:
for (curr = head; curr != NULL && curr->item != element; curr = curr->next)
You need to decrement numberofelements in the else-case when prev != NULL too.
You can "factor out" the delete, the numberofelements--; and the return true; from the if-else statements when curr != NULL.
Here's how I would write it:
bool list::deleteElement(int element)
{
node *curr = head, *prev = NULL;
for (; curr != NULL && curr->item != element; prev = curr, curr = curr->next);
if (curr == NULL) // element not found
return false;
if (prev == NULL)
head = head->next;
else
prev->next = curr->next;
delete curr;
--numberofelements;
return true;
}
Issues that I see:
You don't have a return statement when head == NULL. That's a problem. It is undefined behavior.
The check curr->item!=element&&curr!=NULL; in the for loop is not right. When curr == NULL, you will run into undefined behavior since you are dereferencing the pointer first and checking whether it is NULL second. That should be curr != NULL && curr->item != element.
You are not decrementing the number of elements when prev != NULL.
Here's the updated function:
bool list::deleteElement(int element)
{
node *curr, *prev;
prev = NULL;
if(head==NULL)
{
// Empty list. Return right away with false.
return false;
}
for(curr=head; curr!=NULL && curr->item!=element; curr= curr->next)
{
prev=curr;
}
if(curr!=NULL)
{
// The number of elements needs to be decremented
// regardless of whether prev is NULL or not.
numberofelements--;
if(prev==NULL)
{
head = head->next;
delete curr;
return true;
}
else
{
prev->next = curr ->next;
delete curr;
return true;
}
}
return false;
}//end of function
I have written a function in C++ that works fine to swap elements pairwise in a singly linked list. However, I was wondering if there was more efficient way to do the same. To be specific the function I wrote has a case which deals when there are only two elements in the linked list. I was wondering if this case can be omitted and put in the general case. I tried doing that but failed to get the output. Is it possible? Here is my code snippet...
struct Node* swap(struct Node* head){
struct Node* temp = NULL;
struct Node* cur;
struct Node* next;
cur = head;
next = cur->next;
// Empty list.
if(head == NULL){
cout<<"List is empty. \n";
return NULL;
}
// Only one element in the list.
if(next == NULL){
return cur;
}
// If two elements in the list.
else if(next->next == NULL){
cur->next = NULL;
next->next = cur;
return next;
}
// General case.
else{
head = next;
while(cur!=NULL){
if(next == NULL)
break;
if(next->next == NULL)
break;
cur->next = next->next;
next->next = cur;
if(temp == NULL)
temp = cur;
else{
temp->next = next;
temp = cur;
}
cur = cur->next;
next = cur->next;
}
return head;
}
}
Yes, you can encompass the two-element case, the one-element case and the zero-element case in the general case. The loop can begin with
while(cur != NULL && cur->next != NULL)
Unless i'm missing something, it seems Beta's solution should do it.
Also, not an answer but bother me:
if(temp == NULL){
temp = cur;
}
else{
temp->next = next;
temp = cur;
}
If temp ends up as cur anyway you can compress this into
if(temp != NULL){
temp->next = next;
}
temp = cur;