I am doing lab, I viewed a lot of Java Generics example, but I cannot understand it. My goal is achieving Linked Stack. It have 2 files: IntStack.java and IntNode.java. The part of these code is:
public class IntNode {
private int element = 0;
private IntNode next = null;
public IntNode(final int data, final IntNode next) {
this.element = data;
this.next = next;
}
public class IntStack {
private IntNode top = null;
public boolean isEmpty() {
return this.top == null;
}
How to convert them to generics type? I know it should use <T>,and I write these code, it is correct or not?
public class Node<T> {
private T element;
private Node<T> next = null;
public Node(final T data,final Node next) {
this.element = data;
this.next = next;
}
}
You are close. The Node parameter of the Node constructor should also be parameterized:
public class Node<T> {
private T element;
private Node<T> next = null;
public Node(final T data,final Node<T> next) {
this.element = data;
this.next = next;
}
}
Related
This one our first class AbstractLinkedList<T>
public abstract class AbstractLinkedList<T> {
public class Node<T> {
public T value;
Node<T> next;
public Node(T value, Node<T> next) {
this.value = value;
this.next = next;
}
}
Node<T> head;
public Node<T> getHead() {
return head;
}
public void addFirst(T value) {
head = new Node<>(value, head);
}
public void addLast(T value){
if(head==null)
head = new Node<>(value, null);
else {
Node<T> node = head;
while (node.next!=null)
node = node.next;
node.next = new Node<>(value, null);
}
}
public void print(){
System.out.println(toString());
}
#Override
public String toString(){
if(head==null) return "boş";
String r="";
Node<T> node=head;
while(node!=null) {
r += node.value + (node.next!=null?" ":"");
node=node.next;
}
return r;
}
public abstract void insertInOrder(T value);
public abstract AbstractLinkedList<T> reverse();
public abstract AbstractLinkedList<T> concatenate(AbstractLinkedList<T> list);
}
This is second one Odev1LinkedList implements Comparable<T>> extends AbstractLinkedList<T>
public class Odev1LinkedList<T extends Comparable<T>> extends AbstractLinkedList<T> {
#Override
public AbstractLinkedList<T> reverse() {
Odev1LinkedList a = new Odev1LinkedList();
Node<T> root = new Node(this, head);
Node<T> iter = root;
Node<T> temp ;
a.head = null;
do {
temp = iter;
a.addFirst(temp.value);
iter = iter.next;
}while (temp !=root );
return a;
}
When I run this code example.reverse should make reverse and return LinkedList without changing ORIGINAL LinkedList.
But its return without reversing
You can't use the < operator on arbitrary objects. It's only defined for primitive numbers. Take a look into the specification.
If your object of type T implements Comparable<T> you can use head.value.compareTo(value) < 0.
If you want your objects of type T to implement Comparable<T> you have to go to your class declaration and write
public class MyLinkedList<T extends Comparable<T>> { ... }
I have a binarySearch Tree and I want to create a method assignFirst.
This method should find the node in the tree with the smallest value and
update the tree's "first" attribute accordingly.
I have a lot of methods, but I don't want to include all of them in here, since I want to keep it short and simple.
Therefore, I will include the class and some features inside that class.
public class BinarySearchTree<E extends Comparable<E>>
{
private BSTNode<E> root; // root of overall tree
private int numElements;
private BSTNode<E> first;
// post: constructs an empty search tree
public BinarySearchTree()
{
this.root = null;
this.numElements = 0;
}
private void assignFirst()
{
if (root.left == null)
{
first.data = root.data;
}
else
{
first.data = root.left.data;
}
}
public class Iterator
{
private BSTNode<E> currentNode;
public Iterator()
{
currentNode = first;
}
public boolean hasNext()
{
return currentNode != null;
}
public E next()
{
E value = currentNode.data;
currentNode = currentNode.next;
return value;
}
}
private static class BSTNode<E>
{
public E data;
public BSTNode<E> left;
public BSTNode<E> right;
public BSTNode<E> parent;
public BSTNode<E> next;
public BSTNode(E data)
{
this(data, null, null, null, null);
}
public BSTNode(E data, BSTNode<E> left, BSTNode<E> right, BSTNode<E> parent, BSTNode<E> next)
{
this.data = data;
this.left = left;
this.right = right;
this.parent = parent;
this.next = next;
}
}
}
I updated my method look like this. I'm still uncertain if this is the correct way of doing it.
private void assignFirst()
{
if (first.left != null)
{
first = first.left;
}
else
{
first = root;
}
}
I figured it out. I wrote it in like this.
private void assignFirst()
{
BSTNode<E> node = root;
while(node.left != null)
{
node = node.left;
}
first = node;
}
I am currently trying to write a method insertEnd that inserts a node at the end of a list, using the tail reference. As I am still learning about it, I do not know how I can approach this. If you have any suggestions or solutions, please could you let me know as it will help me greatly.
package lib;
public class LinkedList {
private Node head;
private Node tail;
public LinkedList(Node h){
head = h;
}
public Node getHead(){
return head;
}
public Node getTail(){
return tail;
}
public void setHead(Node n){
head = n;
}
public void insertEnd(Node newNode){
}
public class ListApp {
public static void main(String[] args){
Node n4 = new Node("green", null);
Node n3 = new Node("orange", n4);
Node n2 = new Node("blue", n3);
Node n1 = new Node("red", n2);
package lib;
public class Node {
private String item;
private Node nextItem;
public Node(String str, Node n){
item = str;
nextItem = n;
}
public String getItem(){
return item;
}
public void setItem(String str){
item = str;
}
public Node next(){
return nextItem;
}
public void setNext(Node n){
nextItem = n;
}
public String getHead(){
return item;
}
}
Here's one possible solution:
public void insertEnd(Node newNode){
newNode.setNext(null);
if (tail == null) {
tail = newNode;
head = newNode;
} else {
tail.setNext(newNode);
tail = newNode;
}
}
Assuming your Node, holds a reference to next,
public class Node<E>{
private E ele;
private Node<E> next;
P.S: Java already does have the LinkedList implemented for our convenience.
So far I have learned that linked list is a Node pointing to the next
first - 1 - 2 - 3 - 4 - 5 -6 - 7-null
My question is, is it possible to divide the list into equal chunks like below
first - |1 2 3| - |4 5 6| - |7| - null
I already tried packaging the data into Objects like below
class IntegerData
{
private int[] data;
private int numData;
public IntegerData(int[] data)
{
data = new int[numData];
numData = 8;
}
}
This is my Node class.
class Node
{
private int m;
private Node next;
private Node prev;
public Node(Node prev,IntegerData data,Node next)
{
this.next = next;
this.prev = prev;
}
public Node getNext()
{
return next;
}
public void setNext(Node next)
{
this.next = next;
}
public Node getPrev()
{
return prev;
}
public void setPrev(Node next)
{
this.prev = next;
}
}
I am stuck as of now. Can someone point me in the right direction?
You can just create a list of lists
List<Integer> list1 = Arrays.asList(1,2,3);
List<Integer> list2 = Arrays.asList(4,5,6);
List<Integer> list3 = Arrays.asList(7);
List<List<Integer>> masterList = Arrays.asList(list1, list2, list3);
Generally your Node should be like this:
public class Node<T> {
private Node<T> prev;
private Node<T> next;
private T data;
public Node(Node<T> prev, T data) {
this.prev = prev;
this.data = data;
if (prev != null) {
prev.setNext(this);
}
}
private void setNext(Node<T> next) {
this.next = next;
}
public Node<T> getPrev() {
return prev;
}
public Node<T> getNext() {
return next;
}
public T getData() {
return data;
}
}
Now you can put anything inside your list, for example an array of Integer:
Node<Integer[]> n1 = new Node<Integer[]>(null, new Integer[]{1, 2, 3});
Node<Integer[]> n2 = new Node<Integer[]>(n1, new Integer[]{4, 5, 6});
Yes It's possible that Each node have multiple values.
For this You have to change the structure of node.
You are using this one
private int m;
private Node next;
private Node prev;
You have to create more variable or even a array (according to your choice)
In This Case I get that you need three variables in one node so just create three like this
private int x;
private int y;
private int z;
private Node next;
private Node prev;
The problem comes in the toString method of the following code:
import java.util.*;
public class LinkedDeque<T> // implements Deque<T>
{
private Node head;
private Node tail;
private int size;
private class Node // Node class
{
T info;
Node next;
Node prev;
private Node (T info, Node prev, Node next)
{
this.info = info;
this.prev = prev;
this.next = next;
}
private T getInfo()
{
return this.info;
}
private Node getNext()
{
return this.next;
}
private Node getPrev()
{
return this.prev;
}
}
public LinkedDeque ()
{
this.head = null;
this.tail = null;
this.size = 0;
}
public static void main()
{
}
public int size ()
{
Node count = head;
while(count.getNext() != null)
{
size++;
count = count.getNext();
}
return size;
}
public String toString()
{
return this.getInfo();
}
public boolean isEmpty()
{
return size() == 0;
}
}
My compiler keeps giving me an error saying that the getInfo method is missing. Any help would be appreciated! Initially, I thought this was due to the fact that the Node class was private, but the Node getNext() method works fine in the method size().
The toString method is a member of LinkedDeque not Node. LinkedDeque does not have a getInfo method.
Not sure what it is you were trying to achieve, but you may consider moving that method into the Node class...