Unit-3 Data structure
Unit-3 Data structure
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng
The data structure is not any programming language like C, C++, java, etc. It is a set of
algorithms that we can use in any programming language to structure the data in the
memory.
You can think of the stack data structure as the pile of plates on top of another.
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng
● A pointer called TOP is used to keep track of the top element in the
stack.
● When initializing the stack, we set its value to -1 so that we can check if
the stack is empty by comparing TOP == -1.
● On pushing an element, we increase the value of TOP and place the
new element in the position pointed to by TOP.
● On popping an element, we return the element pointed to by TOP and
reduce its value.
● Before pushing, we check if the stack is already full
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng
Types of Queue
There are four different types of queue that are listed as follows -
In Linear Queue, an insertion takes place from one end while the deletion
occurs from another end. The end at which the insertion takes place is known
as the rear end, and the end at which the deletion takes place is known as
front end. It strictly follows the FIFO rule.
The major drawback of using a linear Queue is that insertion is done only from
the rear end. If the first three elements are deleted from the Queue, we cannot
insert more elements even though the space is available in a Linear Queue. In
this case, the linear Queue shows the overflow condition as the rear is
pointing to the last element of the Queue.
2) Circular Queue
In the Circular Queue, all the nodes are represented as circular. It is similar to
the linear Queue except that the last element of the queue is connected to the
first element. It is also known as Ring Buffer, as all the ends are connected to
another end. The representation of circular queue is shown in the below
image -
The drawback that occurs in a linear queue is overcome by using the circular
queue. If the empty space is available in a circular queue, the new element
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng
can be added in an empty space by simply incrementing the value of rear. The
main advantage of using the circular queue is better memory utilization.
3) Priority Queue
It is a special type of queue in which the elements are arranged based on the
priority. It is a special type of queue data structure in which every element has
a priority associated with it. Suppose some elements occur with the same
priority, they will be arranged according to the FIFO principle. The
representation of priority queue is shown in the below image -
Insertion in the priority queue takes place based on the arrival, while deletion
in the priority queue occurs based on the priority. Priority queue is mainly used
to implement the CPU scheduling algorithms.
There are two types of priority queue that are discussed as follows -
In Deque or Double Ended Queue, insertion and deletion can be done from
both ends of the queue either from the front or rear. It means that we can
insert and delete elements from both front and rear ends of the queue. Deque
can be used as a palindrome checker means that if we read the string from
both ends, then the string would be the same.
Deque can be used both as stack and queue as it allows the insertion and
deletion operations on both ends. Deque can be considered as stack because
stack follows the LIFO (Last In First Out) principle in which insertion and
deletion both can be performed only from one end. And in deque, it is possible
to perform both insertion and deletion from one end, and Deque does not
follow the FIFO principle.
● Enqueue: The Enqueue operation is used to insert the element at the rear end of the
queue. It returns void.
● Dequeue: It performs the deletion from the front-end of the queue. It also returns the
element which has been removed from the front-end. It returns an integer value.
● Peek: This is the third operation that returns the element, which is pointed by the
front pointer in the queue but does not delete it.
● Queue overflow (isfull): It shows the overflow condition when the queue is
completely full.
● Queue underflow (isempty): It shows the underflow condition when the Queue is
empty, i.e., no elements are in the Queue.
4. linked list-
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng
Linked List is a sequence of links which contains items. Each link contains a
connection to another link. Linked list is the second most-used data structure
after array. Following are the important terms to understand the concept of
Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link called
First.
Linked List Representation
Linked list can be visualized as a chain of nodes, where every node points to
the next node.
1. Singly Linked List −A singly linked list is a type of linked list that is
unidirectional, that is, it can be traversed in only one direction from head
to the last node (tail).
Each element in a linked list is called a node. A single node contains
data and a pointer to the next node which helps in maintaining the
structure of the list.
The first node is called the head; it points to the first node of the list and
helps us access every other element in the list. The last node, also
sometimes called the tail, points to NULL which helps us in determining
when the list ends.
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng
2. Doubly Linked List- Doubly linked list is a complex type of linked list
in which a node contains a pointer to the previous as well as the next
node in the sequence. Therefore, in a doubly linked list, a node consists
of three parts: node data, pointer to the next node in sequence (next
pointer) ,pointer to the previous node (previous pointer). A sample node
in a doubly linked list is shown in the figure.
3. Circular Linked List − In a circular Singly linked list, the last node of
the list contains a pointer to the first node of the list. We can have
circular singly linked lists as well as circular doubly linked lists.
We traverse a circular singly linked list until we reach the same node where
we started. The circular singly liked list has no beginning and no ending.
There is no null value present in the next part of any of the nodes.
The following image shows a circular singly linked list.
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng
Basic Operations
Following are the basic operations supported by a linked list.