Download presentation
Presentation is loading. Please wait.
Published byRandell Fields Modified over 9 years ago
1
Data Structures Lakshmish Ramaswamy
2
Removing Element public Object remove(int index){ Object retobj; if(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 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 0 1 2 C-1 Front 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
Tree – Informal Definition Each node has a single parent node except for root which has no parent 9 6 5 3 8 7 root parent nodes children nodes leaf nodes
24
Types of Trees 6 382 7 6 3 7 8 5 7 4 8 a tree a binary tree a Binary search Tree not a tree 7 5 6 a tree and a... 9 6 5 3 8 7
25
Formal Definition Graph is a set of vertices V = {v 1, v 2,,, v n } and a set of edges connecting them E = {e 1, e 2, …, e M } –Each edge is a tuple of vertices e l = (v i, v j ) 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.