Skip to content

Commit 0e302f6

Browse files
authored
Add files via upload
1 parent 2803f0a commit 0e302f6

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

queue1.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
#include <iostream>
3+
#define MAX 5 // Define the maximum size of the queue
4+
5+
using namespace std;
6+
7+
class Queue {
8+
int arr[MAX]; // Array to store queue elements
9+
int front, rear; // Indices for the front and rear of the queue
10+
11+
public:
12+
// Constructor to initialize the queue
13+
Queue() : front(-1), rear(-1) {}
14+
15+
// Function to check if the queue is full
16+
bool isFull()
17+
18+
19+
{
20+
return (rear + 1) % MAX == front;
21+
}
22+
23+
// Function to check if the queue is empty
24+
bool isEmpty()
25+
26+
27+
{
28+
return front == -1;
29+
}
30+
31+
// Function to add an element to the queue
32+
void enqueue(int x)
33+
{
34+
if (isFull())
35+
{
36+
cout << "Queue is full" << endl;
37+
return;
38+
}
39+
40+
if (front == -1) front = 0; // Initialize front if queue is empty
41+
rear = (rear + 1) % MAX; // Move rear to the next position
42+
arr[rear] = x; // Insert the element
43+
}
44+
45+
// Function to remove an element from the queue
46+
int dequeue() {
47+
if (isEmpty()) {
48+
cout << "Queue is empty" << endl;
49+
return -1;
50+
}
51+
int result = arr[front]; // Store the front element to return
52+
if (front == rear) { // Check if the queue becomes empty after dequeue
53+
front = rear = -1; // Reset front and rear
54+
} else {
55+
front = (front + 1) % MAX; // Move front to the next position
56+
}
57+
return result; // Return the dequeued element
58+
}
59+
60+
// Function to get the front element of the queue
61+
int getFront() {
62+
if (isEmpty()) {
63+
cout << "Queue is empty" << endl;
64+
return -1;
65+
}
66+
return arr[front]; // Return the front element
67+
}
68+
69+
// Function to get the rear element of the queue
70+
int getRear() {
71+
if (isEmpty()) {
72+
cout << "Queue is empty" << endl;
73+
return -1;
74+
}
75+
return arr[rear]; // Return the rear element
76+
}
77+
};
78+
79+
int main() {
80+
Queue q; // Create a queue instance
81+
q.enqueue(1); // Add elements to the queue
82+
q.enqueue(2);
83+
q.enqueue(3);
84+
q.enqueue(4);
85+
q.enqueue(5);
86+
q.enqueue(6); // This will show "Queue is full"
87+
88+
cout << "Front element: " << q.getFront() << endl; // Display the front element
89+
cout << "Rear element: " << q.getRear() << endl; // Display the rear element
90+
91+
q.dequeue(); // Remove an element from the queue
92+
q.enqueue(6); // Add another element to the queue
93+
94+
while (!q.isEmpty()) { // Print all elements until the queue is empty
95+
cout << q.dequeue() << " ";
96+
}
97+
98+
return 0;
99+
}

stack final.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <iostream> // Include the input-output stream library
2+
using namespace std;
3+
4+
#define MAX 5 // Define the maximum size of the stack
5+
6+
// Declare the stack and the top variable
7+
int stack[MAX], top = -1;
8+
9+
// Function to check if the stack is empty
10+
bool isEmpty() {
11+
// If top is -1, stack is empty
12+
return top == -1;
13+
}
14+
15+
// Function to check if the stack is full
16+
bool isFull() {
17+
// If top is equal to the last index, stack is full
18+
return top == MAX - 1;
19+
}
20+
21+
// Function to push an element onto the stack
22+
void push(int x) {
23+
// Check if the stack is full
24+
if (isFull()) {
25+
cout << "The stack is full." << endl; // Display a message if the stack is full
26+
} else {
27+
top++; // Increment the top index
28+
stack[top] = x; // Add the new element to the top of the stack
29+
}
30+
}
31+
32+
// Function to pop an element from the stack
33+
void pop() {
34+
// Check if the stack is empty
35+
if (isEmpty()) {
36+
cout << "The stack is empty." << endl; // Display a message if the stack is empty
37+
} else {
38+
cout << "Popped element: " << stack[top] << endl; // Display the popped element
39+
top--; // Decrement the top index to remove the top element
40+
}
41+
}
42+
43+
// Function to peek the top element of the stack
44+
int peek() {
45+
// Check if the stack is empty
46+
if (isEmpty()) {
47+
cout << "The stack is empty." << endl; // Display a message if the stack is empty
48+
return -1; // Return -1 if the stack is empty
49+
} else {
50+
return stack[top]; // Return the top element of the stack
51+
}
52+
}
53+
54+
// Function to display the elements of the stack
55+
void display() {
56+
// Check if the stack is empty
57+
if (isEmpty()) {
58+
cout << "The stack is empty." << endl; // Display a message if the stack is empty
59+
} else {
60+
cout << "Stack elements: " << endl; // Display a message indicating stack elements
61+
// Loop through the stack from top to bottom
62+
for (int i = top; i >= 0; i--) {
63+
cout << stack[i] << endl; // Print each element of the stack
64+
}
65+
}
66+
}
67+
68+
// Main function
69+
int main() {
70+
push(1); // Push 1 onto the stack
71+
push(2); // Push 2 onto the stack
72+
push(3); // Push 3 onto the stack
73+
push(4);
74+
display(); // Display the stack elements
75+
pop(); // Pop the top element from the stack
76+
display(); // Display the stack elements after popping
77+
cout << "Top element: " << peek() << endl; // Display the top element of the stack
78+
return 0; // Return 0 to indicate successful execution
79+
}
80+

0 commit comments

Comments
 (0)