The following naive code implements a linked list, without printing all the elements in the main function, everything would be fine. However, the LinkedList::printll function will trigger a set fault(Gcc 5.3.0), the problem is related to the appropriate handling of the head node I suppose...
So, is there any way to make this code work with least modification of the printll function?
#include <iostream>
using namespace std;
struct Node{
int value;
Node* next;
};
struct LinkedList{
Node* head= NULL ;
void append(int);
void printll();
};
void LinkedList::append(int data){
Node* cur = head;
Node* tmp = new Node;
tmp->value = data;
tmp->next = NULL;
if(!cur){
cur = tmp; // cur-> head
}
else{
while(cur->next != NULL){
cur = cur->next;
}
cur->next = tmp;
}
std::cout<<cur->value<<std::endl; // cur-> temp
delete tmp; // comment out
}
void LinkedList::printll(){
Node* cur = head;
while(cur->next != NULL){ //
std::cout<<cur->value<<std::endl;
cur = cur->next;
}
}
int main(){
LinkedList LL;
LL.append(5);
LL.append(6);
LL.append(7);
LL.printll(); // --without this, the program is fine
return 0;
}
You have some bugs in append:
if(!cur){
cur = tmp;
}
This only assigns to the local copy. I assume you are trying to set head here, so do that: head = tmp;. Note that in this case, you can't print cur, since you haven't set it. You could print tmp->value though.
Then:
delete tmp;
You only just created it and assigned it into place - why are you deleting it? You know that there is still a pointer to it. Only delete it when you come to clean up the list when you are done with it (which you don't do at all at the moment).
Other than that, your printll won't print the last element - think about when it will stop:
A -> B -> C -> NULL
It will stop on node C, but never print C's value. You can just replace:
while(cur->next != NULL){
with
while(cur != nullptr){
(Also, I don't like endl).
See here for these changes running:
#include <iostream>
struct Node{
int value;
Node* next;
};
struct LinkedList{
Node* head = nullptr ;
void append(int);
void printll();
};
void LinkedList::append(int data){
Node* cur = head;
Node* tmp = new Node;
tmp->value = data;
tmp->next = nullptr;
if(!cur){
head = tmp;
}
else{
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = tmp;
}
}
void LinkedList::printll(){
Node* cur = head;
while(cur != nullptr){
std::cout << cur->value << '\n';
cur = cur->next;
}
}
int main(){
LinkedList LL;
LL.append(5);
LL.append(6);
LL.append(7);
LL.printll();
}
1.you cann't
delete tmp;
cause tmp is a pointer, when you run delete tmp, you delete the object.
2.the print function should like this:
void LinkedList::printll(){
Node* cur = head;
while(cur->next != NULL){ // -> problems is here
std::cout<<cur->value<<std::endl;
cur = cur->next;
}
std::cout<<cur->value<<std::endl;
}
Related
int main(){
node** head = nullptr;
push(&head,1);
push(&head,2);
push(&head,3);
printlist(&head);
return 0;
}
void push(node*** head_ptr,int data){
node* new_node = new node;
new_node->data = data;
if(*head_ptr != nullptr){
node* temp = **head_ptr;
while(temp != nullptr){
temp = temp->next;
}
temp->next = new_node;
}
else{
*head_ptr = &new_node;
}
}
Whenever node* new_node = new node; executes it overwrites head_ptr or head.
Is it because new is allocating same memory block everytime?
You assign the address of a local variable, and then use that value after the function has returned, which means your program's behaviour is undefined.
*head_ptr = &new_node;
Having fixed that, you dereference a null pointer, which means your program's behaviour is still undefined.
node* temp = *head_ptr;
while(temp != nullptr){
temp = temp->next;
}
temp->next = new_node; // you have just looped until temp is null
You don't need ***. You shouldn't use **. If you want to modify something, pass it by reference, not pointer.
#include <iostream>
struct node {
int data;
node * next;
};
void printlist(node* n) {
for (; n; n = n->next) {
std::cout << n->data;
}
}
void push(node*& head,int data){
node* new_node = new node{ data, nullptr };
if(head != nullptr){
node* temp = head;
while(temp->next != nullptr){
temp = temp->next;
}
temp->next = new_node;
}
else{
head = new_node;
}
}
int main(){
node* head = nullptr;
push(head,1);
push(head,2);
push(head,3);
printlist(head);
return 0;
}
There are several issues:
There is one level of indirection too much. You return the pointer to new_node but you need to return new_node itself. Once the function has ended, the local variable new_node doesn't exist anymore.
You need to initialize the next pointer to nullptr somewhere. I'm not sure if you did this, because you didn't post all of your relevant code.
while (temp != nullptr) is wrong, once the loop has ended, temp is obviously nullptr but you want the last element of the list. You need while (temp->next != nullptr).
struct node
{
int data;
node* next = nullptr; // initialize next to nullptr
};
void push(node** head_ptr, int data) { // only 2 stars
node* new_node = new node;
new_node->data = data;
if (*head_ptr != nullptr) {
node* temp = *head_ptr;
while (temp->next != nullptr) { // use temp->next instead of temp
temp = temp->next;
}
temp->next = new_node;
}
else {
*head_ptr = new_node; // return new_node instead of a pointer
} // to new_node
}
int main() {
node* head = nullptr;
push(&head, 1);
push(&head, 2);
push(&head, 3);
printlist(&head); // you didn't show printlist
// so there might be problems there too
return 0;
}
Bonuses:
Your code is very inefficient, because for each push you need to find the end of the list which is becoming slower and slower as the list grows. Hint: google schlemiel the painter.
There is room for further improvements, for example having a constructor for node and passing head as reference rather than as pointer to a pointer.
I am trying to code for deletion of a node with k as data of linked list. The below program is running fine, but it is not giving desired output if we have to delete the head node. For example, if the linked list is 98->6->1 and I have to delete 98, then the output which the program is showing is 0->6->1. Except for the deletion of the head node, it is working correctly for all other cases.
Below is the c++ code for the same.
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node* next;
};
Node * insert(Node* head, int data) {
Node* new_node= new Node();
new_node->data = data;
new_node->next = head;
head = new_node;
return head;
}
void deleteNode(Node *head, int key)
{
Node *temp = head;
Node *prev = NULL;
if(temp!=NULL && temp->data==key){
head = temp->next;
delete temp;
}
else{
while(temp!=NULL && temp->data!=key){
prev = temp;
temp = temp->next;
}
if(temp == NULL){
return;
}
prev->next = temp->next;
delete temp;
}
}
void display(Node * head) {
while(head != NULL)
{
cout<<head->data<<" ";
head = head->next;
}
}
int main() {
Node * head = NULL;
head = insert(head, 1);
head=insert(head,6);
head=insert(head,98);
deleteNode(head,98);
display(head);
return 0;
}
In deleteNode(), you are passing in the head node by value, so any modification made to it is not reflected back to the caller. You need to either return the new head, like you do with Insert(), or else you need to pass in the head by reference:
void deleteNode(Node* &head, int key)
The mistake in your code is that the head you are passing to deleteNode function is by value so the changes made to the head function is not displayed. The head in your function is not the same head in your main function it is just the copy of that head, so to apply the same change being applied to the head in deletenode function to the original head, you have to pass the address of the head(or pass by reference).
Editing in your code-
I have applied changes in deleteNode function first five lines and the third last line when you are passing value by reference deleteNode(&head,98);
Edited code-
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node* next;
};
Node * insert(Node* head, int data) {
Node* new_node= new Node();
new_node->data = data;
new_node->next = head;
head = new_node;
return head;
}
void deleteNode(Node **head, int key)
{
Node *temp = *head;
Node *prev = NULL;
if(temp!=NULL && temp->data==key){
*head = temp->next;
temp->next = NULL;
delete temp;
}
else{
while(temp!=NULL && temp->data!=key){
prev = temp;
temp = temp->next;
}
if(temp == NULL){
return;
}
prev->next = temp->next;
// temp->next = NULL;
delete temp;
}
}
void display(Node * head) {
while(head != NULL)
{
cout<<head->data<<" ";
head = head->next;
}
}
int main() {
Node * head = NULL;
head = insert(head, 1);
head=insert(head,6);
head=insert(head,98);
deleteNode(&head,98);
display(head);
return 0;
}
Now if you run the above code the output you will get after deleting node of value 98 will be 6->1.
Hope you will find it helpful.
I was trying to delete alternate nodes in a linklist. I observed a strange behaviour.
void delete_alternate_node_LinkedList(Node *head) {
Node *prev = head;
Node *curr = head->next;
while (prev != NULL and curr != NULL) {
prev->next = curr->next;
free(curr);
prev = prev->next;
if (prev != NULL) {
curr = prev->next;
}
}
}
This code works fine except the head being nullptr when I use free to delicate or intentionally keep a memory leak but if I change the line free(curr) with delete curr, I get a segmentation fault.
Can anyone explain me the reason?
Here are the boilerplate codes
class Node {
public:
int data;
Node * next;
Node(int data){
this -> data = data;
this -> next = NULL;
}
~Node() {
if(next) {
delete next;
}
}
};
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;
}
Your destructor has a problem
Let's assume
A->B->C->D->nullptr
Now when you delete B it invokes destructor (if you use free it won't).
it will delete recursively C (which in turn delete D) and ..... till the end
so in next iteration you are holding on to a dangling pointer (C) and getting the segfault when you are trying to derefence it.
How do I make my program print the Linked List backwards? I got the printForward function working fine but the printBackwards function just doesn't seem to do anything. I think I'm on the right track but I'm a little stuck right now. I think the while loop isn't running because temp is NULL for some reason.
Any help would be great.
Thanks
List.h
#include <iostream>
using namespace std;
class LinkedList
{
private:
struct Node
{
int data;
Node * next;
Node * prev;
};
Node * head, *tail;
public:
LinkedList();
bool addAtBeginning(int val);
bool remove(int val);
void printForward() const;
void printBackward() const;
};
#endif
List.cpp
#include "List.h"
LinkedList::LinkedList()
{
head = NULL;
tail = NULL;
}
bool LinkedList::addAtBeginning(int val)
{
Node* temp;
temp = new Node;
temp->data = val;
temp->next = head;
head = temp;
return false;
}
bool LinkedList::remove(int val)
{
return false;
}
void LinkedList::printForward() const
{
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void LinkedList::printBackward() const
{
Node* temp = tail;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
app.cpp
#include "list.h"
int main()
{
LinkedList aList;
aList.addAtBeginning(3);
aList.addAtBeginning(10);
aList.addAtBeginning(1);
aList.addAtBeginning(7);
aList.addAtBeginning(9);
aList.addAtBeginning(12);
aList.printForward();
aList.printBackward();
system("pause");
return 0;
}
I find it a bit odd that you only have an addAtBeginning method, and no method to add at the end, the latter which I would consider to be normal use of a linked list. That being said, I think the immediate problem here is that you never assign the tail to anything. Try this version of addAtBeginning:
bool LinkedList::addAtBeginning(int val)
{
Node* temp;
temp = new Node;
temp->data = val;
temp->next = head;
if (head != NULL)
{
head->prev = temp;
}
if (head == NULL)
{
tail = temp;
}
head = temp;
return false;
`}
The logic here is that for the first addition to an empty list, we assign the head and tail to the initial node. Then, in subsequent additions, we add a new element to the head of the list, and then assign both the next and prev pointers, to link the new node in both directions. This should allow you to iterate the list backwards, starting with the tail.
Update addAtBeginning function with given:
bool LinkedList::addAtBeginning(int val)
{
Node* temp;
temp = new Node;
temp->data = val;
temp->prev = temp->next = NULL;
// If adding first node, then head is NULL.
// Then, set Head and Tail to this new added node
if(head == NULL){
// If this linked list is circular
temp->next = temp->prev = temp;
head = tail = temp;
}else{ // If we already have at least one node in the list
// If this linked list is circular
temp->prev = head->prev;
temp->next = head;
head->prev = temp;
head = temp;
}
return false;
}
But remember, if you copy this function with the parts that it makes this list circular, you will get an infinite loop. So, either change print function or dont copy that parts.
It's been a week since i started learning about linked list and i only managed to learn about singly linked list. So today i implemented the linked list which i learned in c++ and while i tried to run it the code goes into an infinite loop of some random numbers. I tried debugging the code but i coudn't find whats so ever is wrong with the code. The code is below. Help is appreciated.Thanks
#include <iostream>
using namespace std;
struct node{
int data;
node * next;
};
class singly{
private:
node * head,*tail;
public:
singly(){
head=NULL;
tail=NULL;
}
void createNode(int value){
node * temp = new node;
temp->data=value;
temp->next=NULL;
if(head==NULL){
head=temp;
tail=temp;
temp=NULL;
}
else{
tail->next=temp;
tail=temp;
}
}
void display(){
node * temp = new node;
head=temp;
while(temp!=NULL){
cout << temp->data << "\t" << endl;
temp->next=temp;
}
}
void insert_end(int value){
node*newnode = new node;
node*temp = new node;
newnode->data=value;
newnode->next=NULL;
temp=head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next=newnode;
}
void delete_node(){
node*current = new node;
node*previous = new node;
current = head;
while(current->next!=NULL){
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
};
int main(){
singly lists;
lists.createNode(32);
lists.createNode(654);
lists.createNode(34);
lists.createNode(234);
cout<<"\n--------------------------------------------------\n";
cout<<"---------------Displaying All nodes---------------";
cout<<"\n--------------------------------------------------\n";
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Inserting At End-----------------";
cout<<"\n--------------------------------------------------\n";
lists.createNode(55);
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Deleing At End-------------------";
cout<<"\n--------------------------------------------------\n";
lists.delete_node();
lists.display();
}
The member function display does not make sense.
It overwtites the data member head with uninitialized newly created temp.
node * temp = new node;
head=temp;
so the function invokes undefined behavior.
The function can look like
void display()
{
for ( node * temp = head; temp != nullptr; temp = temp->next )
{
cout << temp->data << "\t";
}
}
Or it is better to define it the following way
std::ostream & display( std::ostream &os = std::cout )
{
for ( node * temp = head; temp != nullptr; temp = temp->next )
{
os << temp->data << "\t";
}
return os;
}
The data member insert_end is also wrong. It does not take into account that head and tail can be equalto nullptr and does not change them.
The function can be defined the following way
void insert_end(int value)
{
node *newnode = new node { value, nullptr };
if ( tail == nullptr )
{
head = tail = newnode;
}
else
{
tail = tail->next = newnode;
}
}
The member function delete_node firstly does not make sense for a singly-linked list and again is wrong and invokes undefined behavior. The function should remove the first node from the list.
Nevertheless if you want to remove the last node from the list then the function can look like
void delete_node()
{
if ( head != nullptr )
{
tail = nullptr;
node *current = head;
while ( current->next )
{
tail = current;
current = current->next;
}
if ( tail == nullptr )
{
head = tail;
}
else
{
tail->next = nullptr;
}
delete current;
}
}
For starters, display() is wrong. You want the update to be temp = temp->next; and it can also be initialized as node * temp = head hence not requiring the second line.
Your delete_node() can be re-written to:
if (head->next == NULL) // handles the case that it consists of 1 element
{
delete head;
head = NULL;
}
else
{
node *nextToEnd = head;
node *end = head->next;
while (end->next != NULL)
{
nextToEnd = end;
end = end->next;
}
delete end;
nextToEnd->next = NULL;
}
As stated in the comments, review the use of the new keyword