STACKS
A stackis a linear data structure like array with some restrictions.
In a stack, insertion and deletion of elements is permitted only at one end. This end is called stack TOP.
Because of this property, stack is also called Last-In-First-Out (LIFO) list.
Insertion of element is called PUSH operation and deletion is called POP operation.
A pictorial representation of stack insertion and deletion operation is as follows.
D
F F
T T T
U U U U
A A A A A
PUSH OPERATION POP OPERATION
A stack can be represented using an array or a linked list.
Since insertion and deletion is done at one end, we don’t need to traverse the entire list for these operations. So stack supports insertion and
deletion in O(1) time i.e. in constant amount of time.
When stack is empty, if we try to remove an element, it is called “Stack underflow”.
Similarly, when we add an element to a stack which is full, it is called “Stack overflow”.
A stack can theoretically grow to an infinite size. But in practice, there is limit. For array representation, the limit is array size whereas for linked list
representation it is the amount of available memory.
D
F F
T T T
U U U U
A A A A A
3.
In arrayrepresentation, the list is empty when front=rear.
Also in array representation, the list is full when front=0 and rear=n-1 where n is the size of the array; or when rear=front-1.
De-queue
De-queue is a double ended queue. It supports insertion and deletion both end of the list but not anywhere in between.
It is basically a generalization of stack and queue.
4.
REFERENCES
1. Fundamentals ofcomputer by P K Sinha
2. Programming in C by Reema Thereja
3. Programming in ANSI C by E Balagurusamy
4. O'REILLY, "Practical C Programming", 3rd Edition
5. Yashavant P.kanetkar, "Let Us C", 5th Edition
6. Brian W. kernighan and Dennis M. Ritchie, "The C Programming
Language"
7. Greg Perry, "C by Example"
8. Stephen Prata, "C Primer Plus", 5th Edition
5.
39
QUEUE
A queueis also a linear data structure like stack. But unlike stack, here insertion is done at one end and deletion at the other end.
Because of this property, it is also called First-In-First-Out (FIFO) list.
The insertion process is called ENQUEUE and deletion process is called DEQUEUE.
The end where element is inserted is called the TAIL of the queue, whereas the other end is called HEAD. So initially head=tail.
Queue can also be represented using an array or a linked list.
If we DEQUEUE an empty queue, it is called queue underflow. Similarly queue overflow is when we ENQUEUE a full queue.
In array representation, when head=tail+1, then queue is full.
Circular queue
A circular queue is one in which after reaching the last position, next element is added in the first position (provided it is free).
In general queue, on reaching the end of the queue, no more elements can be added even though elements in the beginning are empty (as a
result of many dequeue process). Circular queue overcomes this limitation of the queue.
This also can be represented using an array or a linked list.