0% found this document useful (0 votes)
45 views25 pages

Data Structures: Lakshmish Ramaswamy

The document discusses various data structures including linked lists, stacks, queues, and trees. It provides code examples for implementing linked lists, doubly linked lists, stacks, queues using arrays and linked lists, and circular queues. It also defines different types of trees and their properties.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views25 pages

Data Structures: Lakshmish Ramaswamy

The document discusses various data structures including linked lists, stacks, queues, and trees. It provides code examples for implementing linked lists, doubly linked lists, stacks, queues using arrays and linked lists, and circular queues. It also defines different types of trees and their properties.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

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
Queue
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
0 Front
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
Tree – Informal Definition
• Each node has a single parent node
except for root which has no parent

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

You might also like