Double Linklist

#include <iostream>

#include <string.h>

using namespace std;


class Pas{

private:

int id;

string name;

Pas* next;

Pas* prev;

public:

Pas(int id, string name, Pas* next=NULL, Pas* prev=NULL){

SetId(id);

SetName(name);

SetNext(next);

SetPrev(prev);

}

void SetId(int id){

this->id=id;

}

int GetId(){

return id;

}

void SetName(string name){

this->name=name;

}

string GetName(){

return name;

}

void SetNext(Pas* next){

this->next=next;

}

Pas* GetNext(){

return next;

}

void SetPrev(Pas* prev){

this->prev=prev;

}

Pas* GetPrev(){

return prev;

}

};

class Linklist{

private:

Pas* head;

Pas* tail;

void delonhead(){

Pas* ptr = GetHead();

ptr->GetNext()->SetPrev(NULL);

SetHead(ptr->GetNext());

delete ptr;

}

void delontail(){

Pas* ptr = GetTail();

ptr->GetPrev()->SetNext(NULL);

SetTail(ptr->GetPrev());

delete ptr;

}

public:

Linklist(Pas* head=NULL, Pas* tail=NULL){

SetHead(head);

SetTail(tail);

}

void SetHead(Pas* head){

this->head=head;

}

Pas* GetHead(){

return head;

}

void SetTail(Pas* tail){

this->tail=tail;

}

Pas* GetTail(){

return tail;

}

bool isempty(){

return GetHead()==NULL;

}

Pas* isavailable(int id){

Pas* ptr = GetHead();

Pas* temp=NULL;

while(ptr!=NULL){

if(ptr->GetId()==id){

temp=ptr;

}

ptr= ptr->GetNext();

}

return temp;

}

void addonhead(int id, string name){

if(isempty()){

Pas* p = new Pas(id,name);

SetHead(p);

SetTail(p);

cout<<"PASSENGER ADDED..!"<<endl;

}else{

Pas* p = new Pas(id,name);

if(isavailable(p->GetId())!=NULL){

cout<<"Passenger Already Exist With Same Id..!"<<endl;

}else{

p->SetNext(GetHead());

GetHead()->SetPrev(p);

SetHead(p);

cout<<"PASSENGER ADDED..!"<<endl;

}

}

}

void addontail(int id, string name){

if(isempty()){

addonhead(id,name);

}else{

Pas* p = new Pas(id,name);

if(isavailable(p->GetId())!=NULL){

cout<<"Passenger Already Exist With Same Id..!"<<endl;

}else{

p->SetPrev(GetTail());

GetTail()->SetNext(p);

SetTail(p);

cout<<"PASSENGER ADDED..!"<<endl;

}

}

}

void display(){

if(isempty()){

cout<<"List is Empty..!"<<endl;

}else{

Pas* ptr = GetHead();

while(ptr!=NULL){

cout<<"Id : "<<ptr->GetId()<<"| Name : "<<ptr->GetName()<<endl;

cout<<"_____________"<<endl;

ptr=ptr->GetNext();

}

}

}

void print(int id){

Pas* temp = isavailable(id);

if(temp!=NULL){

cout<<"Id :"<<temp->GetId()<<" | Name :"<<temp->GetName()<<endl;

cout<<"______________"<<endl;

}else{

cout<<"No Passenger with Such ID..!"<<endl;

}

}

void addbefor(int count, int id, string name){

if(isempty()){

addonhead(id,name);

}else{

if(isavailable(id)!=NULL){

cout<<"Passenger Already Exist With Same Id..!"<<endl;

}else{

Pas* p = new Pas(id,name);

Pas* temp = isavailable(count);

p->SetPrev(temp->GetPrev());

temp->GetPrev()->SetNext(p);

p->SetNext(temp);

temp->SetPrev(p);

cout<<"PASSENGER ADDED..!"<<endl;

}

}

}

void dell(int id){

if(isempty()){

cout<<"LIST IS EMPTY..!"<<endl;

}else{

Pas* temp = isavailable(id);

if(temp->GetPrev()==NULL){

delonhead();

}else if(temp->GetNext()==NULL){

delontail();

}else{

temp->GetPrev()->SetNext(temp->GetNext());

temp->GetNext()->SetPrev(temp->GetPrev());

delete temp;

}

}

}

};



int main(){

Linklist l;

cout<<"_____WELCOME TO FAISAL MOVERS_____"<<endl;

int choice, id, s;

string name;

do{

cout<<"______________"<<endl;

cout<<"For Add PASSENGER ON FRONT SEAT PRESS/1"<<endl;

cout<<"FOR ADD PASSENGER ON LAST SEAT PRESS /2"<<endl;

cout<<"FOR ADD PASSENGER BEFORE ANY SEAT PRESS/3"<<endl;

cout<<"FOR SEARCH ANY PASSENGER PRESS /4"<<endl;

cout<<"FOR DELETE ANY PASSENGER PRESS /5"<<endl;

cout<<"FOR WATCHING WHOLE BOOKING LIST PRESS /6"<<endl;

cout<<"______________"<<endl;

cout<<"ENTER YOUR CHOICE :";cin>>choice;

switch(choice){

case 1:

cout<<"ENTER ID : ";cin>>id;

cout<<"ENTER NAME: ";cin>>name;

l.addonhead(id,name);

break;

case 2:

cout<<"ENTER ID : ";cin>>id;

cout<<"ENTER NAME: ";cin>>name;

l.addontail(id,name);

break;

case 3:

cout<<"ENTER ID : ";cin>>id;

cout<<"ENTER NAME: ";cin>>name;

cout<<"ENTER SPECIFIC SEAT";cin>>s;

l.addbefor(s,id,name);

break;

case 4:

cout<<"ENTER ID : ";cin>>id;

l.print(id);

break;

case 5:

cout<<"ENTER ID : ";cin>>id;

l.dell(id);

break;

case 6:

l.display();

break;

case 0:

exit(0);

break;

default:

cout<<"INVALID SELECTION..!"<<endl;

}

}while(1);

return 0;

}

Comments

Popular posts from this blog