Hello I need to implement a Hashtable with a separate chaining. For that I am implementing a Node class and LinkedList. Nodes included in the linked list have the attributes data, count, a link to next and a link to previous.Linked list should not have values with duplicate value but given another node with the same value inserted, the count of that node should be increased. Above is my code but I could not figure out on how to implement the insert function so that when a duplicate node is found, the count is increased.
public void insertToHead(Node newNode) {
newNode.setNext(head);
head = newNode;
}
public void drop(Node newNode) {
newNode.getPrev().setNext(newNode.getNext());
}
public void insert(String newData) {
Node newNode = new Node(newData);
Node temp = head;
if (temp == null) {
insertToHead(newNode);
newNode.incrementCount();
nodeCount++;
} else {
for(int i=0;i<nodeCount;i++){
if(temp.getData().equals(newNode.getData())){
if(temp.getPrev()==null){
temp.incrementCount();
head=temp;
}
if(temp.getNext()==null){
temp.getPrev().setNext(null);
temp.incrementCount();
insertToHead(temp);
}
else{
temp.incrementCount();
drop(temp);
insertToHead(temp);
}
}
temp = temp.getNext();
}
insertToHead(newNode);
nodeCount++;
}
}
You need to traverse the linked list until you found an equal node and then increment the count on the same node, not the previous. If you reached the end of the list in that process, you do your normal insertion.
Node newNode = new Node(newData);
Node temp = head;
if (temp == null) {
insertToHead(newNode);
newNode.incrementCount();
nodeCount++;
} else {
while(temp != null){
if(temp.getData().equals(newNode.getData())){
temp.incrementCount();
break;
}
temp = temp.getNext();
}
// didn't find anything, prepend to the list
if(temp == null){
insertToHead(newNode);
nodeCount++;
}
}
Related
I'm newbie in programming and I'm practicing a Java Programming Language. I was having a rough day in finding the solution of my program because I cannot get my "next" pointer and I really want to print my last value. Could someone help me to fix this and explain to me? Thank you in advance. Here's my code.
Note: The output of my program is 5.
public class Node {
private int data;
private Node next;
public Node (int data){
this.data = data;
}
public int getData() {
return this.data;
}
public void setNext(Node n) {
this.next = n;
}
public Node getNext() {
return this.next;
}
}
public class LinkedList {
private static Node head, next;
public LinkedList (int data) {
head = new Node (data);
}
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
public void printList() {
head.setNext(next);
while (head.getNext() != null) {
System.out.println(head.getData());
head = head.getNext();
}
System.out.println(head.getData());
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
I suggest following two amendment to your code.
with following else block in addLast method.
Node temp = head;
temp.setNext(next); // this line causing the next object to be set to null all the time. commenting this line will help in making sure the follwing loop reaches to end of the list, otherwise the while loop will always exit without any iteration.
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n; // this will also not change the linking. Its basically assigned a new value to t.
use following suggestion
Node temp = head;
while (temp.getNext() != null) {
temp = temp.getNext();
}
// now we reached end of list and temp.next is null.
// assign newly createdd node to temp.next
temp.setNext(n);
While iterating the element in printList same problem exist as mentioned in point 1. try to use following suggestion for printList method.
// head.setNext(next); // This line will always set head.next to null and whole list will be lost. Instead of this use following line
Node temp = head;
while (temp.getNext() != null) { // here if you use head its position will move to end. So use temp variable for iteration
System.out.println(temp.getData());
temp= temp.getNext();
}
System.out.println(temp.getData());
You may also need to study list iteration algorithm to have better understanding.
I've made some amendments to make your code work. The If statement in your addLast method:
if (head == null) {
is redundant since your LinkedList can only be initialized by passing some data, hence head will never be null, it will always point to the Node containing data
Also the line
head.setNext(next);
in your printList() was problematic, it was always pointing to null
public class LinkedList {
private static Node head, next;
public LinkedList(int data) {
head = new Node(data);
}
public void addLast(int data) {
Node n = new Node(data);
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
}
public void printList() {
while (head.getNext() != null) {
System.out.println(head.getData());
head = head.getNext();
}
System.out.println(head.getData());
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
TL;DR:
You set null as the next node in your printList() method;
Your addLast does not work either (you do not set the next node (see details below);
You should never set the node (or do any logical alteration whatsoever) in your print method. Print must just print, as the name suggests, and it should not contain any side-effect, amending your data structure. That is: you have to clearly separate your concerns.
In your current addLast, you do:
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
which means, that when your temp's next node is null, you never add the Node you instantiate with your int argument.
Change the else block as follows:
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
//two redundant lines removed
}
Correspondingly, remove head.setNext(next); (and possibly unnecessary System.out.println() statement) from your printList() method.
P. S. I would really recommend you to spend some time on the Linked List Data Structure (Data Structure, and not the Java code), as your current design, shows that you need to have a better grasp of it.
I have implemented a simple binary tree program, but there is a problem I encounter while traversing the tree, only root element is accessed. I suspect the nodes aren't linked. I tried my best to figure out the problem but didn't find anything wrong with my code.
I tried printing the data from the insertion function, right before exiting the function, which did print the data correctly.
public class BinaryTree {
Node root;
public void addNode(int data){
Node newNode = new Node(data);
if(root == null){
root = newNode;
}
else{
Node currentNode = root;
while(true){
if(data <= currentNode.data){
currentNode = currentNode.leftChild;
if(currentNode == null){
currentNode = newNode;
return;
}
}
else{
currentNode = currentNode.rightChild;
if(currentNode == null){
currentNode = newNode;
return;
}
}
}
}
}
public void inorderTraversal(Node currentNode){
if(currentNode != null){
inorderTraversal(currentNode.leftChild);
System.out.print(currentNode.data + " ");
inorderTraversal(currentNode.rightChild);
}
}
}
In fact you are not adding the new node correctly to the tree during the recursive step. The logic you should be using is when you a reach a node whose left or right pointer be null, and the new node belongs in that direction, you should add the new node either to the left or right. Otherwise, keep traversing until you reach such node.
while(true) {
if (data <= currentNode.data) {
if (currentNode.leftChild == null) {
currentNode.leftChild = newNode;
return;
}
else {
currentNode = currentNode.leftChild;
}
else {
if (currentNode.rightChild == null) {
currentNode.rightChild = newNode;
return;
}
else {
currentNode = currentNode.rightChild;
}
}
}
Keep in mind that the above simple algorithm for adding new nodes is not guaranteed to necessarily result in a balanced binary tree. To ensure that, you would have to add more logic which handle rebalancing.
You are not assigning the element to the left or right child. You are just assigning it to the local variable currentNode - which is not linked to the tree.
Follow the below code to put inside the while loop & it should work for you.
if(data <= currentNode.data){
if(currentNode.leftChild == null){
currentNode.leftChild = newNode;
return;
}
else {
currentNode = currentNode.leftChild;
}
}
else{
if(currentNode.rightChild == null){
currentNode.rightChild = newNode;
return;
}
else {
currentNode = currentNode.rightChild;
}
}
I want to emulate a queue by using linked lists in Java. My general schema is to have a list of nodes, with two nodes that points to the first and last element of my queue. When I perform the dequeue() I want to get ride of the first element. So far what I have done is the following:
public class Node {
public Object e;
Node next;
public Node(Object e) {
this.e = e;
}
}
public class Queue {
Node queueList;
Node first, last;
int count;
public void enQueue(Object n) {
Node temp = new Node(n);
temp.next = last;
last = temp;
if (queueList == null) {
first = temp;
}
count++;
queueList=temp;
}
public Object deQueue() {
Node previous = last;
Node current = last.next;
Object num = null;
if (count == 0)
System.out.println("empty queue");
else {
while (current.next != null) {
previous = previous.next;
current = current.next;
}
num = first.e;
first = previous;
count--;
}
return num;
}
public void print() {
Node current = last;
while (current != null) {
System.out.println(current.e);
current = current.next;
}
}
}
I do not want to use double linked lists, so for the dequeue() operation what I do is to traverse my list with two pointers like this:
So when the current.next points to a null, I want that previous to be the first node. The problem that I got is when I print the elements of my queue it stills prints me: 10,15,5,18, but the 18 value is not deleted. Any help?
Thanks
There are basically two things I would correct in your code.
First of all, the Queue field was not assigned any value, and I also doubt its utility, basically its logic can be applied using first
public void enQueue(Object n) {
Node temp = new Node(n);
temp.next = last;
last = temp;
if (first == null) { <--- HERE
first = temp;
}
count++;
}
The second one is that you never put the value of the new head of the queue next to null which is needed in that case
public Object deQueue() {
Node previous = last;
Node current = last.next;
Object num = null;
if (count == 0) System.out.println("empty queue");
else {
while (current.next != null) {
previous = previous.next;
current = current.next;
}
num = first.e;
first = previous;
first.next = null; <--- HERE
count--;
}
return num;
}
I try printing a reverse linked list without recursion and reversing the linked list. How can I do that?
Questions: How to print a reverse linked list without using recursion and not reversing the list?
Requirements: No extra space, cannot reverse a linked list, cannot use recursion.
Here is the definition of the linked list Node
class Node {
int value;
Node next;
public Node(int val) {
this.value = val;
}
}
Here is my recursion version of printReverseLinkedList:
public void printReverseList(Node head) {
Node temp = head;
if (temp.next != null) {
printReverseList(temp.next);
}
System.out.print(temp.value);
}
Performace does not matter, because I just want to do in this way.
If you may neither reverse the list, nor use recursion, the only way to do this is this:
public void printReversList(Node head) {
Node current = head; // used to search through the list
Node last = null; // stores the last element that we printed
while (last != head) { // = we didn't print everything yet
// find next element to print - it's one element before we reach "last"
while (current.next != last) {
current = current.next;
}
// Store the current element as the new last and print it
last = current;
system.out.print(last.value);
// reset current and start all over
current = head;
}
}
It is highly ineffective, but there is no other way I can think of.
How about using a Stack and then poping ? You said using another data-structure will be fine. This is not the fine code, but, should get the job done.
public void printReversList(Node head) {
Stack<Node> stack = new Stack<>();
while (head != null){
stack.push(head);
head = head.next;
}
while (!stack.isEmpty()){
System.out.println(stack.pop());
}
}
you can try this:
public void printReverseList(Node head) {
if(head == null) return;
Node prev = null;
Node revers = head;
Node nex = null;
while (revers != null){
nex = revers.next;
revers.next = prev;
prev = revers;
revers = nex;
}
System.out.println(prev);
}
void ReversePrint(Node head) {
// This is a "method-only" submission.
// You only need to complete this method.
Stack<Node> stk=new Stack<>();
Node temp=head;
while(temp!=null){
stk.push(temp);
temp=temp.next;
}
while(!stk.isEmpty()){
System.out.println(stk.pop().data);
}
}
public void printReverseList(Node head) {
Node node = head;
List<Integer> list = new ArrayList<Integer>();
if (head == null){
System.out.println(head.data);
}
else{
while (node != null){
list.add(0, node.data);
node = node.next;
}
for (int item:list){
System.out.println(item);
}
}
}
I am beginner in programming. I am trying to implement linked list in java i have tried to write function to insert element at nth position but its not working properly its not showing data before that position. It may seem silly question or mistake to you but as i am beginner so your answer will be helpful and it will be appreciated.
The Code is below.
class Node{
int data;
Node next;
Node(){
data=0;
next=null;
}
}
class LinkedList{
Node head;
LinkedList(){
head=null;
}
void pushB(int item){
Node temp=new Node();
temp.data=item;
temp.next=null;
if(head==null){
head=temp;
}
else{
temp.next=head;
head=temp;
}
}
void pushnth(int item, int pos){
Node cur=new Node();
cur.data=item;
cur.next=null;
Node temp=head;
int i=0;
while(i<pos-1){
temp=temp.next;
i++;
}
cur.next=temp;
head=cur;
}
void print(){
if(head==null){
System.out.println("List empty");
}
else{
Node temp=head;
while(temp!=null){
System.out.println(temp.data);
temp=temp.next;
}
}
}
}
public class MyFirstJavaProgram {
public static void main(String []args) {
System.out.println("Hello World");
LinkedList l1=new LinkedList();
l1.pushB(90);
l1.pushB(80);
l1.pushB(70);
l1.pushB(60);
l1.pushB(50);
l1.pushB(30);
l1.pushB(20);
l1.pushB(10);
l1.pushnth(40,4);
l1.print();
}
}
Your pushnth method changes the head of the list, and therefore discards all the elements before the new element.
In order to add an element at the middle of the list you have to set 2 links.
The new node should point to the next link, which you do here :
cur.next=temp;
The node that comes before temp should be linked to the new node. That's the part you are missing.
Something like this should work :
void pushnth(int item, int pos){
Node cur=new Node();
cur.data=item;
Node temp=head;
int i=0;
while(i<pos-2){ // note that I changed the end condition
temp=temp.next;
i++;
}
// the new node is placed between temp and temp.next
cur.next = temp.next;
temp.next = cur;
}
Note that this code lacks some validations. For example, if there are too few elements in the linked list, this code will fail, so some additional checks should be added.
// please this is the correct add Node in position.
public static Node addAtPosition(Node head3, int position) {
// add node contains 0 in its data
Node nodeAddAtPosition = new Node(1000);
Node temp = head3;
if (position == 0) {
// add node contains 1000 in its data
nodeAddAtPosition.next = head3;// assigning addFront next to head
head3 = nodeAddAtPosition; // we need to return head assigning head to front.
} else {
for (int i = 1; i < position; i++) {
System.out.print(i);
temp = temp.next;
}
nodeAddAtPosition.next = temp.next;
temp.next = nodeAddAtPosition;
}
return head3;// return head
}
You must distinguish the case where the new item is inserted at the head of the list:
void pushnth(int item, int pos){
Node cur=new Node();
cur.data=item;
if (pos == 0) {
cur.next = head;
head = cur;
} else {
Node temp=head;
for (int i=1; i<pos; ++i) {
temp=temp.next;
}
cur.next = temp.next;
temp.next = cur;
}
}