Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Person{
- private String first;
- private String last;
- public Person(String x, String y){
- first=x;
- last=y;
- }
- public String toString(){
- return(first+" "+last);
- }
- }
- class LinkedListQueue {
- //instance variables
- private Node start;
- private class Node {
- private Object data;
- private Node next;
- Node(Object a){
- data=a;
- next=null;
- }
- }
- public String toString() {
- Node b = start;
- /*while (b != null) {
- b = b.next;
- toString();
- }\
- */
- return ("Name:" + b.data.toString() + "\n");
- }
- public void add(Object element) {
- Node second = new Node(element);
- if (start == null) {
- start = new Node(element);
- return;
- }
- second.next = null;
- Node last = start;
- while (last.next !=null){
- last=last.next;
- }
- last.next=second;
- return;
- }
- public int queueSize(){
- int s = 0;
- Node n = start;
- while(n != null){
- s++;
- n = n.next;
- }
- return s;
- }
- //remove from the front of queue, return null if empty
- public String remove(){
- Node temp=start;
- start=start.next;
- return temp.data.toString();
- }
- //returns the first item without removing it, null if empty
- public String peek(){
- if(start==null)
- return null;
- return start.data.toString()+"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement