Delete node in linked list c++ - c++

I think I am having an issue with the logic in my DeleteNode function in this linked list class. When I test the code it sometimes returns false when the node is in the list, and I haven't found a pattern.
If there is absolutely nothing wrong with this implementation of DeleteNode, the problem could be in the code where I am using the method, but if it is I want to take a look at that on my own before posting that code. (The purpose of me saying that is to let you know this code might be fine, but I want to know for sure).
Thanks so much!
Header file:
#include <string.h>
#ifndef LIST_H
#define LIST_H
class List {
public:
typedef struct node {
char * data;
node * next;
}* nodePtr;
nodePtr curr;
nodePtr temp;
nodePtr head;
public:
List();
void AddNode(char * addData);
bool DeleteNode(char * delData);
bool containsNode(char * Data);
void PrintList();
};
#endif
.cpp file:
#include <iostream>
#include "SLList.h"
using namespace std;
List::List() {
head = NULL;
curr = NULL;
temp = NULL;
}
bool List::DeleteNode(char * delData) {
nodePtr delPtr = NULL;
temp = head;
curr = head;
while (curr != NULL && curr->data != delData) {
temp = curr;
curr = curr->next;
}
if (curr == NULL) {
return false;
}
else {
delPtr = curr;
curr = curr->next;
temp->next = curr;
delete delPtr;
return true;
}
}

Related

Tried appending multiple nodes in LinkedList c++ but it's just printing 1 node

