dequeue class implementation

i am trying to make a Deque class....the problem i am facing is that it is not inserting elements in the queue....
here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// deque assng2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

template<class type>
class deque
{
	type front,rear;
	type MAX;
	type* items;
public:
	deque(int max)
	{
		front=-1;rear=-1;
		MAX=max;
		items=new type[max];
	}
	void insertfront(type a)
	{
		if(isempty())
		{
				front,rear=0;
				items[front]=a;
		}
		else if(isfull())
			cout<<"Over flow\n";
		else 
		{
			items[front]=a;
			front++;
		}
	}

	void insertrear(type a)
	{
		if(isempty())
			{
				front,rear=0;
				items[rear]=a;
		}
		else if(isfull())
			cout<<"Over flow\n";
		else if(rear==0)
		{
			items[rear]=a;
			rear=MAX-1;
		}
		else
		{
			items[rear]=a;
			rear--;
		}
	}

	void removefront(type &a)
	{
		if(isempty())
			cout<<"under flow \n";
		else if(front==rear)
		{
			a=items[front];
			front=rear=-1;
		}
		else
		{
			a=items[front];
			front=(front+1)%MAX;
			front --;
		}
	}
	void removerear(type &a)
	{
		if(isempty())
		cout<<"under flow \n";
		else if(rear==front)
		{
			a=items[rear];
			rear=front=-1;
		}
		else
		{
			a=items[rear];
			rear=(rear+1)%MAX;
			rear --;
		}
	}
	//type first();
	//type last();
	bool isempty()
	{
		if(front==-1 && rear==-1)
		{
			cout<<"the queue is empty \n";			
			return true;
		}
		else return false;
	}
	bool isfull()
	{
		if(front==MAX-1 && rear==front-1)
		{
			cout<<"the queue is full \n";
			return true;
		}
		else return false;
	}
	//int size();
};

int main()
{
	int dequeSize;
	cout<<"enter the size of the Queue\n";
	cin>>dequeSize;
	deque<int> D(dequeSize);
	D.insertfront(10);
	D.insertfront(20);
	D.insertfront(30);
	int a;
	D.removefront(a);
	cout<<a<<endl;
	D.removefront(a);
	cout<<a<<endl;
	D.removefront(a);
	cout<<a<<endl;
	int b;
	D.insertrear(1);
	D.insertrear(2);
	D.insertrear(3);
	D.removerear(b);
	cout<<b<<endl;
	D.removerear(b);
	cout<<b<<endl;
	D.removerear(b);
	cout<<b<<endl;
	
	system("pause");
	return 0;
}


Last edited on
front,rear=0; will only set rear to zero and not modify front. If you want to set both front and rear to zero you can do front = 0; rear = 0; or front = rear = 0;.
i chaged front = 0; rear = 0;
but stil am nt getting the the desired output...
in insertfront function it is inserting garbage values.....and showing me garbage values..

but in remove rear function it is giving me the last value tht i entered at insertfront function
Check these declarations. You're saying they're generic but you're using them like integers all over the place (assigning, incrementing, decrementing). The only reason this is working in the slightest is because you're creating your generic type parameterized by int.
1
2
type front,rear;
type MAX;


I think these variables could be int, and the only member that needs to have the generic type is the items.
Last edited on
does this really effect if i change the data type... ?
i mean the problem is there in the insert functions
Topic archived. No new replies allowed.