HI,
I have this assignment for school and I really need help on this one. I am supposed to write a program with a menu that gives me the possibility to choose what to do. The program will end by choice in menu.The list will not be sorted and numbers may show up several times in the list. I am not able to use array list These functions must be implemented in the program:
1. Delete an element at the beginning at a list
2. Add an element at the end of the list
3. Delete an element at the end of the list
4. Delete an element with known value (only the first)
5. Add an element after a element with known value (only the first)
6. Add an element before a element with known value (only the first)
7. print the length of the list
8. count the number of times a known number appears in the list. print the number of times
9. Print the whole list, maximum 5 elements pr. line
10. Delete the whole list. Number of elements deleted will be printed.
This is how far i got on the whole thing. It is 7-10 i need help with:
package oblig1;
class Node {
int element;
Node next;
public Node(int e, Node n) {
element = e;
next = n;
}
public int findElement() {
return element;
}
public Node findNext() {
return next;
}
}
package oblig1;
import oblig1.Node;
class SingleLink {
private Node head = null;
private int size = 0;
public int findNumberOf() {return size;}
public Node findhead() {return head;}
public void insertFront(int verdi) {
head = new Node(verdi, head);
++size;
}
public void insertBack(int verdi) {
if (head != null) {
Node this = head;
while (this.next != null) this = this.next;
this.next = new Node(verdi, null);
}
else head = new Node(verdi, null);
++size;
}
public Node remove(Node n) {
Node previous = null;
Node this = head;
while ( this != null && this != n) {
previous = this;
this = this.next;
}
// Now this points at the searched for and previous to the one in front
if (this != null) {
if (previous != null) previous.next = this.next;
else head = this.next;
this.next = null;
--size;
return this;
}
else return null;//Doesnt exist
}
public void DeleteNodeWithValue(value)
{
Node cp, pp;
cp = hode;
pp = null;
if(pp == null)
{
hode = cp.next
}
while(cp != null && cp. element != value)
{
pp = cp;
cp = cp.next
}
if (cp == null)
return;
pp.next = cp.next
else
{
size--;
}
public Node findNum(int Num) {
Node this = head;
if (Num < size) {
for (int i = 0; i < Num; ++i) this = this.next;
return this;
}
else return null;
}
public void delAll() {
head = null;
size = 0;
}
}