/
LinkedListIngredient.java
152 lines (138 loc) · 3.78 KB
/
LinkedListIngredient.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
A LinkedListIngredient class in which a LinkedListIngredient object can be created
@version 1.0 2015-03-02
@author Vinoth Maruthalingam
*/
public class LinkedListIngredient
{
IngredientNode header; // Instance fields
public LinkedListIngredient() // Constructor
{
header = null;
}
public void addAtFront(Ingredient info)
{
if (header == null)
{
header = new IngredientNode(info, null);
}
else
{
IngredientNode temp = header; // temp gets the value of the address that header is pointing to.Also known as the first node
header = new IngredientNode(info, temp); // header now points to a new node with data info and pointer temp which was the first node
}
}
public void deleteAtFront()
{
if (header != null && header.getNext() != null)
{
header = header.getNext();
//header has null data but some reference point. The get symbol '=' changes the reference point. header.getNext() returns the second node in the list
}
}
public void delete(Ingredient n)
{
IngredientNode current = header;
IngredientNode previous = null;
while (current != null)
{
if (current.ingredient.equals(n))
{
if (previous == null) // This means that the first node is the target node
{
header = header.getNext();
previous = current;
}
else
{
previous.setNext(current.getNext());
current = null;
}
}
else
{
previous = current;
current = current.next;
}
}
}
public void push(Ingredient a) // For stacks, where a node of data a is inserted at the end of the list
{
if (header == null)
{
header = new IngredientNode(a, null); // header points to the new node
}
else
{
IngredientNode current = header;
while (current.getNext() != null) // second node doesn't equal to null
{
current = current.getNext(); // second node gets third node...third node gets fourth node...until the LAST NODE (current.getNext() == null)
}
current.setNext(new IngredientNode(a, null)); //Set reference for last node to the new node
}
}
public void pop() // For stacks, where a node at the end of the list is deleted
{
if (header.getNext() == null) // if there is not second node AKA one node in the list
{
header = null; // header points to nothing
}
else if (header.getNext() != null) //If there is a second node AKA more than two nodes in the list
{
header = header.getNext(); // header points to the second node in the list.
}
}
public void queue(Ingredient b) // For queues, where a node of data b is added at the end of the list. SAME AS PUSH
{
push(b);
}
public void dequeue() // For queues, where a node at the front is deleted. SAME AS deleteAtFront()
{
deleteAtFront();
}
public String printData()
{
IngredientNode current = header;
String s = "";
while (current!= null)
{
s = s + "\n" + current.getIngredient();
current = current.getNext();
}
return s;
}
public Ingredient[] arrayOfIngredientsFromList()
{
IngredientNode current = header;
Ingredient array[] = new Ingredient[this.size()];
for (int i = 0; i < this.size(); i++)
{
array[i] = current.ingredient;
current = current.next;
}
return array;
}
public int size()
{
int size = 0;
IngredientNode current = header;
while(current.next != null)
{
current = current.next;
size++;
}
return size+1;
}
public int sumOfStock(IngredientNode n)
{
if (n == null)
{
return 0;
}
else
{
return n.getIngredient().getStock() + sumOfStock(n.getNext());
}
}
}