if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stek to'liq" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stek bo'sh" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
void reverse() {
int n = top + 1; // Get the number of elements in the stack
int* tmp = new int[n]; // Create a temporary array to store the reversed elements
for (int i = 0; i < n; i++) {
tmp[i] = arr[top--]; // Pop elements from the original stack and store them in the temporary array
}
for (int i = 0; i < n; i++) {
push(tmp[i]); // Push the reversed elements back onto the original stack
}
delete[] tmp; // Free the temporary array
}
};
int main() {
//Initialize the stack stk
Stack stk;
cout << "Stek elementlari:";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(5);
stk.push(1);
stk.push(0);
// Reverse the elements of the stack
stk.display();
stk.reverse();
cout << "Elementlarni teskari chop etish:";
stk.display();
cout << "\nIkkita elementini ochirish:";
stk.pop();
stk.pop();
stk.display();
cout << "\nYana ikkita element kiritish";
stk.push(-1);
stk.push(10);
stk.display();
stk.reverse();
cout << "Teskari tartibta chop etish:";
stk.display();
cout << endl;
return 0;
}
#include
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}
bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
void sort() {
// Initialize a temporary stack
Stack tmp;
while (!isEmpty()) {
int x = pop();
// Pop an element from the original stack
while (!tmp.isEmpty() && tmp.peek() > x) {
// Pop elements from the temporary stack and push them back onto the original stack
push(tmp.pop());
}
// Push the popped element onto the temporary stack
tmp.push(x);
}
// Copy elements from the temporary stack back to the original stack
while (!tmp.isEmpty()) {
push(tmp.pop());
}
}
};
int main() {
//Initialize the stack stk
Stack stk;
cout << "Stek elemenlarini kiritish\n";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(5);
stk.push(1);
stk.push(0);
// Display the elements of the stack
stk.display();
cout << "\nStekni saralash:\n";
stk.sort();
cout << "Saralangan stekni chop etish:";
stk.display();
cout << "\nikkita elementni o'chirish";
stk.pop();
stk.pop();
stk.display();
cout << "\nYana ikkita element kirtish";
stk.push(-1);
stk.push(10);
stk.display();
cout << "\nstek elementlarini saralash:\n";
stk.sort();
cout << "Saralangan massiv:";
stk.display();
cout << endl;
return 0;
}
#include
using namespace std;
#define MAX_SIZE 15 // Maximum size of stack
class Stack {
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}
bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
double average() {
if (isEmpty()) {
return 0.0; // Return 0 if the stack is empty
}
int sum = 0;
for (int i = 0; i <= top; i++) {
sum += arr[i]; // Calculate the sum of all elements in the stack
}
return (double) sum / (top + 1); // Return the average value
}
};
int main() {
//Initialize the stack stk
Stack stk;
cout << "Stekka elementlarni kiritish:";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(5);
stk.push(1);
stk.push(0);
stk.display();
// Calculate the average value of the elements in the stack
double avg_val = stk.average();
printf("Stek elementlarining o'rtachasi: %0.2f\n", avg_val);
cout << "\nIkkita element o'chirish:";
stk.pop();
stk.pop();
stk.display();
cout << "\nIkkita element kiritish";
stk.push(-1);
stk.push(10);
stk.display();
avg_val = stk.average();
printf("Stek elementlarining o'rtachasi: %0.2f\n", avg_val);
cout << endl;
return 0;
}
#include
using namespace std;
#define MAX_SIZE 15 // Maximum size of stack
class Stack {
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}
bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
int findMiddleElement() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return -1;
}
int middleElement;
for (int i = 0; i < mid; i++) {
int element = pop();
if (i == mid - 1) {
// Store the middle element
middleElement = element;
}
// Push other elements onto the temporary stack
temp.push(element);
}
while (!temp.isEmpty()) {
// Return elements from the temporary stack to the original stack
push(temp.pop());
}
return middleElement;
}
};
int main() {
//Initialize the stack stk
Stack stk;
cout << "Input some elements onto the stack:";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(5);
stk.display();
// Find the maximum value
int middleElement = stk.findMiddleElement(); // Find the middle element of the stack
cout << "Steknin' o'rtada joylashgan elementi: " << middleElement << endl;
cout << "\nYana uchta element kiritish";
stk.push(-1);
stk.push(-2);
stk.push(-3);
stk.display();
middleElement = stk.findMiddleElement(); // Find the middle element of the stack
cout << "Steknin' o'rtada joylashgan elementi: " << middleElement << endl;
return 0;
}
#include using namespace std;
#define MAX_SIZE 15 // Maximum size of stack
class Stack {
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}
bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
int find_and_Remove_max_val() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return -1;
}
int maxElement = INT_MIN;
Stack temp;
while (!isEmpty()) {
int element = pop();
if (element > maxElement) {
//If a larger element is found, update maxElement
maxElement = element;
}
// Add other elements to the temporary stack
temp.push(element);
}
while (!temp.isEmpty()) {
int element = temp.pop();
// All elements except the largest are pushed back onto the stack
if (element != maxElement) {
push(element);
}
}
return maxElement;
}
};
int main() {
//Initialize the stack stk
Stack stk;
cout << "Stek elementlarini kiritish:";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(5);
stk.display();
int z = stk.find_and_Remove_max_val();
cout << "Stekdan " << z << " eng katta elementini topish va o'chirish.";
stk.display();
cout << "\nYana ikkita element kiritish";
stk.push(-1);
stk.push(20);
stk.display();
z = stk.find_and_Remove_max_val();
cout << "Stekda " << z << " eng katta elementini topish va o'chirish.";
stk.display();
cout << endl;
return 0;
}
#include
using namespace std;
#define MAX_SIZE 15 // Maximum size of stack
class Stack {
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}
bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
void remove_duplicates(Stack& stk);
};
void Stack::remove_duplicates(Stack& stk) {
if (stk.isEmpty()) {
cout << "Stack is empty." << endl;
return;
}
int size = stk.top + 1;
int temp[size];
int count = 0;
//The stack elements are copied into a temporary array
while (!stk.isEmpty()) {
temp[count++] = stk.pop();
}
// Remove duplicates from the temporary array
for (int i = 0; i < count; i++) {
for (int j = i+1; j < count; j++) {
if (temp[i] == temp[j]) {
//Duplicate elements are marked with -1
temp[j] = -1;
}
}
}
//Non-duplicate elements are pushed back onto the stack
for (int i = count-1; i >= 0; i--) {
if (temp[i] != -1) {
stk.push(temp[i]);
}
}
}
using namespace std;
#define MAX_SIZE 15 // Maximum size of stack
class Stack {
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}
bool push(int x) {
if (isFull()) {
cout << "Stack is full" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
void replaceKthPosition(Stack& stk, int k, int value) {
if (stk.isEmpty()) {
cout << "Stack is empty." << endl;
return;
}
int size = stk.top + 1;
int temp[size];
int count = 0;
//The stack elements are copied into a temporary array
while (!stk.isEmpty()) {
temp[count++] = stk.pop();
}
Stack stk;
cout << "Stek elementlarini kiritish (linked list tan foydalangan holda):\n";
stk.push(6);
stk.push(5);
stk.push(3);
stk.push(1);
stk.display();
cout << "\nstekdan ikkita elementni o'chirish:\n";
stk.pop();
stk.pop();
stk.display();
cout << "\nYana ikkita element kiritish:\n";
stk.push(8);
stk.push(9);
stk.display();
return 0;
}
// The elements are pushed back into the stack in reverse order
for (int i = count-1; i >= 0; i--) {
stk.push(temp[i]);
}
}
};
int main() {
Stack stk;
cout << "Input some elements onto the stack:";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(2);
stk.push(5);
stk.display();
cout << "\nk chi elementni berilgan qiymat bilan almashtirish:";
int k = 2, new_val = 14;
cout << "\nk = " << k << " Yangi qiymat = " << new_val;
stk.replaceKthPosition(stk, k, new_val);
stk.display();
cout << "\nk chi elementni berilgan qiymat bilan almashtirish:";
k = 5, new_val = 56;
cout << "\nk = " << k << " Yangi qiymat = " << new_val;
stk.replaceKthPosition(stk, k, new_val);
stk.display();
cout << endl;
}
#include
using namespace std;
#define MAX_SIZE 15 // Maximum size of stack
class Stack {
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}
bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
// Return top element and decrement top index
return arr[top--];
}
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
// Return top element without modifying top index
return arr[top];
}
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}
void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStek elementlari: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
int find_and_remove_lowest() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return -1;
}
int minElement = INT_MAX;
Stack temp;
while (!isEmpty()) {
int element = pop();
if (element < minElement) {
minElement = element; // Update minElement if a smaller element is found
}
temp.push(element); // Push other elements onto the temporary stack
}
while (!temp.isEmpty()) {
int element = temp.pop();
if (element != minElement) { // Push all elements except for the minElement back onto the original stack
push(element);
}
}
return minElement;
}
};
int main() {
//Initialize the stack stk
Stack stk;
cout << "Input some elements onto the stack:";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(5);
stk.display();
int z = stk.find_and_remove_lowest();
cout << "Stekda eng kichik elementi: " << z << " ni o'chirish.";
stk.display();
cout << "\nYana ikkita element kiritish";
stk.push(-1);
stk.push(2);
stk.display();
z = stk.find_and_remove_lowest();
cout << "Stekda eng kichik elementi: " << z << " ni o'chirish.";
stk.display();
cout << endl;
return 0;
}
#include
#include using namespace std;
const int MAX_SIZE = 100;
class Queue {
private: int front;
int rear;
int arr[MAX_SIZE];
public: Queue() {
front = -1;
rear = -1;
}
bool isFull() {
return (rear == MAX_SIZE - 1);
}
bool isEmpty() {
return (front == -1 && rear == -1);
}
void enqueue(int x) {
if (isFull()) {
cout << "Error: Queue is full" << endl;
return;
}
if (isEmpty()) {
front = 0;
rear = 0;
} else {
rear++;
}
arr[rear] = x;
}
void dequeue() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl;
return;
}
if (front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
}
int peek() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl;
return -1;
}
return arr[front];
}
void display() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl;
return;
}
cout << "Navbat elementlari: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void reverseQueue(Queue & q) {
if (q.isEmpty()) {
cout << "Error: Queue is empty" << endl;
return;
}
stack < int > s;
while (!q.isEmpty()) {
s.push(q.peek());
q.dequeue();
}
while (!s.empty()) {
q.enqueue(s.top());
s.pop();
}
}
};
using namespace std;
const int MAX_SIZE = 100;
class Queue {
private: int front;
int rear;
int arr[MAX_SIZE];
public: Queue() {
front = -1;
rear = -1;
}
bool isFull() {
return (rear == MAX_SIZE - 1);
}
bool isEmpty() {
return (front == -1 && rear == -1);
}
void enqueue(int x) {
if (isFull()) {
cout << "Error: Queue is full" << endl;
return;
}
if (isEmpty()) {
front = 0;
rear = 0;
} else {
rear++;
}
arr[rear] = x;
}
void dequeue() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl;
return;
}
if (front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
}
int peek() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl;
return -1;
}
return arr[front];
}
void display() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl;
return;
}
cout << "Navbat elementlari: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int sum_Queue(Queue & q) {
if (q.isEmpty()) {
cout << "Error: Queue is empty" << endl;
return 0;
}
int sum = 0;
for (int i = q.front; i <= q.rear; i++) {
sum += q.arr[i];
}
return sum;
}
};