1 / 25

Data Structures

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){

cree
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures Lakshmish Ramaswamy

  2. 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); }

  3. 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); }

  4. 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); }

  5. 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); }

  6. 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;

  7. 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); }

  8. 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()

  9. 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)

  10. 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

  11. Structures class ListNode{ Object data; ListNode next; ListNode prev; public ListNode(); } public class DoublyLinkedList{ int size; ListNode first; ListNode last; }

  12. 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

  13. 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

  14. 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

  15. Queue

  16. 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

  17. 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

  18. Circular Queue Illustration Front 0 C-1 1 2 End

  19. 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;}

  20. 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); }

  21. 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);}

  22. Tree • Hierarchical data structure • Several real-world systems have hierarchical concepts • Physical and biological systems • Organizations • Many other applications • Sorting • Searching • Databases • Internet

  23. 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

  24. 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

  25. 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

More Related