Professional Documents
Culture Documents
It is a linear data structure that follows a particular order in which the operations
are performed.
LIFO( Last In First Out ):
This strategy states that the element that is inserted last will come out first. You
can take a pile of plates kept on top of each other as a real-life example. The
plate which we put last is on the top and since we remove the plate that is at the
top, we can say that the plate that was put last comes out first.
Stack
Push:
Adds an item to the stack. If the stack is full, then it is said to be an Overflow
condition.
Algorithm for push:
begin
if stack is full
return
endif
else
increment top
stack[top] assign value
end else
end procedure
Pop:
Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.
Algorithm for pop:
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
Top:
Returns the top element of the stack.
Algorithm for Top:
begin
return stack[top]
end procedure
isEmpty:
Returns true if the stack is empty, else false.
Algorithm for isEmpty:
begin
if top < 1
return true
else
return false
end procedure
Understanding stack practically:
There are many real-life examples of a stack. Consider the simple example of
plates stacked over one another in a canteen. The plate which is at the top is
the first one to be removed, i.e. the plate which has been placed at the
bottommost position remains in the stack for the longest period of time. So, it
can be simply seen to follow the LIFO/FILO order.
Complexity Analysis:
Time Complexity
Operations Complexity
push() O(1)
pop() O(1)
isEmpty() O(1)
size() O(1)
Types of Stacks:
Register Stack: This type of stack is also a memory element present in the
memory unit and can handle a small amount of data only. The height of the
register stack is always limited as the size of the register stack is very small
compared to the memory.
Memory Stack: This type of stack can handle a large amount of memory
data. The height of the memory stack is flexible as it occupies a large
amount of memory data.
Applications of the stack:
Infix to Postfix /Prefix conversion
Redo-undo features at many places like editors, photoshop.
Forward and backward features in web browsers
Used in many algorithms like Tower of Hanoi, tree traversals , stock span
problems, and histogram problems .
Backtracking is one of the algorithm designing techniques. Some examples
of backtracking are the Knight-Tour problem, N-Queen problem, find your
way through a maze, and game-like chess or checkers in all these problems
we dive into someway if that way is not efficient we come back to the
previous state and go into some another path. To get back from a current
state we need to store the previous state for that purpose we need a stack.
In Graph Algorithms like Topological Sorting and Strongly Connected
Components
In Memory management, any modern computer uses a stack as the primary
management for a running purpose. Each program that is running in a
computer system has its own memory allocations
String reversal is also another application of stack. Here one by one each
character gets inserted into the stack. So the first character of the string is
on the bottom of the stack and the last element of a string is on the top of the
stack. After Performing the pop operations on the stack we get a string in
reverse order.
Stack also helps in implementing function call in computers. The last called
function is always completed first.
Stacks are also used to implement the undo/redo operation in text editor.
push(stack, 10);
push(stack, 20);
push(stack, 30);
return 0;
}
Output
10 pushed to stack
20 pushed to stack
30 pushed to stack
30 popped from stack
Top element is 20
Elements present in stack : 20 10
QUEUE
What is Queue Data Structure?
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.
Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Queue Representation:
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.
Queue
+1 more
Goldman Sachs
Amazon
Solve Problem
Applications of Queue:
Queue is used when things don’t have to be processed immediately, but have to be
processed in First In First Out order like Breadth First Search. This property of Queue
makes it also useful in following kind of scenarios.
When a resource is shared among multiple consumers. Examples include CPU
scheduling, Disk Scheduling.
When data is transferred asynchronously (data not necessarily received at same rate
as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.
Queue can be used as an essential component in various other data structures.
Array implementation Of Queue:
For implementing queue, we need to keep track of two indices, front and rear. We
enqueue an item at the rear and dequeue an item from the front. If we simply increment
front and rear indices, then there may be problems, the front may reach the end of the
array. The solution to this problem is to increase front and rear in circular manner.
Steps for enqueue:
1. Check the queue is full or not
2. If full, print overflow and exit
3. If queue is not full, increment tail and add the element
Steps for dequeue:
1. Check queue is empty or not
2. if empty, print underflow and exit
3. if not empty, print element at the head and increment head
Array Implementation of Queue
// C program for array implementation of queue
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
return 0;
}
Output
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
10 dequeued from queue
Front item is 20
Rear item is 40
Linked-List
Advantages of Linked Lists over arrays:
Dynamic Array.
Ease of Insertion/Deletion.
Drawbacks of Linked Lists:
Random access is not allowed. We have to access elements sequentially starting from
the first node(head node). So we cannot do a binary search with linked lists efficiently
with its default implementation.
Extra memory space for a pointer is required with each element of the list.
Not cache-friendly. Since array elements are contiguous locations, there is the locality
of reference which is not there in the case of linked lists.
It takes a lot of time in traversing and changing the pointers.
Reverse traversing is not possible in singly linked lists.
It will be confusing when we work with pointers.
Direct access to an element is not possible in a linked list as in an array by index.
Searching for an element is costly and requires O(n) time complexity.
Sorting of linked lists is very complex and costly.
Types of Linked Lists:
Simple Linked List – In this type of linked list, one can move or traverse the linked
list in only one direction. where the next pointer of each node points to other nodes
but the next pointer of the last node points to NULL. It is also called “Singly Linked
List”.
Doubly Linked List – In this type of linked list, one can move or traverse the linked
list in both directions (Forward and Backward)
Basic operations on Linked Lists:
Deletion
Insertion
Search
Display
Array Representation of Singly Linked Lists:
A linked list is represented by a pointer to the first node of the linked list. The first node
is called the head of the linked list. If the linked list is empty, then the value of the head
points to NULL.
Each node in a list consists of at least two parts:
A Data Item (we can store integers, strings, or any type of data).
Pointer (Or Reference) to the next node (connects one node to another) or An
address of another node
In C, we can represent a node using structures. Below is an example of a linked list
node with integer data.
// A linked list node
struct Node {
int data;
struct Node* next;
};
A Doubly Linked List (DLL) contains an extra pointer, typically called the previous
pointer, together with the next pointer and data which are there in the singly linked list.
struct Node {
int data;
A Binary tree is represented by a pointer to the topmost node (commonly known as the “root”) of
the tree. If the tree is empty, then the value of the root is NULL. Each node of a Binary Tree
contains the following parts:
1. Data
2. Pointer to left child
3. Pointer to right child
Basic Operation On Binary Tree:
Inserting an element.
Removing an element.
Searching for an element.
Traversing the tree.
Auxiliary Operation On Binary Tree:
Finding the height of the tree
Find the level of a node of the tree
Finding the size of the entire tree.
Topic :
Introduction
Basic Operation
Traversals
Standard Problems on Binary Trees
Introduction :
1. Introduction to Binary Tree – Data Structure and Algorithm Tutorials
2. Properties of Binary Tree
3. Types of Binary Tree
4. Applications, Advantages and Disadvantages of Binary Tree
5. Binary Tree (Array implementation)
6. Complete Binary Tree
7. Perfect Binary Tree
Basic Operations on Binary Tree:
1. Insertion in a Binary Tree
2. Deletion in a Binary Tree
3. Enumeration of Binary Trees
4. Tree Traversals (Inorder, Preorder and Postorder)