I'm working on a project which simulates (very simple/watered down) a network. The basis of this is to learn how to implement a Queue
. I understand there are many ways to go about this, but I ultimately went with a LinkedList
mainly because of it's nature (FIFO) which makes it easy to utilize in a Queue
. I would appreciate any feedback on my implementation, and would also greatly appreciate with any critiques on my Queue
's toString()
, I know it is an unconventional way of doing it but I thought it would by a nice chance to work on recursion.
The data class in the this project is the Packet
. It represents a packet and code is as follows. I've omitted the getters and setters for brevity:
private static int packetCount; // Determined at the time of the objects creation [incremental] private int id; // Assigned by packetCount private int packetSize; // Size of the packet being sent private int timeArrive; // Creation "time stamp" private int timeToDest; // Number of simulation units it took for the packet to arrive to destination /** * Default constructor */ public Packet() { id = 0; packetSize = 0; timeArrive = 0; timeToDest = 0; } public Packet(int id, int packetSize, int timeArrive, int timeToDest) { this.id = id; this.packetSize = packetSize; this.timeArrive = timeArrive; this.timeToDest = timeToDest; } @Override public String toString() { return "Packet #" + id + " => arrive at simulation unit: " + timeArrive + " => packet size: " + packetSize; } }
Here is my Node
class:
public class Node { private Packet data; private Node next; private Node prev; public Node() { next = null; data = null; prev = null; } public Node(Packet data) { this.data = data; next = null; prev = null; } public Packet getData() { return data; } public Node getNext() { return next; } public Node getPrev() { return prev; } public void setData(Packet data) { this.data = data; } public void setNext(Node next) { this.next = next; } public void setPrev(Node prev) { this.prev = prev; } }
And my Queue
class which is actually a router within this simulation:
/** * Represents a router on the network [queue] */ public class Router{ private Node head; private Node tail; int size; public Router() { head = null; tail = null; size = 0; } public boolean isEmpty() { return head == null; } public int size() { return size; } public void enqueue(Packet packet) { Node node = new Node(packet); if(head == null) { head = node; tail = node; } tail.setNext(node); node.setPrev(tail); tail = node; size++; } public Packet dequeue(){ if(isEmpty()) { try { throw new Exception("Underflow"); } catch (Exception e) { e.printStackTrace(); } } Node ptr = head; head = ptr.getNext(); if(head == null) { tail = null; } size--; return ptr.getData(); } public Packet peek() { if(isEmpty()) { try { throw new Exception("Underflow"); } catch (Exception e) { e.printStackTrace(); } } return head.getData(); } public String recToString(StringBuilder strBlder, Node cursor) { if(cursor !=head) { strBlder.append(cursor.getData().toString() + "\n"); recToString(strBlder, cursor.getPrev()); } strBlder.append(cursor.getData().toString()); return strBlder.toString(); } @Override public String toString() { StringBuilder s = new StringBuilder(); return recToString(s, tail); } }