-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
369c5e9
commit fa703c6
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Queue { | ||
private: | ||
int* arr; | ||
int capacity; | ||
int front; | ||
int rear; | ||
int count; | ||
|
||
public: | ||
Queue(int size) { | ||
arr = new int[size]; | ||
capacity = size; | ||
front = 0; | ||
rear = -1; | ||
count = 0; | ||
} | ||
|
||
~Queue() { | ||
delete[] arr; | ||
} | ||
|
||
void enqueue(int item) { | ||
if (isFull()) { | ||
cout << "Queue is full! Cannot enqueue " << item << endl; | ||
return; | ||
} | ||
rear = (rear + 1) % capacity; | ||
arr[rear] = item; | ||
count++; | ||
cout << "Enqueued: " << item << endl; | ||
} | ||
|
||
void dequeue() { | ||
if (isEmpty()) { | ||
cout << "Queue is empty! Cannot dequeue." << endl; | ||
return; | ||
} | ||
cout << "Dequeued: " << arr[front] << endl; | ||
front = (front + 1) % capacity; | ||
count--; | ||
} | ||
|
||
int getFront() { | ||
if (isEmpty()) { | ||
cout << "Queue is empty!" << endl; | ||
return -1; | ||
} | ||
return arr[front]; | ||
} | ||
|
||
int getRear() { | ||
if (isEmpty()) { | ||
cout << "Queue is empty!" << endl; | ||
return -1; | ||
} | ||
return arr[rear]; | ||
} | ||
|
||
bool isEmpty() { | ||
return count == 0; | ||
} | ||
|
||
bool isFull() { | ||
return count == capacity; | ||
} | ||
|
||
int size() { | ||
return count; | ||
} | ||
}; | ||
|
||
int main() { | ||
Queue queue(5); | ||
|
||
queue.enqueue(10); | ||
queue.enqueue(20); | ||
queue.enqueue(30); | ||
queue.enqueue(40); | ||
queue.enqueue(50); | ||
|
||
cout << "Front element: " << queue.getFront() << endl; | ||
cout << "Rear element: " << queue.getRear() << endl; | ||
|
||
queue.dequeue(); | ||
queue.dequeue(); | ||
|
||
cout << "After two dequeues:" << endl; | ||
cout << "Front element: " << queue.getFront() << endl; | ||
cout << "Rear element: " << queue.getRear() << endl; | ||
|
||
queue.enqueue(60); | ||
queue.enqueue(70); | ||
|
||
cout << "Queue size: " << queue.size() << endl; | ||
|
||
return 0; | ||
} |