Only the last element of the linked list is displayed - c++

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

Related

i dont why it isnt printing the linked list

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;
}

creating linked list but wouldnt work as it is intended in cpp

#include <iostream>
using namespace std;
struct Node{
int data;
Node* next=NULL;
};
class list{
Node *head,*tail;
public:
void insert(int data){
Node *node =new Node;
node->data=data;
node->next=NULL;
if(head==NULL){
head=node;
tail=node;
}else{
tail->next=node;
tail=tail->next;
}
}
void show(){
Node *n=head;
while(n->next!=NULL){
cout<<n->data<<" ";
n=n->next;
}
cout<<n->data<<endl;
}
};
int main(){
list x;
int n;
for(int i=0;i<10;i++){
cin>>n;
x.insert(n);
}
x.show();
return 0;
}
the program is compiling perfectly but while running it stops and its not working if i put the insert function in loop only then the problem arises but otherwise it is running fine
When you declare a pointer, it doesn't have a default value of NULL necessarily.
related question
Initialize head and tail with NULL and the program will be run successfully.

What is wrong with this implementation of linked list?

This is my code for creating a singly-linked list in C++.
The function del_alt() deletes every alternate element starting from the second element.
The compiler gives no error as such , but during run the program terminates abruptly after showing the original list.
I have tried my best to find the possible errors but can't find any.
Help appreciated.
Cheers.
#include<iostream>
using namespace std;
class Node
{
public:
Node()
{
next=0;
}
Node *next;
int info;
};
class List
{
private:
Node *head,*tail;
public:
List()
{
head=0;
tail=0;
}
void del_alt();
void add_to_head(int);
void show_list();
};
void List :: del_alt()
{
if(!head||(head==tail))
return;
else
{
Node *current,*after,*ptr;
for(current=head,after=head->next;current!=0;current=current->next,after=current->next)
{
ptr=after;
current->next==after->next;
if(ptr==tail)
tail=current;
delete ptr;
}
}
}
void List :: add_to_head(int el)
{
Node *ptr;
ptr->info=el;
if(!head)
{
ptr->next=0;
head=tail=ptr;
}
else
{
ptr->next=head;
head=ptr;
}
}
void List::show_list()
{ Node *ptr;
cout<<"\n\n";
ptr=head;
while(ptr!=0)
{
cout<<"\t"<<ptr->info;
ptr=ptr->next;
}
}
int main()
{
List l;
int el;
char ch;
cout<<"\n\n\t\t enter elements\n\n\t";
do
{
cin>>el;
l.add_to_head(el);
cout<<"\n\t want to enter more ? (y/n) \n";
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"\n\t Original list -> \n";
l.show_list();
l.del_alt();
cout<<"\n\t After deletion -> \n";
n.show_list();
cout<<"\n\n \\===============================================";
}
The problem comes from the non-initialization of ptr in the method add_to_head.
Node *ptr;
ptr->info=el
At least ptr should be a new allocated cell
Node *ptr = new Node;
ptr->info=el

Regarding insertion of nodes at the beginning of a linked list (Program isn't executing properly)

#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;
}

link list insert() print();

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
}