Skip to content

Commit

Permalink
Implemented a data structure to track mobiles
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishqg5325 committed Sep 3, 2018
0 parents commit cc0c4a7
Show file tree
Hide file tree
Showing 10 changed files with 947 additions and 0 deletions.
85 changes: 85 additions & 0 deletions Exchange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.util.*;

public class Exchange
{
int id;
Exchange parent;
ExchangeList children;
MobilePhoneSet residentSet;

public Exchange(int number)
{
id = number;
parent = null;
children = new ExchangeList();
residentSet = new MobilePhoneSet();
}

public boolean equals(Exchange e)
{
if(this == e) return true;
if(e == null) return false;
return (id == e.id);
}

public Exchange parent()
{
return parent;
}

public int numChildren()
{
return children.size();
}

public Exchange child(int i)
{
try
{
return children.getMember(i);
}
catch(RuntimeException e)
{
throw e;
}
}

public boolean isRoot()
{
return (parent == null);
}

public void addChild(Exchange e)
{
children.Insert(e);
}

public int[] getMobiles()
{
return residentSet.getMobiles();
}

public MobilePhone getMobile(int m)
{
return residentSet.getMobile(m);
}

public RoutingMapTree subtree(int i)
{
RoutingMapTree tree = new RoutingMapTree();
try
{
tree.root = this.child(i);
return tree;
}
catch(RuntimeException e)
{
throw e;
}
}

public MobilePhoneSet residentSet()
{
return residentSet;
}
}
99 changes: 99 additions & 0 deletions ExchangeList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.util.*;

public class ExchangeList
{
Node head;
static class Node
{
Exchange exchange;
Node next, prev;
Node(Exchange e)
{
exchange = e;
next = prev = null;
}
}

public boolean IsEmpty()
{
return (head==null);
}

public boolean IsMember(Exchange e)
{
Node temp = head;
while(temp != null)
{
if(temp.exchange.equals(e))
return true;
temp = temp.next;
}
return false;
}

public void Insert(Exchange e)
{
Node tmp = new Node(e);
if(head==null)
{
head = tmp;
return;
}
Node n = head;
while(n.next != null)
n = n.next;
tmp.prev = n;
n.next = tmp;
}

public void InsertHead(Exchange e)
{
Node tmp = new Node(e);
tmp.next = head;
if(head != null)
head.prev = tmp;
head = tmp;
}

public void Delete(Exchange e)
{
Node temp = head;
while(temp != null)
{
if(temp.exchange.equals(e))
{
Node t = temp.next;
if(temp.prev != null) temp.prev.next = t;
else head = t;
if(t != null) t.prev = temp.prev;
return;
}
temp = temp.next;
}
throw new RuntimeException("Error - Exchange " + e.id + " is not present in list");
}

public int size()
{
Node tmp = head;
int count = 0;
while(tmp != null)
{
count++;
tmp = tmp.next;
}
return count;
}

public Exchange getMember(int i)
{
if(this.size() > i && i>=0)
{
Node temp = head;
for(int j=0; j<i; j++)
temp = temp.next;
return temp.exchange;
}
throw new RuntimeException("Error - Index out of bound");
}
}
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all:
javac assn3checker.java
javac Myset.java
javac MobilePhone.java
javac MobilePhoneSet.java
javac Exchange.java
javac ExchangeList.java
javac RoutingMapTree.java
java assn3checker

clean:
find . -name "*.class" -type f -delete
clear
48 changes: 48 additions & 0 deletions MobilePhone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.*;

public class MobilePhone
{
int id;
boolean status;
Exchange exchange;

public MobilePhone(int number)
{
id = number;
status = false;
exchange = null;
}

public boolean equals(MobilePhone m)
{
if(this == m) return true;
if(m == null) return false;
return (id == m.id);
}

public int number()
{
return id;
}

public boolean status()
{
return status;
}

public void switchOn()
{
status = true;
}

public void switchOff()
{
status = false;
}

public Exchange location()
{
if(status) return exchange;
throw new RuntimeException("Error - Mobilephone " + number() + " is switched off");
}
}
59 changes: 59 additions & 0 deletions MobilePhoneSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.util.*;

public class MobilePhoneSet
{
Myset mobileSet;

public MobilePhoneSet()
{
mobileSet = new Myset();
}

public void Insert(MobilePhone mobile)
{
mobileSet.Insert(mobile);
}

public void Delete(MobilePhone mobile)
{
try
{
mobileSet.Delete(mobile);
}
catch(RuntimeException e)
{
throw e;
}
}

public MobilePhone getMobile(int m)
{
int n = mobileSet.size();
Object[] objs = mobileSet.getItems();
MobilePhone mobile;
for(int i=0; i<n; i++)
{
mobile = (MobilePhone)objs[i];
if(mobile.number() == m) return mobile;
}
return null;
}

public int[] getMobiles()
{
int n = mobileSet.size();
Vector<Integer> v = new Vector<Integer>();
MobilePhone m;
Object[] objs = mobileSet.getItems();
for(int i=0; i<n; i++)
{
m = (MobilePhone)objs[i];
if(m.status) v.add(m.number());
}
n = v.size();
int[] mobiles = new int[n];
for(int i=0; i<n; i++)
mobiles[i] = v.get(i);
return mobiles;
}
}
Loading

0 comments on commit cc0c4a7

Please sign in to comment.