Removing nodes from linked list not working properly - c++

I have a problem with deleting nodes in linked list. This is my code (except for addElement function which works fine). I initialize nodes in the list trough input, then call the function which removes the nodes on right side with higher value and then print the modified list, lastly delete the list.
The problem is that with certain inputs my program doesn't work properly.
For example if I input 1,2,3,4,3 then the output should be 1 and 3 (the 2nd three) but my output is only 1.
What could be the problem? Can't seem to figure it out.
Edit 1: Here's the includes.
Edit 2: Included the addElement function
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
struct digits {
int value;
digits *next
};
int main() {
int a, b, c;
digits *head = NULL, *tale = NULL, *current;
cout << "How many digits you want in the linked list?" << endl;
cin >> a;
for (int i = 0; i < a; i++) {
cin >> b;
current = new digits;
current->value = b;
current->next = NULL;
if (head == NULL)
head = tale = current;
else {
tale->next = current;
tale = current;
}
if (!cin.good()) {
cin.clear();
cin.ignore(256, '\n');
cout << "Input can be int value! You can still input " << (a - i) - 1
<< " digits." << endl;
continue;
}
}
cout << "Want to add element? Press J if so, otherwise any other key" << endl;
cin >> add;
if (add == 'J') {
cin >> c;
addElement(&head, c);
}
removeElement(head);
for (current = head; current != NULL; current = current->next)
cout << current->value << endl;
current = head;
while (current != NULL) {
head = head->next;
delete current;
current = head;
}
}
// function which removes elements which have greater value on right side
void removeElement(struct digits *head) {
struct digits *current = head;
struct digits *max = head;
struct digits *temp;
while (current != NULL && current->next != NULL) {
if (current->next->value > max->value) {
temp = current->next;
current->next = temp->next;
free(temp);
} else {
current = current->next;
max = current;
}
}
}
void addElement(struct digits **head, int a) {
struct digits *newelem = (struct digits*) malloc(sizeof (struct digits));
newelem->value = a;
newelem->next = NULL;
struct digits *temp = *head;
if (*head == NULL) {
*head = newelem;
} else {
while (temp->next != NULL)
temp = temp->next;
temp->next = newelem;
}
}

This gets much easier if you can start at the end and work towards the head.
You can't do this directly with a singly-linked list, but you can use recursion.
First, if the list isn't empty, clean out the rest of the list.
Then you see if the node to the right is greater and remove it if it is.
And then you're done.
void scrub(digits* link)
{
if (link != nullptr)
{
scrub(link->next);
if (link->next != nullptr && link->next->value > link->value)
{
digits* scrap = link->next;
link->next = link->next->next;
delete scrap;
}
}
}

