I am trying to create singly-linked list. After the first push, head is still null. Why is the head not updated after the first push?
using namespace std;
typedef struct node {
int data; // will store information
node *next; // the reference to the next node
};
void push(node*,int);
void print(node*);
int main()
{
node* head = NULL; //empty linked list
push(head, 2);
if (head == NULL) {
cout << "vrvrvr";
}
push(head, 3);
push(head, 5);
push(head, 2);
//print(head);
getchar();
return 0;
}
void push(node* x, int y){
node *temp = new node();
if (x == NULL) { // check linked list is empty
temp->next = x;
temp->data = y;
x = temp;
}
else {
node *temp1 = new node();
temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
delete temp1; // 'temp' node will be the last node
}
}
void print(node* x){
node *temp1 = new node();
temp1 = x;
while (temp1->next != NULL) {
cout << temp1->data << endl;
temp1 = temp1->next;
}
}
The main problem with push is that the changes made to x in the function are local to the function. It does not change the value of head in main.
You can fix that by changing the argument type to node*&.
void push(node*& x, int y) {
...
}
The other problems I see are in the block:
else {
node *temp1 = new node();
temp1 = x;
// Problem 1:
// After this, the memory returned by the previous line is lost.
// It is a memory leak.
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
delete temp1; // 'temp' node will be the last node
// Problem 2:
// You are deleting a node from the linked list.
// The linked list now has a dangling pointer.
}
You can correct those problems by using:
node *temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
}
Suggested improvements
Remove typedef from definition of node. It is a dangling typedef in your posted code. Also, you can use node without a typedef in C++.
struct node {
int data;
node *next;
};
Add a constructor to node.
struct node {
node(int d) : data(d), next(nullptr) {}
int data;
node *next;
};
That will simplify your code in push.
void push(node*& x, int y){
node *temp = new node(y);
if (x == NULL) { // check linked list is empty
x = temp;
}
else {
node *temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
}
}
Related
I am really new to data structures. I am trying to figure out why my insertback() function doesn't work. The first print does 3,2,1 but the second doesn't print anything. I think it has something to do with head, but I'm not really sure. Please help.
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
class lst
{
public:
void Insertfron(int x);
void Print();
void Insertback(int x);
private:
Node* head;
};
void lst::Insertfron(int x)
{
Node* temp = new Node;
temp->data = x;
temp->next = head;
head = temp;
}
void lst::Print()
{
Node* temp = head;
while(temp->next!=NULL)
{
cout<<temp->data<<' ';
temp=temp->next;
}
cout<< endl;
}
void lst::Insertback(int x)
{
Node* backinst = new Node;
backinst->data = x;
backinst->next = NULL;
Node* temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = backinst;
}
int main()
{
lst listt;
listt.Insertfron(1);
listt.Insertfron(2);
listt.Insertfron(3);
listt.Print();
listt.Insertback(4);
listt.Print();
return 0;
}
You are not initializing head to NULL to indicate an empty list, so ``head` will have a random garbage value, and thus all of your methods exhibit undefined behavior.
Once that is fixed, your while loops in both Print() and Insertback() are buggy, as they are not account for head being NULL when the list is empty.
Also, you are leaking every node you create. You need to add a destructor to free the nodes when you are done using the list.
With that said, try something more like this instead:
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
class lst
{
public:
lst();
~lst();
void Insertfron(int x);
void Print();
void Insertback(int x);
private:
Node* head;
};
lst::lst()
: head(NULL)
{
}
lst::~lst()
{
while (head != NULL)
{
Node *next = head->next;
delete head;
head = next;
}
}
void lst::Insertfron(int x)
{
Node* temp = new Node;
temp->data = x;
temp->next = head;
head = temp;
}
void lst::Print()
{
Node* temp = head;
while (temp != NULL)
{
cout << temp->data << ' ';
temp = temp->next;
}
cout << endl;
}
void lst::Insertback(int x)
{
Node* backinst = new Node;
backinst->data = x;
backinst->next = NULL;
if (head == NULL)
{
head = backinst;
}
else
{
Node* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = backinst;
}
}
int main()
{
lst listt;
listt.Insertfron(1);
listt.Insertfron(2);
listt.Insertfron(3);
listt.Print();
listt.Insertback(4);
listt.Print();
return 0;
}
That being said, Insertback() can be simplified to avoid the extra if by using an extra level of pointer indirection:
void lst::Insertback(int x)
{
Node **temp = &head;
while (*temp != NULL)
{
temp = &((*temp)->next);
}
Node* backinst = new Node;
backinst->data = x;
backinst->next = NULL;
*temp = backinst;
}
I am learning DSA, and was trying to implement linked list but the insertion function that i wrote is not
working in a for or while loop, its not the same when i call that function outside the loop, it works that way. I am not able to figure it out, please someone help me.
#include <iostream>
class Node {
public:
int data;
Node *next;
Node(int &num) {
this->data = num;
next = NULL;
}
};
class LinkedList {
Node *head = NULL;
public:
void insert(int num) {
Node *tmp;
if (head == NULL) {
head = new Node(num);
tmp = head;
} else {
tmp->next = new Node(num);
tmp = tmp->next;
}
}
void printList() {
Node *tmp = head;
while (tmp) {
std::cout << tmp->data << " ";
tmp = tmp->next;
}
std::cout << std::endl;
}
void reverseList() {
Node *curr = head, *prev = NULL, *nextNode;
while (curr) {
nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
head = prev;
}
};
int main() {
LinkedList list1;
// This is not working
int num;
while (num != -1) {
std::cin >> num;
list1.insert(num);
}
// This is working
// list1.insert(1);
// list1.insert(2);
// list1.insert(3);
// list1.insert(4);
// list1.insert(5);
list1.printList();
list1.reverseList();
list1.printList();
return 0;
}
I expect this after insertion
Edit:
although #Roberto Montalti solved this for me, but before that I tried passing incrementing value using a for loop which worked but as soon as I pull that cin out it crashes. can someone tell me what's happening under the hood?
for (int i = 1; i <= 10; i++)
{
list1.insert(i);
}
When inserting the nth item (1st excluded) tmp is a null pointer, i don't understand what you are doing there, you are assigning to next of some memory then you make that pointer point to another location, losing the pointer next you assigned before, you must keep track of the last item if you want optimal insertion. This way you are only assigning to some *tmp then going out of scope loses all your data... The best way is to just keep a pointer to the last inserted item, no need to use *tmp.
class LinkedList
{
Node *head = NULL;
Node *tail = NULL;
public:
void insert(int num)
{
if (head == NULL)
{
head = new Node(num);
tail = head;
}
else
{
tail->next = new Node(num);
tail = tail->next;
}
}
...
}
You need to loop until you reach the end of the list and then add the new node after that. Like this.
void insert(int num) {
Node *tmp = head;
if (head == NULL) {
head = new Node(num);
}
else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = new Node(num);
}
}
first of all you need to define a node for each of the tail and head of the list as follows
Node *h;
Node *t;
you may also separate the Node from the LinkedList class so you can modify easily
class Node{
public:
int data;
Node *next;
Node(int data, Node* next);
~Node();
};
Node::Node(int data, Node* next)
{
this->data= data;
this->next= next;
}
Node::~Node(){}
}
after that you can try to add these functions to your LinkedList class
so it can deal with other special cases such empty list or full, etc..
void addToHead(int data){
Node *x = new Node(data,h);
h=x;
if(t==NULL){
t=x;
}
void addToTail(int data){
Node *x = new Node(data,NULL);
if(isEmpty()){
h=t=x;
}
else
{
t->next=x;
t=x;
}
}
now for the insert function try this after you implemented the Node class and the other functions,
void insert(int v){
if(h==nullptr){addToHead(v); return;}
if(h->data>=v) {addToHead(v);return;}
if(t->data<=v) {addToTail(v); return;}
// In this case there is at least two nodes
Node *k=h->next;
Node *p=h;
while(k != nullptr){
if(k->data >v){
Node *z =new Node(v,k);
p->next=z;
return;
}
p=k;
k=k->next;
}
}
the idea of making all of this is not lose the pointer when it goes through elements in the Linked List so you don't end up with a run time error.
I hope this can be useful to you.
There was an issue with your insert function.
Read about segmentation fault here https://www.geeksforgeeks.org/core-dump-segmentation-fault-c-cpp/#:~:text=Core%20Dump%2FSegmentation%20fault%20is,is%20known%20as%20core%20dump.
for a quick workaround you can use this
using namespace std;
#include <iostream>
class Node
{
public:
int data;
Node *next;
Node(int num)
{
this->data = num;
next = NULL;
}
};
class LinkedList
{
Node *head = NULL;
public:
void insert(int num)
{
Node *tmp= new Node(num);
tmp->next=head;
head=tmp;
}
void printList()
{
Node *tmp = head;
while (tmp)
{
std::cout << tmp->data << " ";
tmp = tmp->next;
}
std::cout << std::endl;
}
void reverseList()
{
Node *curr = head, *prev = NULL, *nextNode;
while (curr)
{
nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
head = prev;
}
};
int main()
{
LinkedList list1;
// This is not working
int num,i=0,n;
cout<<"Type the value of n";
cin>>n;
while (i<n)
{
cin >> num;
cout<<num<<" "<<&num<<endl;
list1.insert(num);
i++;
}
list1.printList();
list1.reverseList();
list1.printList();
return 0;
}
The below code is for merge sorting a linked list. Its giving out a segmentation fault. I really dont know how to deal with the above. All I could find was that I was trying to access a restricted part of the memory, the only place I think i could've gone wrong is re combining the two linked lists after splitting and sorting them under the split function body. I'd appreciate if I could get some guidance on how to deal with segmentation faults from here on & how to rectify them.
//Segmentation fault
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
next = NULL;
}
};
void print(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
Node *insert()
{
int data;
cin >> data;
Node *head = NULL;
Node *tail = NULL;
while (data != -1)
{
Node *n = new Node(data);
if (head == NULL)
{
head = n;
tail = n;
}
else
{
tail->next = n;
tail = tail->next;
}
cin >> data;
}
return head;
}
Node *sortedMerge(Node *h1, Node *h2)
{
// Node *fHead = NULL;
// Node *fTail = NULL;
if (!h1)
{
return h2;
}
if (!h2)
{
return h1;
}
if (h1->data < h2->data)
{
h1->next = sortedMerge(h1->next, h2);
return h1;
}
else
{
h2->next = sortedMerge(h1, h2->next);
return h2;
}
}
void split(Node *head, Node *h1, Node *h2)
{
Node *slow = head;
Node *fast = head->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
h1 = head;
h2 = slow->next;
slow->next = NULL;
}
void mergeSort_LL(Node *head)
{
Node *temp = head;
Node *h1;
Node *h2;
if ((temp == NULL) || (temp->next == NULL))
{
return;
}
split(temp, h1, h2);
mergeSort_LL(h1);
mergeSort_LL(h2);
head = sortedMerge(h1, h2);
}
int main()
{
Node *head = insert();
print(head);
cout << endl;
mergeSort_LL(head);
cout << "Sorted List is : " << endl;
print(head);
return 0;
}
Your call to split will not make h1 or h2 get a value. Arguments are passed by value. Since you evidently need h1 and h2 to get a different value from that split call, you should pass their addresses:
split(temp, &h1, &h2)
The function itself should therefore accept these addresses instead of the node pointers themselves:
void split(Node *head, Node **h1, Node **h2) {
// ...
*h1 = head;
*h2 = slow->next;
// ...
}
I have the following linked list:
2->1->9->8->3->1->nullptr.
I want to partition the linked list around the value 4, such that all values less than 4, come before all values greater than or equal to 4.
I can partition the linked list using a single function. But, I want to do it using two function - a function lesserThan(head,x) and a function greaterThan(head, x) - where x is the value around which I want to partition the list.
But, I am running into the following problem: If I use both functions together, the list nodes are modified by the first function - and, the second function works on that modified nodes. The functions work fine, when the other one is commented out. That is, lesserThan(head,x) works fine, when greaterThan(head, x) is commented out, and vice-versa.
How can I partition the linked list, by still using both the functions in main()? The main problem I am having is that the nodes are getting modified in both lesserThan and greaterThan functions, and that is getting reflected in main().
Following is the code:
struct Node
{
int data;
Node* next;
};
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = nullptr;
return temp;
}
Node* lesserThan(Node* head, int x)
{
if (head == nullptr)
{
return nullptr;
}
Node* list1=nullptr, *temp1 = nullptr;
if ((head)->data < x)
{
temp1=list1 = head;
}
else
{
while (head && head->data >= x)
{
head = head->next;
}
if (head && head->data < x)
{
temp1 = list1 = head;
}
}
Node* curr = temp1;
if(curr)
curr = curr->next;
while (curr)
{
Node* next = curr->next;
if (curr->data<x)
{
list1->next = curr;
list1 = curr;
list1->next = nullptr;
}
curr = next;
}
return temp1;
}
Node* greaterThan(Node* head, int x)
{
Node* temp2 = nullptr, *list2=nullptr;
if (head->data >= x)
{
temp2 =list2= head;
}
else
{
while (head && head->data < x)
{
head = head->next;
}
if (head && head->data >= x)
{
temp2 = list2 = head;
}
}
Node* curr = list2;
if (curr)
curr = curr->next;
while (curr)
{
Node* next = curr->next;
if (curr->data >= x)
{
list2->next = curr;
list2 = curr;
list2->next = nullptr;
}
curr = next;
}
return temp2;
}
int main()
{
Node* head = newNode(2);
head->next = newNode(1);
head->next->next = newNode(9);
head->next->next->next = newNode(8);
head->next->next->next->next = newNode(3);
head->next->next->next->next->next = newNode(1);
int x = 4;
Node* p1 = lesserThan(head,x);
Node* p2 = greaterThan(head, x);
if (p1 != nullptr)
p1->next = p2;
while (p1)
{
cout << p1->data << " ";
p1 = p1->next;
}
cout << endl;
return 0;
}
Following are the two functions, that fail to work together, because the List nodes are modified by the first function (and second function), and that is reflected in main() -
How can I have the two functions in main, so that they don't effect each other? I tried creating different variables for head, and passing them to the functions. But that didn't work. Thanks for the help!
It will be better to use insert recursive function instead of your style and note that you have undeleted allocated nodes. I didn't consider them. Any way, I think the following code works as intented
struct Node
{
Node() = default;
Node( int dataVal ):data{dataVal}{}
int data{};
Node* next{};
};
Node*& lessThan( Node* const & head, int x){
if( !head ) throw std::invalid_argument("Empty linked list");
Node* toBeReturned;
Node* currentHeadNode = head;
Node** currentReturned = & toBeReturned;
while( currentHeadNode ){
if(currentHeadNode -> data < x ){
*currentReturned = new Node{ currentHeadNode -> data };
currentReturned = &((*currentReturned) -> next);
}
currentHeadNode = currentHeadNode->next;
}
return toBeReturned;
}
int main()
{
Node* head = new Node(2);
head->next = new Node(1);
head->next->next = new Node(9);
head->next->next->next = new Node(8);
head->next->next->next->next = new Node(3);
head->next->next->next->next->next = new Node(1);
int x = 4;
Node* p1 = lessThan(head,x);
while (p1)
{
std::cout << p1->data << " ";
p1 = p1->next;
}
std::cout << std::endl;
return 0;
}
I have a linked list in C++, after inserting several nodes now I see that all of them are the same, although I'm using different values to add to node each time, but it's like all of them are the same, even when trying to change a node all of them are changing together or it's the same node that is always being returned, I don't know.
class node
{
public:
int ochance = 3;
string question;
string option1;
int peopleeffectop1;
int courteffectop1;
int treasuryeffectop1;
string option2;
int peopleeffectop2;
int courteffectop2;
int treasuryeffectop2;
node *next;
};
class list
{
private:
node *head, *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void createnode(int value , string q , string ans1 , int ans1ef1 , int ans1ef2, int ans1ef3 , string ans2, int ans2ef1 , int ans2ef2, int ans2ef3 )
{
node *temp = new node;
temp->ochance = value;
temp->question = q;
temp->option1 = ans1;
temp->peopleeffectop1 = ans1ef1;
temp->courteffectop1 = ans1ef2;
temp->treasuryeffectop1 = ans1ef3;
temp->option2 = ans2;
temp->peopleeffectop2 = ans2ef1;
temp->courteffectop2 = ans2ef2;
temp->treasuryeffectop2 = ans2ef3;
temp->next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
node getnth(int pos)
{
node* tmp = new node;
tmp = head;
int i = 0;
while(tmp!=NULL)
{
if (i=pos)
{
return *tmp;
}
i++;
tmp = tmp->next;
}
}
int getlen()
{
node* tmp = new node;
tmp = head;
int i = 0;
while(tmp!=NULL)
{
i++;
tmp = tmp->next;
}
return i;
}
void minus(int pos)
{
node* tmp = new node;
tmp = head;
int i = 0;
while(tmp!=NULL)
{
if (i=pos)
{
tmp->ochance -=1;
}
i++;
tmp = tmp->next;
}
}
void delete_first()
{
node *temp = new node;
temp = head;
head = head->next;
delete temp;
}
void delete_last()
{
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;
}
void delete_position(int pos)
{
node *current = new node;
node *previous = new node;
current = head;
for(int i = 1; i < pos; i++)
{
previous = current;
current = current->next;
}
previous->next = current->next;
}
};
For starters many member functions has a memory leak as for example in this function
node getnth(int pos)
{
node* tmp = new node;
tmp= head;
//
At first memory was allocated and its address was stored in the pointer tmp and then the pointer is reassigned. As a result the address of the allocated memory is lost and the memory is not deleted.
These statements
node* tmp = new node;
tmp= head;
must be substituted for this one statement
node* tmp = head;
Moreover this function
node getnth(int pos)
{
node* tmp = new node;
tmp= head;
int i =0 ;
while(tmp!=NULL){
if (i=pos) {
return *tmp;
}
i++;
tmp = tmp->next;
}
}
has undefined behavior in case when pos is higher than there are nodes in the list. In this case the function returns nothing.
In the function minus there is used the assignment operator instead of the comparison operator
while(tmp!=NULL){
if (i=pos) {
^^^^^
In this function
void delete_first()
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
there is no check whether head is equal to NULL and tail is not adjusted if it is point to the first node.
The same problems are also in the function delete_last only that instead of the adjacent of the tail node as in the previous function you have to adjust the head node.
This function delete_position has the same drawbacks as the previous functions but also it has a bug in the loop
for(int i=1;i<pos;i++)
A node at position 1 will never be deleted.