Data Structure Lesson 1_VLSA_2023CMP
Data Structure Lesson 1_VLSA_2023CMP
DATA
STRUCTURE AND
ALGORITHM
INSTRUCTOR
Email: [email protected]
INTRODUCTION TO DATA
STRUCTURE
TOPIC 1
Data Structure
▪A data structure is a
collection of data organized
in some fashion. A data
structure not only stores data,
but also supports the
operations for manipulating
data in the structure.
BASICs of
Data
Data Definition defines a particular data with the following
characteristics.
▪ Built-in Data Type are Those data types for which a language
has built-in support are known as Built-in Data types. For example, most of
the languages provide the following built-in data types.
▪ Integers
▪ Boolean (true, false)
▪ Floating (Decimal numbers)
▪ Character and Strings
▪ Derived Data Type are those data types which are
implementation independent as they can be implemented in one or
the other way are known as derived data types. These data types
are normally built by the combination of primary or built-in
data types and associated operations on them. E.g −
▪ List
▪ Array
▪ Stack
▪ Queue
▪ Basic Operations
▪ The data in the data structures are processed by certain operations,
such as
▪ Traversing
▪ Searching
▪ Insertion
▪ Deletion
▪ Sorting
▪ Merging
A data structure is a scheme for
organizing data in the memory of a
computer.
lists stacks
binary
queues
Some Classic trees
Data
arrays
Structures heaps
and
graphs
Analysis of Algorithms
© 2014 Goodrich, Tamassia, Goldwasser 12
Arrays
© 2014 Goodrich, Tamassia,
13
Goldwasser
A
0 1 2 i n
Arrays
• An array can also handle complex data
structures by storing data in a two-
dimensional array.
Characteristics of • An array is also used to implement other
Array data structures like Stacks, Queues, Heaps,
Hash tables, etc.
© 2014 Goodrich, Tamassia,
15
Goldwasser
a
0 1 2 k n
Arrays
© 2014 Goodrich, Tamassia, Goldwasser 16
(first way)
▪ The elementType can be any C++/Python
base type or class name, and arrayName can
be any valid C++/Python identifier. The initial
values must be of the same type as the array.
© 2014 Goodrich, Tamassia, Goldwasser 17
Arrays
18
int
array[10]={1,2,3,4,5,6,7,8,9,10};
Declaring
int *array = new int[10];
Arrays
(second way)
list1= [3, 5, 8,9]….. Python
declaration
Analysis of Algorithms
© 2014 Goodrich, Tamassia, Goldwasser 19
Arrays of
Characters or
Object
References
© 2014 Goodrich, Tamassia,
20
Goldwasser
Adding an Entry
▪ To add an entry e into array board at
index i, we need to make room for it
by shifting forward the n - i entries
board[i], …, board[n - 1]
board
0 1 2 i n
board
0 1 2 i n
board e
0 1 2 i n
© 2014 Goodrich, Tamassia,
21
Goldwasser
Removing an Entry
board e
0 1 2 i n
board
0 1 2 i n
board
0 1 2 i n
Arrays
22
Once an array is
created, its size
cannot be altered.
Limitations of
arrays Array provides
inadequate support
for inserting, deleting,
sorting, and searching
operations.
It is also used to implement
matrix problems.
USES of Array
An array is used in solving Database records are also
implemented by an array.
It helps in implementing a
sorting algorithm.
other data structures like
Stacks, Queues, Heaps,
Hash tables,
The array is used in many The array is used in the In games like online chess, To save images in a
management systems like a online ticket booking where the player can store specific dimension in the
library, students, system. Contacts on a cell his past moves as well as android Like 360*1200
parliament, etc. phone are displayed by current moves. It indicates
this array. a hint of position.
Applications of Array
24
Dynamic Data
Structures
▪ Dynamic data structures
▪ Grow and shrink during execution
▪ Linked lists
▪ Stacks and Queues
▪ Binary trees
▪ Facilitate
▪ High-speed searching and sorting
▪ Elimination of duplicates
▪ Used in
▪ Representations of file system
directories
▪ Compilation of expressions into
machine language
▪ Dynamic memory allocation
▪ Enables a program to obtain more
Dynamic memory at execution time
▪ That memory can be released when it is no
Memory longer needed
Structures memory
▪ Memory must be shared among many
programs
29
List and Linkedlist
It is used in the
representation of Linked lists are used to
They are used to store the
It helps in memory Polynomial Manipulation display image containers.
history of the visited
management. where each polynomial Users can visit past,
page.
term represents a node in current, and next images.
the linked list.
Applications of LinkedList
34
A B C D
37
Inserting at the Head
• Update head to
point to next
node in the list
• Allow garbage
collector to
reclaim the
former first
node
nodes
t
elements
43
f
elements
Two Ways to Implement Lists
48
49
Python
implementation
In Programming class
Analysis of Algorithms
50
List ADT
isFirst(p),
▪ insertFirst(o), insertLast(o)
Query methods: isLast(p)
▪ remove(p)
Operations in a linked list:
Insertion and
A linked list can deletion of nodes
easily grow or is quicker with
shrink in size. linked lists than
with vectors.
▪ What is the running time to search for
Linked Lists - an entry in a linked list ? Deleting from
Query the tail ? Inserting from the head ?
Doubly Linked List
▪ A doubly linked list provides a natural
implementation of the List ADT
▪ Nodes implement Position and store: prev next
▪ element
▪ link to the previous node
▪ link to the next node
▪ Special trailer and header nodes elem node
elements
56
A B C
p
A B q C
X
p q
A B X C
Deletion
▪ We visualize remove(p), where p = last()
p
A B C D
A B C p
A B C
▪ In the implementation of the List ADT
by means of a doubly linked list
Linked List
average, requiring O(n) comparisons.
61
▪ A circular, singly linked
list is like a singly linked
list, except that the
pointer of the last node
points back to the first
Circular node.
Linked Lists