-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
87 lines (74 loc) · 1.81 KB
/
Copy pathLinkedQueue.java
File metadata and controls
87 lines (74 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package queue;
/**
*
* @author Zohaib Hassan Soomro
*/
public class LinkedQueue implements Queue {
private int size;
private Node head = new Node(null);
private class Node {
private Object object;
private Node previous = this;
private Node next = this;
public Node(Object obj) {
this.object = obj;
}
public Node(Object object, Node previous, Node next) {
this.object = object;
this.previous = previous;
this.next = next;
}
}
@Override
public Object first() {
if (this.isEmpty()) {
throw new IllegalStateException("Queue is empty!");
}
return head.next.object;
}
@Override
public Object remove() {
if (this.isEmpty()) {
throw new IllegalStateException("Queue is empty!");
}
Object firstElement = head.next.object;
head.next = head.next.next;
head.next.previous = head;
size--;
return firstElement;
}
@Override
public void add(Object obj) {
head.previous = head.previous.next = new Node(obj, head.previous, head);
size++;
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
public static void main(String[] args) {
LinkedQueue carsBought = new LinkedQueue();
LinkedQueue carsSold = new LinkedQueue();
carsBought.add(1000);
carsBought.add(2000);
carsBought.add(500);
carsSold.add(1100);
carsSold.add(1999);
carsSold.add(600);
int totalProfit = 0;
int size = carsBought.size();
for (int i = 0; i < size; i++) {
totalProfit += (int) carsSold.remove() - (int) carsBought.remove();
}
System.out.println("Total Profit On these Three Cars: "+totalProfit);
}
}