Why your code won't work:
Have a close look at this code:
while (current != NULL && current->next != NULL) {
if (current->next->value > max->value) {
temp = current->next;
current->next = temp->next;
free(temp);
}
You are changing current but not max. Having max var in your code seems totally irrelevant.
Actually you never enter into the else part of the code, current value is always compared with max which throughout remains fixed at 1, and eventually while loop finishes when current is the last node(value = 3), as current->next != NULL fails for last node. So, it fails to get rid of last node. As a result of that, you get:
1(first node) and 3(last node)
Solution: Try this iterative approach:
Node *last, *lastTail = NULL;
current = *head;
int last_val = INT_MAX;
while (current != NULL) {
if(current->value > last_val) {
last = current;
last_val = current->value;
current = current->next;
if(lastTail) {
lastTail->next = current;
}
else {
*head = current;
lastTail = current;
}
delete last;
}
else{
lastTail = current;
last_val = current->value;
current = current->next;
}
}

Related

Delete Nodes With The Value 0 In Singly Linked List In C++

I can't for the life of me figure this out I've spent days on this exercise but to no avail.
I'm trying to delete nodes with the value 0 from a singly liked list.
Let's say i have |1|3|0|4|0|5|0|0|. The outcome should be |1|3|4|5|
Here is all the code for reference
#include <iostream>
#include <fstream>
using namespace std;
struct node {
int data;
node* next;
};
node* head, *last;
int n;
void creating_list()
{
node* aux;
ifstream f("in.txt");
f >> n;
for(int i=0;i<n;i++)
{
if (head == NULL)
{
head = new node;
f >> head->data;
head->next = NULL;
last = head;
}
else
{
aux = new node;
f >> aux->data;
last->next = aux;
aux->next = NULL;
last = aux;
}
}
}
void displaying_list()
{
node* a;
a = head;
if (a == NULL)
cout << "List is empty! ";
else
{
cout << "Elements of list are: | ";
while (a)
{
cout << a->data<<" | ";
a = a->next;
}
}
}
void delete_first_node()
{
if (head == NULL)
cout << "List is empty";
else
{
cout << "Deleting first node\n";
node* aux;
aux = head;
head = head->next;
delete aux;
}
}
void delete_last_node()
{
if (head == NULL)
cout << "List is empty";
else
{
if (head == last)
{
delete head;
head = last = NULL;
}
else
{
node* current;
current = head;
while (current->next != last)
current = current->next;
delete current->next;
current->next = NULL;
last = current;
}
}
}
void delete_value_0()
{
node* aux;
if (head == NULL)
cout << "List is empty. Can't delete! ";
else
// if (head->data == 0)
// delete_first_node();
// if (last->data == 0)
// delete_last_node();
// else
{
node* a;
a = head;
while (a)
if (a->next->data != 0)
{
a = a->next;
cout << a->data<<" | ";
}
else
if (a->next != last)
{
aux = a->next;
a->next = a->next->next;
delete aux;
break;
}
}
}
int main()
{
creating_list();
displaying_list(); cout <<endl;
delete_value_0();
return 0;
}
Here is the problem that gives me metal problems
I've tried to move one node short of the node that has the 0 value, store the value in another node, aux in this case and delete aux;
I've put comment on those lines because if I don't and the condition it's met it doesn't execute the rest of the code...
If I put break at the end it only shows me the first few numbers until the 0 and then stops short, doesn't move through the full list.
if I don't put break the the program is doesn't stop, it's in an infinite loop, it doesn't exit with code 0
void delete_value_0()
{
node* aux;
if (head == NULL)
cout << "List is empty. Can't delete! ";
else
// if (head->data == 0)
// delete_first_node();
// if (last->data == 0)
// delete_last_node();
// else
{
node* a;
a = head;
while (a)
if (a->next->data != 0)
{
a = a->next;
cout << a->data<<" | ";
}
else
if (a->next != last)
{
aux = a->next;
a->next = a->next->next;
delete aux;
break;
}
}
}
Honestly I'm at a loss I've spent so much time trying to figure this out, and this should be a very simple exercise. I feel like the answear Is really simple but i don't know what to do anymore, Maybe this is not for me.
This is much simpler than it appears on the first glance. The trick to this task is instead of using a pointer to the current node, a pointer to the pointer to the current node gets used instead. The entire task becomes laughably trivial: only one loop, and one if statement that takes care of all possibilities: the list is empty; the node to delete is the first node in the list; ot the last node in the list; or anywhere in the middle of it.
void delete_value_0()
{
node **p= &head;
while (*p)
{
if ((*p)->data == 0)
{
node *nextptr=*p;
*p=(*p)->next;
delete nextptr;
}
else
{
p= &(*p)->next;
}
}
}
The naive solution is something like this:
void delete_value_0()
{
while (head && head->data == 0)
delete_first_node();
if (head == nullptr)
return;
node *cur = head->next;
node *pre = head;
while (cur)
{
if (cur->data == 0)
{
pre->next = cur->next;
delete cur;
cur = pre->next;
}
else
{
pre = cur;
cur = cur->next;
}
}
}
The key point is that you need to have a pointer to both the element you are inspecting and to the previous element in the list. This allows you to pull the current element out if it has data == 0.
The issue with this is that you have to treat the first element special (since it has no previous element).
My suggestion is to study this solution until you understand how it works, then move on to the (much better) solution by #Sam Varshavchik and study that - it does basically the same, but uses a pointer to pointer in a clever way to make the special cases here irrelevant.
I've put comment on those lines because if I don't and the condition it's met it doesn't execute the rest of the code...
OK why there the sketchy iteration is in else for if (last->data == 0)? Your input seems to have 0 as last item so in this case it would never be triggered. Also, if you want to have first/last items as special case, instead of
if (head->data == 0)
delete_first_node();
you would want something like
while (head && head->data == 0)
delete_first_node();
That being said, the real WTF is treating first/last item specially instead of using just single iteration. Also, you don't really check whether the pointers are non-null before trying to access the contents. With C (or C++ in the case you try it at some point) you need to take care with memory access when dealing with pointers.
Some random pieces of help:
You need to break from last item when it's 0 to exit loop simply because you don't assign a to the next item in this case.
If this is your schoolwork this might not be your fault, reading amount of items from the input file (assuming it was given part of the assignment) before actual items is huge WTF as you're reading into a linked list. There is no need to loop for any n items when you can be simply reading a line of input at the time until the file runs out.
Arguments and return values. You should learn those.
#include <iostream>
struct Node {
int data;
Node* next;
};
// Function to delete nodes with the value 0 in a singly linked list
void deleteNodes(Node** head) {
// Edge case: empty list
if (*head == nullptr) {
return;
}
// Delete all nodes with the value 0 at the beginning of the list
while (*head != nullptr && (*head)->data == 0) {
Node* temp = *head;
*head = (*head)->next;
delete temp;
}
// Edge case: list with only one node
if (*head == nullptr) {
return;
}
// Delete nodes with the value 0 in the rest of the list
Node* current = *head;
while (current->next != nullptr) {
if (current->next->data == 0) {
Node* temp = current->next;
current->next = temp->next;
delete temp;
} else {
current = current->next;
}
}
}
int main() {
// Create a singly linked list: 1 -> 0 -> 2 -> 0 -> 3 -> 0 -> 4
Node* head = new Node{1, new Node{0, new Node{2, new Node{0, new Node{3, new Node{0, new Node{4, nullptr}}}}}};
// Delete nodes with the value 0
deleteNodes(&head);
// Print the resulting list: 1 -> 2 -> 3 -> 4
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
return 0;
}
hope it help

How to display a message saying doubly linked list is empty,cannot delete element from it?

I have implemented a sorted doubly linked list with the help of pointers in C++.I want to display an error message on deleting the last element saying "doubly linked list is empty cannot delete any more elements" and also display a message before deleting the last element saying"the last element in the node are you sure you want to delete it"?
#include "stdafx.h"
#include "DoublyLinkedList.h"
#include<iostream>
using namespace std;
DoublyLinkedList::DoublyLinkedList():Head(nullptr),Tail(nullptr) {
}
DoublyLinkedList::~DoublyLinkedList() {
Node *current = Head;
while (current != NULL)
{
Node* next = current->Next; //The objects pointed to by head and tail at the beginning of the destructor are deleted
Node* prev = current->Prev; // through the current pointer. Then they are deleted again at the end of the destructor.
delete current;
current = next;
current = prev;
}
}
void DoublyLinkedList::SortedInsert(const int& new_element) {
if(new_element !=0){
Node* np = new Node(new_element);
if (!Head)
{
Head = np;
Tail = np;
}
else if (new_element < Head->Element)
{
np->Next = Head;
Head->Prev = np;
Head = np;
}
else
{
Node *cur = Head->Next;
while ((cur) && (new_element >= cur->Element))
cur = cur->Next;
if (cur)
{
np->Prev = cur->Prev;
np->Next = cur;
cur->Prev->Next = np;
cur->Prev = np;
}
else
{
Tail->Next = np;
np->Prev = Tail;
Tail = np;
}
}
}
}
void DoublyLinkedList::Delete(const int& del_element)
{
Node *cur = Head;
while (cur)
{
if (cur->Element == del_element)
{
if (cur->Prev)
cur->Prev->Next = cur->Next;
if (cur->Next)
cur->Next->Prev = cur->Prev;
if (cur == Head)
Head = cur->Next;
if (cur == Tail)
Tail = cur->Prev;
delete cur;
break;
}
cur = cur->Next;
}
}
void DoublyLinkedList::traverse_head() {
Node *t = Head;
while (t != NULL)
{
cout << t->Element << "\t";
t = t->Next;
}
cout << endl;
}
void DoublyLinkedList::traverse_tail() {
Node *temp = Tail;
while (temp != NULL) {
cout << temp->Element << "\t";
temp = temp->Prev;
}
}
main.cpp
// Question1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"DoublyLinkedList.h"
#include<iostream>
using namespace std;
int main()
{
DoublyLinkedList intlist;
int i = 0, x=0,delete_elm=0;
//intlist.SortedInsert(3);
//intlist.SortedInsert(6);
//intlist.SortedInsert(7);
//intlist.SortedInsert(10);
//intlist.SortedInsert(-1);
//intlist.traverse_head();
do
{
cout << "\nEnter value of node.Press 0 to stop\n";
cin >> x;
if ((!cin) || cin.peek() != '\n') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input, no string allowed!!" << endl;
}
else {
intlist.SortedInsert(x);
i++;
}
} while (x != 0);
intlist.traverse_head();
cout << "\nTraversing Doubly Linked List head first\n";
intlist.traverse_head();
cout << "\nTraversing Doubly Linked List tail first\n";
intlist.traverse_tail();
cout << endl;
do {
cout << "Which element do you want to delete? 0 to stop delete operation" << endl;
cin >> delete_elm;
cout << endl;
if ((!cin) || cin.peek() != '\n' || x < 0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input, no string allowed!!" << endl;
}
else {
intlist.Delete(delete_elm);
cout << "\nTraversing Doubly Linked List head first\n";
intlist.traverse_head();
cout << "\nTraversing Doubly Linked List tail first\n";
intlist.traverse_tail();
cout << endl;
}
} while (delete_elm != 0);
system("pause");
return 0;
}
Since you seem to not be using dummy nodes, checking if the list is empty is the same as checking if the Head or Tail pointers are null. Checking if it's only one element is the same as checking if Head->Next is null (be sure to check that Head is not null first, of course).
Alternatively, you can maintain a size variable (incremented in insert and decremented in remove) or write a method to calculate the size by traversing the list.

Printing the singly linked list

I am a newbie to programming
Here I wrote a code for accepting and displaying the values using linked list.
However the code takes all the values but displays only the last value
Here is the code
#include <iostream>
using namespace std;
struct node {
int value;
node* next;
};
class llist {
public:
void create();
void display();
node* head = NULL;
};
void llist::create()
{
struct node* temp;
temp = NULL;
struct node* p;
p = new struct node;
cin >> p->value;
if (head == NULL) {
head = p;
}
else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->value = p->value;
temp->next = NULL;
}
}
void llist::display()
{
struct node* temp = head;
while (temp != NULL) {
cout << "VALUE:" << temp->value << endl;
temp = temp->next;
}
}
int main()
{
int n, i;
llist l1;
cin >> n;
for (i = 0; i < n; i++)
l1.create();
cout << "Displaying list\n";
l1.display();
return 0;
}
Input:
4
1
2
3
4
Displaying list
VALUE:4
I am wondering what went wrong...
Change this:
else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->value = p->value;
temp->next = NULL;
}
to this:
else {
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = p;
}
When inserting a new element at the end of a linked list, you find the last element inside the while loop and put it in the temp variable. Then you assign its next value to your new p element. The way you were doing before, you were just overriding the integer number of the last element. That is why when you printed your list you only got the last number you entered.
Also, when creating a new element p, be sure to initialize its next value to NULL:
p = new struct node;
p->next = NULL;
Problem is with the last 2 lines in the else block.
You are overwriting the value and maintaining just the single mode in your list class. And that's the reason, only last value is displayed.
Replace
temp->value = p->value;
temp->next = NULL;
With
temp->next = p;

