1 List
1 List
1 List
Outline
I. Introduction to List Structure
Circular Lists
List implementation:
• 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.
Array
Array:
A linear data structure that collects elements of the same data type and
stores them in contiguous and adjacent memory locations.
Limitations:
Requires size information for creation
Inserting/deleting an element in the middle of an array leads to moving
other elemenets around
Self-Referential Structure
A structure that points to the same type of structure. It contains one or
more pointers that ultimately point to the same structure.
Employee
String name;
int age;
DataNode DataNode
Employee info; Employee info;
DataNode next; DataNode left;
DataNode right;
Linked List
Linked List:
A linear data structure composed of nodes, each node holding some in-
formation and a reference to another node in the list
24 57 20
24 57 57
data
Node
next
24 57 57
24 57 57
class MyList
{Node head,tail;
MyList() {head=tail=null;}
boolean isEmpty()
{return(head==null); }
void clear() {head=tail=null;}
class Node
void add(int x)
{int info;
Node prev,next; {if(isEmpty())
Node() {} head=tail=new Node(x,null,null);
Node(int x, Node p, Node q) else
{info=x;prev=p; next=q; {Node q =new Node(x,tail,null);
} tail.next=q;
} tail=q;
}
}
...
}
Doubly Linked List Example (1/2)
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));
} }
} }
Array-List in Java.util
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.
Summary
List Data-Structure:
A sequential data structure, i.e. it is a sequence of items of a given base type.
It can be implemented as an array, or as a dynamic array to avoid imposing a max-
imum size.
Linked-list:
An alternative implementation of the list,
Items in linked list are stored in nodes that are linked together with pointers.
Singly linked list:
• Node has a link to its successor (next node) only.
Circular linked list:
• Nodes form a ring: The list is finite and each node has a successor.
Doubly linked list:
• Node has links to its previous and to the next nodes.
Reading at home
Text book: Data Structures and Algorithms in Java