Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

List Data Structure: Data Structures and Algorithms in Java 1/23

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

1.

List Data Structure

Data Structures and Algorithms in Java 1/23


Objectives
2

• Describe List structures


• Describe self-referential structures
• Explain types of linked lists
• Singly Linked Lists
• Circular Lists
• Doubly Linked Lists
• Lists in java.util

Data Structures and Algorithms in Java 2/23


List Data Structures
• A list is a sequential data structure, i.e. it is a sequence of items of a given
base type, where items can be added, deleted, and retrieved from any
position in the list.
• A list can be implemented as an array, or as a dynamic array to avoid
imposing a maximum size.
• An alternative implementation is a linked list, where the items are stored
in nodes that are linked together with pointers. These two
implementations have very different characteristics.
• The possible values of this type are sequences of items of type BaseType
(including the sequence of length zero). The operations of the ADT are:

getFirst(), getLast(), getNext(p), getPrev(p), get(p),


set(p,x), insert(p,x), remove(p),removeFirst(),
removeLast(), removeNext(p), removePrev(p),
find(x),size()

Data Structures and Algorithms in Java 3/23


Drawbacks of Arrays
4

• Array is a very useful data structure in many


situations. However, it has some important
limitations:
– They require size information for creation
– Inserting an element in the middle of an array leads to moving
other elements around
– Deleting an element from the middle of an array leads to moving
other elements around
• Other data structures are more efficient in such
situations.

Data Structures and Algorithms in Java 4/23


Self-Referential Structures
Many dynamic data structures are implemented through the use of a self-
referential structure.
A self-referential structure is an object, one of whose elements is a
reference to another object of its own type.
With this arrangement, it is possible to create ‘chains’ of data of varying
forms:

Self-Referential Structures

Data Structures and Algorithms in Java 5/23


Self-Referential Structures

Employee
String name;
int age;

Linked lists trees

DataNode DataNode
Employee info; Employee info;
DataNode next; DataNode left;
DataNode right;

Self-Referential Structures

10/14/21 Data Structures and Algorithms in Java 6/23


Linked Lists
7
• A linked structure is a collection of nodes storing data
and links to other nodes
• A linked list is a linear data structure composed of nodes,
each node holding some information and a reference to
another node in the list
• Types of linked lists:
– Singly-Linked List
– Doubly-Linked List
In programming, linear means that they are described by one (single) series of data … ie.
Each data item has at most one predecessor and at most one successor.
And, Non-linear means anything else.
Linear are – Array, Linked List, Stack, Queue. Non Linear are – Tree, Graph

Data Structures and Algorithms in Java 7/23


Singly Linked Lists
8

A singly linked list is a list whose node includes two


datafields: info and next. The info field is used to store
information, and this is important to the user. The next
field is used to link to its successor in this sequence
The following image depicts a simple integer linked list.

head tail

Singly Linked List

Data Structures and Algorithms in Java 8/23


Singly Linked List Implementation
9
class Node class MyList void add(int x)
{int info; {Node head,tail; { if(isEmpty())
Node next; MyList() head=tail=new Node(x,null);
Node() {} {head=tail=null;} else
Node(int x, Node p) boolean isEmpty() {Node q =new Node(x,null);
{info=x;next=p; {return(head==null); tail.next=q; tail=q;
} } }
} void clear() }
{head=tail=null; void traverse()
} {Node p=head;
while(p!=null)
{System.out.print(" " + p.info);
p=p.next;
}
System.out.println();
}
Node search(int x) {...}
void dele(int x) {...}
}
Data Structures and Algorithms in Java 9/23
Singly Linked Lists - 1
10
Inserting a new node at the beginning of a list

Inserting a new node at the beginning of a Singly Linked List

Data Structures and Algorithms in Java 10/23


Singly Linked Lists - 2
11
Inserting a new node at the end of a list

Inserting a new node at the end of a Singly Linked List

Data Structures and Algorithms in Java 11/23


Singly Linked Lists - 3
12
Deleting a node from the beginning of a list

Deleting a node from the beginning of a Singly Linked List

Data Structures and Algorithms in Java 12/23


Singly Linked List - 4
13
Deleting element from the end of a list

Deleting a node from the end of a Singly Linked List

Data Structures and Algorithms in Java 13/23


Circular Lists - 1
14

• A circular list is when nodes form a ring: The


list is finite and each node has a successor

Circular SIngly Linked List

Data Structures and Algorithms in Java 14/23


Circular Lists - 2
15
Inserting nodes

Inserting nodes at the front of a circular singly linked list (a) and at its end (b)

Data Structures and Algorithms in Java 15/23


