3 Array and Linked Lists
3 Array and Linked Lists
1/32
Outline
2/32
Types of Data Structure
linear fashion.
Array: Fixed-size
Linked-list: Variable-size
priority
3/32
Types of Data Structure
Non-Linear: The data values in this structure are not
arranged in order.
Hash tables: Unordered lists which use a ‘hash function’ to insert and
search
4/32
Type of Data Structures
Homogeneous: In this type of data structures, values
of the same types of data are stored.
Array
Structures
Classes
5/32
Abstract Data Type and Data Structure
Definition:-
Abstract Data Types (ADTs) stores data and allow various
operations on the data to access and change it.
A mathematical model, together with various operations defined
on the model
An ADT is a collection of data and associated operations for
manipulating that data
Data Structures
Physical implementation of an ADT
data structures used in implementations are provided in a
language (primitive or built-in) or are built from the language
constructs (user-defined)
Each operation associated with the ADT is implemented by one
or more subroutines in the implementation
6/32
Abstract Data Type
ADTs support abstraction, encapsulation, and
information hiding.
8/32
Is there a data structure that works well for
all purpose?
all purposes
9/32
Array
Memory
a b c d
start
Storing data in a sequential memory locations
Access each element using integer index
Very basic, popular, and simple
int a[10]; int *a = new int(10);
10/32
Array
Array representation: You can access each value in constant
time through its index.
middle.
11/32
Array: Problems
New insertion and deletion: difficult
Linked List
12/32
Linked List
A List (or Linked List) is a linear data structure where each
object has a pointer to the next one creating a chain. You can
also have a back reference to the previous node.
13/32
Types of Linked List
1. Singly linked lists.
2. Doubly linked lists.
3. Circular linked lists.
4. Circular doubly linked lists.
14/32
Singly Linked List
A singly linked list is a concrete data structure
consisting of a sequence of nodes
node
Each node stores
element
next
link to the next node
element
A singly linked list is a unidirectional linked list.
Traversal is possible only in one direction, i.e., from head node
to tail node.
15/32
Singly Linked List of Strings: Picture
StringLinkedList
*head
16/32
Inserting at the Head node
Questions?
1. Allocate a new node
2. Insert a new element
3. Have the new node point to the old head
4. Update head to point to new node 17/32
Inserting at the Head node
18/32
Removing at the Head
Questions?
3. …
4. …
20/32
Inserting node at the Tail node
21/32
Doubly Linked List
A doubly linked list is a bi-directional linked list.
previous next
element node
22/32
Doubly Linked List
elements
23/32
Circular Linked List
In Circular, singly linked list, Pointer in the last node
points back to the first node
Rather than having a head or a tail, it forms a cycle
Cursor
A virtual starting node
This can be varying as we perform operations
24/32
Circular Linked List
A circular Linked list is a unidirectional linked list.
25/32
Circular Doubly Linked List
It is a mixture of a doubly linked list and a circular linked list
Like the circular linked list, its last node points at the head
node.
26/32
Circular Doubly Linked List
27/32
Applications of linked list
Applications of linked list in computer science:
1. Implementation of stacks and queues
2. Implementation of graphs: Adjacency list representation of
graphs is the most popular which uses a linked list to store
adjacent vertices.
3. Dynamic memory allocation: Uses a linked list of free blocks.
4. Maintaining a directory of names.
5. Performing arithmetic operations on long integers.
6. Manipulation of polynomials by storing constants in the node
of the linked list.
7. Representing sparse matrices.
28/32
Applications of linked list
Applications of linked list in the real world:
1. Image viewer – Previous and next images are linked and can
be accessed by the next and previous buttons.
previous and next songs. So you can play songs either from
starting or ending of the list.
29/32
Applications of linked list
Applications of Circular Linked Lists:
30/32
Applications of linked list
Applications of Circular Linked Lists :
32/32