Tuesday 21 October 2014

Queue (insertion and deletion both at beginning and at end)

#include<iostream>
#include<conio.h>
using namespace std;
struct queue
{
    int data;
    queue *link;
}*FRONT,*REAR=NULL,*ptr,*aptr,*pptr;
void iar()
{
    int item;
    cout<<"enter the item to be inserted"<<endl;
    cin>>item;
    if(REAR==NULL)
    {
        ptr=new queue;
        ptr->data=item;
        ptr->link=NULL;
        REAR=ptr;
        FRONT=ptr;
    }
    else
    {
        ptr=new queue;
        ptr->data=item;
        ptr->link=REAR;
        REAR=ptr;
    }
}
void display()
{
    ptr=REAR;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<endl;
        ptr=ptr->link;
    }
}
void iaf()
{
    int item1;
    cout<<"enter the item to be inserted"<<endl;
    cin>>item1;
    ptr=new queue;
    ptr->data=item1;
    ptr->link=NULL;
    FRONT->link=ptr;
    FRONT=ptr;
}
void daf()
{
    pptr=REAR;
    ptr=REAR;
    int c=0;
    while(ptr!=NULL)
    {
        ptr=ptr->link;
        c++;
    }
    for(int i=1;i<c-1;i++)
    {
        pptr=pptr->link;
    }
    pptr->link=NULL;
    delete ptr;
    FRONT=pptr;
}
void dar()
{
    ptr=REAR;
    REAR=ptr->link;
    delete ptr;
}
int main()
{
    int option;
    while(option!=6)
    {
    cout<<"enter 1. to insert at rear \n 2. to insert at front \n 3. to delete at front \n 4. to delete from rear \n 5. to display \n 6. to exit "<<endl;
    cin>>option;
    switch(option)
    {
        case 1: iar();
        break;
        case 2: iaf();
        break;
        case 3: daf();
        break;
        case 4: dar();
        break;
        case 5: display();
        break;
        default : cout<<"wrongly entered"<<endl;
    }
    }
    getch();
    return 0;
}

No comments:

Post a Comment