//Queue implementation in C++
#include<iostream>
using namespace std;
class Queue {
int item[5];
int front;
int rear;
public:
Queue() { //Constructor....
front = 0;
rear = -1;
}
void insert(int x) { // INSERTION FUNCTION TO INSERT VALUES IN QUEUE....
item[++rear] = x;
}
bool isempty() {
if (front>rear)
return true;
return false;
}
void apinsert(int x) { // ORDERING OF DATA IN QUEUE IN ASSENDING ORDER....
if (isempty()) {
insert(x);
}
else if (x >= item[rear])
{
insert(x);
}
else
{
Queue q2;
while (!isempty()) {
int t = remove();
if (x<t) {
q2.insert(x);
q2.insert(t);
break;
}
else
{
q2.insert(t);
}
}
while (!isempty())
q2.insert(remove());
front = 0;
rear = -1;
while (!q2.isempty()) {
insert(q2.remove());
}
}
}
int remove()
{
return item[front++];
}
void dislpay()
{
for (int k = 4; k>rear; k--)
cout << "\nEmpty";
for (int i = rear; i >= front; i--)
cout << endl << item[i];
for (int j = (front - 1); j >= 0; j--)
cout << "\nEmpty";
}
bool isfull()
{
if (rear >= 4)
return true;
return false;
}
};
int main() // MAIN DRIVER....
{
Queue q1;
char repeat = 'y';
do {
int input;
cout << "1) Press '1' to insert: " << endl;
cout << "2) Press '2' to remove: " << endl;
cout << "3) Press '3' to Display: " << endl;
cin >> input;
system("cls"); //STANDARD LIBARAY HEADER FILE (TO CLEAR SCREEN)
switch (input) {
case 1: {
int num;
cout << "Enter Number: ";
cin >> num;
system("cls");
q1.apinsert(num);
break;
}
case 2: {
if (q1.isempty())cout << "\nThe queue is already empty";
else
cout << "\nThe removed value is:" << q1.remove();
break;
}
case 3: {
q1.dislpay();
break;
}
default:
cout << "Error 404: ";
}
cout << "\nRepeat: y/n..";
cin >> repeat;
system("cls");
} while (repeat == 'y');
return 0;
}
-
Infix to Postfix Converstion
* C++ implementation to convert infix expression to postfix*
-
DOUBLY QUEUE IMPLEMENTATION C++
Double Queue Implemention Guide.
-
Data Structures in JavaScript
As business logic moves from the back to the front, expertise in front-end engineering becomes even more important. As for front-end engineers, we rely on viable libraries to be effective.
-
Coding Problem To Solve Exercise 1
here is Some Coding Problem For Beginners To Boost up their Confidence and to get grip on programming. This problem is for beginner levels for their brainstorming.
-
Coding Problem To Solve Exercise 2
here is Some Coding Problem For Beginners To Boost up their Confidence and to get grip on programming. This problem is for beginner levels for their brainstorming.
-
Coding Problem To Solve Exercise 3
* here is Some Coding Problem For Beginners To Boost up their Confidence and to get grip on programming. This problem is for beginner levels for their brainstorming.*
Queue implementation in C++
Linked List vs Array
2. In the array the elements belong to indexes, i.e., if you want to get into the fourth element you have to write the variable name with its index or location within the square bracket.
3. In a linked list though, you have to start from the head and work your way through until you get to the fourth element.
4. Accessing an element in an array is fast, while Linked list takes linear time, so it is quite a bit slower.
5. Operations like insertion and deletion in arrays consume a lot of time. On the other hand, the performance of these operations in Linked lists is fast.
6. Arrays are of fixed size. In contrast, Linked lists are dynamic and flexible and can expand and contract its size.
7. In an array, memory is assigned during compile time while in a Linked list it is allocated during execution or runtime.
9. Elements are stored consecutively in arrays whereas it is stored randomly in Linked lists.
10. The requirement of memory is less due to actual data being stored within the index in the array. As against, there is a need for more memory in Linked Lists due to storage of additional next and previous referencing elements.
11. In addition memory utilization is inefficient in the array. Conversely, memory utilization is efficient in the linked list.
2) Ease of insertion/deletion
2) Extra memory space for a pointer is required with each element of the list.
3) Arrays have better cache locality that can make a pretty big difference in performance.
Infix to postfix converstion
Infix to postfix conversion
/* C++ implementation to convert infix expression to postfix*/
#include<iostream>
using namespace std;
const int size = 5;
class Stack{ // Stack Class...
char arr[size];
int top;
public:
Stack()
{
top = -1;
}
void push(char x)
{
top++;
arr[top] = x;
}
char pop()
{
return arr[top--];
}
int peek()
{
return arr[top];
}
bool isfull()
{
if(top>size)
return true;
return false;
}
bool isempty()
{
if(top<-1)
return true;
return false;
}
};
int prec(char c) //Function to return precedence of operators....
{
if(c == '^' || c=='$')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
int main() // Main Driven....
{
Stack s;
string exp="(m*n/o/q^(a+b-d/q))";
string copy_string; // for Storing converstion....
int l = exp.length();
for(int i =0; i<l;i++)
{
if(exp[i] >= 'a' && exp[i] <= 'z') // If the scanned character is an operand, add it to output string....
copy_string+=exp[i];
else if(exp[i]=='(') // If the scanned character is an ‘(‘, push it to the stack....
s.push('(');
// If the scanned character is an ‘)’, pop and to output string from the stack
// until an ‘(‘ is encountered.
else if(exp[i]==')')
{
while(s.peek()!='\0' && s.peek() != '(' )
{
char c = s.peek();
s.pop();
copy_string+=c;
}
if(s.peek()=='(' )
{
char c= s.peek();
s.pop();
}
}
//If an operator is scanned....
else{
while(s.peek() != '\0' && prec(exp[i]) <= prec(s.peek()))
{
char c = s.peek();
s.pop();
copy_string+= c;
}
s.push(exp[i]);
}
}
//Pop all the remaining elements from the stack....
while(s.peek() != '\0')
{
char c = s.peek();
s.pop();
copy_string+= c;
}
cout << copy_string << endl;
return 0;
}