I tried appending multiple nodes in LinkedList c++ but it is just printing 1 node as I run it. Kindly review it, and help me fix it.
#include <iostream>
using namespace std;
//here is the node
struct Node {
int data;
struct Node* next;
};
// ------------here is linkedlist-----------
class LinkedList {
private:
Node* head;
public:
LinkedList()
{
head = NULL;
}
//----------- append function ------------
void appendNode(int d)
{
Node* newNode = new Node;
Node* nodePtr;
newNode->data = d;
newNode->next = NULL;
if (!head)
{
head = newNode;
}
else
{
nodePtr = head;
while (nodePtr->next)
{
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
}
//--------------- display function--------------
void display()
{
Node* nodePtr;
nodePtr = head;
while (nodePtr != NULL)
{
cout << nodePtr->data << endl;
nodePtr = nodePtr->next;
}
}
};
//-------------- main function -------------
int main()
{
LinkedList ll;
ll.appendNode(2);
ll.appendNode(21);
ll.appendNode(11);
ll.display();
}
Change your while loop from:
while (nodePtr->next)
{
nodePtr = nodePtr->next;
nodePtr->next = newNode;
...
}
to
while (nodePtr->next)
{
nodePtr = nodePtr->next;
...
}
nodePtr->next = newNode;
Also, in C++ use nullptr instead of NULL.

List not printing all the Elements

So basically I created a linked list with the append and prepend functions and when I print the list it is only printing out 12 which I prepended to front of the list and not the other elements, how do I fix this issue? so that all the elements will be printed. I think that my append and prepend functions are correct but I do not know what could be wrong.
#include <iostream>
#include "List.h"
using namespace std;
int main() {
List* list = new List();
list->Append(1);
list->Append(2423);
list->Prepend(12);
list->print();
system("pause");
return 0;
}
#include<iostream>
#include "List.h"
using namespace std;
List::List() {
this->HEAD = NULL;
this->TAIL=NULL;
}
void List::Append(int Data_val) {
NODE* Current = new NODE;
Current->data = Data_val;
if (HEAD == NULL) {
HEAD = Current;
TAIL = Current;
Current->next = nullptr;
}
else {
TAIL->next= Current;
TAIL = Current;
Current->next = nullptr;
}
}
void List::Prepend(int data_val) {
NODE* Current = new NODE;
Current->data = data_val;
if (HEAD = NULL) {
Current->next = nullptr;
HEAD = Current;
TAIL = Current;
}
else {
Current->next = HEAD;
HEAD = Current;
Current->next = nullptr;
}
}
void List::print() {
NODE* Current = HEAD;
while (Current != NULL) {
cout << Current->data << endl;
Current=Current->next;
}
}
#ifndef LIST_H
#define LIST_H
struct NODE {
int data;
NODE* next;
};
class List
{
public:
List();
void Append(int data_val);
void Prepend(int data_val);
//create prepend
//create insertafter
//create delte
void print();
private:
//
NODE * HEAD;
NODE* TAIL;
};
#endif
You havae a bug with your List::Prepand() method. The "Current->next = nullptr;"
line is not needed within "else" block.
void List::Prepend(int data_val) {
NODE* Current = new NODE;
Current->data = data_val;
if (HEAD == NULL) {
Current->next = nullptr;
HEAD = TAIL = Current;
}
else {
Current->next = HEAD;
HEAD = Current;
Current->next = nullptr; // <---- remove this line
}
}

Is this the proper way to create a search function while passing int num? Using a linked list libary

Header File:
#ifndef LL_H
#define LL_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
// include iostream so anything that includes this file can use cout
#include <iostream>
// Struct which will be the building block of our list
struct node
{
int val;
node* next;
};
// Contents of 11.h
// Linked list class definition
class LL
{
public:
LL();
bool removeFront();
bool removeBack();
node* search(int);
void print();
private:
node* head;
};
#endif
Source File:
#include "ll.h"
LL::LL()
{
head = NULL;
}
void LL::search (int num)//heading of the function
{
node* newNode = new node;
newNode->val = num;
newNode->next = NULL;
while (newNode == NULL)
{
if (newNode->val == num)
{
return newNode;
}
newNode = newNode->next; //point to the next node
}
return; //not sure if needed
}
The program will read in a
text file called “cmd.txt” that will instruct what operations to run. our program should implement a C++ class which will be used to represent the linked list.
Search should look more like:
node* LL::Search(int num)
{
node* current = head;
while (current != null)
{
if (current->val == num)
{
return current;
}
current = current->next;
}
return null;
}
This is the correct format
node* LL::search(int num)
{
node* newNode = new node;
newNode = head;
while (newNode->next != NULL)
{
if (newNode->val == num)
{
return newNode;
}
newNode = newNode->next;
}
return NULL;
}

Simple Binary Search Tree Non recursive Add function

I am trying to write a BST() that would take in strings as nodes without using recursion. here is the add function of my code, can you please review and see if it is easy to understand/follow and point out mistakes. I am new to programming and would appreciate any feedback.
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main() {
class BST {
private:
struct Node {
string value;
Node* left;
Node* right;
};
Node* root;
public:
BST()
{
root = NULL;
}
~BST()
{
stack<Node*> nodes;
nodes.push( root);
while( ! nodes.empty())
{
Node* topNode = nodes.top();
nodes.pop();
if ( topNode->left )
nodes.push(topNode->left);
if ( topNode->right )
nodes.push(topNode->right);
delete topNode;
}
}
Node* GetNewNode(string data){
Node* newNode = new Node();
newNode->value=data;
newNode->left = newNode->right = NULL;
return root;
}
Node* Insert(Node* rootPtr,string data){
if(rootPtr == NULL){
rootPtr = GetNewNode(data);
return rootPtr;
}
else if(data<= rootPtr->value){
rootPtr->left = Insert(rootPtr->left,data);
}
else {
rootPtr->right = Insert(rootPtr->right,data);
}
return rootPtr;
}
};
}
1 - In the insert function:
while (root != NULL) { // this shouldn't be root, the root isn't the node that traverse your tree, this has to be current
.....
}
2- you never add your new node, you just keep looping till you reach a null.
You should traverse your tree till u find the right position to insert your node then add the new node, something like:
current = root;
prev = current;
while (current!= NULL) {
prev = current;
if (current->value.compare(word) < 0)
current = current->left;
else
current = current->right;
}
//here your new node should be on the left or the right of the prev node
3 - in "GetNewNode" return the new node not the root
4 - your delete function is a mess, u shall think about it again and re-implement it.
Finally I strongly recommend you to check and understand a ready made implementation from the web then try to write your BST again.

Issue with Printing Data stored in Linked List

I'm having some issues when I call the print function for a linked list I created. I created three list objects, stored within them the ints 1,2,3 and then when I run the program, the data is printed out, followed by a hexadecimal number. Here is the output:
10x47e864
20x47e864
30x47e864
Process returned 0 (0x0) execution time : 0.026 s
Press any key to continue.
Here is the .h file:
#ifndef LIST_H
#define LIST_H
class List
{
public:
List();
void AddNode(int addData);
void DeleteNode(int delData);
void PrintList();
private:
typedef struct node{
int data;
node* next;
}* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
};
#endif // LIST_H
Here is the .cpp
#include "List.h"
#include <cstdlib>//for NULL const
#include <iostream>
using namespace std;
int main(){
List li;
li.AddNode(1);
li.AddNode(2);
li.AddNode(3);
li.PrintList();
}
List::List()
{
head = NULL;
curr = NULL;
temp=NULL;
}
void List::AddNode(int addData){
nodePtr n = new node;
n->next=NULL;
n->data=addData;
if(head!=NULL)//if a linked list exists
{
curr = head;
while(curr->next!=NULL){
//while not at end of list
curr=curr->next;//advances ptr until last node in list
}
curr->next=n;//make n point to last node;
}
else{//if list does not exist
head = n;
}
}
void List::DeleteNode(int delData){
nodePtr delPtr = NULL;
temp = head;
curr = head;
while(curr!=NULL && curr->data!= delData){
//to pass through list
temp = curr;
curr = curr->next;//traverse list until delData not found
}
if(curr==NULL)//passed through list, delData int not found
{
cout<<delData<<" was not in list\n";
delete delPtr;
}
else{
delPtr = curr;
curr == curr->next;//patching hole in list
delete delPtr;
cout<<"the value "<<delData<<" was deleted\n";
}
}
void List::PrintList(){
curr = head;
while(curr!=NULL){
cout<<curr->data<<cout<<endl;
curr=curr->next;//adv the curr ptr
}
}
Thank you all for the help!
You have an incorrect cout statement in the PrintList() remove the second cout from this line, i.e, change
cout<<curr->data<<cout<<endl;
to
cout << curr->data << endl;