use java programming only! I have a problem with sorting a linked list I\'m gonn
ID: 3756958 • Letter: U
Question
use java programming only!
I have a problem with sorting a linked list I'm gonna post my code (solve without importing LinkedList nor collections it has to be done from scratch please!) I already did a linked list class.
here's my code:
this is the linked list class:
package hw2;
public class LinkedLists {
private static class Node{
private E element;
private Node next;
public Node(E e, Node n) {
element = e;
next = n;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
private Node head;
private Node tail;
private int size = 0;
public LinkedLists() {
head = null;
tail = null;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public E getFirst() {
if(isEmpty())
return null;
return head.getElement();
}
public E getLast() {
if(isEmpty())
return null;
return tail.getElement();
}
public void addFirst(E e) {
head = new Node<>(e, head);
if (size == 0)
tail = head;
size++;
}
public void addLast(E e) {
Node n = new Node<>(e, null);
if(isEmpty())
head = n;
else tail.setNext(n);
tail = n;
size++;
}
public E removeFirst() {
if (isEmpty())
return null;
E answer = head.getElement();
head = head.getNext();
size--;
if(size == 0)
tail = null;
return answer;
}
public Node get(int index)
{
int counter = 0;
Node i;
for( i=head; counter !=index && i!=null; i=i.getNext()){
counter++;
}
if (counter !=index){
System.out.println("Error your index is not is the list (the list has less elements)");
return null;
}
else
return i;
}
public void add(E e) {
Node n = new Node<>(e, null);
Node x;
if (head == null)
head = n;
else {
x = head;
while (x.getNext() != null) {
x = x.getNext();
}
x.setNext(n);
}
}
public boolean search(E e) {
Node current;
if(head == null)
return false;
else {
current = head;
while (current != null) {
if( current.getElement() == e)
return true;
current = current.getNext();
}
return false;
}
}
public void sort(LinkedLists list) {
if(!list.isEmpty()) {
//Node current = list.getFirst();
}
}
public void print() {
for(Node i = head;i!=null;i=i.getNext())
System.out.print(i.getElement() + " ");
System.out.println();
}
}
this is the main class: (the sorting isnt working please help)
Problem 2 Write a program that inserts 10 random integers from 1 to 100 into two different single lists 1- Unordered single list 2- Ordered Linked List should then print the elements of each list. Sample output: Elements of unorder list: 20 73 10 17 9 2 93 49 69 81 Elements of ordered list: 2 9 10 17 20 49 69 73 81 93Explanation / Answer
/**
*
* @author VISHAL
* @param <E>
*/
public class LinkedLists <E>{
private static class Node<E>{
private E element;
private Node next;
public Node()
{
element=null;
next=null;
}
public Node(E e, Node n) {
element = e;
next = n;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
private Node head;
private Node tail;
private int size = 0;
public LinkedLists() {
head = null;
tail = null;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public E getFirst() {
if(isEmpty())
return null;
return (E) head.getElement();
}
public E getLast() {
if(isEmpty())
return null;
return (E) tail.getElement();
}
public void addFirst(E e) {
head = new Node<>(e, head);
if (size == 0)
tail = head;
size++;
}
public void addLast(E e) {
Node n = new Node<>(e, null);
if(isEmpty())
head = n;
else tail.setNext(n);
tail = n;
size++;
}
public E removeFirst() {
if (isEmpty())
return null;
E answer = (E) head.getElement();
head = head.getNext();
size--;
if(size == 0)
tail = null;
return answer;
}
public Node get(int index)
{
int counter = 0;
Node i;
for( i=head; counter !=index && i!=null; i=i.getNext()){
counter++;
}
if (counter !=index){
System.out.println("Error your index is not is the list (the list has less elements)");
return null;
}
else
return i;
}
public void add(E e) {
Node n = new Node<>(e, null);
Node x;
if (head == null)
head = n;
else {
x = head;
while (x.getNext() != null) {
x = x.getNext();
}
x.setNext(n);
}
}
public boolean search(E e) {
Node current;
if(head == null)
return false;
else {
current = head;
while (current != null) {
if( current.getElement() == e)
return true;
current = current.getNext();
}
return false;
}
}
public void sort(LinkedLists list) {
if(!list.isEmpty()) {
Node current = list.head;
Node counter = current;
while(counter.getNext().getNext() != null) {
current = counter;
while(current.getNext() != null) {
if(current.getElement() > current.getNext().getElement()) {
E temp = (E) current.getElement();
current.setElement(current.getNext().getElement());
current.getNext().setElement(temp);
}
current = current.getNext();
}
counter = counter.getNext();
}
}
}
public void print() {
for(Node i = head;i!=null;i=i.getNext())
System.out.print(i.getElement() + " ");
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.