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

1

Data Structures

Data and Structure:


- Data: Collection of values, numbers, measurements, etc.,
- Structure: A set of rules that holds data together
Data Structure:
- A specific way of storing and organizing data in a
computer for efficient processing
- Provides a means to manage huge amount of data
- Amenable data structures are a key to designing smart
algorithms
- Data structures used are specific to applications
Data Structures

Classifications:
- Linear (arrays, linked lists)
- Nonlinear (trees, graphs)

Linear Data Structure:


- Has a unique element called the first
- Has a unique element called the last
- All the elements except the last has a successor
- All the elements except the first has a predecessor
Data Structures

Linear Data Structure: Representation


- Through sequential memory locations (arrays)
- Through pointers or links (linked lists)

Arrays:
- Data is often available in tabular form
- Arrays are used to represent tabular data
- Matrix represents tabular date and can be represented
as a 2-dimensional array!
Data Structures

Arrays: Properties
- All elements of the array of the same type (int, double)
- Array is a random access data structure
- It is a static data structure. Once defined, can’t be
changed
- Access time for an array element is constant ( O(1))
- Array is perfect when fewer number of
insertions/deletions are required
- Array is suitable when lots of searching, and retrieval are
required.
Data Structures

Arrays: Parameters

- Base Address (b): The memory address of the first byte


of the first array element
- Component Length (L): The memory byte(s) required to
store one element of an array
- Upper and Lower Bounds (li, ui): Each index type has a
smallest and a largest value
- Dimension
Data Structures

Arrays: Representation in Memory


Array Mapping Function (AMF):
- AMF converts index value to element address
Linear (1 D) Arrays:
a: array [l1 .. u1] of element_type
Then addr (a[ i ]) = b +(i – l1) × L
= C0 + C1 × i

Therefore, the time for calculating the address of an


element is same for any value of i.
Data Structures

Arrays: Representation in Memory


Array Mapping Function (AMF):
- AMF converts index value to element address
2D Arrays:
a: array [l1 .. u1 , l2.. u2] of element_type
Order of the elements:
- Row major order
- Column major order
Data Structures

Arrays: Representation in Memory


Data Structures

Arrays: Representation in Memory


Data Structures

Arrays: Representation in Memory


Data Structures

Arrays: Representation in Memory


Data Structures

Arrays: Representation in Memory


Data Structures

Abstract Data Structure:

- Relation: explicit linear ordering


- Operations
- Implementations
 Linked lists
 Arrays
- Memory requirements
Data Structures

Abstract Data Structure:

- An Abstract List (List ADT) is linearly ordered data


where the programmer explicitly defines the
ordering
- Most Common Operations
 The most obvious implementation is to use either
an array or linked list
 These are, however, not always the most optimal
Data Structures

Abstract Data Structure: Operations


Data Structures

Abstract Data Structure: Operations


Data Structures
Array: Operations (Shuffling)

Code Snippet
Data Structures

Array: Operations (Shifting)

Code Snippet
Data Structures

Array Lists
- While collecting inputs, we don’t have idea how
many?
- In that case, an array list could be a solution:

 Array Lists can grow and shrink as needed


 Operations such as inserting/removing could be handy
Data Structures

Array Lists
- How to declare and use?
ArrayList <String> myfriends = new ArrayList<String>();
myFriends.add(“John”); An array list
String myfriend = myFriends.get(i); object of size 0

myFriends.set(i, Garry);

i is >=0 and <


Get() and myFriends.size Add() appends
set() used an element to
for the list,
accessing increasing
the its size
element
Data Structures

Array Lists

myFriends.add(“Georgre”);
myFriends.add(“Mustaque”);
myFriends.add(“Robi”); String name =
myFriends.get(2);?

myFriends
George
Mustaque
Robi
Data Structures

Array Lists

myFriends.add(1, “John”);
myFriends.remove(1);

Draw the
arrayList
(myFriends)
Data Structures

Array Lists
- There is no ArrayList<int> or ArrayList<double> (primitive data
types!)
- But way out, using Wrapper class!
- Example:

- ArrayList<Double> myValues = new ArrayList<Double>;


myValues.add(275.135);
double valueVar = myValues.get(0);
Data Structures

Array and Array Lists


- Array Lists can grow and shrink
- Arrays have nicer syntax for element access and initialization
- If the size of a collection never changes, use an array
- If the collection involves huge primitive data and efficiency is
a concern, array is the choice
- Otherwise, use an array list.

Code Snippet
Data Structures

Linked Lists
Data Structures

Linked Lists
- Linear collection of data elements (nodes) using
pointers
- Having two parts
 Information field
 Link field (next pointer field), contains the address
of the next node in the list
Data Structures

Linked Lists: Basic Operation

- Insert: Add a new node at the beginning, end or in


between
- Delete: Delete a node from the beginning, end or in
between of the list
- Search: Search a node having particular value (key)
in the linked list
Data Structures

Basic Operation: Insertion


- Add a node at the beginning, end or in-between of a
linked list
Data Structures

Insertion: At the beginning


- Construct a new node that is pointed to by pointer newitem
- We have a global variable head which points to the first node
in the linked list
- The new node points to the first node in the list. The head is
then set to point to the new node
Data Structures