Circular List application
1.
16
Round-Robin Scheduling
One of the most important roles of an operating system is in managing the
many processes that are currently active on a computer, including the
scheduling of those processes on one or more central processing units
(CPUs). In order to support the responsiveness of an arbitrary number of
concurrent processes, most operating systems allow processes to effectively
share use of the CPUs, using some form of an algorithm known as round-robin
scheduling. A process is given a short turn to execute, known as a time slice,
but it is interrupted when the slice ends, even if its job is not yet complete.
Each active process is given its own time slice, taking turns in a cyclic order.

2. Using circular linked list to implement Round-Robin Scheduling


We can use circular linked list to implement Round-Robin Scheduling by the
following method: rotate( ): Moves the first element to the end of the list.
With this new operation, round-robin scheduling can be efficiently
implemented by repeatedly performing the following steps on a circularly
linked list C:
1. Give a time slice to process C.first( )
2. C.rotate( )
Data Structures and Algorithms in Java 16/23
Doubly Linked Lists - 1
• In a doubly linked list, each
17
node has two reference class MyList
fields, one to the successor {Node head,tail;
and one to the predecessor MyList() {head=tail=null;}
boolean isEmpty()
{return(head==null); }
void clear() {head=tail=null;}
void add(int x)
{if(isEmpty())
Doubly Linked List head=tail=new Node(x,null,null);
class Node else
{int info; {Node q =new Node(x,tail,null);
Node prev,next; tail.next=q;
Node() {} tail=q;
Node(int x, Node p, Node q) }
{info=x;prev=p; next=q; }
} ...
} }

Data Structures and Algorithms in Java 17/23


Doubly Linked Lists - 2
18
Adding a new node at the end

Adding new node at the end of Doubly Linked List

Data Structures and Algorithms in Java 18/23


Doubly Linked Lists -3
19
Deleting a node from the end

Deleting a node from the end of Doubly Linked List

Data Structures and Algorithms in Java 19/23


Lists in java.util - LinkedList class
boolean add(E o)  Appends the specified element to the end of this list.
 void addFirst(E o)  Inserts the given element at the beginning of this list.
 void addLast(E o)  Appends the given element to the end of this list.
 void clear() Removes all of the elements from this list.
 E get(int index)  Returns the element at the specified position in this list.
 E getFirst() Returns the first element in this list.
 E getLast()  Returns the last element in this list.
 E remove(int index) Removes the element at the specified position in
this list.
 E removeFirst()Removes and returns the first element from this list.
 E removeLast() Removes and returns the last element from this list.
 int size() Returns the number of elements in this list.
 Object[] toArray() Returns an array containing all of the elements in this list in
the correct order.

Data Structures and Algorithms in Java 20/23


Lists in java.util
LinkedList class example
import java.util.*;
class Node
{ String name;
int age; class Main
Node() {} {
Node(String name1, int age1) public static void main(String [] args)
{ name=name1; age=age1; {
} LinkedList t = new LinkedList();
void set(String name1, int age1) Node x; int n,i;
{ name=name1; age=age1; x = new Node("A01",25); t.add(x);
} x = new Node("A02",23); t.add(x);
public String toString() x = new Node("A03",21); t.add(x);
{ String s = name+" "+age; for(i=0;i<t.size();i++)
return(s); System.out.println(t.get(i));
} }
} }

Data Structures and Algorithms in Java 21/23


Lists in java.util - ArrayList class
boolean add(E o)  Appends the specified element to the end of this list.
 void add(int index, E o)  Inserts the given element at the specified pos.
 void clear() Removes all of the elements from this list.
 E get(int index)  Returns the element at the specified position in this list.
 E remove(int index) Removes the element at the specified position in this
list.
 int size() Returns the number of elements in this list.
 void ensureCapacity(int minCapacity)    Increases the capacity of this ArrayList
instance, if necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
 void trimToSize()       Trims the capacity of this ArrayList instance to be the list's
current size.
 Object[] toArray() Returns an array containing all of the elements in this list in the
correct order.

Data Structures and Algorithms in Java 22/23


Summary
• A list is a sequential data structure, i.e. it is a
sequence of items of a given base type.
• A list can be implemented as an array, or as a dynamic
array to avoid imposing a maximum size.
• An alternative implementation is a linked list , where
the items are stored in nodes that are linked together
with pointers.
• A singly linked list is when a node has a link to its
successor (next node) only.
• A circular list is when nodes form a ring: The list is
finite and each node has a successor.
• A doubly linked list is when a node has links to its
previous and to the next nodes.
Data Structures and Algorithms in Java 23/23
Reading at home
Text book: Data Structures and Algorithms in Java

• 3 Fundamental Data Structures 103


• 3.1 Using Arrays - . 104
• 3.2 Singly Linked Lists - 122
• 3.3 Circularly Linked Lists - 128
• 3.4 Doubly Linked Lists - 132

Data Structures and Algorithms in Java 24/23

You might also like