answersLogoWhite

0

#include<iostream.h>

class node

{

public:

int data;

node* link;

};

class queue

{

node* front;

node* back;

public:

queue()

{

front=NULL;

back=NULL;

}

void enqueue(int d)

{

node* ptr;

ptr=new node;

ptr->data=d;

ptr->link=NULL;

if(front==NULL)

{

front=ptr;

back=ptr;

}

else

{

back->link=ptr;

back=back->link;

}

}

void dequeue()

{

node* ptr;

ptr=front;

if(front==NULL)

cout<<"\n Nothing can be deleted. \n";

else

{

ptr=front;

front=front->link;

delete ptr;

ptr=NULL;

cout<<"\n The first element has been deleted.\n";

Front();

}

}

void If_Empty()

{

if(front==NULL)

cout<<"\n The queue is empty.\n ";

}

void Front()

{

if(front!=NULL)

cout<<"\n The first element is "<<front->data<<endl;

}

};

void main()

{

queue q;

int data;

char opt;

do

{

cout<<" Enter your data:\t";

cin>>data;

q.enqueue(data);

cout<<"\n Do you want to continue:\t";

cin>>opt;

}while(opt=='y'opt=='Y');

q.Front();

q.dequeue();

q.If_Empty();

q.dequeue();

q.dequeue();

q.If_Empty();

q.dequeue();

}

User Avatar

Wiki User

14y ago

What else can I help you with?

Continue Learning about Engineering

How can implement round robin sceduler in java using circular doubly linked list?

I'm sorry brother


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


Does each node in a doubly linked list contain a link to the previous as well as the next node?

Yes, each node in a doubly linked list contain a link to the previous as well as the next node. That is the definition of the doubly linked list.


Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.


What are the applications for circular linked lists?

A singly-linked circular list is useful for implementing queue data structures with minimum overhead. Normally we implement a queue with two pointers: one to the tail for insertions and one to the head for extractions. With a circular list we only need to maintain a single pointer to the tail because the tail always points "forwards" to the head (instead of null as it normally would), thus achieving constant-time access to both the head and tail via a single pointer. Circular linked lists are generally useful wherever "wraparound" is necessary. That is, from any given node in the list, we can traverse forwards with the guarantee that we will eventually arrive back at that same node. With doubly-linked circular lists we have the advantage of traversing in either direction (bi-directional traversal).

Related Questions

C program to implement deque using circular linked list?

You'll need to use a doubly-linked circular list, since otherwise when you pop off the tail element you'll need to whizz all the way round the list to find its predecessor. See the links section for an implementation of a doubly-linked circular list.


How can implement round robin sceduler in java using circular doubly linked list?

I'm sorry brother


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


What is the difference between doubly linked list and circular linked list?

A doubly linked list is a linked list in which each node knows where both of its neighbors are.A circular linked list is a linked list in which the "tail" of the list is linked to the "root". (Note that both the tail and root of the list are undefined/arbitrary in a circular linked list)Doubly linked lists are actually not necessarily related to circular linked list (aside from both being based on a linked list structure). In fact, you can have a circular doubly linked list, where each node knows where both of its neighbors are andwhere the list wraps around to connect to itself.


Can we use doubly linked list as a circular linked list?

Yes. The tail node's next node is the head node, while the head node's previous node is the tail node.


How do you implement a doubly linked list by using singly linked list?

Add another pointer to the nodes for the previous node: struct node { struct node *next; struct node *previous; void *data; }; typedef struct node node; Then change the logic for insertion and removal to make sure you set the previous pointer as well as the next one.


Does each node in a doubly linked list contain a link to the previous as well as the next node?

Yes, each node in a doubly linked list contain a link to the previous as well as the next node. That is the definition of the doubly linked list.


Write a program in C to create a Circular Linked list?

#include&lt;iostream.h&gt;


Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.


How to write aC program to merge two singly linked list?

write a c program to circular queue


what are the differences between singly link list and doubly link list?

singly linked list stores only the address of next node while doubly linked list stores the address of previous node and next node and hence it is called doubly linked list. In singly linked list only forward traversing is possible while in doubly linked list forward and backward traversal is possible.


What are the applications for circular linked lists?

A singly-linked circular list is useful for implementing queue data structures with minimum overhead. Normally we implement a queue with two pointers: one to the tail for insertions and one to the head for extractions. With a circular list we only need to maintain a single pointer to the tail because the tail always points "forwards" to the head (instead of null as it normally would), thus achieving constant-time access to both the head and tail via a single pointer. Circular linked lists are generally useful wherever "wraparound" is necessary. That is, from any given node in the list, we can traverse forwards with the guarantee that we will eventually arrive back at that same node. With doubly-linked circular lists we have the advantage of traversing in either direction (bi-directional traversal).