الأحد، 23 فبراير 2014
10:36 ص

Deque (Double-ended queue)


#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;
class linked_deque
{
private:
struct node
{
char data;
node *next;
node *prev;
node(char item)
{
data = item;
next = prev = NULL;
}
};
node *first;
node *last;
int count;
public:
linked_deque()
{
first = last = NULL;
count = 0;
}
bool isempty(){ return (count == 0); }
void addfirst(char item)
{
node *p = new node(item);
if (isempty())
first = last = new node(item);
else
{
last->next = p;
p->prev = last;
last = p;
}
count++;
}
void addlast(char item)
{
node *p = new node(item);
last->next = p;
p->prev = last;


last = p;
}

char removefirst()
{
if (isempty())
{
cout << "the Queue is Empty";
return NULL;
}
else
{
char item = first->data;
first = first->next;
return item;
}
count--;
}
char removelast()
{
char temp;if(!isempty()){temp=last->data;last=last->prev;last->next=NULL;count--;return temp;}else if(first==last){temp=first->data;first=last=NULL;return temp;}else{cout<<"queue is empty."<<endl;return NULL;}}
void display()
{
node *p = first;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
};
void main()
{
linked_deque d;
d.addfirst('a');
d.addfirst('b');
d.addlast('c');
d.display();
d.removefirst();
cout << endl;
d.display();
d.removelast();
cout << endl;
d.display();

_getch();
}

after correct and for more advantage:


#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;
class linked_deque
{
private:
struct node
{
char data;
node *next;
node *prev;
node(char item)
{
data = item;
next = prev = NULL;
}
};
node *first;
node *last;
int count;
public:
linked_deque()
{
first = last = NULL;
count = 0;
}
bool isempty(){ return (count == 0); }
void addfirst(char item)
{
if (isempty())
first = last = new node(item);
else
{
node *p = new node(item);
first->prev = p;
p->next = first;
first = p;


}
count++;
}
void addlast(char item)
{
node *p = new node(item);
p->prev = last;
last->next = p;
last = p;


count++;
}

char removefirst()
{
if (isempty())
{
cout << "the Queue is Empty";
return NULL;
}
else
{
char item = first->data;
first = first->next;
return item;
}
count--;
}
char removelast()
{
char item = last->data;
last = last->prev;
last->next = NULL;
return item;
count--;
}

void display()
{
node *p = first;
cout << "---------------------------\n";
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << "\n---------------------------\n";
}

};
void main()
{
linked_deque d;
int ch;
int k = 1;
while (k == 1)
{
cout << "\n0-insert item from first and display\n1- insert item from last and diplay\n2-delete first item and disply\n3-delete last item and display\n4-exit\n";
cin >> ch;
switch (ch)
{
case 0:
d.addfirst('a');
d.display();
break;
case 1:
d.addlast('b');
d.addlast('c');
d.display();
break;
case 2:
d.removefirst();
cout << endl;
d.display();
break;
case 3:
d.removelast();
cout << endl;
d.display();
break;
case 4:
cout << "good bye";
k = 0;
}
}
_getch();
}



0 التعليقات:

إرسال تعليق