I decided to practice my linked-list knowledge, and decided to create one in C++!
I ran this code on two different online compilers - one worked, and the other is giving me a segfault. I cannot figure out what the problem is within my code, and am wondering if you can help me.
#include <iostream>
using namespace std;
struct Node {
int val;
Node *next;
Node(int val){
this->val = val;
this->next = NULL;
}
};
class LinkedList {
public:
Node *head;
void insertAtHead(Node *temp)
{
if (head == NULL)
{
head = temp;
}
else
{
temp->next = head;
head = temp;
}
}
void printList()
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->val << endl;
temp = temp->next;
}
}
void insertAtBack(Node *temp)
{
if (head == NULL)
{
head = temp;
return;
}
Node *current = head;
while (current->next != NULL){
current = current->next;
}
current->next = temp;
}
void deleteNode(Node *temp)
{
if (head == NULL)
{
cout << "Empty List";
return;
}
if (head->val == temp->val)
{
head = head->next;
return;
}
Node *current = head;
while (current->next != NULL)
{
if (current->next->val == temp->val)
{
current->next = current->next->next;
return;
}
current = current->next;
}
}
};
int main()
{
Node *temp = new Node(10);
Node *temp2 = new Node(4);
Node *temp3 = new Node(17);
Node *temp4 = new Node(22);
Node *temp5 = new Node(1);
LinkedList x;
x.insertAtHead(temp);
x.insertAtHead(temp2);
x.insertAtBack(temp3);
// x.insertAtBack(temp4);
// x.insertAtBack(temp5);
// x.deleteNode(temp);
x.printList();
return 0;
}
The problem I am encountering is when I use the insertAtBack() method. It gives me a segfault, but I do not see what's wrong with the logic. It is pretty straight forward. The insertAtFront() method works, but once I call insertAtBack() my code fails.
make sure to initialize Node *head to NULL.
After insert temp(which is value 10), temp->next value becomes undefined value, because Node *head is undefined value.
Your LinkedList class is not initializing its head member. You need to add a constructor to initialize head to NULL.
Also, the class is leaking memory, as there is no destructor to free the nodes when a LinkedList instance is destroyed, and deleteNode() doesn't free the node being removed, either.
Try something more like this:
#include <iostream>
using namespace std;
struct Node
{
int val;
Node *next;
Node(int val) : val(val), next(NULL) { }
};
class LinkedList
{
private:
Node *head;
// if you are NOT using C++11 or later, add these
// until you are reading to tackle copy semantics!
/*
LinkedList(const LinkedList &);
LinkedList& operator=(const LinkedList &);
*/
public:
LinkedList() : head(NULL) {} // <-- add this!
~LinkedList() // <-- add this!
{
Node *current = head;
while (current)
{
Node *next = current->next;
delete current;
current = next;
}
}
void insertAtHead(Node *temp)
{
if (!head)
{
head = temp;
}
else
{
temp->next = head;
head = temp;
}
}
void printList()
{
Node *current = head;
while (current)
{
cout << current->val << endl;
current = current->next;
}
}
void insertAtBack(Node *temp)
{
if (!head)
{
head = temp;
return;
}
Node *current = head;
while (current->next) {
current = current->next;
}
current->next = temp;
}
void deleteNode(Node *temp)
{
if (!head)
{
cout << "Empty List";
return;
}
if (head == temp)
{
head = temp->next;
delete temp;
return;
}
Node *current = head;
while (current->next)
{
if (current->next == temp)
{
current->next = temp->next;
delete temp;
return;
}
current = current->next;
}
}
// if you ARE using C++11 or later, add these until
// you are reading to tackle copy and move semantics!
/*
LinkedList(const LinkedList &) = delete;
LinkedList(LinkedList &&) = delete;
LinkedList& operator=(const LinkedList &) = delete;
LinkedList& operator=(LinkedList &&) = delete;
*/
};
int main()
{
Node *temp = new Node(10);
Node *temp2 = new Node(4);
Node *temp3 = new Node(17);
Node *temp4 = new Node(22);
Node *temp5 = new Node(1);
LinkedList x;
x.insertAtHead(temp);
x.insertAtHead(temp2);
x.insertAtBack(temp3);
// x.insertAtBack(temp4);
// x.insertAtBack(temp5);
// x.deleteNode(temp);
x.printList();
return 0;
}
Which can then be simplified further:
#include <iostream>
using namespace std;
struct Node
{
int val;
Node *next;
Node(int val, Node *next = NULL) : val(val), next(next) { }
};
class LinkedList
{
private:
Node *head;
// if you are NOT using C++11 or later, add these
// until you are reading to tackle copy semantics!
/*
LinkedList(const LinkedList &);
LinkedList& operator=(const LinkedList &);
*/
public:
LinkedList() : head(NULL) {} // <-- add this!
~LinkedList() // <-- add this!
{
Node *current = head;
while (current)
{
Node *next = current->next;
delete current;
current = next;
}
}
Node* insertAtHead(int value)
{
Node *temp = new Node(value, head);
if (!head)
head = temp;
return temp;
}
void printList()
{
Node *current = head;
while (current)
{
cout << current->val << endl;
current = current->next;
}
}
Node* insertAtBack(int value)
{
Node **current = &head;
while (*current)
current = &((*current)->next);
*current = new Node(value);
return *current;
}
/*
void deleteNode(Node *temp)
{
Node *current = head, *previous = NULL;
while (current)
{
if (current == temp)
{
if (previous)
previous->next = temp->next;
if (head == temp)
head = temp->next;
delete temp;
return true;
}
previous = current;
current = current->next;
}
cout << "Not found" << endl;
return false;
}
*/
bool deleteValue(int value)
{
Node *current = head, *previous = NULL;
while (current)
{
if (current->val == value)
{
if (previous)
previous->next = temp->next;
if (head == temp)
head = temp->next;
delete temp;
return true;
}
previous = current;
current = current->next;
}
cout << "Not found" << endl;
return false;
}
// if you ARE using C++11 or later, add these until
// you are reading to tackle copy and move semantics!
/*
LinkedList(const LinkedList &) = delete;
LinkedList(LinkedList &&) = delete;
LinkedList& operator=(const LinkedList &) = delete;
LinkedList& operator=(LinkedList &&) = delete;
*/
};
int main()
{
LinkedList x;
x.insertAtHead(10);
x.insertAtHead(4);
x.insertAtBack(17);
// x.insertAtBack(22);
// x.insertAtBack(1);
// x.deleteValue(10);
x.printList();
return 0;
}
Related
I am making a LinkedList class in C++ with its methods, like adding nodes, traversing, and searching. When implementing the search function, it seems not working properly because it does not find the value in linked list when in fact it is inside the linked list. The code is shown below.
#include <iostream>
class Node {
public:
int value;
Node* next;
Node(int value, Node* next) {
this->value = value;
this->next = next;
}
};
class LinkedList {
public:
Node* head;
Node* tail;
LinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
LinkedList(Node* node) {
this->head = node;
this->tail = node;
}
void addNodeFront(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
this->tail = this->head;
this->head = node;
node->next = tail;
}
void addNodeBack(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
this->tail->next = node;
this->tail = node;
}
void addNodeAfterNode(Node* prevNode, Node* node) {
node->next = prevNode->next;
prevNode->next = node;
}
bool searchVal(int val) {
while(this->head != nullptr) {
if(this->head->value == val) return true;
this->head = this->head->next;
}
return false;
}
void deleteNode(Node* node) {
Node* prevNode = this->head;
while(prevNode->next != node) {
}
}
void traverseLinkedList() {
while(this->head!=nullptr) {
std::cout << this->head->value << "->";
this->head = this->head->next;
}
std::cout << "\n";
}
void sortLinkedList() {
}
};
int main() {
Node node1(2,nullptr);
Node node2(4,nullptr);
Node node3(3,nullptr);
LinkedList ls;
ls.addNodeFront(&node1);
ls.addNodeBack(&node3);
ls.addNodeAfterNode(&node3, &node2);
ls.traverseLinkedList();
if(ls.searchVal(4)) std::cout << "value found\n";
else std::cout << "value not found\n";
}
When I call the searchVal() function inside the main function it outputs value not found while value 4 is inside the linked list. What is wrong with my code?
When I call the searchVal() function inside the main function it
outputs value not found while value 4 is inside the linked list. What
is wrong with my code?
Just before you call searchVal(4) you call traverseLinkedList(), and traverseLinkedList() is implemented in such a way that when it returns, this->head will be NULL, which means that at that point your linked list is empty (and you have leaked memory). You'll want to modify traverseLinkedList() and searchVal() to not change the value of this->head (or any other member-variables of the LinkedList object) so that they don't modify the state of the list as a side effect.
The following code works fine:
#include <iostream>
using namespace std;
struct Node{
int data;
struct Node *next;
Node(int data, Node *next = nullptr){
this->data = data;
this->next = next;
}
};
void push_back(Node **head, int data){
if(*head == nullptr){
*head = new Node(data);
}
else{
Node *current = *head;
while(current->next != nullptr){
current = current->next;
}
current->next = new Node(data);
}
}
void Print(Node **head){
Node *current = *head;
while(current != nullptr){
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
int main(){
Node *head = nullptr;
push_back(&head, 5);
push_back(&head, 2);
push_back(&head, 1);
push_back(&head, -7);
Print(&head);
}
But when I try that bellow, nothing happens and head remains nullptr along with all the operations.
All I did was that I passed single pointers to function instead of double pointers:
#include <iostream>
using namespace std;
struct Node{
int data;
struct Node *next;
Node(int data, Node *next = nullptr){
this->data = data;
this->next = next;
}
};
void push_back(Node *head, int data){
if(head == nullptr){
head = new Node(data);
}
else{
Node *current = head;
while(current->next != nullptr){
current = current->next;
}
current->next = new Node(data);
}
}
void Print(Node *head){
Node *current = head;
while(current != nullptr){
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
int main(){
Node *head = nullptr;
push_back(head, 5);
push_back(head, 2);
push_back(head, 1);
push_back(head, -7);
Print(head);
}
I don't understand why do I need double pointers to make it work?
Is the second program only sending a copy of head to the functions and nothing more?
void push_back(Node *head, int data){
if(head == nullptr){
head = new Node(data);
}
else{
Node *current = head;
while(current->next != nullptr){
current = current->next;
}
current->next = new Node(data);
}
}
you can't change the value of pointer head by push_back in main function. We can change a object by passing its pointer or reference to another function, but not by passing itself! So each time head = new Node(data);(try to change head) actually not change head in the function which called push back() and caused memory overflow
I implemented my own simple version of a doubly linked list. Unfortunately, there seems to be an error with it. The head of the list seems to move to the new Node, each time I add one with push_back. Because of this, print will print the last value indefinitely.
Linked List:
struct doubly_linked_list
{
Node *head = nullptr;
Node *tail = nullptr;
void push_back(Node n)
{
if (this->head == nullptr)
{
this->head = &n;
this->tail = nullptr;
}
n.prev = this->tail;
if (this->tail)
{
n.prev->next = &n;
}
this->tail = &n;
}
void print()
{
Node *tmp = this->head;
while (tmp != nullptr)
{
std::cout << tmp->data << ", ";
tmp = tmp->next;
}
}
};
Where Node is implemented as
struct Node
{
int data;
Node *next = nullptr;
Node *prev = nullptr;
Node(int data)
{
this->data = data;
}
Node()
{
this->data = -1;
}
};
main
int main()
{
doubly_linked_list dl;
dl.push_back(Node{3});
dl.push_back(Node{2});
dl.push_back(Node{1});
dl.push_back(Node{0});
dl.push_back(Node{5});
dl.print(); // print 5 forever
}
Disclaimer: Pls be aware that the topic of this post is educational. I know about the lists in the c++ standard.
Here is a working example with raw pointers, deppending on what you are doing you might want to change that to smart pointers.
#include <iostream>
struct Node
{
int data;
Node *next = nullptr;
Node *prev = nullptr;
Node(int data)
{
this->data = data;
}
Node()
{
this->data = -1;
}
};
struct doubly_linked_list
{
Node *head = nullptr;
Node *tail = nullptr;
void push_back(Node* n)
{
if (this->head == nullptr)
{
this->head = n;
this->tail = nullptr;
}
n->prev = this->tail;
if (this->tail)
{
n->prev->next = n;
}
this->tail = n;
}
void print()
{
Node *tmp = this->head;
while (tmp != nullptr)
{
std::cout << tmp->data << ", ";
tmp = tmp->next;
}
}
};
int main()
{
doubly_linked_list dl;
dl.push_back(new Node{3});
dl.push_back(new Node{2});
dl.push_back(new Node{1});
dl.push_back(new Node{0});
dl.push_back(new Node{5});
dl.print(); // print 5 forever
}
Fairly new to implementing stacks and was looking for some possible feedback. My code gives the correct output, but I know this doesn't always mean it is working as it is suppose to. I chose to take the approach that implementing a stack using a linked list was essentially the same as your regular linked list implementation except that all the operations are done on the end of the list. I was not too sure if this approach was correct, but it followed the first in last out approach, and has the same complexity for access & search (O(n)) and insertion and deletion O(1). Such as pop() would just be deleting a node from the end of the linked list, and push() would just be appending a node to the end of the linked list. I have pasted my code below with comments within them explaining what I am doing or trying to do (if it is incorrect).
#include <iostream>
struct Node{
int data;
Node* next;
};
bool isEmpty(Node** stack){
if(*stack == NULL){
return true;
}
return false;
}
void push(Node** stack, int data){
Node* new_node = new Node();
new_node->data = data;
new_node->next=NULL;
// stack similar to "head"
if(isEmpty(&(*stack))){
*stack = new_node;
return;
}
Node* temp = *stack;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new_node;
}
void pop(Node** stack){
// checking if stack is empty
if(isEmpty(&(*stack))){
std::cout<<"Stack underflow"<<std::endl;
return;
}
Node* deleteMe = *stack;
// if at the first element in the stack
if(deleteMe->next == NULL){
*stack = (*stack)->next;
delete deleteMe;
return;
}
while(deleteMe->next != NULL){
if(deleteMe->next->next==NULL){
// saving the current location of the node before the node which I want to delete
Node* temp = deleteMe;
// updating the deleteMe pointer to the node which I want to delete
deleteMe = deleteMe->next;
// setting the current node before the deleteMe node to point to NULL instead of the node which I want to delete
temp->next = NULL;
delete deleteMe;
return;
}
deleteMe = deleteMe->next;
}
}
void printList(Node* stack){
Node* temp = stack;
while(temp!=NULL){
std::cout<<temp->data<<" ";
temp = temp->next;
}
std::cout<<"\n";
}
int top(Node** stack){
Node* top = *stack;
while(top->next!=NULL){
top = top->next;
}
return top->data;
}
int main(){
Node* stack = NULL;
// testing implementation below
push(&stack,10);
std::cout<<top(&stack)<<std::endl;
push(&stack,20);
std::cout<<top(&stack)<<std::endl;
push(&stack,30);
push(&stack,40);
printList(stack);
std::cout<<top(&stack)<<std::endl;
pop(&stack);
pop(&stack);
push(&stack,40);
std::cout<<top(&stack)<<std::endl;
}
Your implementation looks fine, one additional improvement can be done by maintaining head and tail pointer so that you can remove 1st and last element as needed. Here is example c++ code.
#include <iostream>
using namespace std;
template <class T> class node {
public:
node<T>() {}
~node<T>() {}
T data;
node<T> *next;
};
template <class T> class linked_list {
public:
linked_list<T>() : head(NULL), tail(NULL) {}
~linked_list<T>() {}
virtual void addFirst(T data) {
node<T> *n = new node<T>();
if (head == NULL)
tail = n;
n->data = data;
n->next = head;
head = n;
size++;
}
virtual void addLast(T data) {
node<T> *n = new node<T>();
n->data = data;
if (tail == NULL) {
head = n;
} else {
tail->next = n;
}
n->next = NULL;
tail = n;
}
virtual void reverse() {
if ((head == NULL) || (head->next == NULL))
return;
node<T> *current = head;
node<T> *previous = NULL;
node<T> *next = current->next;
tail = current;
while (current) {
next = current->next;
current->next = previous;
previous = current;
current = next;
}
head = previous;
}
virtual void print_nodes() {
node<T> *temp = head;
while (temp) {
cout << temp->data << " " << flush;
temp = temp->next;
}
cout << endl;
}
virtual void removeLast() {
node<T> *temp = head;
while (temp->next->next) {
temp = temp->next;
}
tail = temp;
delete temp->next;
temp->next = NULL;
}
virtual void removeFirst() {
node<T> *temp = head;
head = head->next;
delete temp;
}
private:
node<T> *head;
node<T> *tail;
uint32_t size;
};
int main(int argc, const char *argv[]) {
linked_list<uint32_t> *llist = new linked_list<uint32_t>();
llist->addLast(1);
llist->addFirst(5);
llist->addFirst(10);
llist->addFirst(15);
llist->addFirst(20);
llist->addLast(30);
llist->addFirst(40);
llist->print_nodes();
llist->reverse();
llist->print_nodes();
llist->removeLast();
llist->print_nodes();
llist->removeFirst();
llist->print_nodes();
return 0;
}
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
}
}