Access violation error while creating linked list

Trying to create Lined List. I am having problem in the deleteNode function created in LinkedList.cpp file. Experiencing given error
Unhandled exception at 0x00D04C3C in LinkedList.exe: 0xC0000005:
Access violation reading location 0x00000004.
previous->link = temp->link;
LinkedList.h file
class Node
{
public:
int data;
Node *link;
};
class LList
{
private:
Node *Head, *Tail;
//void recursiveTraverse(Node *);
public:
LList();
~LList();
void create();
Node *getNode();
void append(Node *);
void insert(Node *, int);
void rtraverse();
void deleteNode(int);
void display();
};
LinkedList.cpp
#include "stdafx.h"
#include "LinkedList.h"
#include <iostream>
using namespace std;
LList::LList()
{
Head = nullptr; Tail = nullptr;
}
LList::~LList()
{
Node *Temp;
while (Head != nullptr)
{
Temp = Head;
Head = Head->link;
delete Temp;
}
}
void LList::create()
{
char choice;
Node *newNode = nullptr;
while (5)
{
cout << "Enter Data in the List (Enter N to cancel) ";
cin >> choice;
if (choice == 'n' || choice == 'N')
{
break;
}
newNode = getNode();
append(newNode);
}
}
Node *LList::getNode()
{
Node *temp = new Node;
//cout << "Enter Data in the List";
cin >> temp->data;
temp->link = nullptr;
return temp;
}
void LList::append(Node *temp)
{
if (Head == nullptr)
{
Head = temp;
Tail = temp;
}
else
{
Tail->link = temp;
Tail = temp;
}
}
void LList::display()
{
Node *temp = Head;
if (temp == nullptr)
{
cout << "No Item in the List" << endl;
}
else
{
while (temp != nullptr)
{
cout << temp->data << "\t";
temp = temp->link;
}
cout << endl;
}
}
void LList::insert(Node *newNode, int position)
{
int count = 0; Node *temp, *previous = nullptr;
temp = Head;
if (temp == nullptr)
{
Head = newNode;
Tail = newNode;
}
else
{
while (temp == nullptr || count < position)
{
count++;
previous = temp;
temp = temp->link;
}
previous->link = newNode;
newNode->link = temp;
}
}
void LList::deleteNode(int position)
{
int count = 1; Node * temp, *previous = nullptr;
temp = Head;
if (temp == nullptr)
{
cout << "No Data to delete." << endl;
}
else
{
while (count <= position + 1)
{
if (position == count + 1)
{
count++;
previous = temp;
previous->link = temp->link;
}
else if (count == position + 1)
{
count++;
previous->link = temp->link;
}
count++;
temp = temp->link;
}
}
}
Main.cpp goes here
I see multiple things wrong here, any one of which could be causing your problem. If they don't fix it I could take another look if someone else doesn't get to it first.
First and foremost, your if statements in your delete function will always execute. Because you are assigning instead of checking for equality, ie '=' instead of '=='. This alone may fix the issue.
The other thing that jumps out of the page is that you are obviously dynamically allocating each node, and your delete function should be delete'ing the memory once you are done with it.
Fix those two first and then see where you are at.
Looks like temp cannot be a nullpointer at the line giving an error, but previous might be.
Important: Note that the line
else if (count = position + 1)
Is actually an assignment. You probably meant
else if (count == position + 1)
The same goes for the if statement before that.
Cheers!

