From 1139800823a3becddb5edd2369c5280914e601cd Mon Sep 17 00:00:00 2001 From: Malik Bilal Date: Tue, 26 Nov 2019 02:43:21 +0500 Subject: [PATCH] Make Changes --- Queue/queue.cpp | 84 +++++++++++++++++++++++----------------- Search/linearsearch.cpp | 39 ++++++++++++------- Search/linearsearch2.cpp | 24 ------------ 3 files changed, 75 insertions(+), 72 deletions(-) delete mode 100644 Search/linearsearch2.cpp diff --git a/Queue/queue.cpp b/Queue/queue.cpp index 8d7f6f4..89fae62 100644 --- a/Queue/queue.cpp +++ b/Queue/queue.cpp @@ -1,63 +1,77 @@ #include -#define SIZE 10 -using namespace std; -//Queue Class -class Queue { + +#define ARRAY_SIZE 10 + +// Circular Queue Class implemented using C array +class Queue +{ private: - int arr[SIZE], front, rear; + int array[ARRAY_SIZE], front, rear; public: Queue() { rear = front = -1; } - //is_empty Method - bool is_empty() { + // Return true if Queue is empty else false + bool empty() + { return (front == -1 && rear == -1); } - //is_full Method - bool is_full() { - return (((rear+1)%SIZE) == front); + // Return true if Queue is full else false + bool full() { + return (((rear+1)%ARRAY_SIZE) == front); } - //enqueue Method - void enqueue(int x) { - if(is_full()) { - cout << "Queue is Full." << endl; + // Push a value at the end of Queue(Array) + void enqueue(int x) + { + // Checikng if Queue has space + if(full()) + { + std::cout << "Queue is Full." << '\n'; } else { - if(is_empty()) { + if(empty()) + { rear = front = 0; - arr[rear] = x; + array[rear] = x; } - else { - rear = (rear+1)%SIZE; - arr[rear] = x; + else + { + rear = (rear+1)%ARRAY_SIZE; + array[rear] = x; } } } - //dequeue Method - void dequeue() { - if(is_empty()) { - cout << "Queue is Empty." << endl; + // Pop a value from the start of Queue(Array) + void dequeue() + { + // Checking if Queue is empty + if(empty()) + { + std::cout << "Queue is Empty." << '\n'; } - else if(front == rear) { + else if(front == rear) + { rear = front = -1; } - else { - front = (front+1)%SIZE; + else + { + front = (front+1)%ARRAY_SIZE; } } - //peek Method - int peek() { - if(is_empty()) { - cout << "Queue is Empty." << endl; - } - else { - return arr[front]; - } + // Return first value of Queue(Array) + int peek() + { + return array[front]; } }; //Main Method int main() { + // Declaring Queue object Queue q; - //Todo + + // Performing operations on Queue + q.enqueue(10); + q.enqueue(12); + std::cout << q.peek() << '\n'; } \ No newline at end of file diff --git a/Search/linearsearch.cpp b/Search/linearsearch.cpp index c718a73..e0c2240 100644 --- a/Search/linearsearch.cpp +++ b/Search/linearsearch.cpp @@ -1,21 +1,34 @@ #include -using namespace std; -//Main Method -int main() { - int const SIZE = 6; - int arr[SIZE] = {19, 16, 22, 87, 92, 23}, value; + +// Define the size of ARRAY +#define ARRAY_SIZE 10 + + +// Perform Linear Search on Array +void search(int array[], int value, int size) +{ bool flag = false; - cout << "Enter a Value to Search : "; - cin >> value; - for(int i = SIZE-SIZE; i < SIZE; i++) { - if(value == arr[i]) { - cout << "Value found at " << i << endl; + for(int i = 0; i < size; i++) + { + if(array[i] == value) + { + std::cout << "Value found at " << i << '\n'; flag = true; break; } } - if(flag == false) { - cout << "Value not found." << endl; - } + if(flag == false) + { + std::cout << "Value not found." << '\n'; + } +} + +int main() { + int value, array[ARRAY_SIZE] = {19, 16, 22, 87, 92, 23}; + // Getting value to search + std::cout << "Enter a value to Search : "; + std::cin >> value; + + search(array, value, ARRAY_SIZE); return 0; } \ No newline at end of file diff --git a/Search/linearsearch2.cpp b/Search/linearsearch2.cpp deleted file mode 100644 index ffedee3..0000000 --- a/Search/linearsearch2.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -using namespace std; -//lsearch Method -void search(int farr[], int fvalue, int size) { - bool flag = false; - for(int i = size-size; i < size; i++) { - if(fvalue == farr[i]) { - cout << "Value found at " << i << endl; - flag = true; - break; - } - } - if(flag == false) { - cout << "Value not found." << endl; - } - -} - -int main() { - int arr[6] = {19, 16, 22, 87, 92, 23}, value; - cout << "Enter a Value to Search : "; cin >> value; - search(arr, value, 6); - return 0; -} \ No newline at end of file