Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Slide02b - Data Structures and Algorithms

Uploaded by

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

Slide02b - Data Structures and Algorithms

Uploaded by

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

Data Structures and Algorithms

TCC236/05

Week 2 (Part 2): Lists and Possible Operations

(WOU) Data Structures and Algorithms 1 / 29


Recall

Table of Contents

3 Double linked lists


1 Recall
4 Cricular Linked Lists
2 What’s new?
5 Summary

(WOU) Data Structures and Algorithms 2 / 29


Recall

Recall

List
Linked lists
Single linear linked list
Comprises only 1 link
The link points to the next data element in the list.
The last element points to the null.
The first node is usually called the Head node.
ADT
Operations / Algorithms:
Create
Insert
Delete
Traverse

(WOU) Data Structures and Algorithms 3 / 29


What’s new?

Table of Contents

3 Double linked lists


1 Recall
4 Cricular Linked Lists
2 What’s new?
5 Summary

(WOU) Data Structures and Algorithms 4 / 29


What’s new?

What’s new?

Double linked lists


Circular linked lists

(WOU) Data Structures and Algorithms 5 / 29


Double linked lists

Table of Contents

3 Double linked lists


1 Recall
4 Cricular Linked Lists
2 What’s new?
5 Summary

(WOU) Data Structures and Algorithms 6 / 29


Double linked lists

Objectives

Identify different types of lists.


Perform operations such as insert, delete, traverse on double linked
lists.

(WOU) Data Structures and Algorithms 7 / 29


Double linked lists

Introduction
Example double linked list:

(WOU) Data Structures and Algorithms 8 / 29


Double linked lists

Introduction
Example double linked list:

The node structure in a double linked list:

(WOU) Data Structures and Algorithms 8 / 29


Double linked lists

(I) Operations

Create a double linked list

(WOU) Data Structures and Algorithms 9 / 29


Double linked lists

(II) Operations

Insertion of a node in a double linked list - MID

1 Traverse the list until data 20 is found. Assign that node as prevnode.
2 Create a new node with data 25 (which is to be inserted)

(WOU) Data Structures and Algorithms 10 / 29


Double linked lists

(II) Operations

Insertion of a node in a double linked list - MID

(WOU) Data Structures and Algorithms 11 / 29


Double linked lists

(II) Operations

Insertion of a node in a double linked list - MID


1 temp = prevnode.getNext();
2 temp.setPrev(newnode);
3 newnode.setNext(temp);
4 newnode.setPrev(prevnode);
5 prevnode.setNext(newnode);

Or without using temp:

(WOU) Data Structures and Algorithms 11 / 29


Double linked lists

(II) Operations

Insertion of a node in a double linked list - MID


1 temp = prevnode.getNext();
2 temp.setPrev(newnode);
3 newnode.setNext(temp);
4 newnode.setPrev(prevnode);
5 prevnode.setNext(newnode);

Or without using temp:


1 newnode.setNext(prev.getNext());
2 newnode.setPrev(prevnode);
3 prevnode.getNext().setPrev(newnode);
4 prevnode.setNext(newnode);

(WOU) Data Structures and Algorithms 11 / 29


Double linked lists

(II) Operations
Insertion of a node in a double linked list - START

1 Create a newnode with data 5


2 Change the links of newnode as follows:
1 newnode.setPrev(null);
2 newnode.setNext(head);
3 head.setPrev(newnode);
3 Change head as newnode:
1 head = newnode;
(WOU) Data Structures and Algorithms 12 / 29
Double linked lists

(III) Operations

Deletion of a node from a double linked list - START


1 head = head.getNext();
2 head.prev = null;

(WOU) Data Structures and Algorithms 13 / 29


Double linked lists

(III) Operations
Deletion of a node from a double linked list - MID

1 Traverse the list until data 25 is found.


2 Assign the node as temp.
3 Change the links as follows:
1 temp.getPrev().setNext(temp.getNext());
2 temp.getNext().setPrev(temp.getPrev());

(WOU) Data Structures and Algorithms 14 / 29


Double linked lists

(IV) Operations

Traverse a double linked list - traverse LEFT


1 Get the address of the first node, i.e. head into temp.
2 Move temp to the next node until temp reaches the last node.
3 Repeat while temp is not null
1 Process the data of temp.
2 Move to the prev node by taking address of the previous node in temp.

(WOU) Data Structures and Algorithms 15 / 29


Cricular Linked Lists

Table of Contents

3 Double linked lists


1 Recall
4 Cricular Linked Lists
2 What’s new?
5 Summary

(WOU) Data Structures and Algorithms 16 / 29


Cricular Linked Lists

Objectives

Apply operations on circular linked list like inserting a node, creating


a circular linked list and traversing it.
Discuss various types of circular linked list.

(WOU) Data Structures and Algorithms 17 / 29


Cricular Linked Lists

Introduction

Linear linked list Circular linked list


Last node set to null Last node point to the first
node

(WOU) Data Structures and Algorithms 18 / 29


Cricular Linked Lists

(I) Operations

Create a circular linked list


1 Create head node and point its next to head node itself:
1 Node head = new Node();
2 head.setNext(head);
3 curr = head;
2 Create a new node named newnode:
1 Node newnode = new Node();
3 Set data using newnode.setData(66).
4 Attach newnode to curr and make link of newnode to head, then
assign newnode to curr:
1 curr.setNext(newnode);
2 newnode.setNext(head);
3 curr=newnode;

(WOU) Data Structures and Algorithms 19 / 29


Cricular Linked Lists

(II) Operations

1 public void traverse(){


2 Node t;
3 t = head.getNext();
4 while (t.getNext() != head){
5 System.out.println(t.data);
6 t = t.getNext();
7 }
8 }

(WOU) Data Structures and Algorithms 20 / 29


Cricular Linked Lists

Types of Circular Linked Lists

Figure: Circular linked list with a head node.

(WOU) Data Structures and Algorithms 21 / 29


Cricular Linked Lists

Types of Circular Linked Lists (cont.)

Figure: Double circular linked list.

Figure: Double circular linked list with head node.

(WOU) Data Structures and Algorithms 22 / 29


Summary

Table of Contents

3 Double linked lists


1 Recall
4 Cricular Linked Lists
2 What’s new?
5 Summary

(WOU) Data Structures and Algorithms 23 / 29


Summary

Summary

Lists are finite, ordered sequence of items.


We can represent lists by:
packing their items contiguously in arrays - getting sequential
representation of lists, or
putting the items in individual nodes into chains - getting the linked
representation of lists.
The many varieties of linked list representations include:
Single/singly (linear) linked list
Single/singly circular linked list
Double/doubly (linear) linked list
Double/doubly circular linked list

(WOU) Data Structures and Algorithms 24 / 29


Summary

Summary - Sequential list vs Linked list Representations

Sequential list representations provide fast access to arbitrary items


by position, but in comparison to linked lists, it may be more costly to
delete or insert items in the middle of sequential list representations,
and they run the risk of overflowing or wasting space not used to
store items.
Linked list provide for fast insertion and deletion of items, but
accessing items by position is slower than is the case of sequential
lists, and linked lists can exhibit poor storage utilization efficiency due
to the additional memory used by their pointers (links).

(WOU) Data Structures and Algorithms 25 / 29


Summary

Summary - Singly linked list

Pros:
Simple in implementation
Requires relatively lesser memory for storage (1 link compared to 2 in
doubly)
Cons:
Cannot be iterated in reverse order.
Needs to maintain a pointer to the head node. Otherwise the list will
be lost in memory.
When deleting or inserting at previous node, one needs to traverse the
list from the head to the node - requires O(N) time.
When to use?
Have limited memory to spare.
Requires heavy insert/delete operations, rather than search/read.

(WOU) Data Structures and Algorithms 26 / 29


Summary

Summary - Doubly linked list

Pros:
Can iterate in forward or backward directions.
In the case of needing to delete previous node, there is no need to
traverse from head node as the node to be deleted can be found from
previous pointer - requires O(1) time.
Cons:
Relatively more complex to implement.
Requires more memory for storing additional pointers per node
(previous and next).
Insertions and deletions are relatively more time consuming, i.e.
assigning/reassigning pointers for neighbour nodes.
When to use?
Have lots of memory to spare.
Requires heavy search/read, rather than insert/delete operations.

(WOU) Data Structures and Algorithms 27 / 29


Summary

Summary - Circular vs Linear linked list


Pros:
In a circular linked list, any node can traverse to the previous node but
not in linear linked list.
Cons:
Extra implementation care is required to avoid infinite loop in the case
of circular linked list.
Sample applications of circular linked list:
Round robin scheduling algorithm in operating system.
Implementation of Queue (FIFO).
To keep track of whose turn in a multi-player board game, simply put all the players
in a circular linked list. After a player takes his turn, then advance to the next
player in the list. The program will automatically cycle among the players.
A jitter buffer is a type of buffer that takes numbered packets from a network and
places them in order, so that (for example) a video or audio player can play them in
order. Packets that are too slow (laggy) are discarded. This can be represented in a
circular buffer, without needing to constantly allocate and deallocate memory, as
slots can be re-used once they have been played. It could be implemented with a
linked-list, but there would be constant additions and deletions to the list, rather
than replacement to the constants (which are cheaper).

(WOU) Data Structures and Algorithms 28 / 29


Summary

Summary - Selective Java Collections Framework

Take home note:


Java provides the libraries for the various data structures. In this course,
you learn how they are actually being implemented. Practice writing the
codes over and over again until you are familiar with them.
(WOU) Data Structures and Algorithms 29 / 29

You might also like