I made a singly linked list program that makes a singly linked list, lets you add any
Question:
I made a singly linked list program that makes a singly linked list, lets you add any item, and it goes through the list. I need to add these functions to my code:
- Return the number of items in the set.
- Determine if an item is a member of the set.
- Return a reference to a specific node in the set (given an item)
- Determine the union and the intersection (i.e. set of all common distinctelements) of two sets.
my code:
public class practice { // = a generic (this means the singly linked list can be created with any element i.e a string, int, etc)
//singly linked lists are composed of nodes
//implement a node
static class Node{
T element;
//Linked list has a node which contains the element itself and then it points to the next element
private Node next;//pointer to the next node
public Node(T e, Node n) { //creating a node with its own element and a pointer (Node) to another type of node (n)
element = e;
next = n;
}
//we want to retrieve the element back from the node above so we can display whatever data is in it (string,int,etc.)
public T getElement() {
return element;
}
//next we need to retrieve the pointer to return the next node
public Node getNext(){
return next;
}
//if we need to change what the pointer is pointing to:
public void setNext(Node n) {//this is going to take a node of the same parameter of a node of the same type
}
}
//singly linked list implementation
//Initialization stage of the singly linked list so both head and tail will be empty
private Node head = null;
private Node tail = null;
private int size = 0; //keeping track of the size of linked list using (size)
public practice() {}; //(name of the file) empty initializer when I don't have anything going on
public void printList(){
Node n = head;
while (n != null) {
System.out.print(n.element + " ,");
n = n.next;
}
}
public int size() {//getting the size back from the singly linked list
return size;
}
public boolean isEmpty() {//return true if the list is empty
return size == 0;
}
public T first(){ //getting the first element in the list
if(isEmpty()) { //if size == o then return nothing
return null;
}//else
return head.getElement(); //if not empty then return the value of the head node
}
public T last(){ //getting the value of the tail node
if(isEmpty()) {
return null;
}//else
return tail.getElement();
}
//this function allows you to add an element to the list itself. You can either add it at the head (beginning) of the linked list and the end (tail) of the linked list
public void addFirst(T e) {//Remember the T = generic, e = element being added
head = new Node<>(e, head);//create a new node which has our node (e) being added and it will point to the head.
if(size == 0) {//remember they (tail/head) have been originally initialized to NULL
tail = head;
}
size++;//size increased
System.out.println("Added head node with " + head.getElement() + " element.");
}
public void addLast(T e) {//adding element at the end (tail) of linked list
Node newNode = new Node<>(e, null);//tail is originally == NULL
if(isEmpty()) {//if we have no other elements within our list the head is going to be the new element added
head = newNode;
}else {
tail.setNext(newNode);
}
tail = newNode;
size++;
System.out.println("Added tail node with " + tail.getElement() + " element.");
}
public T removeElement(T e) {//e = element we are looking to remove
Node current = head;
Node previous = head;
int position = 0; //this helps keep track of how many steps have been made
while(current !=null && current.getElement() != e) {// while we are NOT at the end of the list and we have NOT found the element we are looking for:
previous = current; //moving the two references we have
current = current.getNext();
position++;
}
if(current == null) { //if the element isn't found
return null;
}else {
if(head == current) {//if the element we are looking for is at the head
head = current.getNext(); //shift the head over
}else if(tail == current) {//if the element we are looking for is at the tail
tail = previous;
tail.setNext(null);
}else {//else we will set the previous to be the next
previous.setNext(current.getNext());
}
System.out.println("found and removed node at position " + position);
size--; //decrement the size of the linked list
return current.getElement();
}
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill