It is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out) .Initially We Declare a Top Variable and store -1 inside it. It is the starting index of our Stack.
- Push
- Pop
- Peek
- isEmpty
- isFull
struct Stack { int *arr; int top; int size; };It means adding a data inside stack. In this case, we will increament top 1st. Then we store that value in that top index.
It means removing the last data added inside stack. In this case, we will store the value of the stack top. Then we decreamented the top bt -1 and return the value at previous index.
To retrive the top of the Stack without removing it.
To Check if the stack is empty or not i.e value of top == -1 ?
To Check if the stack is full or not i.e value of stack == stack size?
A queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. The element which is first pushed into the order, the operation is first performed on that.
--> A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e. First come first serve). Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See the below figure.
Queue can handle multiple data. We can access both ends. They are fast and flexible.
Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented using the array. Variables used in this case are
Queue: the name of the array storing queue elements. Front: the index where the first element is stored in the array representing the queue. Rear: the index where the last element is stored in an array representing the queue.
In queue, insertion and deletion happen at the opposite ends, so implementation is not as simple as stack. To implement a queue using array, create an array arr of size n and take two variables front and rear both of which will be initialized to 0 which means the queue is currently empty. Element rear is the index upto which the elements are stored in the array and front is the index of the first element of the array. Now, some of the implementation of queue operations are as follows: Addition of an element to the queue. Adding an element will be performed after checking whether the queue is full or not. If rear < n which indicates that the array is not full then store the element at arr[rear] and increment rear by 1 but if rear == n then it is said to be an Overflow condition as the array is full. Removal of an element from the queue. An element can only be deleted when there is at least an element to delete i.e. rear > 0. Now, element at arr[front] can be deleted but all the remaining elements have to shifted to the left by one position in order for the dequeue operation to delete the second element from the left on another dequeue operation. Get the front element from the queue i.e. arr[front] if queue is not empty. Print all element of the queue. If the queue is non-empty, traverse and print all the elements from index front to rear.