Queue Using Double Link list


#include <iostream.h>
#include <conio.h>

struct mynode {
int data;
struct mynode* next;
struct mynode* prev;
};

mynode * headptr = NULL;

mynode * createnode()
{
int val;
cout<<"enter value for new node"<<endl;
cin>>val;

mynode * ptr = new mynode;
if(ptr==NULL)//failed
{
cout<<"failed to create node\n";
return NULL;
}
ptr->data = val;
ptr->next = NULL;
ptr->prev = NULL;
return ptr;
}

void AddEnd()
{
//navigate to end
mynode* current;
current = headptr;
while (current != NULL)
{
if(current->next==NULL)
break;
current = current->next;
}
//create new node
mynode* newnodeptr;
newnodeptr = createnode();

//if list is empty
if(current==NULL)
headptr=newnodeptr;

//add node to the end
current->next = newnodeptr;
newnodeptr->prev = current;
}

void Delete_Beg()

{
mynode* delptr;

if(headptr==NULL)
cout<<"link list is empty"<<endl;

delptr=headptr;//store address of first node

headptr=delptr->next;
headptr->prev=NULL;
delete delptr;
delptr=NULL;

}
void AddAfter()
{
int found;
int val;
cout<<"Enter the value to add after"<<endl;
cin>>val;

//search for the element
mynode* current;
current=headptr;
found=0;
while(current != NULL)
{
if(current->data == val)
{
found = 1;
break;
}
else
{
current = current->next;
}
}
if(found==0)
{
cout<<"Element not found"<<endl;
return;
}
else
cout<<"Element found"<<endl;
//create new node
mynode* newnodeptr;
newnodeptr = createnode();

//add new node
newnodeptr->next = current->next;
current->next->prev = newnodeptr;
newnodeptr->prev = current;
current->next = newnodeptr;
}

void DeleteAt()
{
int found;
int val;
cout<<"Enter the value to delete"<<endl;
cin>>val;

//search for the element
mynode* current;
mynode* delptr;

current=headptr;

found=0;

while(current != NULL)
{
if(current->data == val)
{
found = 1;
break;
}
else
{
current = current->next;
}
}
if(found==0)
{
cout<<"Element not found"<<endl<<endl;
return;
}
cout<<"Element found"<<endl;
//if element found is only element
if(current->prev==NULL && current->next==NULL)
{
delete headptr;
headptr=NULL;
}
else if (current->prev==NULL && current->next!=NULL) //first element
{
delptr = headptr;
headptr=delptr->next;
delptr->next->prev=NULL;

delete delptr;
delptr=NULL;
}
else if (current->prev!=NULL && current->next==NULL)//last element
{
current->prev->next = NULL;
delete current;
current=NULL;
}
else
{
current->prev->next = current->next;
current->next->prev = current->prev;
delete current;
current=NULL;
}
}
void get_rear()
{
mynode* current;
current= headptr;

if(headptr==NULL)
cout<<"\nqueue is empty\n"<<endl;
while (current != NULL)
{
if(current->next==NULL)
break;
current = current->next;
}
cout<<current->data<<endl;
}
void get_front()
{
if(headptr==NULL)
cout<<"queue is empty"<<endl;
else
{
cout<<headptr->data;
}
}
void isempty()
{
if(headptr==NULL)
cout<<"queue is empty"<<endl;
else
cout<<"queue is not empty"<<endl;

}
void Display()
{
mynode* current;
current = headptr;

if(headptr==NULL)
cout<<"queue is empty"<<endl;

while (current != NULL)
{
cout << current->data << " ";
current = current->next;
}
cout<<endl<<endl;
}

void main()
{
int choice;
clrscr();
cout<<"LINK LIST PROGRAM"<<endl;
while(1)
{
cout<<"MAIN MENU"<<endl;
cout<<"1.enqueue"<<endl;
cout<<"2.dequeue"<<endl;
cout<<"3.get rear"<<endl;
cout<<"4.get front"<<endl;
cout<<"5.isempty"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Exit"<<endl<<endl;
cout<<"Enter choice"<<endl;
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
AddEnd();
break;
case 2:
Delete_Beg();
break;
case 3:
get_rear();
break;
case 4:
get_front();
break;
case 5:
isempty();
break;
case 6:
Display();
break;
case 7:
return;
default: cout<<"\n\nInvalid Choice . ";
}
} //end of outer while
}

Topic archived. No new replies allowed.