Question
java oop data structures and algorithms how would i write an analysis and algorithm for a linked list queue? for that section only. example: the
java oop data structures and algorithms
how would i write an analysis and algorithm for a linked list queue? for that section only.
example:
the code may not be necessary
public interface Queue { // most important methods public void enqueue(Object n); // add an object at the rear of the queue public Object dequeue(); // remove an object from the front of the queue // others public boolean isEmpty(); // true if queue is empty public boolean isFull(); // true if queue is full (if it has limited storage) public Object front(); // examine front object on queue without removing it }
public class SLinkedList { protected Node head; // head node of the list protected Node tail; // last position in the list protected Node curr; // node referencing current position in the list protected long size; // number of nodes in the list /** Default constructor that creates an empty list. */ public SLinkedList() { curr = tail = head = null; size = 0; } public long size() { return size; } public boolean isEmpty() { return (head == null); } public Object getCurr() { if (curr == null) // Verify that there is a current node return null; return curr.getElement(); } public boolean gotoHead() { if (isEmpty()) return false; curr = head; return true; } public boolean gotoNext() { if (curr == tail) return false; curr = curr.getNext(); return true; } public boolean gotoTail() { if (isEmpty()) return false; curr = tail; return true; } public void insertNext(Object el) { if (head == null) { insertHead(el); // If haven't inserted a head, do so now (for convenience) return; } Node newnode = new Node(el, curr.getNext()); // create new node with its next node equal to curr's next node curr.setNext(newnode); // update the next node of the current node to point to the new one size++; // update the tail if at the end of the list if (tail == curr) tail = newnode; // make this new node the current one curr = newnode; } public void deleteNext() { if (curr == null || curr.getNext() == null) return; // no next: list empty or already at end // update the tail if the node we are deleting is the tail if (tail == curr.getNext()) tail = curr; curr.setNext(curr.getNext().getNext()); // set curr's next equal to the next node's next // Note: Garbage collector will automatically clear up the node no longer referenced size--; } public void insertHead(Object el) { Node oldhead = head; head = new Node(el, oldhead); size++; curr = head; if (size == 1) // if this is the first node, it is both head and tail tail = head; } public void deleteHead() { if (head == null) return; // list already empty head = head.getNext(); size--; curr = head; if (size == 0) // if this was the only node, get rid of ref to tail as well as head tail = null; } }
//This defines a single node used in a Singly Linked List.
public class Node
{ // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node() { this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; } }
////////////////////////LLQueue.java///////////////////////////////
public class LLQueue implements Queue{
private SLinkedList queue;
public LLQueue() {
queue = new SLinkedList();
}
@Override
public void enqueue(Object n) {
queue.gotoTail();
queue.insertNext(n);
}
@Override
public Object dequeue() {
queue.gotoHead();
Object o = queue.getCurr();
queue.deleteHead();
return o;
}
@Override
public boolean isEmpty() {
return queue.isEmpty();
}
@Override
public boolean isFull() {
return false;
}
@Override
public Object front() {
queue.gotoHead();
return queue.getCurr();
}
}
/////////////////////Test.java//////////////////////////////
public class Test {
public static void main(String []args) {
LLQueue l = new LLQueue();
System.out.println("IsEmpty: "+l.isEmpty());
l.enqueue("one");
l.enqueue("two");
l.enqueue("three");
l.enqueue("four");
l.enqueue("five");
l.enqueue("six");
System.out.println("After Inserting elements");
System.out.println("IsEmpty: "+l.isEmpty());
while(!l.isEmpty()){
String s = (String)l.dequeue();
System.out.println(s);
}
System.out.println("After Deleting elements");
System.out.println("IsEmpty: "+l.isEmpty());
}
}
this is the output:
Statement: Design, implement and test a Java program as specified in Assignment 1, Q1 Analysis & Algorithm: 1. 2. Start Ask user to enter a number:1 for square; 2 for rectangle; 3 for triangle a. InputDialog returns string: set return value to input: String b. Convert input to choice: int c. If choice not in range 1-3, repeat Step 2 3. Test value of choice a. If 1: i. ShapeType Square ii. Ask for length of square (convert String to double) ii. Area-lengthlength b. If 2: i. ShapeType-Rectangle ii. Ask for length and width ii. Area-lengthwidth c. If3: i. ShapeType-Triangle ii. Ask for base and height ii. Area-0.5 base *height 4. 5. Display: The area of Shape Type is Area End. Analysis Notes: I am validating that choice is in range 1-3 To get the full marks for efficiency, I should also validate that length, width, base and height are greater than 0 Implementation See Attached. Testing: Tests: No. Test Expected Asks again Correct option selected Area of Area of rectangle is 20 As expected Result Asks again Correct As expected Choice-1, 0,4: selected 2 Choice ,2, 3 3 | Choice 1 : length 4 is 16 Choice-2 length 4, width 5 Choice-3: base 4, width- Area of triangle is 10 As expected
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started