I didn't understand why i am getting error in line head=NULL I am just pointing head towards NULL
I have also tried creating a new node for head and then pointing towards NULL but still it doesn't work
Code show here
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL;
node *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 deletek(int n,int k)
{
if(head==NULL)
return;
node *temp;
temp=head;
while(temp->data<temp->next->data)
{
head=temp->next;
temp=temp->next;
k--;
}
while(temp!=NULL&&k!=0)
{
if(temp->next->data<temp->next->next->data)
{
temp->next=temp->next->next;
k--;
}
temp=temp->next;
}
}
void display()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main() {
int t;
cin>>t;
int n,k;
for(int i=0;i<t;i++)
{
cin>>n>>k;
int arr[n];
for(int j=0;j<n;j++)
{
cin>>arr[j];
createnode(arr[j]);
}
deletek(n,k);
display();
head=NULL;
}
}
I expect that whenever the line head=NULL runs the previous linked list should get delete and I should be able to enter elements in the linked list from the beginning
First of all: C++ likes "nullptr" (since C++11) for this, instead of "NULL".
=> e.g. https://en.cppreference.com/w/cpp/types/NULL vs. https://en.cppreference.com/w/cpp/language/nullptr
Second: The exact compiler output could help.
Third: The list will not be deleted if you throw away the address of it's location. maybe you should rather create a class and/or add destructor for your node. If you want to implement LL by yourself
Or have a look at std::list -> https://en.cppreference.com/w/cpp/container/list. I use this rather often.
Or if you like to add boost dependency: https://www.boost.org/doc/libs/1_35_0/doc/html/intrusive/list.html more fancy but you'll add an external library
Then: The Segfault (on my PC) happens in if(temp->next->datanext->next->data) I guess you can never know if temp->next->next->data exists... maybe not even if temp->next->data => if temp->next points to NULL (or nullptr) it is not a valid instance of node to dereference
try:
`
#include <bits/stdc++.h>
#define HERE() printf("HERE: %d\n", __LINE__)
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL;
node *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 deletek(int n,int k)
{
if(head==NULL)
return;
node *temp;
temp=head;
while(temp->data<temp->next->data)
{
head=temp->next;
temp=temp->next;
k--;
}
HERE();
while(temp!=NULL&&k!=0)
{
HERE();
int d1 = temp->next->data;
HERE();
int d2 = temp->next->next->data;
if(temp->next->data < temp->next->next->data)
{
temp->next=temp->next->next;
k--;
}
temp=temp->next;
}
}
void display()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main() {
int t;
cout << "input t:" << endl;
cin>>t;
int n,k;
for(int i=0;i<t;i++)
{
cout << "input n/k" << endl;
cin>>n>>k;
int arr[n];
for(int j=0;j<n;j++)
{
cout << "input arr[" << j <<"]" << endl;
cin>>arr[j];
createnode(arr[j]);
}
deletek(n,k);
display();
head=NULL;
}
}
`
Related
Here I created a doubly circular linked list and try to add a node between the list but it is giving wrong output
I first created a doubly circular linked list and then displayed it and then I added a node between them and then again displayed it
#include<iostream>
using namespace std;
class node //node class
{
public:
int data;
node *next;
node *prev;
node(int a)
{
data=a;
next=nullptr;
prev=nullptr;
}
};
class linkedlist //linkedlist class
{
node *head,*tail;
public:
linkedlist()
{
head=nullptr;
tail=nullptr;
}
void addnode(int val) //creating node function
{
node *newnode;
newnode=new node(val);
if(head==0)
head=tail=newnode;
else
{
tail->next=newnode;
head->prev=newnode;
newnode->prev=tail;
newnode->next=head;
tail=newnode;
}
}
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
temp=temp->next;
cout<<" "<<temp->data;
}
void addin(int val,int pos) //addin function
{
node *newnode=new node(val);
node *temp=head;
for(int i=0;i<pos;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
newnode->prev=temp;
temp->next->prev=newnode;
temp->next=newnode;
}
};
int main()
{
linkedlist l1;
int s,val,val1,pos;
cin>>s;
for(int i=0;i<s;i++)
{
cin>>val;
l1.addnode(val);
}
l1.disp(); //display function
cout<<"\n\n";
cin>>pos;
cin>>val1;
l1.addin(val,pos); //calling addin function
l1.disp();
}
Input
3
1 2 3
1
0
Expected output
1 2 3
1 2 0 3
Current output
1 2 3 1
I don't know what mistake I am doing and also I am beginner so any tip is also too helpful for me
You are printing the head node instead of the tail node due to the extra temp=temp->next; after the loop in the disp() function.
You are adding val instead of val1 after the loop in the main() function.
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
// remove this
// temp=temp->next;
cout<<" "<<temp->data;
}
cin>>pos;
cin>>val1;
// add val1, not val
//l1.addin(val,pos); //calling addin function
l1.addin(val1,pos); //calling addin function
l1.disp();
in main function
you mistakenly add val instead of val1
cin>>val1;
l1.addin(val1,pos);
then in disp() function, remove temp=temp->next which is outside the loop.
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
// temp=temp->next;
cout<<" "<<temp->data;
}
and finally in addin() function, change pos to pos-1
for(int i=0;i<pos-1;i++)
{
temp=temp->next;
}
that's all.
#MikeCAT was spot on, you are using val1 and passing val. However, I think the while loop in disp will skip the last element and not print it. and the logic in addin are not going to add the new element in the new place. Here is my version of your code
#include<iostream>
using namespace std;
class node //node class
{
public:
int data;
node *next;
node *prev;
node(int a)
{
data=a;
next=nullptr;
prev=nullptr;
}
};
class linkedlist //linkedlist class
{
node *head,*tail;
public:
linkedlist()
{
head=nullptr;
tail=nullptr;
}
void addnode(int val) //creating node function
{
node *newnode;
newnode = new node(val);
if(head == NULL)
head = tail = newnode;
else
{
tail->next = newnode;
head->prev=newnode;
newnode->prev=tail;
newnode->next=head;
tail=newnode;
}
}
void disp() //display function
{
node *temp = head;
if (temp == NULL)
{
cout << "List is empty " << endl;
return;
}
do
{
cout << temp->data << endl;
temp = temp->next;
} while (temp != head);
}
void addin(int val,int pos) //addin function
{
node *newnode=new node(val);
node *temp=head;
for(int i=0;i<pos;i++)
{
temp=temp->next;
}
//newnode->next=temp->next;
newnode->next=temp;
newnode->prev=temp->prev;
temp = temp->prev;
temp->next = newnode;
}
};
int main()
{
linkedlist l1;
int s,val,pos;
cin>>s;
cout << "val of size is " << s << endl;
for(int i=0;i<s;i++)
{
cin>>val;
l1.addnode(val);
}
l1.disp(); //display function
cout<<"\n\n";
cin>>pos;
cin>>val;
l1.addin(val,pos); //calling addin function
l1.disp();
}
I have been trying to create a linked list in c++. But only the last element of the linked list is displayed. I have searched for the error but i cannot find it. I have implemented the logic that I learned form c language. All the nodes are connecting properly. But still i cannot find the error.
This logic works on c language.
Please help.
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
}*head,*newnode,*temp;
node* getnode();
node* create(int);
void display(node*);
int main()
{
int n;
head=getnode();
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *getnode()
{
head=new node();
head->next=nullptr;
return(head);
}
node *create(int n)
{
head=getnode();
cout<<"Enter the value of node 1: ";
cin>>head->data;
temp=getnode();
temp=head;
for(int i=1;i<n;i++)
{
newnode=getnode();
cout<<"Enter the value of node "<<i+1<<": ";
cin>>newnode->data;
newnode->next=nullptr;
temp->next=newnode;
temp=newnode;
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
node(int x)
{
data=x;
next=nullptr;
}
}*head,*newnode,*temp;
node* create(int);
void display(node*);
int main()
{
int n;
cout<<"Enter the no of nodes: ";
cin>>n;
head=create(n);
display(head);
return 0;
}
node *create(int n)
{
for(int i=0;i<n;i++)
{
int x;
cout<<"Enter the value of node "<<i+1<<": ";
cin>>x;
newnode=new node(x);
if(i==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return(head);
}
void display(node *head)
{
while(head!=nullptr)
{
cout<<"->"<<head->data;
head=head->next;
}
}
Ive just created a constructor for creating a new node and used a temp pointer for keeping track of the last inserted element in the list. Do keep in mind it is always best to have your head pointer fixed and use another pointer for traversals. The problem with your code was that your head pointer points to the last inserted element always.
Use local variables
*head,*newnode,*temp are globals. Everytime you call a function, you're overwriting them. Make them local variables.
Memory leaks
You also leak memory in main() with:
head=getnode();
And in create() with:
temp=getnode();
Put it all together
https://repl.it/repls/MedicalEquatorialFlashmemory#main.cpp
Here's my C++ program which is used to insert values at the beginning of the linked list. The logic of the program seems to be fine for me but it is unable to display the values of the list. I guess the problem is in the Print() function. Please help!
#include<iostream.h>
struct Node
{
int data;
Node* next;
};
struct Node* head;
void Insert(int x)
{
Node *temp=new Node();
temp ->data=x;
temp ->next=NULL;
if(head!=NULL)
{
temp->next=head;
head=temp;
}
}
void Print()
{
Node *temp=head;
cout<<"List is:";
do
{
cout<<temp->data<<"->";
temp=temp->next;
}while(temp!=NULL);
cout<<endl;
}
int main()
{
int n,i,x;
head=NULL;
cout<<"How many numbers \n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the number \n";
cin>>x;
Insert(x);
Print();
}
return 0;
}
void Insert(int x)
{
Node *temp=new Node();
temp ->data=x;
temp ->next=NULL;
if(head!=NULL)
{
temp->next=head;
head=temp;
}
}
in main program head is null so in insert function it will never update because of if(head!=NULL) check.
Correct Solution is
#include<iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
struct Node* head;
void Insert(int x)
{
Node *temp=new Node();
temp ->data=x;
temp ->next=NULL;
if(temp!=NULL)
{
temp->next=head;
head=temp;
}
}
void Print()
{
Node *temp=head;
cout<<"List is:";
do
{
cout<<temp->data<<"->";
temp=temp->next;
}while(temp!=NULL);
cout<<endl;
}
int main()
{
int n,i,x;
head=NULL;
cout<<"How many numbers \n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the number \n";
cin>>x;
Insert(x);
}
Print();
return 0;
}
You need to update head which is never changed from initial NULL because of if(head!=NULL) condition checking.
Change
if(head!=NULL)
{
temp->next=head;
head=temp;
}
to
if(head!=NULL)
{
temp->next=head;
}
head=temp;
So the logic goes like this:
Suppose the link list consists of (6,7,8) as data and I pass insert(1,5),so the list will be as (5,6,7,8).
Similarly on insert(3,2) link list is (6,7,2,8).
I tried compiling the below code but it gives me an error stating-
Undefined reference to main by '-start'
I tried debugging,even searching for answers but found no help.Kindly suggest a solution.Any further suggestions and bug fixes shall be welcomed.
(I have used codepad for compiling)
#include<iostream>
using namespace std;
class Link_no
{
struct node
{
int data;
node *next;
};
void insert(int n,int d,node *head)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print(node *start)
{
node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int main()
{
node *head=NULL;
Link_no o1;
o1.insert(1,5,head);
o1.insert(2,7,head);
o1.insert(1,9,head);
o1.print(head);
return 0;
}
}
C++ isnt java, the main does not belong inside a class. The compiler complains because there is no int main() in your code only a int Link_no::main() but that is not the entry point of the program.
Take out int main() from class Link_no. Take out struct node from class Link_no. It should compile.
The following compiles
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print()
{
node *temp=head;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.insert(2,7);
o1.insert(1,9);
o1.print();
return 0;
}
It does not completely do what you want yet only prints out 5 and 9 as data so you need to debug some more.
Edit:
I suggest you take a paper and pen and manually try to do what you're doing in your else since there is something going wrong there.
If you can't find it out on your own the following works for me, I haven't tried testing for extreme cases yet.
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
{
cout << "foo" << endl;
temp1=head;
for(int i=1;i<n-1;i++)
{
temp1=temp1->next;
}
node *temp2 = temp1->next;
temp1->next = temp;
temp->next=temp2;
}
}
void print()
{
node *temp=head;
cout << "link" << endl;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.print();
o1.insert(2,7);
o1.print();
o1.insert(1,9);
o1.insert(2,6);
o1.print();
return 0;
}
This is a code to make a linked list with 2 values- one user input and another 7.
#include<iostream>
#include<cstdlib>
using namespace std;
class node{
public:
node();
~node();
void printList();
void insert_front(int);
void delete_front();
private:
int data;
node *head;
node *next;
};
node::node()
{
head=NULL;
}
node::~node( ){//destructor
cout <<"destructor called";
while( head!= NULL) delete_front() ;
}
void node::delete_front(){
node *h=head;
if(head==NULL)
{
cout<< "Empty List.\n";
return;
}
head = head->next;
delete(h);
}
void node::printList()
{
node *h=head;
cout<< "Printing the list";
while(h!=NULL)
{
cout<< h->data;
cout<< '\n';
h->next= h->next->next;
}
}
void node::insert_front(int value){
node *temp = new node;
temp->data=value;
temp -> next = NULL;
if (head != NULL){
temp->next =head;
}
head= temp;
}
int main()
{
node ListX;
cout<< "enter integer";
int as;
cin>> as;
ListX.insert_front(as);
ListX.insert_front(7);
ListX.printList();
ListX.~node( );//call destructor to free objects
return 0;
}
Please tell the error in this as it shows an error while compiling it online on http://www.compileonline.com/compile_cpp_online.php and even on my laptop.
h->next= h->next->next;
What are you trying to achieve here ?
Change while loop in void node::printList() to:
while(h!=NULL)
{
cout<< h->data;
cout<< '\n';
h= h->next;
}