Data Structures: Lakshmish Ramaswamy
Data Structures: Lakshmish Ramaswamy
Lakshmish Ramaswamy
Removing Element
public Object remove(int index){
Object retobj;
if(index < 0 || index => size){
throw IndexOutOfBoundsException }
if (size == 1){
retobj = first.data; first = null;
last = null; size --;
return retobj;}
if(index == 0){
retobj = first.data;
first = first.next; size --;
return(retobj); }
Removing Element
ListNode tmpNode = first;
for(int i = 0; i < (index-1); i++)
tmpNode = tmpNode.next;
retobj = (tmpNode.next).data
tmpNode.next = (tmpNode.next).next;
if (index == (size - 1))
last = tmpNode;
size--;
return(retobj);
}
Searching for an Element
public int indexOf(Object o){
if(size == 0) return -1;
ListNode tmpNode = first;
int index = 0;
while(index < size){
if(tmpNode.data.equals(o)) return(index);
index++;
tmpNode = tmpNode.next;
}
return(-1);
}
Retrieving an Element
public Object get(int index){
if(index < 0 || index => size){
throw IndexOutOfBoundsException;
}
ListNode tmpNode = first;
for(int i=0; i<index; i++)
tmpNode = tmpNode.next;
return(tmpNode.data);
}
Removing a Given Object
public boolean remove(Object o){
if(size == 0) return false;
if(size == 0){
if(first.data.equals(o) {
first = null;
last = null;
size --;
return(true); }
else return(false);
}
ListNode currNode = first;
ListNode prevNode = null;
int index = 0;
Removing a Given Object (Contd.)
while(index<size && !
currNode.data.equals(o)){
prevNode = currNode;
currNode = currNode.next;
index++;
}
if(index == size) return(false);
if(index == 0) first = currNode.next;
else{
prevNode.next = currNode.next;
if(index==(size-1)) last = currNode.next;
}
size --; return(true); }
Additional Methods
• public boolean add(Object o)
• public boolean addAll(Collection c)
• public boolean addAll(int index,Collection c)
• public void addFirst(Object o)
• public void addLast(Object o)
• public void clear()
• public Object clone()
• public boolean contains(Object o)
• public Object getFirst()
Additional Methods (Contd.)
• public Object getLast()
• public lastIndexOf(Object o)
• public listIterator(int index)
• public Object removeFirst()
• public Object removeLast()
• public Object set(int index, Object o)
• public int size()
• public[] Object toArray()
• public[] Object toArray(Object[] a)
Doubly Linked List
• Linked lists allow for uni-directional
traversal
– From First to Last
• Doubly-linked list allow for traversal in both
directions
– List nodes store both previous node and next
node
• Doubly linked list stores First, Last and
Size
Structures
class ListNode{
Object data;
ListNode next;
ListNode prev;
public ListNode();
}
root
parent nodes
6
5 8 children nodes
3 7 9
leaf nodes
Types of Trees
6 6 5 6 6
7 8
7 8
5 5 8
3 4
2 8 3
7 7 3 7 9
a tree a binary
a tree a Binary
tree not a tree
and a ... search Tree
Formal Definition
• Graph is a set of vertices V = {v1, v2, ,, vn}
and a set of edges connecting them E =
{e1, e2, …, eM}
– Each edge is a tuple of vertices el = (vi, vj)
• A graph is directed if edges are directed
(pair of vertices is ordered)
• A directed graph with no directed cycles
• A connected DAG with exactly one path
between any two vertices