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

Data Structure Lesson 1_VLSA_2023CMP

The document provides an introduction to data structures and algorithms, focusing on data structures like arrays and linked lists. It explains the characteristics of data types, basic operations, and the limitations of arrays, as well as the implementation and applications of linked lists. The document also discusses dynamic data structures and their uses in various real-life applications.

Uploaded by

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

Data Structure Lesson 1_VLSA_2023CMP

The document provides an introduction to data structures and algorithms, focusing on data structures like arrays and linked lists. It explains the characteristics of data types, basic operations, and the limitations of arrays, as well as the implementation and applications of linked lists. The document also discusses dynamic data structures and their uses in various real-life applications.

Uploaded by

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

ECIE 3312

DATA
STRUCTURE AND
ALGORITHM
INSTRUCTOR

A. Prof. Dr Rashidah Funke Olanrewaju

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.

Atomic − Definition should define a single concept.


Traceable − The definition should be able to be mapped to some
data element.
Accurate − Definition should be unambiguous.
Clear and Concise − Definition should be understandable.
Data Object:: represents an object having a data.
Data Object: represents an object having a data.

▪ Data type is a way to classify various types of data such as integer,


string, etc. which determines the values that can be used with the
corresponding type of data, the type of operations that can be performed
on the corresponding type of data. There are two data types −​
▪ Built-in Data Type
▪ Derived Data Type

▪ 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.

What is a For example, an array is a data


structure that holds a collection of data

Data in sequential order. You can find the


size of the array, store, retrieve, and
modify data in the array.
Structure?

Array is simple and easy to use, but it


has two limitations:
10

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

▪ An array is a sequenced collection


of variables all of the same type.
Each variable, or cell, in an array
has an index, which uniquely refers
to the value stored in that cell. The
Array Definition cells of an array, A, are numbered 0,
1, 2, and so on.
▪ Each value stored in an array is
often called an element of that
array.

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

Array Length & Capacity

In Java, the length of an array


Since the length of an array named a can be accessed using
determines the maximum number the syntax a.length, In python
of things that can be stored in the len(a). Thus, the cells of an array,
array, we will sometimes refer to a, are numbered 0, 1, 2, and so on,
the length of an array as its up through a.length−1, and the
capacity. cell with index k can be accessed
with syntax a[k].

a
0 1 2 k n
Arrays
© 2014 Goodrich, Tamassia, Goldwasser 16

▪ The first way to create an array is to use an


assignment to a literal form when initially
Declaring Arrays declaring the array, using a syntax as:

