this is my code for inserting a node at the head of the linked list. there is no error but my printllist function is not working I don't know why. I think I have done silly somewhere. help me out I am stuck here for 3 hours.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Node
{ public:
int data;
Node* next;
};
void insert(int y,Node* head){
Node *temp=new Node();
temp->data=y;
temp->next=head;
head=temp;
}
void printllist(Node *head){
if(head=NULL)
{
cout<<"list is empty"<<endl;
}
else
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
}
int main(){
Node *head;
head=NULL;
int x,y,z;
cout<<"hey can you tell me how many number you want to insert"<<endl;
cin>>x;
for(int i=0;i<x;i++)
{
cout<<"enter the data that you want to insert"<<endl;
cin>>y;
insert(y,head);
printllist(head);
}
return 0;
}
You need to pass the head of the linked list by reference so that the changes which are made in the insert function actually remain preserved between the function calls. Also, keep in mind that all the new nodes are inserted at the beginning of the linked list. So, for example if you enter 1,2,3 as the three elements of the linked list, the print function will print 3 2 1.
Try this:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Node
{ public:
int data;
Node* next;
};
void insert(int y,Node** head){
Node* temp = new Node();
temp->data = y;
temp->next = *head;
*head = temp;
}
void printllist(Node* head)
{
if(head==NULL)
cout<<"List is empty";
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
int main(){
Node *head;
head=NULL;
int x,y,z;
cout<<"hey can you tell me how many number you want to insert"<<endl;
cin>>x;
for(int i=0;i<x;i++)
{ cout<<"enter the data that you want to insert"<<endl;
cin>>y;
insert(y,&head);
}
printllist(head);
return 0;
}
Related
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;
Good Evening! I am trying to create a C++ Linked List that will create a random number & store randoms in 100 nodes. I haven't gotten any errors in the code I created but when I run the program, the output loops the number "42" to the point where I have to terminate the program. Please help. The code is below.
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node{
int xdata;
Node* next;
};
struct Node *head;
void insert_node(int y)
{
Node* temp = new Node;
temp-> xdata = y;
temp-> next = NULL;
if(head==NULL)
{
head=temp;
}
else{
temp->next=head;
head=temp;
}
};
int main(){
int z =rand()%100 + 1;
for(int i=0; i<100; i++)
{
insert_node(z);
}
while(head!=NULL)
{
cout<<head->xdata<<" "<<endl;
}
return 0;
}
You need to advance your head pointer.
while(head!=NULL)
{
cout<<head->xdata<<" "<<endl;
head = head->next;
}
#include <iostream>
using namespace std;
struct node{
int data;
node* link;
};
node* a;//global variable declared for creating head, not local as in 1.cpp
void Insert(int x);
void Print();
int main()
{
int i,n,x;
a=NULL;
cout<<"How many numbers do you want to enter?";
cin>>n;
for (i=0; i<n; i++) {
cout<<"Enter your desired numbers\n:";
cin>>x;
Insert(x);
Print();
}
return 0;
}
void Insert(int x)//first node from file 1
{
node* temp=new node();
temp->data=x;
temp->link=NULL;
a=temp;
}
void Print()//check this part out again.
{
node* temp=a;
cout<<"The list is";
while (temp!=NULL) {
cout<<temp->data;
temp=temp->link;
}
printf("\n");
}
I think something is wrong in the Print function I created.
I tried to debug it a couple times but couldn't arrive at a solution
Can you tell me whats wrong with it? I want to use this same method, using functions.
You are not creating the links properly.
void Insert(int x)
{
node* temp=new node();
temp->data=x;
// This makes temp a standalone node.
// temp->link=NULL;
// Make the link between the new node and the
// existing nodes.
temp->link= a;
a=temp;
}
I am beginner and I have created link list, but my code is not working correctly. plz help thx in advance
#include <iostream>
using namespace ::std;
class node{
public:
int data;
node *link;
};
class linklist{
private:
node *head=NULL;
node *tail;
node *temp;
public:
// I think there is some issue but it seems perfect to me plz help
void insert(int n)
{
if(head==NULL)
{
tail=new node;
tail->data=n;
tail->link=NULL;
head=tail;
temp=tail;
}
else
{
tail=new node;
tail->data=n;
tail->link=NULL;
temp->link=tail;
}
}
void print()
{
while(head->link==NULL)
{
cout<<head->data<<endl;
head=head->link;
}
}
};
int main() {
linklist s;
cout<<"how many numbers you want to enter"<<endl;
int n,l;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter nummber";
cin>>l;
s.insert(l);
s.print();
}
}
After printing section printing is not doing and it keep printing current element
There are some mistakes in your code.
print function changes head value every time, you need to use local variable. Also your condition for while loop is wrong.
void print()
{
node* t = head;
while(t->link!=NULL)
{
cout<<t->data<<endl;
t=t->link;
}
}
You don't change temp when adding new nodes.
else
{
tail=new node;
tail->data=n;
tail->link=NULL;
temp->link=tail;
temp = tail; // here
}