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;
Related
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;
}
}
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.
I try to solution a question in LeetCode,it's ask to implement a LRUCache.
And when I submit my code, the System told me the result is Wrong Answer.
Because the TestCase is too long ,I can't find the problem in my code.And when I choice "Run code" to sumbit my code,it's correct.
Here is my code
public class LRUCache {
private int capacity;
private int size;
private HashMap<Integer, Node> cache = new HashMap<>();
private Node tail;
private Node head;
public LRUCache(int capacity) {
this.capacity = capacity;
size = 0;
tail = new Node(-1, -1);
head = new Node(-1, -1);
tail.setPrev(head);
head.setNext(tail);
}
public Integer get(int key) {
Integer value = -1;
Node old = cache.get(key);
if (old != null){
//move to tail
Node node = new Node(key, old.getValue());
removeNode(old);
moveToTail(node);
value = node.getValue();
}
return value;
}
public void put(int key, int value) {
Node n = new Node(key, value);
Node old = cache.get(key);
boolean isExist = old != null;
if (isExist){
removeNode(old);
size--;
}
//move to tail
moveToTail(n);
cache.put(key, n);
size++;
//remove node if size upper than capacity
while (capacity < size){
Node rm = head.getNext();
cache.remove(rm.getKey());
removeNode(rm);
size--;
}
}
private void removeNode(Node node){
if (node.getPrev() != head){
node.getPrev().setNext(node.getNext());
node.getNext().setPrev(node.getPrev());
}else {
head.setNext(node.getNext());
node.getNext().setPrev(head);
}
node = null;
}
private void moveToTail(Node node){
node.setPrev(tail.getPrev());
tail.getPrev().setNext(node);
tail.setPrev(node);
node.setNext(tail);
}
private class Node{
private int key;
private int value;
private Node prev;
private Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
public int getKey() {
return key;
}
public int getValue() {
return value;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
I guess there is problem in your get and put methods. Every time you are creating new nodes. Ideally it should be the same node moved across the DLL. Also, the node should have a setValue() method for updates.
The following update should work.
public Integer get(int key) {
Integer value = -1;
Node old = cache.get(key);
if (old != null){
//move to tail
/////Node node = new Node(key, old.getValue());
removeNode(old);
moveToTail(old);
value = old.getValue();
}
return value;
}
public void put(int key, int value) {
Node n = null;
n = cache.get(key);
if (n != null){
//Update the value of node and move
n.setValue(value);
removeNode(n);
size--;
}
else {
n = new Node(key, value);
}
//move to tail
moveToTail(n);
cache.put(key, n);
size++;
//remove node if size upper than capacity
while (capacity < size){
Node rm = head.getNext();
cache.remove(rm.getKey());
removeNode(rm);
size--;
}
}
Hope it helps!
My program is not sorting the list and I can't figure out the problem.
The list is the same before sorting and after sorting.
public void SelectionSort(){
for (Node index = head; ((index != null)&&(index.getnext()!=null)); index = index.getnext()) {
Node min = index;
for (Node test = min.getnext(); test != null; test = test.getnext()) {
if (test.getAcc().compareTo(min.getAcc()) < 0){
min = test;
}
}
if(index!=min){
Node temp = new Node();
temp=index;
index=min;
min =temp;
}
}
}
Below is my class Node:
public class Node {
private int ID;
private String Acc;
private String Symbol;
private String Function;
private String UniGene;
private String Chromosome;
private Node next;
public Node(){
}
public Node(int id, String acc,String unigene, String symbol, String chromosome, String function){
ID=id;
Acc=acc;
Symbol=symbol;
UniGene = unigene;
Chromosome = chromosome;
Function=function;
}
public void displayNode() // display
{
System.out.print("{"+ID+","+Acc+","+Symbol+","+Function+"} \n");
}
int getID(){
return ID;
}
String getAcc(){
return Acc;
}
String getUniGene(){
return UniGene;
}
String getSymbol(){
return Symbol;
}
String getChromosome(){
return Chromosome;
}
String getFunction(){
return Function;
}
void setnext(Node newnode)
{
next = newnode;
}
Node getnext()
{
return next;
}
}
I think that the problem is that you need to take care of the next pointer when you move nodes. In your original code you just swap 2 references, but you don't change the order in the list:
Node next = min.getnext();
min.setnext(index);
index.setnext(next);
This won't work directly, but the problem lies there. You'll need to save the "previous" node, and set previous.setnext(index) or something like that.
BTW:
Node temp = new Node();
temp=index;
You create a new Node, but you don't use it, 'cause in the next line you assign index to temp.
Node temp = index;
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...