Basic Operations: Stack
Basic Operations: Stack
htm
https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm
https://www.geeksforgeeks.org/commonly-asked-data-structure-interview-questions-set-1/
STACK
Basic Operations
Push: Add an element to the top of a stack
Pop: Remove an element from the top of a stack
IsEmpty: Check if the stack is empty
IsFull: Check if the stack is full
Peek: Get the value of the top element without removing it
Implementation
// Stack implementation in C++
#include <stdlib.h>
#include <iostream>
// Creating a stack
struct stack {
int items[MAX];
int top;
};
typedef struct stack st;
// Driver code
int main() {
int ch;
st *s = (st *)malloc(sizeof(st));
createEmptyStack(s);
push(s, 1);
push(s, 2);
push(s, 3);
push(s, 4);
printStack(s);
pop(s);
Time Complexity
For the array-based implementation of a stack, the push and pop operations take constant
time, i.e. O(1)
Applications
To reverse a word - Put all the letters in a stack and pop them out. Because of the LIFO
order of stack, you will get the letters in reverse order.
In compilers - Compilers use the stack to calculate the value of expressions like 2 + 4 / 5
QUEUE
Basic Operations
Enqueue: Add an element to the end of the queue
Dequeue: Remove an element from the front of the queue
IsEmpty: Check if the queue is empty
IsFull: Check if the queue is full
Peek: Get the value of the front of the queue without removing it
Working
Queue operations work as follows:
Implementation
// Queue implementation in C++
#include <iostream>
#define SIZE 5
using namespace std;
class Queue {
private:
int items[SIZE], front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
bool isEmpty() {
if (front == -1)
return true;
else
return false;
}
int deQueue() {
int element;
if (isEmpty()) {
cout << "Queue is empty" << endl;
return (-1);
} else {
element = items[front];
if (front >= rear) {
front = -1;
rear = -1;
} /* Q has only one element, so we reset the queue after deleting it. */
else {
front++;
}
cout << endl
<< "Deleted -> " << element << endl;
return (element);
}
}
void display() {
/* Function to display elements of Queue */
int i;
if (isEmpty()) {
cout << endl
<< "Empty Queue" << endl;
} else {
cout << endl
<< "Front index-> " << front;
cout << endl
<< "Items -> ";
for (i = front; i <= rear; i++)
cout << items[i] << " ";
cout << endl
<< "Rear index-> " << rear << endl;
}
}
};
int main() {
Queue q;
//enQueue 5 elements
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);
q.display();
//deQueue removes element entered first i.e. 1
q.deQueue();
return 0;
}
Complexity Analysis
The complexity of enqueue and dequeue operations in a queue using an array is O(1) .
Applications of Queue
CPU scheduling, Disk Scheduling
When data is transferred asynchronously between two processes.The queue is used for
synchronization. For example: IO Buffers, pipes, file IO, etc
Call Center phone systems use Queues to hold people calling them in order.
Types of Queues
A queue is a useful data structure in programming. It is similar to the ticket queue outside a
cinema hall, where the first person entering the queue is the first person who gets the ticket.
There are four different types of queues:
Simple Queue
Circular Queue
Priority Queue
An in-place sorting algorithm sorts the elements in place: that is, it needs only O(1) extra space. An out-of-
place sorting algorithm needs extra space to put the elements in as it's sorting them. Usually this means O(n)
extra space.
https://www.geeksforgeeks.org/real-time-application-of-data-structures/
https://www.programiz.com/dsa/divide-and-conquer
Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well.
The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers
smaller than it and the greater numbers on the right
The time complexity of Quicksort is O(n log n) in the best case, O(n log n) in the average case, and O(n^2) in
the worst case. But because it has the best performance in the average case for most inputs, Quicksort is
generally considered the “fastest” sorting algorithm