Implementation of Queues using Linked lists - c++

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;

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

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

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
}

linked list using class in c++

I am trying this linked list using class in C++. I have implemented tree using the same approach before. But in the below code, it seems that the next pointer in the linked_list isn't working as example. The line in the main function has been commented, is where the main problem lies.
#include<iostream>
#include<cstdio>
using namespace std;
class node{
node* next;
char data;
public:
node(char x){
data=x;
next=NULL;
}
node(){
data='~';
next=NULL;
}
node* get_next_node(){
return next;
}
char get_data(){
return data;
}
void set_data(char x){
data=x;
}
};
class Linked_List{
node *Head;
public:
Linked_List(char v){
Head= new node(v);
}
Linked_List(){
Head= new node();
}
void append(char v){
node *Cur;
for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
;
}
Cur= new node(v);
cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
}
node* get_Head(){
return Head;
}
void clear(){
Head=NULL;
}
void show(){
node *Cur;
for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
cout<<Cur->get_data()<<" ";
}
}
};
class Graph{
int vertices;
public:
Linked_List *arr;
Graph(int v){
vertices=v;
arr=new Linked_List[v];
}
void addEdge(char x, char y){
int i;
bool flag;
bool flag2=false;
for(i=0;i<vertices;i++){
if(arr[i].get_Head()->get_data()==x){
arr[i].append(y);
flag=true;
break;
}
else if(arr[i].get_Head()->get_data()=='~'){
flag=false;
break;
}
}
if(flag==false){
arr[i].get_Head()->set_data(x);
arr[i].append(y);
}
/*int j;
for( j=0;j<vertices;j++){
if(arr[j].get_Head()->get_data()==y){
flag2= true;
break;
}
if(arr[j].get_Head()->get_data()=='~'){
break;
}
}
if(flag2==false){
arr[j].get_Head()->set_data(y);
}*/
}
void show(){
for(int i=0;i<vertices;i++){
arr[i].show();
cout<< endl;
}
}
};
int main(){
int v;
char x,y;
cin>>v;
Graph bfs(v);
int edge;
cin>>edge;
for(int i=0;i<edge;i++){
cin>>x>>y;
bfs.addEdge(x,y);
}
bfs.show();
/*Linked_List ll('4');
ll.append('5');
ll.append('6');
char a=ll.get_Head()->get_data();
cout<<a;
a=ll.get_Head()->get_next_node()->get_data();
cout<<a;*/
char a=bfs.arr[0].get_Head()->get_data();
cout<<a<<endl;
if(bfs.arr[0].get_Head()->get_next_node()){ //this condition should be true if there
//is other values. but it's not working.
a=bfs.arr[0].get_Head()->get_next_node()->get_data();
}
cout<<a;
return 0;
}
/*
4
5
0 1
0 2
1 3
1 2
2 1
*/
In class Linked_List, modify append():
void append(char v){
node *Cur;
for(Cur=Head;Cur->get_next_node()!=NULL;Cur=Cur->get_next_node()){
;
}
Cur->set_next_node(new node(v));
cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
}
In class node, add set_next_node() method:
void set_next_node(node *n)
{
this->next=n;
}
In a linked list, every next of a node should contain the next node. But what you've done is loop till Cur is NULL. If you do that, you cannot set the next of the last node to the new node.
To add a new node after the current node, the set_next_node() method is used.

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.