0% found this document useful (0 votes)
2 views

Unit-3 Data structure

java data structure

Uploaded by

bhoomigupta2907
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-3 Data structure

java data structure

Uploaded by

bhoomigupta2907
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Ragini modi classes

https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng

What is Data Structure?


The data structure name indicates itself by organizing the data in memory. There are many
ways of organizing the data in the memory.

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.

Types of Data Structures

There are two types of data structures:

1. Primitive data structure


2. Non-primitive data structure

1) Primitive Data structure


The primitive data structures are primitive data types. The int, char, float, double, and
pointer are the primitive data structures that can hold a single value.

2) Non-Primitive Data structure


The non-primitive data structure is divided into two types:
● Linear data structure
● Non-linear data structure
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng

● Linear data Structure:-


The arrangement of data in a sequential manner is known as a linear data structure. The
data structures used for this purpose are Arrays, Linked list, Stacks, and Queues. In these
data structures, one element is connected to only one another element in a linear form.

● Non Linear data Structure:-


A non-linear data structure is also another type of data structure in which the data elements
are not arranged in a contiguous manner. As the arrangement is nonsequential, so the data
elements cannot be traversed or accessed in a single run.
Trees and Graphs are the types of non-linear data structure.

Linear data Structure:-

1. Array- An array is a collection of elements of the same type


placed in contiguous memory locations that can be
individually referenced by using an index to a unique
identifier.

2. Stack- A stack is a linear data structure that follows the principle


of Last In First Out (LIFO). This means the last element inserted
inside the stack is removed first.

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

Basic Operations of Stack


There are some basic operations that allow us to perform different actions on
a stack.

Push: Add an element to the top of a stack


Pop: Remove an element from the top of a stack
IsEmpty: Check if the stack is empty
IsFull: Check if the stack is full
Peek: Get the value of the top element without removing it

Working of Stack Data Structure


The operations work as follows:

● 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

● Before popping, we check if the stack is already empty.

Applications of Stack Data Structure:-


● To reverse a word - Put all the letters in a stack and pop them out.
Because of the LIFO order of stack, you will get the letters in reverse
order.
● In compilers - Compilers use the stack to calculate the value of
expressions like 2 + 4 / 5 * (7 - 9) by converting the expression to prefix
or postfix form.
● In browsers - The back button in a browser saves all the URLs you
have visited previously in a stack. Each time you visit a new page, it is
added on top of the stack. When you press the back button, the current
URL is removed from the stack, and the previous URL is accessed.

3. Queue:- A Queue is a linear structure which follows a particular


order in which the operations are performed. The order is First In
First Out (FIFO). A good example of a queue is any queue of
consumers for a resource where the consumer that came first is
served first. The difference between stacks and queues is in
removing. In a stack we remove the item the most recently added;
in a queue, we remove the item the least recently added.

Types of Queue
There are four different types of queue that are listed as follows -

1) Simple Queue or Linear Queue


2) Circular Queue
3) Priority Queue
4) Double Ended Queue (or Deque)

1) Simple Queue or Linear Queue


Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng

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 -

Ascending priority queue - In ascending priority queue, elements can be


inserted in arbitrary order, but only the smallest can be deleted first. Suppose
an array with elements 7, 5, and 3 in the same order, so insertion can be done
with the same sequence, but the order of deleting the elements is 3, 5, 7.
Descending priority queue - In descending priority queue, elements can
be inserted in arbitrary order, but only the largest element can be deleted first.
Suppose an array with elements 7, 3, and 5 in the same order, so insertion
can be done with the same sequence, but the order of deleting the elements is
7, 5, 3.

4) Deque (or, Double Ended Queue)


Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng

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.

There are two types of deque-

1. Input restricted deque - As the name implies, in input

restricted queue, insertion operation can be performed at only one end,


while deletion can be performed from both ends.
Ragini modi classes
https://youtube.com/channel/UCg5vaj43MigZEPA9mvkn8Ng

2. Output restricted deque - As the name implies, in output restricted


queue, deletion operation can be performed at only one end, while
insertion can be performed from both ends.

Operations performed on queue


The fundamental operations that can be performed on queue are listed as follows -

● 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

A linked list is a sequence of data structures, which are connected together


via links.

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.

Types of Linked List


Following are the various types of linked list.

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

4. Circular Doubly Linked List


Circular doubly linked list is a more complex type of data structure in which a
node contains pointers to its previous node as well as the next node. Circular
doubly linked list doesn't contain NULL in any of the nodes. The last node of
the list contains the address of the first node of the list. The first node of the
list also contains the address of the last node in its previous pointer.

A circular doubly linked list is shown in the following figure.

Basic Operations
Following are the basic operations supported by a linked list.

● Insertion − Adds an element at the beginning of the list.


● Deletion − Deletes an element at the beginning of the list.
● Display − Displays the complete list.
● Search − Searches an element using the given key.
● Delete − Deletes an element using the given key.

You might also like