Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #include <windows.h>
- #include <iostream>
- #include <bits/stdc++.h>
- #define MEMORY_SIZE 1024
- using namespace std;
- struct Queue{
- int front, rear, capacity, temp;
- int* queue;
- Queue(int c)
- {
- front = rear = 0;
- capacity = c;
- queue = new int;
- }
- ~Queue() { delete[] queue; }
- bool empty(){
- return front == rear;
- }
- void empty_or_not(){
- if (empty()){
- cout << "Queue is empty" << endl;
- }
- else{
- cout << "Queue is not empty"<<endl;
- }
- }
- void Enqueue(int data)
- {
- if (capacity == rear) {
- printf("\nQueue is full\n");
- return;
- }
- else {
- /* links to material: https://tdoc.ru/c/cpp-sources/win32/virtualalloc-i-virtualfree.html
- and https://learn.microsoft.com/en-us/windows/win32/Memory/reserving-and-committing-memory
- все это должно добавлять в массив объекты размером в 20Кб (не пробовал, т.к. онлайн комплиятор не поддерживает windows.
- а устанавливать расширение для MS VS не хочется (долго) )
- cout << "Enter your element (only int value): "
- cin >> queue[rear];
- VirtualAlloc(NULL, 20*MEMORY_SIZE, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- */
- queue[rear] = data;
- rear++;
- cout << "\n" << data << " was added in queue" << endl;
- }
- return;
- }
- void Dequeue()
- {
- if (empty()) {
- printf("\nQueue is empty\n");
- return;
- }
- else{
- temp = queue[front];
- for (int i = 0; i < rear - 1; i++) {
- queue[i] = queue[i + 1];
- }
- rear--;
- }
- cout << temp << " was deleted from queue"<< endl;
- return;
- }
- void view_head()
- {
- if (empty()) {
- printf("\nQueue is Empty\n");
- return;
- }
- else{
- printf("Head Element is: %d ", queue[front]);
- }
- return;
- }
- void show_queue(){
- int i;
- if (empty()){
- printf("\nQueue is empty\n");
- return;
- }
- else{
- cout << endl;
- cout << "Queue elements: ";
- for (i = front; i < rear; i++) {
- printf("%d ", queue[i]);
- }
- return;
- }
- }
- void swap_head(){
- if (empty()){
- printf("\nQueue is empty\n");
- return;
- }
- else{
- temp = queue[front];
- //cout << "\n" <<temp << endl;
- queue[front] = queue[rear-1];
- //cout << "\n" <<queue[front] << endl;
- queue[rear-1] = temp;
- }
- }
- };
- int main(){
- Queue q(5);
- q.empty_or_not();
- cout << "\nAfter enquing elements:" << endl;
- q.Enqueue(1);
- q.empty_or_not();
- q.Enqueue(2);
- q.Enqueue(3);
- cout << "\nBefore swapping head to tail:" << endl;
- q.show_queue();
- q.swap_head();
- cout << "\nAfter swapping head to tail:" << endl;
- q.show_queue();
- cout << endl;
- q.view_head();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement