This document discusses stacks and queues as linear data structures. Stacks follow the LIFO (Last In First Out) principle, while queues follow the FIFO (First In First Out) principle. Both can be implemented using arrays or linked lists. Common operations on stacks include push, pop, and display. Common operations on queues include enqueue, dequeue, front, and size. Examples of using stacks and queues are also provided.
2. STACKS
• Stack is known as a linear data structure. It follows
the Last-In-First-Out rule.
• If you push something to the stack then the last
value which you pushed, will be the first to pop
out.
• The insertion and deletion operation on the stack
will only take place from one end which is the top
of the stack.
3. IMPLEMENTATION OF STACKS
1. Statically:- In C, you can implement a stack using an array. It
allows static memory allocation of its data elements. In this, the
stack inherits all the features of the array.
2. Dynamically:- You can also implement a stack using a linked list. It
allows dynamic memory allocation of its data elements. In this, the
stack takes over the characteristics of the linked list.
6. OPERATIONS ON STACKS
• Display : The stack data elements are displayed in the stack according
to the LIFO rule.
7. OPERATIONS ON STACKS
• Stack Overflow : Here we are talking about the static memory
allocation of data elements of a stack. Therefore, if the stack is filled
completely, that is, no more elements can be inserted in the stack, then
the condition would be called STACK-FULL condition. It is also
referred to as stack overflow.
• Stack Underflow : In case we wish to display the data elements of the
stack or perform the deletion operation, but no elements have been
inserted into the stack yet, this condition is called STACK-EMPTY. It
is also referred to as stack underflow.
8. APPLICATIONS OF STACKS
1. Number Reversing: You can easily reverse a sequence of characters
or digits.
2. Undo Operation: In text editors, you can see an undo operation. If
you click it then all the changes you made will go back to normal. All the
changes are stored into the stack.
3. Infix to Postfix Conversion: With the help of stacks, you can
convert from infix to postfix.
4. Backtracking: You can use stacks to solve various maze puzzling
problems.
5. Depth First Search: With the help of stacks, you can perform a
searching algorithm named Depth-First Search.
9. QUEUES
• Queue is known as a linear data structure. It follows the First-In-First-
Out rule.
• So, if you push something to the stack then the first value of the stack
will pop out.
• In the queue, the insertion operation is done from the rear end(back)
and the deletion is done from the front.
10. IMPLEMENTATION OF QUEUES
• Statically: Array implementation of queues allows the static memory
allocation of its data elements. It is important to note that in this
method, the queue acquires all the features of an array.
• Dynamically: Linked list implementation of queues follow the
dynamic memory allocation of its data elements. It is important to note
that in this method, the queue inherits all the characteristics of a linked
list.
11. OPERATIONS ON QUEUES
• Enqueue - adding an element in the queue if there is space in the
queue.
12. OPERATIONS ON QUEUES
• Dequeue- Removing elements from a queue if there are any elements
in the queue
13. OPERATIONS ON QUEUES
• Front - This function returns the front element of the queue.
• Size - This function returns the size of a queue or the number of
elements in a queue.
14. TYPES OF QUEUES
• The standard queue data structure has the following variations:
1. Double-ended queue
2. Circular queue
• Double-ended queue: In a standard queue, a character is inserted at
the back and deleted in the front. However, in a double-ended queue,
characters can be inserted and deleted from both the front and back of
the queue.
15. TYPES OF QUEUES
• Circular queues: A circular queue is an improvement over the
standard queue structure.
• In a standard queue, when an element is deleted, the vacant space is not
reutilized. However, in a circular queue, vacant spaces are reutilized.
• While inserting elements, when you reach the end of an array and you
need to insert another element, you must insert that element at the
beginning.