public class test {
public static void main(String[] args) {
LinkedList<Character> linkedList = new LinkedList<>();
}
}
class Node<E>{
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
class LinkedList<E>{
int size = 0;
Node<E> first;
Node<E> last;
public boolean palindrome(LinkedList<E> list){
Node<E> x = first;
Node<E> y = last;
int count = 1;
while (count <= (size / 2)){
if (x.item != y.item){
return false;
}
x = x.next;
y = y.prev;
count++;
}
return true;
}
public void add (E e){
Node<E> l = last;
Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null){
first = newNode;
}else {
l.next = newNode;
}
size++;
}
public void remove(Object o){
if (o == null){
for (Node<E> x = first; x != null; x = x.next){
if (x.item == null){
delete(x);
}
}
}else {
for (Node<E> x = first; x != null; x = x.next){
if (o.equals(x.item)){
delete(x);
}
}
}
}
public void delete(Node<E> x) {
E element = x.item;
Node<E> prev = x.prev;
Node<E> next = x.next;
if (prev == null){
first = next;
}else {
prev.next = next;
x.prev = null;
}
if (next == null){
last = prev;
}else {
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
}
public void print(){
for (Node x = first; x != null; x = x.next){
System.out.print(x.item + " ");
}
System.out.println();
}
}
|