SlideShare a Scribd company logo
Data Structures and Algorithms in Java 1/23
1. List Data Structure
Data Structures and Algorithms in Java 2/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 3/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 4/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 5/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
11/12/2021 Data Structures and Algorithms in Java 6/23
DataNode
Employee info;
DataNode next;
trees
Linked lists
Employee
String name;
int age;
DataNode
Employee info;
DataNode left;
DataNode right;
Self-Referential Structures
Self-Referential Structures
Data Structures and Algorithms in Java 7/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 8/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 9/23
Singly Linked List Implementation
9
class Node
{int info;
Node next;
Node() {}
Node(int x, Node p)
{info=x;next=p;
}
}
void add(int x)
{ if(isEmpty())
head=tail=new Node(x,null);
else
{Node q =new Node(x,null);
tail.next=q; tail=q;
}
}
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) {...}
}
class MyList
{Node head,tail;
MyList()
{head=tail=null;}
boolean isEmpty()
{return(head==null);
}
void clear()
{head=tail=null;
}
Data Structures and Algorithms in Java 10/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 11/23
11
Singly Linked Lists - 2
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 12/23
Singly Linked Lists - 3
Deleting a node from the beginning of a list
12
Deleting a node from the beginning of a Singly Linked List
Data Structures and Algorithms in Java 13/23
Singly Linked List - 4
Deleting element from the end of a list
13
Deleting a node from the end of a Singly Linked List
Data Structures and Algorithms in Java 14/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 15/23
Circular Lists - 2
Inserting nodes
15
Inserting nodes at the front of a circular singly linked list (a) and at its end (b)
Data Structures and Algorithms in Java 16/23
Circular List application
16
1. 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 17/23
Doubly Linked Lists - 1
17
• In a doubly linked list, each
node has two reference
fields, one to the successor
and one to the predecessor
class Node
{int info;
Node prev,next;
Node() {}
Node(int x, Node p, Node q)
{info=x;prev=p; next=q;
}
}
class MyList
{Node head,tail;
MyList() {head=tail=null;}
boolean isEmpty()
{return(head==null); }
void clear() {head=tail=null;}
void add(int x)
{if(isEmpty())
head=tail=new Node(x,null,null);
else
{Node q =new Node(x,tail,null);
tail.next=q;
tail=q;
}
}
...
}
Doubly Linked List
Data Structures and Algorithms in Java 18/23
Doubly Linked Lists - 2
Adding a new node at the end
18
Adding new node at the end of Doubly Linked List
Data Structures and Algorithms in Java 19/23
Doubly Linked Lists -3
Deleting a node from the end
19
Deleting a node from the end of Doubly Linked List
Data Structures and Algorithms in Java 20/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 21/23
Lists in java.util
LinkedList class example
import java.util.*;
class Node
{ String name;
int age;
Node() {}
Node(String name1, int age1)
{ name=name1; age=age1;
}
void set(String name1, int age1)
{ name=name1; age=age1;
}
public String toString()
{ String s = name+" "+age;
return(s);
}
}
class Main
{
public static void main(String [] args)
{
LinkedList t = new LinkedList();
Node x; int n,i;
x = new Node("A01",25); t.add(x);
x = new Node("A02",23); t.add(x);
x = new Node("A03",21); t.add(x);
for(i=0;i<t.size();i++)
System.out.println(t.get(i));
}
}
Data Structures and Algorithms in Java 22/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 23/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 24/23
Reading at home
• 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
Text book: Data Structures and Algorithms in Java

More Related Content

What's hot (19)

PPT
Chapter 4 ds
Hanif Durad
 
PPTX
stacks and queues for public
iqbalphy1
 
PPTX
هياكلبيانات
Rafal Edward
 
PPT
Chapter 7 ds
Hanif Durad
 
PPT
Chapter 6 ds
Hanif Durad
 
PPTX
Standard Template Library
GauravPatil318
 
PDF
LectureNotes-06-DSA
Haitham El-Ghareeb
 
PPTX
Searching Algorithms
Afaq Mansoor Khan
 
PDF
LectureNotes-03-DSA
Haitham El-Ghareeb
 
PPT
Data structure
Muhammad Farhan
 
PPTX
Data structures and algorithms
Hoang Nguyen
 
PPT
Data Structure In C#
Shahzad
 
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
PDF
Datastructure
Griffinder VinHai
 
PPTX
Collections in .net technology (2160711)
Janki Shah
 
PPT
Introduction of data structure
eShikshak
 
PPSX
Dynamic memory allocation
Moniruzzaman _
 
PPTX
Data structures
Sneha Chopra
 
PPTX
Lecture11 standard template-library
Hariz Mustafa
 
Chapter 4 ds
Hanif Durad
 
stacks and queues for public
iqbalphy1
 
هياكلبيانات
Rafal Edward
 
Chapter 7 ds
Hanif Durad
 
Chapter 6 ds
Hanif Durad
 
Standard Template Library
GauravPatil318
 
LectureNotes-06-DSA
Haitham El-Ghareeb
 
Searching Algorithms
Afaq Mansoor Khan
 
LectureNotes-03-DSA
Haitham El-Ghareeb
 
Data structure
Muhammad Farhan
 
Data structures and algorithms
Hoang Nguyen
 
Data Structure In C#
Shahzad
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
Datastructure
Griffinder VinHai
 
Collections in .net technology (2160711)
Janki Shah
 
Introduction of data structure
eShikshak
 
Dynamic memory allocation
Moniruzzaman _
 
Data structures
Sneha Chopra
 
Lecture11 standard template-library
Hariz Mustafa
 

Similar to 1 list datastructures (20)

PPTX
Introduction in Data Structure - stack, Queue
BharathiKrishna6
 
PPTX
General Data structures
Youssef Elsalhawy
 
PPTX
Data Structures Algorithms and Applications
harshavardhan543715
 
PDF
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
PPTX
Data Structure
HarshGupta663
 
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
PPTX
oop lecture framework,list,maps,collection
ssuseredfbe9
 
PPT
description of Collections, seaching & Sorting
mdimberu
 
PPTX
Data Structures & Algorithms Unit 1.pptx
UsriDevi1
 
PDF
UNITIII LDS.pdf
meenamadhuvandhi2
 
PDF
data structure and algorithm note for IT students
RameshTharu5
 
PPTX
Data Structure Introduction- Arrays, Matrix, Linked List
JasmineJinitha
 
PPTX
Collections lecture 35 40
bhawna sharma
 
PPT
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
PPTX
Data -structures for class 12 , easy ppt
anikedheikhamsingh
 
PPTX
Data Structures and algoithms Unit - 1.pptx
mexiuro901
 
PPT
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
Farhana859326
 
PPTX
DATA STRUCTURE AND ALGORITHM with linked list
shanmugapriyacsecs
 
PPT
3.ppt
ArifKamal36
 
PPT
3.ppt
ArifKamal36
 
Introduction in Data Structure - stack, Queue
BharathiKrishna6
 
General Data structures
Youssef Elsalhawy
 
Data Structures Algorithms and Applications
harshavardhan543715
 
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
Data Structure
HarshGupta663
 
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
oop lecture framework,list,maps,collection
ssuseredfbe9
 
description of Collections, seaching & Sorting
mdimberu
 
Data Structures & Algorithms Unit 1.pptx
UsriDevi1
 
UNITIII LDS.pdf
meenamadhuvandhi2
 
data structure and algorithm note for IT students
RameshTharu5
 
Data Structure Introduction- Arrays, Matrix, Linked List
JasmineJinitha
 
Collections lecture 35 40
bhawna sharma
 
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
Data -structures for class 12 , easy ppt
anikedheikhamsingh
 
Data Structures and algoithms Unit - 1.pptx
mexiuro901
 
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
Farhana859326
 
DATA STRUCTURE AND ALGORITHM with linked list
shanmugapriyacsecs
 
Ad

Recently uploaded (20)

PDF
soil and environmental microbiology.pdf
Divyaprabha67
 
PPTX
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
PPTX
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
PPTX
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
PDF
Unit-3 ppt.pdf organic chemistry - 3 unit 3
visionshukla007
 
PDF
Plant growth promoting bacterial non symbiotic
psuvethapalani
 
DOCX
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
PPTX
Class12_Physics_Chapter2 electric potential and capacitance.pptx
mgmahati1234
 
PPTX
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
PPTX
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
PDF
Unit-5 ppt.pdf unit 5 organic chemistry 3
visionshukla007
 
PPTX
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
PDF
Insect Behaviour : Patterns And Determinants
SheikhArshaqAreeb
 
PDF
Pharma Part 1.pdf #pharmacology #pharmacology
hikmatyt01
 
PPTX
Ghent University Global Campus: Overview
Ghent University Global Campus
 
PDF
Preserving brand authenticity amid AI-driven misinformation: Sustaining consu...
Selcen Ozturkcan
 
PPTX
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
PDF
Annual report 2024 - Inria - English version.pdf
Inria
 
PDF
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
PPTX
GB1 Q1 04 Life in a Cell (1).pptx GRADE 11
JADE ACOSTA
 
soil and environmental microbiology.pdf
Divyaprabha67
 
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
Unit-3 ppt.pdf organic chemistry - 3 unit 3
visionshukla007
 
Plant growth promoting bacterial non symbiotic
psuvethapalani
 
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
Class12_Physics_Chapter2 electric potential and capacitance.pptx
mgmahati1234
 
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
Unit-5 ppt.pdf unit 5 organic chemistry 3
visionshukla007
 
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
Insect Behaviour : Patterns And Determinants
SheikhArshaqAreeb
 
Pharma Part 1.pdf #pharmacology #pharmacology
hikmatyt01
 
Ghent University Global Campus: Overview
Ghent University Global Campus
 
Preserving brand authenticity amid AI-driven misinformation: Sustaining consu...
Selcen Ozturkcan
 
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
Annual report 2024 - Inria - English version.pdf
Inria
 
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
GB1 Q1 04 Life in a Cell (1).pptx GRADE 11
JADE ACOSTA
 
Ad

1 list datastructures

