Thursday 23 October 2014

Queue(Linked-List)

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *link;
}*head=NULL,*FRONT,*REAR=NULL,*ptr,*pptr;
void insert()
{
    int item;
    cout<<"enter the data to inserted"<<endl;
    cin>>item;
    if(REAR==NULL)
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=NULL;
        REAR=ptr;
        FRONT=ptr;
    }
    else
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=REAR;
        //head->link=ptr;
        REAR=ptr;
    }
}
void display()
{
    ptr=REAR;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<"  ";
        ptr=ptr->link;
    }
    cout<<endl;
}
void delete_q()
{
    int c=0;
    ptr=REAR;
    pptr=REAR;
    while(ptr!=NULL)
    {
        ptr=ptr->link;
        c++;
    }
    for(int i=1;i<c-1;i++)
    {
        pptr=pptr->link;
    }
    pptr->link=NULL;
    FRONT=pptr;
}

int main()
{
    int option;
    while(option!=4)
    {
    cout<<"enter 1. to insert \n 2. to display \n 3. delete \n 4. to exit"<<endl;
    cin>>option;
    switch(option)
    {
        case 1: insert();
        break;
        case 2: display();
        break;
        case 3: delete_q();
        break;
        default: cout<<"wrongly entered"<<endl;
    }
    }
    getch();
    return 0;
}

No comments:

Post a Comment