public class List {
private class Node {
public Node next = null;
Object element;
Node text;
Node(Object element, Node prevNode) {
this.element = element;
prevNode = this;
}
Node(Object element) {
this.element = element;
element = null;
}
}
private Node head;
private Node tail;
private int count;
public List() {
this.head = null;
this.tail = null;
this.count = 0;
}
public void add(Object item) {
if (head == null) {
// We have empty list
head = new Node(item);
tail = head;
} else {
// We have non-empty list
Node newNode = new Node(item, tail);
tail = newNode;
}
count++;
}
public Object remove(int index) {
if (index >= count || index < 0) {
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
// Find the element at the specified index
int currentIndex = 0;
Node currentNode = head;
Node prevNode = null;
while (currentIndex < index) {
prevNode = currentNode;
currentNode = currentNode.next;
currentIndex++;
count--;
if (count == 0) {
head = null;
tail = null;
} else if (prevNode == null) {
head = currentNode.next;
} else {
prevNode.next = currentNode.next;
}
}
return currentNode.element;
}
public int remove(Object item) {
// Find the element containing searched item
int currentIndex = 0;
Node currentNode = head;
Node prevNode = null;
while (currentNode != null) {
if ((currentNode.element != null && currentNode.element
.equals(item))
|| (currentNode.element == null)
&& (item == null)) {
break;
}
prevNode = currentNode;
currentNode = currentNode.next;
currentIndex++;
}
if (currentNode != null) {
// Element is found in the list. Remove it
count--;
if (count == 0) {
head = null;
tail = null;
} else if (prevNode == null) {
head = currentNode.next;
} else {
prevNode.next = currentNode.next;
}
return currentIndex;
} else {
// Element is not found in the list
return -1;
}
}
public int indexOf(Object item) {
int index = 0;
Node current = head;
while (current != null) {
if ((current.element != null && current.element.equals(item))
|| (current.element == null) && (item == null)) {
return index;
}
current = current.next;
index++;
}
return -1;
}
public boolean contains(Object item) {
int index = indexOf(item);
boolean found = (index != -1);
return found;
}
public Object elementAt(int index) {
if (index >= count || index < 0) {
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
Node currentNode = this.head;
for (int i = 0; i < index; i++) {
currentNode = currentNode.next;
}
return currentNode.element;
}
public int getLength() {
return count;
}
public static void main(String[] args) throws NullPointerException {
List shoppingList = new List();
shoppingList.add("Milk");
shoppingList.add("Honey");
shoppingList.add("Olives");
shoppingList.add("Beer");
shoppingList.remove("Olives");
System.out.println("We need to buy:");
for (int i = 0; i < shoppingList.getLength(); i++) {
System.out.println(shoppingList.elementAt(i));
}
System.out.println("Do we have to buy Bread? "
+ shoppingList.contains("Bread"));
}
}
Its giving me that in maina and index of have nullpointer.
Where is my mistake ?
And i want to know how to fix this problem. Thank you.
I would be very grateful if you could give me some feedback for code.
You do not set next when you create your Nodes, so next is always null.
This
Node(final Object element, Node prevNode) {
this.element = element;
prevNode = this;
}
should be
Node(final Object element, Node prevNode) {
this.element = element;
prevNode.next = this;
}
I understand there's something in your code that's giving you nullpointerException when you run the program, but there's no need of it being in the main method, any method called by main may be giving the exception. Still, try this:
Replace this line:
for (int i=0; i<shoppingList.getLength(); i++) {
by this one:
for (int i=0; i < shoppingList.length(); i++) {
if .length() doesn't work, maybe .size() will do.
for (int i=0; i<shoppingList.size(); i++) {
If you are using eclipse, netbeans or any other environment, post which line is throwing the NullPointerException. We can't help you unless you give us more data.
Related
I'm asked this question and I'm not really sure what it means,
In our LinkedList class, the Node class is an internal class. Briefly
describe two things that would have to be coded differently if the
Node class was a separate class from the LinkedList class.
Here's the code for my LinkedList class:
public class LinkedList<T> implements LinkedListInterface<T> {
private Node head;
private Node tail;
private int count;
public LinkedList() {
head = null;
tail = null;
count = 0;
}
class Node {
T data;
Node next;
Node(T data) {
this.data = data;
next = null;
}
}
public Node getHead() {
return head;
}
public int add(T item) {
Node newNode = new Node(item);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
count++;
return count;
}
public void add(T item, int pos) throws ListException {
if (pos < 1 || pos > count + 1) {
throw new ListException("Invalid position to insert at");
}
Node newNode = new Node(item);
if (count == 0) {
add(item);
} else if (pos == count + 1) {
add(item);
} else if (pos == 1) {
newNode.next = head;
head = newNode;
count++;
} else {
Node prev = jump(pos - 2);
newNode.next = prev.next;
prev.next = newNode;
count++;
}
}
private Node jump(int numJumps) {
Node nd = head;
for (int i = 0; i < numJumps; i++) {
nd = nd.next;
}
return nd;
}
public LinkedList<T> combine(LinkedList<T> obj2) {
Node list1 = this.getHead();
Node list2 = obj2.getHead();
if (list2 == null) {
return this;
}
if(list1==null) {
return obj2;
}
Node temp=list1;
while(temp.next!=null) {
temp = temp.next;
}
temp.next=list2;
return this;
}
public int contains(T item) {
Node nd = this.head;
for (int pos = 1; pos <= count; pos++) {
if (nd.data.equals(item)) {
return pos;
}
nd = nd.next;
}
return 0;
}
public LinkedList<T> copy() {
LinkedList<T> l = new LinkedList<T>();
try {
for (int pos = 1; pos <= count; pos++)
l.add(retrieve(pos));
}
catch(ListException e) {
System.out.println("Should not occur (Copy)");
}
return l;
}
public boolean equals(Object list) {
if (list == null) {
return false;
}
LinkedList myLinkedList = (LinkedList)list;
if(myLinkedList.getClass() != this.getClass()) {
return false;
}
if(myLinkedList.length() != this.length()) {
return false;
}
try{
for (int pos = 1; pos <= count; pos++) {
if(!myLinkedList.retrieve(pos).equals(this.retrieve(pos))) {
return false;
}
}
}
catch (ListException e) {
System.out.println("Should not occur");
}
return true;
}
public boolean isEmpty() {
return length() == 0;
}
public int length() {
return count;
}
#SuppressWarnings("unchecked")
public T remove(int pos) throws ListException {
if (pos < 1 || pos > count) {
throw new ListException("Invalid position to remove from");
}
Node removedItem = null;
if (count == 1) {
removedItem = head;
head = null;
tail = null;
} else if (pos == 1) {
removedItem = head;
head = head.next;
} else if (pos == count) {
removedItem = tail;
Node prev = jump(pos - 2);
prev.next = null;
tail = prev;
} else {
Node prev = jump(pos - 2);
removedItem = prev.next;
prev.next = prev.next.next;
}
count--;
return removedItem.data;
}
public int remove(T item) {
int numRemoved = 0;
int pos = -1;
try {
while ((pos = contains(item)) > 0) {
remove(pos);
numRemoved++;
}
} catch (ListException e){
System.out.print(e);
}
return numRemoved;
}
int find_length(Node list){
int count = 0;
while (list!=null) {
count++;
list=list.next;
}
return count;
}
public void replace(T item, int pos) throws ListException {
if (pos < 1 || pos > count + 1) {
throw new ListException("Invalid position to replace at");
}
Node list = this.getHead();
int length = find_length(list);
if(pos<1||pos>length){
return;
}
while(list!=null){
pos--;
if(pos==0){
list.data=item;
return;
}
list=list.next;
}
}
public T retrieve(int pos) throws ListException {
T item = null;
if (pos < 1 || pos > count) {
throw new ListException("Invalid position to retrieve from");
}
if (pos == 1) {
return head.data;
}
Node prev = jump(pos - 2);
item = prev.next.data;
return item;
}
public LinkedList<T> reverse() {
LinkedList<T> l = new LinkedList<T>();
try {
for (int pos = count; pos > 0; pos--)
l.add(retrieve(pos));
}
catch (ListException e) {
System.out.println("Should not occur (Reverse)");
}
return l;
}
public String toString() {
String temp = "";
Node nd = head;
while (nd != null) {
temp += nd.data + "-";
nd = nd.next;
}
return temp;
}
}
What I'm trying to get is the since the Node class is located in the same class as my LinkedList class, it is referred to as an internal class. If it were to be separate, would I have to implement a Node class similarly to the LinkedListInterface<T> class? What else would have to be coded differently?
I would like to write a method public void reverseFrom(int index) which reverses a list from the given index.
I would like to only use the LinkedList class below.
public class LinkedList {
public Node head = null;
public class Node {
public int value;
public Node next;
Node(int value, Node next) {
this.value = value
this.next = next;
}
}
}
I have a method to reverse a LinkedList:
public void reverse() {
Node tmp = head;
Node prev = null;
Node nextNode = null;
while(tmp != null) {
nextNode = tmp.next;
tmp.next = prev;
prev = tmp;
tmp = nextNode;
}
head = prev;
}
So far, for the reverseFrom method, I have:
public void reverseFrom(int index) {
if(index == 0) {
reverse();
} else if (index == 1) {
return;
} else {
Node tmp = head;
for(int i = 0; i < index; i++) {
tmp = tmp.next;
}
Node newHead = tmp;
/*** Node prev = null;
Node nextNode = null;
while(newHead != null) {
nextNode = newHead.next;
newHead.next = prev;
prev = newHead;
newHead = nextNode;
}
newHead = prev; ***/
}
}
I have tried using the code from reverse() but it does not work (that which is commented out).
How can I then reverse the list from newHead?
You could base the logic on the reverse method, but use an extra reference to the node that is at index - 1. During the loop decrement index. As long as it is positive, don't change the next reference of the current node. When it hits zero, take note of where we start the reversal, and as the current node will become the very last one, set its next reference to null. When index is negative, perform the usual reversal logic.
After the loop, check whether we need to change the head or whether we need to attach the reversed part to the node at index-1:
public void reverseFrom(int index) {
Node tmp = head;
Node prev = null;
Node nextNode = null;
Node tail = null; // node at index - 1
while (tmp != null) {
nextNode = tmp.next;
if (index == 0) {
// We arrived at the part that needs reversal
tmp.next = null;
tail = prev;
} else if (index < 0) {
// Perform normal reversal logic
tmp.next = prev;
}
index--;
prev = tmp;
tmp = nextNode;
}
if (tail == null) {
head = prev;
} else {
tail.next = prev;
}
}
public class ReverseList<T> extends LinkedList<T>{
public void reverseFrom(int idx) {
if (idx < 0 || idx > size()) {
return;
}
Stack<T> stack = new Stack<T>();
while (idx < size()) {
stack.push(remove(idx));
}
while (!stack.isEmpty()) {
add(stack.pop());
}
}
/******* Alternative slution ***************/
public void reverseFrom(int idx) {
if (idx < 0 || idx > size()) {
return;
}
reverseFromInternal(idx, size()-1);
}
private void reverseFromInternal(int a, int b) {
if (a >= b) {
return;
}
T far = remove(b);
T near = remove(a);
add(a, far);
add(b, near);
reverseFromInternal(a + 1, b - 1);
}
/****************************************/
public static void main(String [] args) {
ReverseList<String> list = new ReverseList<>();
for (int i = 0; i < 10; i++) {
list.add(Integer.toString(i));
}
list.reverseFrom(5);
System.out.println(list);
}
}
public void reverseFrom(int index) {
if (index == 0) {
reverse();
return;
}
Node tmp = head;
Node previous = null;
for (int i = 0; i < index; i++) {
previous = tmp;
tmp = tmp.next;
}
Node newHead = tmp;
LinkedList subLinkedList = new LinkedList();
subLinkedList.head = newHead;
subLinkedList.reverse();
previous.next = subLinkedList.head;
}
I'm practicing with LinkLists and trying to make a remove method from scratch that removes the node in the linked list and also updates index values that I have associated with each node. I have tried some logic for different cases but they don't seem right. The node index updating for each case doesn't seem right either.
LinkedList Class instance variables
public class LinkedList<E> implements DynamicList<E> {
LLNode<E> head;
LLNode<E> tail;
int llSize;
LinkedList(){
this.head = null;
this.tail = null;
this.llSize =0;
}
Node class
public class LLNode<E>{
E obj;
LLNode<E> previousPointer;
LLNode<E> nextPointer;
int index;
public LLNode(E obj){
this.obj = obj;
this.index=0;
}
public E getObj() {
return obj;
}
public LLNode<E> getPreviousPointer() {
return previousPointer;
}
public LLNode<E> getNextPointer() {
return nextPointer;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
remove method
#Override
public E remove(E data) {
LLNode<E> nodeToRemove = new LLNode<>(data);
if(head == null){
return null;
}
if(head.getObj().equals(nodeToRemove.getObj())){
head = nodeToRemove.nextPointer;
//not sure if this works....
LLNode<E> currentNode = this.head;
for(int i=0; i < size()-2; i++){
currentNode.setIndex(i);
currentNode = currentNode.nextPointer;
}
return nodeToRemove.getObj();
}
//this is not right
if(nodeToRemove.nextPointer != null){
nodeToRemove.nextPointer.previousPointer = nodeToRemove.previousPointer;
// do I have to update the nodeToRemove's previous to point to its new next?
LLNode<E> currentNode = nodeToRemove;
int startIndex = size()- countFrom(nodeToRemove);
for(int i = 0; i < countFrom(nodeToRemove); i++){
currentNode.setIndex(startIndex);
startIndex++;
currentNode = currentNode.nextPointer;
}
return nodeToRemove.getObj();
}
if(nodeToRemove.previousPointer != null){
nodeToRemove.previousPointer.nextPointer = nodeToRemove.nextPointer;
//node index updating
LLNode<E> currentNode = nodeToRemove;
int startIndex = size()- countFrom(nodeToRemove);
for(int i = 0; i < countFrom(nodeToRemove); i++){
currentNode.setIndex(startIndex);
startIndex++;
currentNode = currentNode.nextPointer;
}
return nodeToRemove.getObj();
}
this.llSize--;
return null;
}
countFrom method
private int countFrom(LLNode<E> startNode){
int count=0;
while(startNode != null){
startNode = startNode.nextPointer;
count++;
}
return count;
}
Let me know if more code is needed.
You should only need to renumber the nodes that follow after the removed node.
So:
public E remove(E data) {
LLNode<E> nodeToRemove = new LLNode<>(data);
if(head == null){
return null;
}
// Adapt head and tail when necessary
if(tail.getObj().equals(nodeToRemove.getObj())){
tail = nodeToRemove.previousPointer;
}
if(head.getObj().equals(nodeToRemove.getObj())){
head = nodeToRemove.nextPointer;
}
// Rewire the list so it skips the node to remove
if(nodeToRemove.nextPointer != null){
nodeToRemove.nextPointer.previousPointer = nodeToRemove.previousPointer;
}
if(nodeToRemove.previousPointer != null){
nodeToRemove.previousPointer.nextPointer = nodeToRemove.nextPointer;
}
// Renumber the nodes that follow the removed node.
// (Those before it retain their index)
LLNode<E> currentNode = nodeToRemove.nextPointer;
int index = nodeToRemove.getIndex();
while (currentNode != null) {
currentNode.setIndex(index);
index++;
currentNode = currentNode.nextPointer;
}
this.llSize--;
return nodeToRemove.getObj();
}
I figured it out and this seems to be working well now, here are my changes.
get() Method
Added a count variable to compare with the passed parameter also my while comparison was checking current.next and not current and was getting null pointer.
public E get(int index) {
LLNode<E> current = this.head;
int count = 0;
while(current != null){
if(index == count) {
return current.getObj();
}
current = current.nextPointer;
count++;
}
return null;
}
remove() Method
I used a while instead of the previous logic. and simplified the logic quite a bit.
#Override
public E remove(E data) {
LLNode<E> current = this.head;
while(current != null){
if(current.getObj().equals(data)) {
LLNode<E> prev = current.previousPointer;
LLNode<E> next = current.nextPointer;
if(prev != null) {
prev.nextPointer = next;
}
if(next != null) {
next.previousPointer = prev;
}
llSize--;
return current.getObj();
}
current = current.nextPointer;
}
return null;
}
I need help with my add method in java. It works with DoublyLinked List.
I am implementing a cyclic DoublyLinkedList data structure. Like a singly
linked list, nodes in a doubly linked list have a reference to the next node, but unlike a singly linked list, nodes in a doubly linked list also have a reference to the previous node. Additionally, because the list is "cyclic", the "next" reference in the last node in the list points to the first node in the list, and the "prev" reference in the first node in the list points to the last node in the list.
What the method is suppose to do is insert the value parameter at the specified index in the list. Be sure to address the case in which the list
is empty and/or the added element is the first in the list. If the index parameter is invalid, an IndexOutOfBoundsException should be thrown.
Here is my code below:
public class DoublyLinkedList<E>
{
private Node first;
private int size;
#SuppressWarnings("unchecked")
public void add(E value)
{
if (first == null)
{
first = new Node(value, null, null);
first.next = first;
first.prev = first;
}
else
{
first.prev.next = new Node(value, first, first.prev);
first.prev = first.prev.next;
}
size++;
}
private class Node<E>
{
private E data;
private Node next;
private Node prev;
public Node(E data, Node next, Node prev)
{
this.data = data;
this.next = next;
this.prev = prev;
}
}
Here is the method where it fails. I will comment the line where I'm stuck on, but other than that, what is done in the previous lines is correct from what I heard.
#SuppressWarnings("unchecked")
public void add(int index, E value)
{
if(index < 0)
{
throw new IndexOutOfBoundsException();
}
if(index > size)
{
throw new IndexOutOfBoundsException();
}
if (first.data == null)
{
throw new NullPointerException();
}
if (index == 0)
{
first = new Node(value, null, null);
first.next = first;
first.prev = first;
}
else
{
Node current = first;
for (int i = 0; i < index; i++)
{
current = current.next;
}
current.prev.next = new Node(value, current, current.prev); // This is the line where I get lost on.
current.prev = current.prev.next;
}
size++;
}
The rest of my code is here. Please focus on the method I'm working on. thank you!
#SuppressWarnings("unchecked")
public void remove(int index)
{
if(index < 0)
{
throw new IndexOutOfBoundsException();
}
if(index > size)
{
throw new IndexOutOfBoundsException();
}
if (first.data == null)
{
throw new IndexOutOfBoundsException();
}
else if (index == 0)
{
first = first.next;
}
else
{
Node current = first.next;
for (int i = 0; i < index; i++)
{
current = current.next;
}
// current.prev = current.next;
current.next = current.next.next;
}
size--;
}
#SuppressWarnings("unchecked")
public E get(int index)
{
if(index < 0)
{
throw new IndexOutOfBoundsException();
}
if(index > size)
{
throw new IndexOutOfBoundsException();
}
Node current = first;
for (int i = 0; i < index; i++)
{
current = current.next;
}
return (E) current.data;
}
#SuppressWarnings("unchecked")
public int indexOf(E value)
{
int index = 0;
Node current = first;
while (current != current.next)
{
if (current.data.equals(value))
{
return index;
}
index++;
current = current.next;
}
return index;
}
public boolean isEmpty()
{
if (size == 0)
{
return true;
}
else
{
return false;
}
}
public int size()
{
return size;
}
#SuppressWarnings("unchecked")
public String toString()
{
if (isEmpty())
{
return "[]";
}
else
{
String result = "[" + first.data;
Node current = first.next;
for(int i = 0; i < size-1; i++)
{
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
}
This was not easy at all, however I figured out the answer to my question.
#SuppressWarnings("unchecked")
public void add(int index, E value)
{
if(index > size || index < 0)
{
throw new IndexOutOfBoundsException();
}
if (first == null)
{
Node n = new Node(value, null, null);
n.next = n;
n.prev = n;
first = n;
}
else
{
Node current = first;
for (int i = 0; i < index; i++)
{
current = current.next;
}
//current points to node that will follow new node.
Node n2 = new Node(value, current, current.prev);
current.prev.next = n2;
current.prev = n2;
//update first if necessary.
if(index == 0)
{
first = n2;
}
}
size++;
}
I'm making a Doubly Linked List that allows you to insert at the front and rear, as well as deleting any node from the list as long as it exists. The problem is that it doesn't work and gives off and either gives off a NullPointerException or it just says That Integer does not exist even though it does exist.The code is:
public class Numbers {
Node head = null; //Head of the list
Node tail = null; //end of the doubly list
int size = 0;
public void FrontInsert(int data) {
Node n = new Node();
if (head == null) {
head = n;
} else {
n.prev = head;
head.next = n;
head = n;
}
size++;
}
public void RearInsert(int data) {
Node n = new Node();
if (head == null) {
head = n;
tail = n;
} else {
n.next = tail;
tail.prev = n;
tail = n;
}
size++;
}
public void Delete(int x) {
if (size == 0) {
System.out.println("The list is empty.");
}
if (head.data == x) {
head = head.next;
if (head != null) {
head.prev = null;
}
size--;
return;
}
tmp = head;
while (tmp != null && tmp.data != x) {
tmp = tmp.next;
}
if (tmp == null) {
System.out.println("That integer does not exist.");
return;
}
if (tmp.data == x) {
tmp.prev.next = tmp.next;
if (tmp.next != null) {
tmp.next.prev = tmp.prev;
}
}
size--;
}
public void printList() {
while (head != null) {
System.out.print(head.data + " ");
head = head.prev;
}
}
public static void main(String[] args) {
Numbers nu = new Numbers();
}
class Node {
Node prev;
Node next;
int data;
public void Node(int data) {
this.data = data;
next = null;
prev = null;
}
}
}
Try this and check output
public class Numbers {
Node head = null;
Node tail = null;
int size = 0;
public void FrontInsert(int data) {
Node n = new Node(data);
if (head == null) { // first insert
head = n;
tail = n;
} else {
n.next = head;
head.prev = n;
head = n;
}
size++;
}
public void RearInsert(int data) {
Node n = new Node(data);
if (head == null) {
head = n;
tail = n;
} else {
n.prev = tail;
tail.next = n;
tail = n;
}
size++;
}
public void Delete(int index) { // index is the position to be remove
if (size == 0) {
System.out.println("The list is empty."); return;
}else if(index < 0 || index > size -1){
System.out.println("Index outOf Bound."); return;
}
Node currentNode = head;
for(int i = 1; i <= index ; i++){
currentNode = currentNode.next;
}
//remove
if (index == 0) {
currentNode.next.prev = null;
head = currentNode.next;
} else if (index == size - 1) {
currentNode.prev.next = null;
tail = currentNode.prev;
} else {
if (currentNode.prev != null) // Ensure its not header
currentNode.prev.next = currentNode.next;
if (currentNode.next != null) // Ensure its not tail
currentNode.next.prev = currentNode.prev;
}
size--;
}
public void printList() {
Node tmp = head;
while (tmp != null) {
System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}
public static void main(String[] args) {
Numbers nu = new Numbers();
nu.FrontInsert(1);nu.printList();
nu.FrontInsert(2);nu.printList();
nu.RearInsert(3);nu.printList();
nu.FrontInsert(4);nu.printList();
nu.RearInsert(3);nu.printList();
nu.FrontInsert(4);nu.printList();
nu.RearInsert(3);nu.printList();
nu.RearInsert(3);nu.printList();
nu.FrontInsert(4);nu.printList();
System.out.println();
nu.Delete(4);
nu.printList();
}
class Node {
Node prev;
Node next;
int data;
public Node(int data) {
this.data = data;
next = null;
prev = null;
}
}
}
Well, your head and tail were mutually exclusive, i mean when you add something to the tail of the list, you were only giving one side reference not both side, you have to says
tail.next = n; n.prev = tail; and tail = n.
Here is a working code:
public class Numbers {
Node head = null; //Head of the list
Node tail = null; //end of the doubly list
int size = 0;
public void FrontInsert(int data) {
Node n = new Node(data);
if (head == null) {
head = n;
tail = head;
} else {
n.next = head;
head.prev = n;
head = n;
}
size++;
}
public void RearInsert(int data) {
Node n = new Node(data);
if (head == null) {
head = n;
tail = head;
} else {
n.next = null;
tail.next = n;
n.prev = tail;
tail = n;
}
size++;
}
#SuppressWarnings("null")
public void Delete(int x) {
if (size == 0) {
System.out.println("The list is empty.");
return;
}
if (head.data == x) {
head = head.next;
if (head != null) {
head.prev = null;
}
size--;
return;
}
Node tmp = head;
while (true) {
if(tmp == null)
break;
if(tmp.data == x)
break;
System.out.println(tmp.data);
tmp = tmp.next;
}
if (tmp == null) {
System.out.println("That integer does not exist.");
return;
}
if (tmp.data == x) {
tmp.prev.next = tmp.next;
if (tmp.next != null) {
tmp.next.prev = tmp.prev;
}
}
size--;
}
public void printList() {
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
}
public static void main(String[] args) {
Numbers nu = new Numbers();
nu.FrontInsert(2);
nu.FrontInsert(3);
nu.FrontInsert(6);
nu.RearInsert(8);
nu.RearInsert(20);
nu.Delete(8);
nu.printList();
// System.out.println(nu.head.data + "data");
// System.out.println(nu.head.next.data + "data");
}
class Node {
Node prev;
Node next;
private int data;
public Node(int data) {
this.data = data;
next = null;
prev = null;
}
}
}