  • 1. Data Structures and Algorithms in Java 1/23 1. List Data Structure
  • 2. Data Structures and Algorithms in Java 2/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
  • 3. Data Structures and Algorithms in Java 3/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()
  • 4. Data Structures and Algorithms in Java 4/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.
  • 5. Data Structures and Algorithms in Java 5/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
  • 6. 11/12/2021 Data Structures and Algorithms in Java 6/23 DataNode Employee info; DataNode next; trees Linked lists Employee String name; int age; DataNode Employee info; DataNode left; DataNode right; Self-Referential Structures Self-Referential Structures
  • 7. Data Structures and Algorithms in Java 7/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
  • 8. Data Structures and Algorithms in Java 8/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
  • 9. Data Structures and Algorithms in Java 9/23 Singly Linked List Implementation 9 class Node {int info; Node next; Node() {} Node(int x, Node p) {info=x;next=p; } } void add(int x) { if(isEmpty()) head=tail=new Node(x,null); else {Node q =new Node(x,null); tail.next=q; tail=q; } } 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) {...} } class MyList {Node head,tail; MyList() {head=tail=null;} boolean isEmpty() {return(head==null); } void clear() {head=tail=null; }
  • 10. Data Structures and Algorithms in Java 10/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
  • 11. Data Structures and Algorithms in Java 11/23 11 Singly Linked Lists - 2 Inserting a new node at the end of a list Inserting a new node at the end of a Singly Linked List
  • 12. Data Structures and Algorithms in Java 12/23 Singly Linked Lists - 3 Deleting a node from the beginning of a list 12 Deleting a node from the beginning of a Singly Linked List
  • 13. Data Structures and Algorithms in Java 13/23 Singly Linked List - 4 Deleting element from the end of a list 13 Deleting a node from the end of a Singly Linked List
  • 14. Data Structures and Algorithms in Java 14/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
  • 15. Data Structures and Algorithms in Java 15/23 Circular Lists - 2 Inserting nodes 15 Inserting nodes at the front of a circular singly linked list (a) and at its end (b)
  • 16. Data Structures and Algorithms in Java 16/23 Circular List application 16 1. 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( )
  • 17. Data Structures and Algorithms in Java 17/23 Doubly Linked Lists - 1 17 • In a doubly linked list, each node has two reference fields, one to the successor and one to the predecessor class Node {int info; Node prev,next; Node() {} Node(int x, Node p, Node q) {info=x;prev=p; next=q; } } class MyList {Node head,tail; MyList() {head=tail=null;} boolean isEmpty() {return(head==null); } void clear() {head=tail=null;} void add(int x) {if(isEmpty()) head=tail=new Node(x,null,null); else {Node q =new Node(x,tail,null); tail.next=q; tail=q; } } ... } Doubly Linked List
  • 18. Data Structures and Algorithms in Java 18/23 Doubly Linked Lists - 2 Adding a new node at the end 18 Adding new node at the end of Doubly Linked List
  • 19. Data Structures and Algorithms in Java 19/23 Doubly Linked Lists -3 Deleting a node from the end 19 Deleting a node from the end of Doubly Linked List
  • 20. Data Structures and Algorithms in Java 20/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.
  • 21. Data Structures and Algorithms in Java 21/23 Lists in java.util LinkedList class example import java.util.*; class Node { String name; int age; Node() {} Node(String name1, int age1) { name=name1; age=age1; } void set(String name1, int age1) { name=name1; age=age1; } public String toString() { String s = name+" "+age; return(s); } } class Main { public static void main(String [] args) { LinkedList t = new LinkedList(); Node x; int n,i; x = new Node("A01",25); t.add(x); x = new Node("A02",23); t.add(x); x = new Node("A03",21); t.add(x); for(i=0;i<t.size();i++) System.out.println(t.get(i)); } }
  • 22. Data Structures and Algorithms in Java 22/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.
  • 23. Data Structures and Algorithms in Java 23/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.
  • 24. Data Structures and Algorithms in Java 24/23 Reading at home • 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 Text book: Data Structures and Algorithms in Java