I want to make LinkedList by Eclipse
but i don't know how to make LinkedList.
First of all, I want to compare with int's data and reference.
but, i don't know how to compare.
package List;
public class DoubleLinkedList
{
CellDouble x;
CellDouble head;//List's head
public DoubleLinkedList()
{
//리스트의 헤더를 할당한다.
head=new CellDouble(0);
head.prev=head.next=head;
}
public void insertAfter(CellDouble p,int data)
{
CellDouble x=new CellDouble(data);
x.prev=p;
x.next=p.next;
p.next.prev=x;
p.next=x;
}
public void insertFirst(int data)
{
//List's next to insert
insertAfter(head.next,data);
}
public void insertLast(int data)
{
//list's last's next to insert.
insertAfter(head.prev,data);
}
public void removeCell(CellDouble p)
{
p.prev.next=p.next;
p.next.prev=p.prev;
}
public Object removeFirst()
{
if(isEmpty())
{
return null;
}
CellDouble cell=head.next;
removeCell(cell);
return cell.data;
}
public Object removeLast()
{
// 요소가 없다면 null 반환
if(isEmpty())
{
return null;
}
CellDouble cell=head.prev;
removeCell(cell);
return cell.data;
}
public boolean isEmpty()
{
return head.next==head;
}
public void FindNumber(int data)
{
if(x==null)
{
insertFirst(data);
}
else if(x.data<data)
{
insertAfter(x,data);
}
}
public void Findnumber(int data)
{
if(x.data==data)
{
removeCell(x);
}
else if(x.data!=data)
{
System.out.println("like this number is nothing");
}
}
}
And, I finished my programming. but, its outcome 'List.DoubleLinkedList#8c1dd9'
Check my answer here for the basics. The elements of your list must implement Comparable, read more here.
Related
public static void main (String[] args)
{
Student st1 = new Student("Adams", 3.6, 26);
Student st2 = new Student("Jones", 2.1, 29);
Student st3 = new Student("Marcus", 4.0, 53);
System.out.println("Testing non-recursive code");
LinkedListStud LL = new LinkedListStud();
//checks if linked list is empty
System.out.println("Linked list is empty?: " + LL.isEmpty());
//adds students to the linked list from the front
LL.addFront(st3);
LL.addFront(st2);
LL.addFront(st1);
//adds students to the linked list from the back
LL.addTail(st1);
LL.addTail(st2);
LL.addTail(st3);
//prints linked list non-recursively
LL.printLL();
System.out.println("Linked list is empty?: " + LL.isEmpty());
}
this is my testing method ^ as you can see I am filling my list with student objects. The printLL() method shows all 6 objects are in fact in the array.
public boolean isEmpty()
{
Boolean e;
if (list==null)
return e=true;
else
return e=false;
}
This is my isEmpty() method. The parameters and data type is specified by my professor. For some reason my list always equals null even though it should contain 6 objects the second time I use the method. What am I missing?
My entire linked list class
public class LinkedListStud
{
private Node list;
public LinkedListStud()
{
list = null;
}
public void addFront(Student s)
{
Node oneNode = new Node(s);
oneNode.next=list;
list=oneNode;
}
public void addTail(Student s)
{
Node current;
Node oneNode = new Node(s);
if (list==null)
list=oneNode;
else
{
current=list;
while (current.next != null)
current=current.next;
current.next=oneNode;
}
}
public boolean isEmpty()
{
Boolean e=true;
if (list!=null)
return false;
else
return e;
}
public Student bestStudent()
{
Student bestStudent=list.data;
while (list.next!=null)
{
if (list.next.data.getGpa()>list.data.getGpa())
{
Student temp = list.data;
list.data=list.next.data;
list.next.data=temp;
}
list=list.next;
}
return bestStudent;
}
public void printLL()
{
while (list!=null)
{
System.out.println(list.data);
list=list.next;
}
}
public void printLLRec(Node list)
{
if (list!=null)
{
System.out.println(list.data);
printLLRec(list.next);
}
}
Student bestStudRec(Node list)
{
Student bestStudent = list.data;
if (list!=null)
{
if (list.next.data.getGpa()>list.data.getGpa())
{
bestStudent=list.next.data;
bestStudRec(list.next);
}
}
return bestStudent;
}
private class Node
{
public Student data;
public Node next;
public Node(Student s)
{
data=s;
next=null;
}
public String toString()
{
return "" + data;
}
}
}
Problem is here:
public void printLL()
{
while (list!=null)
{
System.out.println(list.data);
list=list.next;
}
}
You replace list variable with nested list until it becomes null. Last iteration there set your LL.list to null, then while checks it and goes out. If you modify while like:
public void printLL()
{
while (list!=null)
{
System.out.println(list.data);
if (list.next == null) break;
list=list.next;
}
}
you will get what you need, but your root LL will have replaced value from last Node.
Following on from #Vadim 's answer, if you want to print it out without changing the internal list, you can introduce a local variable.
public void printLL()
{
Node temp = list;
while (temp!=null)
{
System.out.println(temp.data);
temp=temp.next;
}
}
I am writing a very simple Double Linked List code in java and everything is okay.
but when i want to print variables it prints null.
I tried and converted object to primitive data type but it isn't working
Node.java
public class Node {
Object data;
Node next,prev;
public Node()
{
next=null;
prev=null;
}
public Node(Object d)
{
data=d;
next=null;
prev=null;
}
public Node(Object d , Node n, Node p)
{
data=d;
next=n;
prev=p;
}
}
List.java
public class List {
Node head;
public List()
{
head=null;
}
public void insert(Node x)
{
x.next=head;
if(head!=null)
{
head.prev=x;
}
head=x;
x.prev=null;
}
public Node search(Object k)
{
Node x=head;
while (x!=null && !x.data.equals(k))
{
x=x.next;
}
return x;
}
public void delete(Node x)
{
if (x.prev!=null)
{
x.prev.next=x.next;
}
else
{
head=x.next;
}
if (x.next!=null)
{
x.next.prev=x.prev;
}
}
public void print()
{
for (Node i=head; i!=null; i=i.next)
{
System.out.println(i.data + " ");
}
}
}
MainClass.java
public class MainClass {
public static void main(String[] args)
{
List t=new List();
Node a=new Node((Integer)(10));
Node b=new Node((Integer)(50));
Node r=new Node("Hello World" + " ");
t.insert(a);
t.insert(b);
t.insert(r);
t.print();
t.delete(r);
t.print();
if(t.search(r)!=null)
{
System.out.println(" Node Found ");
}
else
{
System.out.println(" Node not Found ");
}
}
}
I am getting the
'null'
instead of,
10 50 Hello world 10 50
Please Help.
Thank you.
It cannot print null with the posted code.
Also, it should print either Node Found or Node not Found.
Maybe you are executing another code?
Link Class:
class Link
{
public int data;
public Link next;
public Link previous;
public int count;
public Link(int x, int y)
{
data=x;
count = y;
}
public void displayLink()
{
System.out.print(data+" ");
}
}
DoublyLinkList class (in quicksort class having some errors or could any one change quick sort method without using indexes):
class DoublyLinkList
{
private Link first;
private Link last;
public DoublyLinkList()
{
first=null;
last=null;
}
public void insert(int x,int y)
{
Link newLink=new Link(x,y);
newLink.next=null;
if(isEmpty())
{
first=newLink;
}
else
{
last.next=newLink;
newLink.previous=last;
}
last=newLink;
}
public boolean isEmpty()
{
return(first==null);
}
public void displayList()
{
Link current=first;
while(current!=null)
{
current.displayLink();
current=current.next;
}
}
public void quicksort()
{
Link pivot = first;
Link too_big_index = first.next;
int temp;
Link too_small_index = last;
while (too_big_index.data <= pivot.data)
too_big_index =too_big_index.next;
while (too_small_index.data > pivot.data)
too_small_index=too_small_index.previous;
if(too_big_index.count < too_small_index.count)
{
temp = too_big_index.data;
too_big_index.data =too_small_index.data;
too_small_index.data=temp;
// swap data[too_big_index] and data[too_small_index]
}
while (too_small_index.count > too_big_index.count)
{
quicksort();
}
temp = too_small_index.data;
too_small_index.data =pivot.data;
pivot.data=temp;
}
}
Main Class (First display method is showing numbers but the other one is not working. Don't know why. I think quicksort class is not working, need help to improve it.)
public class Test
{
public static void main(String args[])
{
DoublyLinkList d = new DoublyLinkList();
d.insert(34,0);
d.insert(75,1);
d.insert(86,2);
d.insert(39,3);
d.insert(10,4);
d.insert(61,5);
d.insert(22,6);
d.insert(3,7);
d.insert(1,8);
d.insert(125,9);
d.insert(162,10);
d.displayList();
d.quicksort();
d.displayList();
}
}
I was studying about hashset in java and for that I am writing creating my own hashset which will double its size everytimme the threshold value is reached..here I am keeping the threshold as 0.75 of original size . However my code is running into an infinite loop. I tried debugging it but was not able to find my error...
here is the code
package drafta;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class HashSet
{
private Node[] buckets;
private int currentSize;
private int current;
public HashSet(int bucketsLength)
{
buckets=new Node[bucketsLength];
currentSize=0;
}
public boolean contains(Object x)
{
return false;
// don't implement for the draft
}
public boolean add(Object x)
{
int key=gethashcode(x);
Node node = buckets[key];
while(node!=null){
if(node.data.equals(x)){
return false;
}
}
if(buckets[current]==null){
node = new Node(x);
current=key;
buckets[key]=node;
currentSize++;
}else{
node = new Node(x);
node.next=buckets[current];
current=key;
buckets[key]=node;
currentSize++;
}
System.out.println("add successful "+ x);
System.out.println(" size "+currentSize+" rehash "+buckets.length*0.75);
if(currentSize>(buckets.length*0.75)){
rehash();
}
return true;
}
private void rehash() {
Node temp=buckets[current];
Object s[]=new Object[buckets.length];
buckets=new Node[2*buckets.length];
currentSize=0;
int i=0;
while(temp!=null){
s[i]=temp.data;
temp=temp.next;
i++;
}
while(i>0){
add(s[--i]);
}
}
public boolean remove(Object x)
{
return false;
// don't implement for draft
}
public int gethashcode(Object x){
int hc = x.hashCode();
if(hc<0)
hc=-hc;
return (hc%buckets.length);
}
public Iterator<Object> iterator()
{
Iterator <Object> i=new HashSetIterator();
return i;
//
}
public int size()
{
return currentSize;
//
}
private void resize(int newLength)
{
}
public int getlength()
{
return buckets.length;
//
}
class Node
{
public Object data;
public Node next;
public Node(Object x) {
data=x;
}
public String toString(){
return data.toString();
}
}
class HashSetIterator implements Iterator<Object>
{
private int bucket=0;
private Node currentnode;
public HashSetIterator()
{
currentnode=buckets[current];
}
public boolean hasNext()
{
if(currentnode.next!=null)
return true;
else
return false;
//
}
public Object next()
{
return currentnode.next;
//
}
#Override
public void remove() {
currentnode.next=currentnode.next.next;
}
}
}
this is the main class which I am using to test my code
package drafta;
import java.util.Iterator;
public class HashSetTester
{
public static void main(String[] args)
{
HashSet names = new HashSet(5);
names.add("Harry");
names.add("Sue");
names.add("Nina");
System.out.println(names.size() + " " + names.getlength());
names.add("Susannah");
System.out.println(names.size() + " " + names.getlength());
System.out.println();
names.add("Larry");
names.add("Juliet");
names.add("Katherine");
names.add("Romeo");
names.add("Maria");
System.out.println(names.size() + " " + names.getlength());
names.add("Ann");
names.add("Taylor");
System.out.println(names.size() + " " + names.getlength());
}
}
can someone please point out my mistake..the code is going into infintie loop when it calls rehash for second time..first time it goes through correctly...
You arn't changing any conditions in your while loop in the add method - so there is no reason for it to break out.
while(node!=null){
if(node.data.equals(x)){
return false;
}
}
You will continue looping until the node is null (which never gets set) or the node data ever equals x, but the data value also never gets set.
Can someone explain what am I doing wrong here ?
I am trying to create a queue from two stacks as per a book exercise. I get error "Stack Underflow" from the peek function. But everything seems right to me :P Please explain. Thanks!
//Program to implement Queue using two Stacks.
import java.util.NoSuchElementException;
public class Ex3_5_Stack {
int N;
int countOfNodes=0;
private Node first;
class Node {
private int item;
private Node next;
}
public Ex3_5_Stack() {
first=null;
N=0;
}
public int size() {
return N;
}
public boolean isEmpty() {
return first==null;
}
public void push(int item){
if (this.countOfNodes>=3) {
Ex3_5_Stack stack = new Ex3_5_Stack();
stack.first.item=item;
N++;
} else {
Node oldfirst = first;
first = new Node();
first.item=item;
first.next=oldfirst;
N++;
}
}
public int pop() {
if (this.isEmpty())
throw new NoSuchElementException("Stack Underflow");
int item = first.item;
first=first.next;
return item;
}
public int peek() {
if (this.isEmpty())
throw new NoSuchElementException("Stack Underflow");
return first.item;
}
}
And MyQueue file
public class Ex3_5_MyQueue {
Ex3_5_Stack StackNewest,StackOldest;
public Ex3_5_MyQueue() {
super();
StackNewest = new Ex3_5_Stack();
StackOldest = new Ex3_5_Stack();
}
public int size() {
return StackNewest.size()+StackOldest.size();
}
public void add(int value) {
StackNewest.push(value);
}
private void transferStack() {
if (StackOldest.isEmpty()) {
while (StackNewest.isEmpty()) {
StackOldest.push(StackNewest.pop());
}
}
}
public int peek() {
this.transferStack();
return StackOldest.peek();
}
public int remove() {
this.transferStack();
return StackOldest.pop();
}
public static void main(String[] args) {
Ex3_5_MyQueue myQueue = new Ex3_5_MyQueue();
myQueue.add(4);
myQueue.add(3);
myQueue.add(5);
myQueue.add(1);
System.out.println(myQueue.peek());
}
}
In transferStack(), you're missing an exclamation mark. It should be:
private void transferStack(){
if(StackOldest.isEmpty()){
while(!StackNewest.isEmpty()){ // you forgot it here
StackOldest.push(StackNewest.pop());
}
}
}