I am new to the data structures and I was trying to write a code which add nodes to the beginning of the linked list. After every time user enters a new node, the program is supposed to display an updates linked list, but my program is only displaying the current entered node.
The code is as follows: -
#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node* next;
};
struct Node* head;
void Insert(int x)
{
Node* temp=new Node();
temp->data=x;
temp->next=NULL;
head=temp;
}
void Print()
{
struct Node* temp=head;
cout<<"List is: ";
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
cout<<"\n";
}
void main()
{
head=NULL;
clrscr();
cout<<"How many numbers?\n";
int n,x,i;
cin>>n;
for(i=0; i<n; i++)
{
cout<<"Enter the number \n";
cin>>x;
Insert(x);
Print();
}
getch();
}
Your Insert method is wrong. You need to assign head to next:
void Insert(int x)
{
Node* temp=new Node();
temp->data=x;
temp->next=head;
head=temp;
}
This will then chain your list correctly.
Your Insert function is wrong. You need to use something like this in order to add new items at the end of the list:
void InsertAtTheEnd(int x) {
Node* temp = new Node();
temp->data=x;
temp->next=NULL;
if (NULL == head) {
head = temp;
} else {
Node *tmp = head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = temp;
}
}
And this will add them to the beggining:
void InsertAtTheBeginning(int x) {
Node* temp=new Node();
temp->data=x;
temp->next=head;
head=temp;
}
Check it out live
Related
can u use head->data as an integer variable, if the "data" is integer type in chained lists.
for example, I have a code where I have to transform a simply chained list of integer variables, into 2 lists, one with positive numbers, the other one with negative numbers. I cant find the error in my code...
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;
Node* newNode(int data)
{
Node* nodNou = new Node();
nodNou -> data = data;
nodNou -> next = NULL;
return nodNou;
}
void Push(Node* &top, int data)
{
Node* nodNou = newNode(data);
nodNou -> next = top;
top = nodNou;
}
void read(){
Node* aux=new Node;
int r,i,v[1000];
cout<<"insert the number of elements in list"<<endl;
cin>>r;
cin>>v[0];
head->data=v[0];
head->next=NULL;
for(i=1;i<r;i++){
cin>>v[i];
aux->data=v[i];
aux->next=head;
head=aux;
aux=new Node;
}
}
write(){
if(head==NULL) return 0;
else{
while(head!=NULL){
cout<<head->data<<"->";
head=head->next;
}
}
cout<<endl<<endl;
}
void stivezlutat(){
Node* aux;
pozitiv=new Node;
pozitiv->next=NULL;
negativ=new Node;
negativ->next=NULL;
while(head!=NULL){
if(head->data >= 0){
Push(pozitiv, head->data);
}
else{
Push(negativ, head->data);
}
head=head->next;
}
cout<<"pozitive list:"<<endl;
while(pozitiv != NULL){
cout<<pozitiv->data<<"->";
pozitiv=pozitiv->next;
}
cout<<endl;
cout<<"negative list:"<<endl;
while(negativ != NULL){
cout<<negativ->data<<"->";
negativ=negativ->next;
}
}
int main()
{
read();
write();
stivezlutat();
}```
Errors in your code:
Required #include directives don't present.
extra "```" exists at the end of code.
No return type is specified for write.
In the function write, return with value is used while no return statement presents at the end of the function, so no return type is valid for that.
The function write breaks the value of head, so the function stivezlutat will see empty list.
Extra nodes are assigned to pozitiv and negativ and they are printed in the funciton stivezlutat.
Fixed code (also indentation is fixed):
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head=new Node;
Node* pozitiv=NULL;
Node* negativ=NULL;
Node* newNode(int data)
{
Node* nodNou = new Node();
nodNou -> data = data;
nodNou -> next = NULL;
return nodNou;
}
void Push(Node* &top, int data)
{
Node* nodNou = newNode(data);
nodNou -> next = top;
top = nodNou;
}
void read(){
Node* aux=new Node;
int r,i,v[1000];
cout<<"insert the number of elements in list"<<endl;
cin>>r;
cin>>v[0];
head->data=v[0];
head->next=NULL;
for(i=1;i<r;i++){
cin>>v[i];
aux->data=v[i];
aux->next=head;
head=aux;
aux=new Node;
}
}
void write(){
if(head==NULL) return;
else{
Node* cursor = head;
while(cursor!=NULL){
cout<<cursor->data<<"->";
cursor=cursor->next;
}
}
cout<<endl<<endl;
}
void stivezlutat(){
Node* aux;
pozitiv=NULL;
negativ=NULL;
while(head!=NULL){
if(head->data >= 0){
Push(pozitiv, head->data);
}
else{
Push(negativ, head->data);
}
head=head->next;
}
cout<<"pozitive list:"<<endl;
while(pozitiv != NULL){
cout<<pozitiv->data<<"->";
pozitiv=pozitiv->next;
}
cout<<endl;
cout<<"negative list:"<<endl;
while(negativ != NULL){
cout<<negativ->data<<"->";
negativ=negativ->next;
}
}
int main()
{
read();
write();
stivezlutat();
}
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();
}
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;
im trying to add node at the starting of linked list but in my code it only displaying the last element which i have entered only the last element .what is the problem why this is happening
#include <stdio.h>
//node structure
struct node
{
int data;
struct node *next;
};
//struct
//datatype declaration
typedef struct node node ;
// head pointer which will indicate starting point of link list
node *head;
//create fuction that will insert values into note and its next pointer field
void create(int num);
//display function will display the link list
void display();
main()
{
int num,i,n;
printf("enter the nno of node to create : ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("enter data for node %d= ",i+1);
scanf("%d",&num);
create(num);
}
display();
//display call
}
void create(int num)
{
head=NULL;
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=num;
temp->next=head;
head=temp;
return;
}
//function create() end
void display()
{
node *temp1;
temp1=head;
while(temp1!=NULL)
{
printf("data : %d-> ",temp1->data);
temp1=temp1->next;
}
return;
}
//fucntion display() end
Your main problem is:
head=NULL;
Thus your line
temp->next=head;
always sets the next to be NULL. Thus all your lists are
of length one.
I would suggest either declaring head to be static (and
hence zeroed from the outset), or intializing head in main.
Your display function seems fine, your create function should look something like this:
void create(int num)
{
node* temp = new node();
if(head ==NULL)
{ temp->data = num;
head = temp;
head->next = NULL;
}
else if(head->next == NULL)
{
temp->data = num;
head->next = temp;
}
else
{
node* temp2 = head;
while(temp2->next !=NULL)
{ temp2 = temp2->next;
}
temp->data = num;
temp2->next = temp;
}
return;
}
This create function above will place the node depending if the list is empty, has one node, or more than one node will insert at Last position. You can now go off this to insert in front of head, or in Link list terms function (InsertBeforeFirst). And of course where you have node* head up top should be initialized to NULL;
The delete from last functionality does not work properly. It shows that the node has been deleted but when I display it, it gets into an infinite loop and displays junk. Couldn't figure what's wrong!
Here's the code:
using namespace std;
class List
{
struct NODE
{
int item;
NODE *next;
};
NODE *Head,*Tail;
public:
List()
{
Head=NULL;
Tail=NULL;
}
~List()
{
while(Head->next!=NULL)
{
Delete_At_Head();
}
Delete_At_Head();
}
void Add_At_First(int);
void Add_At_Last(int);
void Delete_At_Head();
void Delete_At_Tail();
int Is_Empty();
void display();
};
void List::Add_At_First(int data)
{
NODE *temp;
temp=new NODE;
if(Head==NULL)
{
temp->item=data;
temp->next=NULL;
Head=temp;
Tail=Head;
}
else
{
temp->item=data;
temp->next=Head;
Head=temp;
}
cout<<"Node added at first!\n";
}
void List::Add_At_Last(int data)
{
NODE *temp;
temp=new NODE;
temp->item=data;
temp->next=NULL;
if(Head==NULL)
{
Head=temp;
Tail=temp;
}
else
{
Tail->next=temp;
Tail=temp;
}
cout<<"Node added at last!\n";
}
void List::Delete_At_Head()
{
NODE *temp;
temp=new NODE;
temp->item=Head->item;
temp->next=Head->next;
delete Head;
Head=temp->next;
delete temp;
cout<<"Node deleted from head!\n";
}
void List::Delete_At_Tail()//Problematic part
{
NODE *temp,*prev;
temp=new NODE;
prev=new NODE;
temp=Head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
delete temp;
delete Tail;
Tail=prev;
delete prev;
cout<<"Node deleted from tail!\n";
}
int List::Is_Empty()
{
if(Head==NULL)
return 1;
else
return 0;
}
void List::display()//does not display after delete from tail
{
NODE *temp;
temp=new NODE;
temp->item=Head->item;
temp->next=Head->next;
do
{
cout<<temp->item<<"-->";
temp=temp->next;
}while(temp->next!=NULL);
cout<<temp->item;
}
int main()
{
List obj;
int ch,data;
do
{
cout<<"\n1.Display\n2.Add at first\n3.Add at last\n4.Delete at
head\n5.Delete at tail\n6.Exit\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
{
if(obj.Is_Empty())
cout<<"List is Empty!\n";
else
obj.display();
break;
}
case 2:
{
cout<<"Enter data: ";
cin>>data;
obj.Add_At_First(data);
break;
}
case 3:
{
cout<<"Enter data: ";
cin>>data;
obj.Add_At_Last(data);
break;
}
case 4:
{
if(obj.Is_Empty())
cout<<"List is Empty!\n";
else
obj.Delete_At_Head();
break;
}
case 5:
{
if(obj.Is_Empty())
cout<<"List is Empty!\n";
else
obj.Delete_At_Tail();
break;
}
case 6:
{
break;
}
}
}while(ch!=6);
return 0;
}
The following should correctly delete the last item in the list:
void List::Delete_At_Tail()
{
if(Head == NULL) return;
// If only one item in the list, delete it and empty the list...
if(Head->next == NULL) {
delete Head;
Head = NULL;
Tail = NULL;
return;
}
// Find the last item in the list
NODE *temp = Head;
while(temp->next!=Tail)
{
temp=temp->next;
}
delete Tail;
temp->next=NULL;
Tail=temp;
cout<<"Node deleted from tail!\n";
}
You are definitely deleting too many objects in your Delete_At_Tail() (I assume this is the function you meant when you wrote delete_from_tail() or "delete from last"; when programming precision is everything!) function: you want to get rid of one object but you are deleteing three objects, including the predecessor of the deleted object. You should only delete the object you are removing and no other. Other than that the function looks OK.
BTW, since you are maintaining a pointer to the Tail you can improve the way to find the predecessor: you can find the node whose next is Tail. This avoids the awkward need to keep two variables up to date in the loop looking for the predecessor.