Cannot Insert into Ordered Linked List

I am writing a three file C++ program for my class. This program is ordered linked list. The program compiles but crashes when I attempt to insert (Run the program, select choice press enter, type an int to insert and press enter). Any help would be greatly appreciated.
Driver File:
#include "SortedLinkedList.h"
#include <iostream>
using namespace std;
int displayMenu();
void proccessChoice(int, SortedLinkedList&);
int main()
{
SortedLinkedList sSList;
int choice = displayMenu();
do
{
if (choice != 3)
{
proccessChoice(choice, sSList);
}
} while (choice != 3);
return 0;
}
void proccessChoice(int input, SortedLinkedList& l)
{
switch(input)
{
case 1:
int num;
cout << "Please enter a int: ";
cin >> num;
l.addItem(num);
break;
case 2:
l.popFirst();
break;
}
}
int displayMenu()
{
int choice;
cout << "menu" << endl;
cout << "===========" << endl;
cout << "1. add an int" << endl;
cout << "2. Show Sorted Linked List" << endl;
cout << "3. Exit" << endl;
cin >> choice;
cin.ignore();
return choice;
}
Declaration File:
struct sslNode
{
sslNode* next;
int item;
};
class SortedLinkedList
{
private:
sslNode* head;
bool isEmpty ();
public:
SortedLinkedList();
~SortedLinkedList();
void addItem(int);
int popFirst();
};
Implementation File:
#include <iostream>
using namespace std;
#include "SortedLinkedList.h"
SortedLinkedList::SortedLinkedList()
{
head = NULL;
}
SortedLinkedList::~SortedLinkedList()
{
sslNode *temp, *nextLink;
nextLink = head;
while(nextLink != NULL)
{
temp = nextLink->next;
delete nextLink;
nextLink = temp;
}
}
bool SortedLinkedList::isEmpty()
{
return (head == NULL);
}
void SortedLinkedList::addItem(int itemToInsert)
{
sslNode* cur;
sslNode* prev;
sslNode* newNode = new sslNode();
newNode->item = itemToInsert;
newNode->next = NULL;
cur = head;
prev = NULL;
bool moreToSearch (cur != NULL);
while (moreToSearch) //Find insertion point
{
if (cur->item > newNode->item) // while current location has a greater value then what needs to be inserted move pointers forward.
{
prev = cur;
cur = cur->next;
moreToSearch = (cur != NULL);
}
else // if current loacation and what is to be inserted are equal or less then we have found the point of insertion
{
moreToSearch = false;
}
}
if (prev = NULL)
{
newNode->next = head->next;
head = newNode;
}
else
{
prev->next = newNode;
newNode->next = cur;
}
//Insert as only item in list
//Insert in found location
}
int SortedLinkedList::popFirst()
{
sslNode* first;
first = head->next;
head = head->next;
int item = first->item;
return item;
}
Your problem is you forgot an =
if (prev = NULL)
{
newNode->next = head->next;
head = newNode;
}
else
{
prev->next = newNode;
newNode->next = cur;
}
if(prev = NULL)
should be
if(prev == NULL)
right now this is always false because prev becomes null which evaluates to false
and then it fails at
prev->next = newNode;
because your are dereferencing the null pointer.
You'll also want to treat the case where head == NULL before trying to insert anything. Basically if head == NULL, head = newNode;
It crashes because head is initialized to NULL. You probably want to make a dummy head node, depending on your design, or check if its NULL before using it in addItem().
This is how things go down:
SortedLinkedLis
t::SortedLinkedList() // ctor is called
...
head = NULL
SortedLinkedList::addItem(int)
sslNode* cur;
...
cur = head;
...
bool moreToSearch (cur != NULL) // this is surely false
...
if (prev = NULL)
{
newNode->next = head->next;
...//BUT head == NULL ! crash!