250 likes | 339 Views
Data Structures. 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){
E N D
Data Structures 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(); } public class DoublyLinkedList{ int size; ListNode first; ListNode last; }
Methods • All methods of linked list can be implemented • Method implementations are very similar • Need to appropriately manipulate the forward and backward pointer • Insertion • Removal • Traversal
Stack Data Structure • Last-in-first-out paradigm • Supports two operations • Push – Insert an element at top • Pop – Remove the element at top • Can be implemented using ArrayLists or LinkedLists • Simple add and remove methods
Queue • First-in-First-Out Paradigm • Many applications • Buffer management • Job schedulers in OS • Operations • enqueue • Dequeue • getFront • Can be implemented using ArrayList or LinkedList
Implementation Using ArrayList/LinkedList • enqueue(o) implemented as add(o) • Added to the end • dequeue() implemented as remove(0) • getFirst() implemented as get(0) • Problem with ArrayList implementation • Every dequeue causes element shifting
Circular Queue • Avoids element shifting on enqueue or dequeue • Circle with numbered slots • Slot numbers increase in clockwise fashion • “Capacity” indicates maximum elements queue can hold • Two pointers – Front and End • -1 indicates empty queue • Both pointers move in clockwise direction • Wraparound on reaching end
Circular Queue Illustration Front 0 C-1 1 2 End
Circular Queue Implementation public class cirQueue{ Object[] arr; int capacity; int front; int end; public cirQueue(int cpty){ capacity = cpty; arr = new Object[capacity]; front = -1; end = -1;}
Enqueue Method public boolean enqueue(Object o){ if(end == -1){ end = 0; first = 0; arr[end] = o; return(true);} int newEnd = (end + 1)%capacity; if (newEnd == first) return(false); end = newEnd; arr[end] = o; return(true); }
Dequeue Method public Object dequeue(){ Object retobj; if(first == -1) return(null); retobj = arr[first]; if(first == end){ first = -1; end = -1;} else{ first = (first+1)%capacity;} return(retobj);}
Tree • Hierarchical data structure • Several real-world systems have hierarchical concepts • Physical and biological systems • Organizations • Many other applications • Sorting • Searching • Databases • Internet
root 6 5 8 3 7 9 Tree – Informal Definition • Each node has a single parent node except for root which has no parent parent nodes children nodes leaf nodes
Types of Trees 6 6 5 6 6 8 7 7 8 5 5 8 4 3 2 8 3 7 3 7 9 7 a binary tree a tree a tree and a ... a Binary search Tree not a 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