Private: int top; int arr[max size]
Download 68.68 Kb.
|
include
#include using namespace std; #define MAX_SIZE 1000 class Stack { private: int top; int arr[MAX_SIZE]; public: Stack() { top = -1; } bool push(int x) { if (top >= MAX_SIZE - 1) { cout << "Stek to'liq" << endl; return false; } arr[++top] = x; return true; } int pop() { if (top < 0) { cout << "Stack underflow" << endl; return 0; } return arr[top--]; } int peek() { if (top < 0) { cout << "Stek bo'sh" << endl; return 0; } return arr[top]; } bool isEmpty() { return (top < 0); } 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; } }; int main() { cout << "Stek obektini kiritish:\n"; Stack s; cout << "Stek bo'sh yo'ki yo'qligini tekshirish "; cout << s.isEmpty() << endl; cout << "\nStekka yangi element kiritish:\n"; s.push(7); s.push(6); s.push(5); s.push(4); s.display(); cout << "\nStekdan elementni ochirish! "; cout << s.pop(); s.display(); cout << "\nEng tepadagi element:\n"; cout << s.peek(); cout << endl; return 0; } #include using namespace std; #define MAX_SIZE 5 class Stack { private: int top; int arr[MAX_SIZE]; public: Stack() { top = -1; } bool push(int x) { if (isFull()) { cout << "Stack overflow" << endl; return false; } arr[++top] = x; return true; } int pop() { if (isEmpty()) { cout << "Stack underflow" << endl; return 0; } return arr[top--]; } int peek() { if (isEmpty()) { cout << "Stack is empty" << endl; return 0; } return arr[top]; } bool isEmpty() { return (top < 0); } bool isFull() { return (top >= MAX_SIZE - 1); } void display() { if (top < 0) { cout << "Stek bo'sh" << endl; return; } cout << "\Stek elementlari: "; for (int i = top; i >= 0; i--) cout << arr[i] << " "; cout << endl; } }; int main() { cout << "Maximum massiv o'lchami: " << MAX_SIZE; Stack s; cout << "\nStek elementlari:\n"; s.push(9); s.push(6); s.push(5); s.push(2); s.push(1); s.display(); cout << "Stek to'liq yoki yo'q ? " << s.isFull() << endl; cout << "\nStektan element o'chirish "; cout << s.pop(); s.display(); cout << "Stek to'liq yoki yo'q ? " << s.isFull() << endl; cout << endl; return 0; } #include using namespace std; #define MAX_SIZE 15 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 << "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 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 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 size = top + 1; int mid = (size % 2 == 0) ? (size/2) : ((size/2) + 1); Stack temp; 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]); } } } int main() { Stack stk; cout << "Stek elementlarini kiritish:"; stk.push(7); stk.push(4); stk.push(2); stk.push(2); stk.push(5); stk.display(); cout << "\nduplikat elementlarini o'chirish:"; stk.remove_duplicates(stk); stk.display(); cout << "\nYana ikkita element kiriting:"; stk.push(7); stk.push(5); stk.display(); cout << "\nduplikat elementlarini o'chirish:"; stk.remove_duplicates(stk); 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 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(); } // Replace the kth element in the temporary array if (k > count || k <= 0) { cout << "Invalid position." << endl; return; } else { temp[k-1] = value; } #include using namespace std; class Node { public: int data; Node* next; }; class Stack { private: Node* top; public: Stack() { top = NULL; } void push(int x) { Node* newNode = new Node(); newNode->data = x; newNode->next = top; top = newNode; } void pop() { if (top == NULL) { cout << "Stack is empty!" << endl; return; } Node* temp = top; top = top->next; delete temp; } void display() { if (top == NULL) { cout << "Stack is empty" << endl; return; } Node* temp = top; cout << "Stek elementlari: "; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } cout << endl; } }; int main() { 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]); } } };
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(); } } }; int main() { cout << "Navbatni Inisilizatsiyalash." << endl; Queue q; cout << "\nNavbat elementlarini kiritish:" << endl; q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); q.enqueue(5); q.display(); q.reverseQueue(q); cout << "\nTeskari yo'nalishta chop etish:\n"; q.display(); 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 << "Queue elements are: "; for (int i = front; i <= rear; i++) { cout << arr[i] << " "; } cout << endl; } void sortQueue(Queue & q) { if (q.isEmpty()) { cout << "Error: Queue is empty" << endl; return; } int n = q.rear - q.front + 1; for (int i = 0; i < n - 1; i++) { int minIndex = i; int minValue = q.arr[q.front + i]; for (int j = i + 1; j < n; j++) { if (q.arr[q.front + j] < minValue) { minIndex = j; minValue = q.arr[q.front + j]; } } for (int j = minIndex; j > i; j--) { q.arr[q.front + j] = q.arr[q.front + j - 1]; } q.arr[q.front + i] = minValue; } } }; int main() { cout << "Navbatni inisilizasiyalash." << endl; Queue q; cout << "\nElemenlarni kiritish:" << endl; q.enqueue(1); q.enqueue(5); q.enqueue(3); q.enqueue(6); q.enqueue(2); q.enqueue(0); q.display(); q.sortQueue(q); cout << "\nBerilgan navbatni saralash:\n"; q.display(); cout << "\nYana ikkita element kiriting:" << endl; q.enqueue(-1); q.enqueue(4); q.display(); q.sortQueue(q); cout << "\nBerilgan navbatni saralash:\n"; q.display(); 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; } 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; } }; int main() { cout << "Navbatni inisilizatsiyalash." << endl; Queue q; cout << "\nNavbat elementlarini kiritish:" << endl; q.enqueue(1); q.enqueue(5); q.enqueue(3); q.enqueue(6); q.enqueue(2); q.enqueue(0); q.display(); int sum_q = q.sum_Queue(q); cout << "Elementlarning yig'indisi topish: " << sum_q; cout << "\n\nyana ikkita element kiritish:" << endl; q.enqueue(-1); q.enqueue(4); q.display(); sum_q = q.sum_Queue(q); cout << "Elementlarning yig'indisi topish: " << sum_q; 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; } float avg_Queue(Queue & q) { if (q.isEmpty()) { cout << "Error: Queue is empty" << endl; return 0.0; } int sum = 0; int count = 0; for (int i = q.front; i <= q.rear; i++) { sum += q.arr[i]; count++; } float avg = static_cast < float > (sum) / count; return avg; } }; int main() { cout << "Navbatni ijor etish." << endl; Queue q; cout << "\nNavbat elementlarini kiritish:" << endl; q.enqueue(1); q.enqueue(5); q.enqueue(3); q.enqueue(6); q.enqueue(2); q.enqueue(0); q.display(); float avg_val = q.avg_Queue(q); cout << "\nNavbat elementlarining yig'indi: " << avg_val; cout << "\n\nYana ikkita element kiritish:" << endl; q.enqueue(-1); q.enqueue(4); q.display(); avg_val = q.avg_Queue(q); cout << "Navbat elementlarinig yig'indisi: " << avg_val; 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; } float median_Queue(Queue & q) { if (q.isEmpty()) { cout << "Error: Queue is empty" << endl; return 0.0; } int count = 0; int mid = 0; for (int i = q.front; i <= q.rear; i++) { count++; } mid = count / 2; if (count % 2 == 0) { float median = static_cast < float > (q.arr[q.front + mid] + q.arr[q.front + mid - 1]) / 2; return median; } else { float median = q.arr[q.front + mid]; return median; } } }; int main() { Queue q; cout << "\nNavbat elementlarini kiritish:" << endl; q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); q.enqueue(5); q.display(); float med_val = q.median_Queue(q); cout << "\nNavbat o'rtasida jolashgan element: " << med_val; cout << "\n\nNavbatka yana element kiritish:" << endl; q.enqueue(6); q.display(); med_val = q.median_Queue(q); cout << "\nNavbat elementlarining o'rtachasini hisoblash: " << med_val; return 0; } #include #include Download 68.68 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling