Data Structures

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

DATA STRUCTURES

DATA STRUCTURE IS A WAY OF COLLECTING AND ORGANIZING DATA


IN SUCH A WAY THAT WE CAN PERFORM OPERATIONS ON THESE
DATA IN AN EFFECTIVE WAY
BASIC TYPES OF DATA STRUCTURES
PRIMITIVE DATA STRUCTURES.
EG. INTEGER, FLOAT, BOOLEAN, CHAR, ETC.,

ABSTRACT DATA STRUCTURE

• LINKED LIST
• TREE
• GRAPH
• STACK
• QUEUES

. We select these data structures based on which type of


operation is required.
CLASSIFICATION OF DATA STRUCTURES

❖ LINEAR – DATA ITEMS ARE ARRANGED IN A LINEAR SEQUENCE EG. ARRAYS


❖ NON-LINEAR- DATA ITEMS ARE NOT IN A SEQUENCE EG. TREES, GRAPHS
❖ HOMOGENOUS – DATA ITEMS ARE OF THE SAME TYPE EG. ARRAYS
❖ NON-HOMOGENOUS – DATA ITEMS NOT NECESSARILY OF THE SAME TYPE
❖ STATIC - DATA STRUCTURES ARE THOSE WHOSE SIZES AND STRUCTURES
ASSOCIATED WITH MEMORY LOCATIONS ARE FIXED, AT COMPILE TIME. EG: ARRAY

❖ DYNAMIC - DYNAMIC STRUCTURES ARE THOSE WHICH EXPAND OR SHRINK


DEPENDING UPON THE PROGRAM’S NEED AND ITS EXECUTION. ALSO, THEIR
ASSOCIATED MEMORY LOCATIONS CHANGES. EXAMPLE: LINKED LIST CREATED
USING POINTERS
ARRAY
AN ARRAY IS SAID TO BE A FIXED LENGTH, ORDERED COLLECTION OF VALUES OF THE SAME
TYPE STORED IN CONTIGUOUS MEMORY LOCATIONS

❖An array consists of a collection of elements (values or variables), each identified by at least one array
index or key

The items in an array can be anything from primitive types such as


integers to more complex types like instances of classes.
LINKED LISTS
LINKED LIST IS A VERY COMMONLY USED LINEAR DATA STRUCTURE WHICH CONSISTS OF GROUP
OF NODES IN A SEQUENCE.
EACH NODE HOLDS ITS OWN DATA AND THE ADDRESS OF THE NEXT NODE HENCE FORMING A CHAIN
LIKE STRUCTURE.

Linked Lists are mostly used to create trees and graphs.


WHAT IS A NODE?

• A Node in a linked list holds the data value and the pointer which points to the location of the next
node in the linked list.
ADVANTAGES OF LINKED LISTS
• They are dynamic in nature which allocates the memory when required.

• Insertion and deletion operations can be easily implemented.

• Stacks and queues can be easily executed.

• Linked List reduces the access time.

Disadvantages of Linked Lists

• The memory is wasted as pointers require extra memory for storage.

• No element can be accessed randomly; it has to access each node sequentially.

• Reverse Traversing is difficult in linked list.


TYPES OF LINKED LISTS
SINGLY LINKED LIST DOUBLY LINKED LISTS CIRCULAR LINKED LISTS
So, What is the difference between an array
and Linked list?
STACKS
BASIC FEATURES OF STACK

❖Stack is an ordered list of similar data types.


❖Stack is a LIFO (Last in First out) structure or we can say FILO (First in Last out).
❖Stack is said to an be in Overflow state when it is completely full and is said to be in Underflow state
if it is completely empty.
❖Insert elements using Push function, Remove elements using Pop function

What are the Applications of Stack? (Exercise)


ANALYSIS OF STACK OPERATIONS

• Push Operation:?
• Pop Operation:
• Top Operation:
• Search Operation:
QUEUE

Queue can be implemented using an Array, Stack or Linked


List. The easiest way of implementing a queue is by using an
Array.
When we remove an element from
Queue, we can follow two
possible approaches (mentioned
[A] and [B] in the diagram).
1. In [A] approach, we remove the
element at the head position,
and then one by one shift all the
other elements to the forward
position.
2. In approach [B] we remove the
element from the head position
and then move the head to the
next position.
Algorithm for ENQUEUE operation

1. Check if the queue is full or not.


2. If the queue is full, then print overflow error and exit the program.
3. If the queue is not full, then increment the tail and add the element.

Algorithm for DEQUEUE operation

1. Check if the queue is empty or not.


2. If the queue is empty, then print underflow error and exit the program.
3. If the queue is not empty, then print the element at the head and increment the
head.

What is the Complexity Analysis of Queue


Operations?
IMPLEMENT QUEUE USING STACKS

❖ Making the Enqueue operation costly

❖ Making the Dequeue operation costly


Double Ended Queue
Double ended queue is a more generalized form of queue data structure which allows
insertion and removal of elements from both the ends, i.e. front and back.

You might also like