What is wrong with this implementation of linked list? - c++

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

Related

Only the last element of the linked list is displayed

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

Implementation of Queues using Linked lists

The queue is implemented using linked list: but the program isn't working, what could be the possible mistake?
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *next;
};
class queue
{
node *front, *rear;
public:
queue()
{
front=rear=NULL;
}
void insert_in_queue();
void delete_in_queue();
void display_queue();
};
void queue:: insert_in_queue()
{
node *ptr;
ptr= new node;
cout<<"\nInsert element\n";
cin>>ptr->data;
if(rear==NULL)
front=rear=ptr;
else
{ rear->next=ptr;
rear=ptr;
}
}
void queue:: delete_in_queue()
{
node *ptr;
ptr=front;
if(rear==NULL)
cout<<"\nUnderflow!!\n";
else if(front==rear)
front=rear=NULL;
else
front=front->next;
cout<<"\nThe deleted element is:: "<<ptr->data<<"\n";
delete ptr;
}
void queue:: display_queue()
{
node *ptr;
ptr=front;
cout<<"\nThe queue is:\n";
while(ptr!=NULL)
{
cout<<"|"<<ptr->data<<"|";
ptr=ptr->next;
}
}
void main()
{
queue q;
char ch;
int a;
ch='y';
cout<<"this is dynamic que progream\n\n\n";
do
{
cout<<"(1)->Insert / (2)->Delete\n";
cin>>a;
if(a==1)
q.insert_in_queue();
else
q.delete_in_queue();
q.display_queue();
cout<<"\nContinue?(y/n)\n";
cin>>ch;
}while(ch=='y');
cout<<"\nThe final queue is:\n";
q.display_queue();
system("pause");
}
The console screen just disappears after entering the first element, I can't figure out where is the mistake.
In the insert_in_queue() function, after allocating memory for ptr, make sure to use
ptr->next=NULL;

Need explanation for the code

Why is the argument for display in main() start and not newptr? Also, how does np->next=save; and np=np->next work in their respective functions? I am very new to the concept of linked lists. Any help would be much appreciated.
#include<iostream>
using namespace::std;
struct node
{
int info;
node *next;
} *start,*newptr,*save,*ptr;
node * create_new_Node(int);
void insert_beg(node *);
void display(node*);
int main()
{
start=NULL;
int inf; char ch='y';
while(ch=='y'||ch=='Y')
{
system("cls");
cout<<"Enter information for the new node : \n";
cin>>inf;
cout<<"\nCreating new node";
system("pause");
newptr=create_new_Node(inf);
if(newptr!=NULL)
{
cout<<"New node created successfully.";
system("pause");
}
else
{
cout<<"\aNot enough memory =S ...\n";
exit(1);
}
cout<<"Now inserting this node to the beginning of the list :";
system("pause");
insert_beg(newptr);
cout<<"Now the list is : ";
display(start);
cout<<"Press Y or y to enter more nodes :::: ";
cin>>ch;
}
return 0;
}
node * create_new_Node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void insert_beg(node *np)
{
if(start==NULL)
{
start=np;
}
else
{
save=start;
start=np;
np->next=save;
}
}
void display(node * np)
{
while(np!=NULL)
{
cout<<np->info<<" -> ";
np=np->next;
}
}
in insert_beg, the pointer to start is changed to the new start position which is the newly inserted node.
in my opinion, pointer operations are better to undrstand in a graphical model..
Why is the argument for display in main() start and not newptr?
Because start is the head pointer in your linked list and always points to the first node in the linked list. To display the linked list, you have to start from the first node and that first node is pointed to by start pointer.
how does np->next=save; and np=np->next work in their respective
functions?
np->next=save; it means that next pointer of node np should point to the node which is pointed to by save pointer.
np=np->next; it is being used in display function to iterate over your linked list.

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
}

C++ merging two linked lists segmentation fault and core dump error

This is my code for merging two linked lists together. When I run it I get a segmentation fault and core dump error. I don't know how to fix it. My brain is fried please help.
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
class list
{
struct node *start;
public:
void create();
void show();
void merge(list,list);
};
void list::create()
{
struct node *nxt_node, *pre_node;
int value, no, i;
start=nxt_node=pre_node=NULL;
cout<<"\nHow many nodes:";
cin>>no;
cout<<"Enter "<<no<<" Eelements:";
for(i=1;i<=no;i++)
{
cin>>value;
nxt_node=new node;
nxt_node->data=value;
nxt_node->next=NULL;
if (start==NULL)
start=nxt_node;
else
pre_node->next=nxt_node;
pre_node=nxt_node;
}
}
void list::show()
{
struct node *ptr=start;
while(ptr!=NULL)
{
cout<<ptr->data<<"->'";
ptr=ptr->next;
}
}
void list::merge(list l1,list l2)
{
struct node *nxt_node, *pre_node, *pptr, *qptr;
int dat;
pptr=l1.start;
qptr=l2.start;
start=nxt_node=pre_node=NULL;
while(pptr!=NULL && qptr!=NULL)
{
if(pptr->data<=qptr->data)
{
dat=pptr->data;
pptr=pptr->next;
}
else
{
dat=qptr->data;
qptr=qptr->next;
}
nxt_node=new node;
nxt_node->data=dat;
nxt_node->next=NULL;
if(start==NULL)
{
start=nxt_node;
}
else
{
pre_node->next=nxt_node;
pre_node=nxt_node;
}
}
if(pptr==NULL)
{
while(qptr!=NULL)
{
nxt_node=new node;
nxt_node->data=qptr->data;
nxt_node->next=NULL;
if(start==NULL)
start=nxt_node;
else
pre_node->next=nxt_node;
pre_node=nxt_node;
qptr=qptr->next;
}
}
else if (qptr==NULL)
{
while(pptr!=NULL)
{
nxt_node=new node;
nxt_node->data=pptr->data;
nxt_node->next=NULL;
if(start==NULL)
start=nxt_node;
else
pre_node->next=nxt_node;
pre_node=nxt_node;
pptr=pptr->next;
}
}
}
int main()
{
list l1,l2,l3;
cout<<"Enter the first list in ascending order.";
l1.create();
cout<<"Enter the Second list in ascending order.";
l2.create();
cout<<"The first list is:"<<endl;
l1.show();
cout<<"The second list is"<<endl;
l2.show();
l3.merge(l1,l2);
l3.show();
return (0);
}
In the method merge
if(start==NULL)
{
start=nxt_node;
}
should be changed to
if(start==NULL)
{
start=nxt_node;
pre_node=start;
}
I think you can figure why.