(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

▪ The second way to create an array is to


use the new operator.
▪ However, because an array is not an
instance of a class, we do not use a
typical constructor. Instead we use the
syntax:

Declaring new elementType[length]

Arrays ▪ length is a positive integer denoting


the length of the new array.
(second way) ▪ The new operator returns a reference
to the new array, and typically this
would be assigned to an array
variable.

Arrays
18

int
array[10]={1,2,3,4,5,6,7,8,9,10};

To use the new operator to


allocate the memory then it
becomes

Declaring
int *array = new int[10];
Arrays
(second way)
list1= [3, 5, 8,9]….. Python
declaration

Mylist = [‘sarah’, 23, ‘hackathon’]

Analysis of Algorithms
© 2014 Goodrich, Tamassia, Goldwasser 19

▪ An array can store primitive elements, such as


characters.
▪ An array can also store references to objects.

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

▪ To remove the entry e at index i, we need to


fill the hole left by e by shifting backward the
n - i - 1 elements board[i + 1], …, board[n - 1]

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,

Arrays can be used in The screen of the computer


An array can be used for Can be applied as a speech processing where is also displayed by an
CPU scheduling. lookup table in computers. every speech signal is an array . Here we use a
array. multidimensional array.

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

Frequently used to store data for


mathematical computations.

It is used in image processing.

Real-Life It is also used in record


Applications management.
of Array:
Book pages are also real-life
examples

Used in ordering boxes


Data Structures in Sara

Sara has two built-in data structures that


can be used to organize data, or to create
other data structures:
• Lists
• Arrays
Arrays and Lists

In a list, the missing spot is filled in when


something is deleted.
Arrays & Lists ▪ In an array, an empty variable is left
behind when something is deleted.
28

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

Allocation & Data ▪ Limited by amount of physical or virtual

Structures memory
▪ Memory must be shared among many
programs

29
List and Linkedlist

▪ A list is a collection of data stored


sequentially. It supports insertion and
deletion anywhere in the list.

▪ A list is a popular data structure to store data


in sequential order. For example, a list of
students, a list of available rooms, a list of
cities, and a list of books, etc. can be stored
using lists.
▪ Each data item is embedded in a link.
▪ Each Link object contains a reference
to the next link in the list of items.
▪ In an array items have a particular
position, identified by its index. Linked
▪ In a list the only way to access an item
is to traverse the list
Lists
▪ A linked list can grow or shrink in size
as the program runs
32

Linked Lists (Cont.)

▪ Linked list can be


▪ One way SLL

▪ Circular, singly linked list


▪ Pointer in last node points back to first node

▪ Doubly linked list


▪ Each node has a link to next node and a link to previous node
▪ Two “start pointers”
▪ One to first node, one to last node
▪ Allows traversals both forward and backward

▪ Circular, doubly linked list


▪ Forward link of last node points back to first node
▪ Backward link of first node points to last node
Linked lists are used to
implement stacks,
queues, graphs, etc.
USES of Array
Linked lists are used to
perform arithmetic
operations on long
It is used for the
representation of sparse
matrices.
It is used in linked
allocation of files.
integers.

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.

Linked are used in Linked lists are used to


They are used to perform software development display social media
undo operations. where they indicate the feeds.
correct syntax of a tag.

Applications of LinkedList
34

A linked list is used in Round-


Robin scheduling to keep track of
the turn in multi-player games.

It is used in image viewer. The


Real-Life previous and next images are
Applications linked, hence can be accessed by
the previous and next buttons.
LinkedList:
In a music playlist, songs are
linked to the previous and next
songs.
Singly Linked Lists

Singly Linked Lists 35


36
Singly Linked List

▪ A singly linked list is a


concrete data structure next
consisting of a sequence
of nodes, starting from a
head pointer
▪ Each node stores element node
▪ element
▪ link to the next node
head

A B C D
37
Inserting at the Head

• Allocate new node


• Insert new element
• Have new node
point to old head
• Update head to
point to new node
Inserting at the Tail

• Allocate a new node


• Insert new element
• Have new node point to null
• Have old last node point to
new node

• Update tail to point to new


node

Singly Linked Lists


39

Removing at the Head

• Update head to
point to next
node in the list
• Allow garbage
collector to
reclaim the
former first
node

Singly Linked Lists


40

• Removing at the tail of a singly linked list is


not efficient!
• There
Removingis no
atconstant-time
the way to update the
tail to point
Tail to the previous node

Singly Linked Lists


41
LL…
The common operations on a list are usually the
following:

Retrieve Retrieve an element from this list.

Insert Insert a new element to this list.

Delete Delete an element from this list.

Find Find how many elements are in this list.

Find Find if an element is in this list

Find Find if this list is empty.


Stack Implementation of Singly Linked List 42

▪ We can implement a stack with a singly linked list


▪ The top element is stored at the first node of the list
▪ The space used is O(n) and each operation of the Stack ADT
takes O(1) time

nodes

t 

elements
43

Queue Implementation of Singly Linked List

▪ We can implement a queue with a singly linked list


▪ The front element is stored at the first node
▪ The rear element is stored at the last node
▪ The space used is O(n) and each operation of the Queue ADT takes
O(1) time
r
nodes

f 

elements
Two Ways to Implement Lists

1. To use an array to store the elements. The array is


dynamically created. If the capacity of the array is
exceeded, create a new larger array and copy all the
elements from the current array to the new array.

2. To use a linked structure. A linked structure consists of


nodes. Each node is dynamically created to hold an
element. All the nodes are linked together to form a list.
46
Array Lists

Array is a fixed-size data structure. Once


an array is created, its size cannot be
changed. Nevertheless, you can still use
array to implement dynamic data
structures. The trick is to create a new
larger array to replace the current array
if the current array cannot hold new
elements in the list.
http://liveexample.pearsoncmg.com/liang/
animation/web/LinkedList.htmlation

48
49

Python
implementation
In Programming class

Analysis of Algorithms
50
List ADT

The List ADT models a Accessor methods:


sequence of positions storing
arbitrary objects ▪ first(), last()
▪ before(p), after(p)
It establishes a before/after
relation between positions ▪ Update methods:
▪ replaceElement(p, o),
swapElements(p, q)
Generic size(),
methods: isEmpty()
▪ insertBefore(p, o),
insertAfter(p, o),

isFirst(p),
▪ insertFirst(o), insertLast(o)
Query methods: isLast(p)
▪ remove(p)
Operations in a linked list:

▪ The simplest methods are


▪ insertfirst() and
▪ deletefirst(),
▪ where the first item in the linked list is
accessed and deleted or a new item is
inserted as the head or root of the list.

▪ To insert or delete items from any other part of


the list, we need to traverse the list starting from
its root and traversing till we get the item that we
are looking for.
Advantages of Linked Lists over Arrays and vectors

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

header nodes/positions trailer

elements
56

Doubly linked list.


Insertion

▪ We visualize operation insertAfter(p, X), which returns position q

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

▪ The space used by a list with n elements


is O(n)
Performance ▪ The space used by each position of the
list is O(1)
▪ All the operations of the List ADT run in
O(1) time
▪ Operation element() of the
Position ADT runs in O(1) time
▪ Insertion and deletion at the beginning of the list are
very fast, O(1).

▪ Finding, deleting or inserting in the list requires


searching through half the items in the list on an

Linked List
average, requiring O(n) comparisons.

Efficiency ▪ Although arrays require same number of


comparisons, the advantage lies in the fact that no
items need to be moved after insertion or deletion.

▪ As opposed to fixed size of arrays, linked lists use


exactly as much memory as is needed and can
expand.
http://liveexample.pearsoncmg.com/liang/animation/web/ArrayList.html

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

Circular, singly linked list.


Circular Doubly Linked Lists

Circular, doubly linked list.

▪ A circular, doubly linked list is doubly linked list, except


that the forward pointer of the last node points to the
first node and the backward pointer of the first pointer
points to the last node.

You might also like