Insertion: At the beginning


- Step 1: Construct a new node that is pointed to by pointer
newItem
- Step 2: Link the new node to the first node of the linked list
- Step 3: Set the pointer head to the new node
Data Structures

Insertion: At the beginning(code snippet)


Data Structures

Insertion: At the end


- Construct a new node and set its link field to “NULL”
- Locate the last node and change its link field to
point to the new node
Data Structures

Insertion: At the end


- Create the new node
- Set a temporary pointer prev to point to the last node
- Set prev to point to the new node and new node as the last
node
Data Structures

Insertion: At the end (code snippet)


Data Structures

Printing List (code snippet)


Data Structures

Linked List: Deletion


- At the beginning of the linked list
- At the end of the linked list
- Within the linked list
Data Structures

Deletion: At the beginning


- Advance the pointer head to the second node
- Release the memory occupied by the abandoned
node
Data Structures

Deletion: At the beginning


- Step1: Initialize the pointer cur point to the first node of the list
- Step 2:Move the pointer head to the second node of the list
- Step 3: Remove the node that is pointed by the pointer cur
Data Structures

Deletion: At the beginning (code snippet)


Data Structures

Deletion: At the end


- Use a local variable, cur
- Cur points to the last node
- Use another variable prev, that points to the second last node
in the linked list
Data Structures

Deletion: At the end


- Step 1: Initialize pointer cur to point to the first node (Head), while
prev points to NULL
- Step 2: Traverse the entire list until cur points to last node (Tail)
- Step 3: Set NULL to next field of the node pointed to by prev
- Step 4: Remove the last node that is pointed to by cur
Data Structures

Deletion: At the end (code snippet)


Data Structures

Deletion: Any
- To delete a node that holds a particular value x in a
linked list, we use
 A pointer cur to point to this node and
 Another pointer, prev, to point to the previous node
Data Structures

Deletion: Any
- Step 1: Initialize pointer cur to point to the first node (Head),
while pointer prev points to NULL
- Step 2: Traverse the entire list until pointer cur points to node
that contains value x, and prev points to the previous node
Data Structures

Deletion: Any
- Step 3: link the node pointed to by pointer prev to the node
after the cur’s node
- Step 4: Remove the node pointed to by cur
Data Structures

Deletion: Any (code snippet)


Data Structures

Doubly Linked List:


- In linear linked list, we can only traverse in one
direction
- It is desirable to traverse a linked list in both
direction (forward/backward)
- As such, each node must contain two link fields,
instead of one
- One link to denote the predecessor node (left link)
- One link to denote the successor (right link)
Data Structures

Doubly Linked List: Graphical presentation


Data Structures

Doubly Linked List: Basic Operations

- Insert: Add a new node at the beginning, end or in-


between
- Delete: Delete a node from the beginning, end or
from in-between
- Search: Search a node containing a particular value
(key) in the doubly linked list
Data Structures

Basic Operations: Insertion

- Adding a new node, at the beginning, end, or in


between of the linked list
- To add a new node to the head or tail of a doubly
linked list is same steps as linear linked list
- Construct a new node that is pointed to by pointer
newItem
- newItem is linked to the left-most node (or right-
most) in the list. Finally, the Left (or Right) is set to
point to the new node.
Data Structures

Basic Operations: Insertion


- Step 1: Create a new node that is pointed to by
newItem
- Step 2: Set the pointer prev to point to the left node
of the node pointed to by cur
- Step 3: Set the left link of the new node to point to
the node pointed to by prev, and the right link of the
new node to point to the node pointed to by cur
- Step 4: Set the right link of the node pointed to by
prev and the left link of the node pointed to by cur to
point to the new node.
Data Structures

Basic Operations: Insertion (Graphical)


Data Structures

Insertion (beginning): Code snippet


Data Structures

Insertion (end) : Code snippet


Data Structures

Insertion (Middle) : Code snippet


Data Structures

Basic Operations: Deletion

- Process of removing a node from a list. It can take


place- at the beginning, end, and in-between.
- In a single linked list, we have to search and find
the predecessor of the discarded node. But in the
doubly linked list, no search is required
- Given the address of the node that is to be deleted,
the predecessor and the successor nodes are
immediately known.
Data Structures

Basic Operations: Deletion


- Step 1: Set pointer prev to point to the left node of old and
pointer cur to point to the node on the right of old
- Step 2: Set the right link of prev to cur, and the left link of cur
to prev
- Step 3: Discard the node pointed to by old
Data Structures
Java Implementation: Node
- A class that implements a node holds a list element and a successor link.
- This type of data structure is called self-referential or recursive data structure

// A node for a list of string elements:


class Node
{
String element; // list element (DATA)
Node next; // successor link
Node(String el, Node n) // Node placed in
//front of n
{
element = el;
next = n;
}
Node(String el) // Node added without
//link
{
element = el;
next = null;
}
}
Data Structures
Java Implementation: Creation of Linked Lists
Data Structures
Java Implementation: Inserting Node
Data Structures
Java Implementation: Removing Node
Acknowledgements
The above slides are adopted from:

1. Dr. Abul Kashem@buet


2. Douglas Wilhelm Harder@uwaterloo
3. Tony & Godfrey@Mohawk

You might also like