Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #define SIZE 5
- using namespace std;
- class myq
- {
- int *arr;
- int top;
- int capacity;
- public:
- myq(int size = SIZE);
- ~ myq();
- void push(int);
- void pop();
- int front();
- int back();
- int size();
- bool isempty();
- bool isfull();
- };
- myq::myq(int size)
- {
- arr = new int[size];
- top = -1;
- capacity = size;
- }
- myq::~myq()
- {
- delete arr;
- }
- void myq::push(int x)
- {
- if (isfull())
- {
- int *A = new int[capacity + 1];
- for (int i = 0; i < capacity; i++)
- A[i] = arr[i];
- A[capacity] = x;
- capacity++;
- delete arr;
- arr = new int[capacity];
- for (int i = 0; i < capacity; i++)
- arr[i] = A[i];
- delete A;
- top++;
- cout << "Inserting " << x << endl;
- }
- else
- {
- cout << "Inserting " << x << endl;
- arr[++top] = x;
- }
- }
- bool myq::isfull()
- {
- return top == capacity - 1;
- }
- bool myq::isempty()
- {
- return top == - 1;
- }
- void myq::pop()
- {
- if (isempty())
- cout << "Underflow" << endl;
- else
- {
- cout << "Removing " << arr[0] << endl;
- for (int i = 1; i <= top; i++)
- arr[i - 1] = arr[i];
- top--;
- }
- }
- int myq::front()
- {
- if (!isempty())
- return arr[0];
- else {
- cout << "Underflow" << endl; exit(EXIT_FAILURE);
- }
- }
- int myq::back()
- {
- if (!isempty())
- return arr[top];
- else {
- cout << "Underflow" << endl; exit(EXIT_FAILURE);
- }
- }
- int myq::size()
- {
- return top + 1;
- }
- int main()
- {
- myq Q = myq();
- Q.push(666); Q.push(112); Q.push(911); Q.push(1); Q.push(2); Q.push(3);
- cout <<"first "<< Q.front()<<" last " << Q.back()<<endl;
- for (int i = 0; i < Q.size();)
- {
- cout << Q.front() << "\n";
- Q.pop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement