Skip to content

Commit 71ca2ef

Browse files
authored
Add files via upload
1 parent e0c292f commit 71ca2ef

6 files changed

+529
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
struct Queue
5+
{
6+
int size; // size of queue
7+
int front; // front of queue // del is done from front
8+
int rear; // last of queue // indertion is donr from rear
9+
int* Q; // dynamically creatng Array
10+
};
11+
12+
void Create(struct Queue* q, int size)
13+
{
14+
q->size = size;
15+
q->front = q->rear = -1; // initially queue is empty
16+
q->Q = (int*)malloc(q->size * sizeof(int)); // everytime taking creating in heap memory
17+
18+
}
19+
void enqueue(struct Queue* q, int x)
20+
{
21+
if (q->rear == q->size - 1)
22+
printf("queue is full");
23+
else
24+
{
25+
q->rear++;
26+
q->Q[q->rear] = x;
27+
}
28+
}
29+
int dequeue(struct Queue* q)
30+
{
31+
int x = -1;
32+
if (q->front == q->rear)
33+
printf("Queue is empty");
34+
else
35+
{
36+
q->front++;
37+
x=q->Q[q->front];
38+
}
39+
return x;
40+
}
41+
void Display(struct Queue q)
42+
{
43+
int i;
44+
for (i = q.front + 1; i <= q.rear; i++)
45+
printf("%d ",q.Q[i]);
46+
printf("\n");
47+
}
48+
int main()
49+
{
50+
struct Queue q;
51+
Create(&q, 5);
52+
enqueue(&q, 10);
53+
enqueue(&q, 15);
54+
enqueue(&q, 20);
55+
enqueue(&q, 30);
56+
Display(q);
57+
printf("%d ",dequeue(&q));
58+
return 0;
59+
}

Queue/02 C++ Queue using Array.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include<iostream>
2+
#include<stdlib.h>
3+
using namespace std;
4+
5+
class Queue
6+
{
7+
private:
8+
int front;
9+
int rear;
10+
int size;
11+
int* Q;
12+
public:
13+
Queue() //Non-parameterized constructor //front and rear are assigned as -1 //indicates Queue is empty
14+
{
15+
front = rear = -1;
16+
size = 10; //Default size of the Queue will be 10
17+
Q = new int[size]; //Memory is allocated for the Queue in the HEAP
18+
}
19+
Queue(int size) //Parameterized constructor with size as the parameter
20+
{
21+
front = rear = -1;
22+
this->size = size;
23+
Q = new int[this->size];
24+
}
25+
void enqueue(int x);
26+
int dequeue();
27+
void display();
28+
};
29+
30+
void Queue::enqueue(int x)
31+
{
32+
if (rear == size - 1)
33+
cout << "Queue is Full" << endl;
34+
else
35+
{
36+
rear++;
37+
Q[rear] = x;
38+
}
39+
}
40+
41+
int Queue::dequeue()
42+
{
43+
int x = -1;
44+
if (front == rear)
45+
cout << "Queue is Empty" << endl;
46+
else
47+
{
48+
front++;
49+
x = Q[front];
50+
}
51+
return x;
52+
}
53+
54+
void Queue::display()
55+
{
56+
int i;
57+
for (i = front + 1; i <= rear; i++)
58+
cout << Q[i] << " ";
59+
cout << endl;
60+
}
61+
62+
int main()
63+
{
64+
Queue q(5);
65+
66+
q.enqueue(10);
67+
q.enqueue(20);
68+
q.enqueue(30);
69+
q.enqueue(40);
70+
71+
q.display();
72+
q.dequeue();
73+
q.display();
74+
75+
return 0;
76+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
struct Queue
5+
{
6+
int size; //size for the fixed size array which is going to be the Queue
7+
int front; //index pointer for deletion from Queue
8+
int rear; //index pointer for insertion in Queue
9+
int* Q; //pointer to create the Queue dynamically in the HEAP
10+
};
11+
12+
void create(struct Queue* q, int size)
13+
{
14+
q->size = size; //size of array assigned
15+
q->front = q->rear = -1; //front and rear are initialised with 0 //it is done for Circular Queue in order to use Mod operations to achieve Circular Mechanism
16+
q->Q = (int*)malloc(q->size * sizeof(int)); //Array is created dynamically in HEAP of size equal to q->size
17+
}
18+
19+
void enqueue(struct Queue* q, int x) //Function to insert element in the Queue
20+
{
21+
if ((q->rear + 1) % q->size == q->front) //Condition to check Queue is full or not //This is the condition to achieve the circular mechanism for adopting Circular Queue
22+
printf("Queue is Full\n");
23+
else
24+
{
25+
q->rear = (q->rear + 1) % q->size; //rear is assigned as (rear+1)%size //it is a mechanism to obtain circular and repeating values in the array //for circular queue
26+
q->Q[q->rear] = x; //value is inserted in Q[rear]
27+
}
28+
}
29+
30+
int dequeue(struct Queue* q) //Function to delete element from the queue
31+
{
32+
int x = -1; //x will store the deleted value //initialised with -1 //if queue is empty the function will return -1
33+
34+
if (q->front == q->rear) //Condtion to check if Queue is empty or not //Queue is empty when front equals rear
35+
printf("Queue is Empty\n");
36+
else
37+
{
38+
q->front = (q->front + 1) % q->size; //front is assigned as (front+1)%size //it is a mechanism to obtain circular and repeating values in the array //for circular queue
39+
x = q->Q[q->front]; //x is assigned with the value of Q[front]
40+
}
41+
return x; //return x
42+
}
43+
44+
void display(struct Queue q)
45+
{
46+
int i = (q.front + 1) % q.size; //Since, front points the index just before the first element, i should start from front+1 //rear points exactly to the previous element of front in a Circular Queue
47+
48+
do
49+
{
50+
printf("%d ", q.Q[i]);
51+
i = (i + 1) % q.size; //i is incremented as (i+1)%size //algorithm to obtain circular and repeating values
52+
} while (i != (q.rear + 1) % q.size); //Here, terminating condition of the do while loop is when i equals to (q.rear+1)%q.size //Since, it is a Circular Queue the terminating condition must follow the same mechanism of circularity
53+
54+
printf("\n");
55+
}
56+
57+
int main()
58+
{
59+
struct Queue q;
60+
create(&q, 5);
61+
62+
enqueue(&q, 10);
63+
enqueue(&q, 20);
64+
enqueue(&q, 30);
65+
enqueue(&q, 40);
66+
67+
//dequeue(&q);
68+
69+
display(q);
70+
71+
return 0;
72+
}

Queue/04 C++ Circular Queue.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class CircularQueue {
6+
private:
7+
int size;
8+
int front;
9+
int rear;
10+
int* Q;
11+
public:
12+
CircularQueue(int size);
13+
~CircularQueue();
14+
bool isFull();
15+
bool isEmpty();
16+
void enqueue(int x);
17+
int dequeue();
18+
void display();
19+
};
20+
21+
CircularQueue::CircularQueue(int size) {
22+
this->size = size;
23+
front = 0;
24+
rear = 0;
25+
Q = new int[size];
26+
}
27+
28+
CircularQueue::~CircularQueue() {
29+
delete[] Q;
30+
}
31+
32+
bool CircularQueue::isEmpty() {
33+
if (front == rear) {
34+
return true;
35+
}
36+
return false;
37+
}
38+
39+
bool CircularQueue::isFull() {
40+
if ((rear + 1) % size == front) {
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
void CircularQueue::enqueue(int x) {
47+
if (isFull()) {
48+
cout << "Queue Overflow" << endl;
49+
}
50+
else {
51+
rear = (rear + 1) % size;
52+
Q[rear] = x;
53+
}
54+
}
55+
56+
int CircularQueue::dequeue() {
57+
int x = -1;
58+
if (isEmpty()) {
59+
cout << "Queue Underflow" << endl;
60+
}
61+
else {
62+
front = (front + 1) % size;
63+
x = Q[front];
64+
}
65+
return x;
66+
}
67+
68+
void CircularQueue::display() {
69+
int i = front + 1;
70+
do {
71+
cout << Q[i] << flush;
72+
if (i < rear) {
73+
cout << " <- " << flush;
74+
}
75+
i = (i + 1) % size;
76+
} while (i != (rear + 1) % size);
77+
}
78+
79+
80+
int main() {
81+
82+
int A[] = { 1, 3, 5, 7, 9 };
83+
84+
CircularQueue cq(sizeof(A) / sizeof(A[0]) + 1);
85+
86+
// Enqueue
87+
for (int i = 0; i < sizeof(A) / sizeof(A[0]); i++) {
88+
cq.enqueue(A[i]);
89+
}
90+
91+
// Display
92+
cq.display();
93+
cout << endl;
94+
95+
// Overflow
96+
cq.enqueue(10);
97+
98+
// Dequeue
99+
for (int i = 0; i < sizeof(A) / sizeof(A[0]); i++) {
100+
cq.dequeue();
101+
}
102+
103+
// Underflow
104+
cq.dequeue();
105+
106+
return 0;
107+
}

0 commit comments

Comments
 (0)