Having Trouble with the method getFrequencyOf(aData), returns zero for every element I am searching for in the Test code...here's my code if anyone can point me to what I'm doing wrong that would be much appreciated
public class FrequencyBag<T>
{
// TO DO: Instance Variables
private Node firstNode;
private int numberOfEntries;
/**
* Constructor
* Constructs an empty frequency bag.
*/
public FrequencyBag()
{
// TO DO
firstNode = null;
numberOfEntries = 0;
}
/**
* Adds new entry into this frequency bag.
* #param aData the data to be added into this frequency bag.
*/
public void add(T aData)
{
// TO DO
if(numberOfEntries != 0){
Node newNode = new Node(aData);
newNode.next = firstNode.next;
firstNode = newNode;
numberOfEntries++;
}
else{
Node newNode = new Node(aData);
firstNode = newNode;
}
}
/**
* Gets the number of occurrences of aData in this frequency bag.
* #param aData the data to be checked for its number of occurrences.
* #return the number of occurrences of aData in this frequency bag.
*/
public int getFrequencyOf(T aData)
{
// TO DO
int counter = 0;
Node currentNode = firstNode;
for(int i = 1; i <= numberOfEntries; i++)
{
if(currentNode.data.equals(aData))
{
counter++;
System.out.println(counter);
}
currentNode = currentNode.next;
}
System.out.println(counter);
return counter;
}
/**
* Gets the maximum number of occurrences in this frequency bag.
* #return the maximum number of occurrences of an entry in this
* frequency bag.
*/
public int getMaxFrequency()
{
// TO DO
Node currentNode = firstNode;
int currentFrequency = 0;
int maxFrequency = currentFrequency;
for(int i = 1; i <= numberOfEntries; i++)
{
currentFrequency = getFrequencyOf(currentNode.data);
if(currentFrequency > maxFrequency)
{
maxFrequency = currentFrequency;
}
currentNode = currentNode.next;
}
return maxFrequency;
}
/**
* Gets the probability of aData
* #param aData the specific data to get its probability.
* #return the probability of aData
*/
public double getProbabilityOf(T aData)
{
// TO DO
int num = getFrequencyOf(aData);
double probability = num / numberOfEntries;
return probability;
}
/**
* Empty this bag.
*/
public void clear()
{
// TO DO
firstNode.next = null;
firstNode.data = null;
}
/**
* Gets the number of entries in this bag.
* #return the number of entries in this bag.
*/
public int size()
{
// TO DO
return numberOfEntries;
}
private class Node
{
private T data;
private Node next;
public Node (T a)
{
data = a;
next = null;
}
public Node(T a, Node n)
{
data = a;
next = n;
}
}
}
To be honest, I did not read the full code, because there is a mistake in the add method, which probably causes the problem you face:
Node newNode = new Node(aData);
newNode.next = firstNode.next; //firstNode.next is null!
firstNode = newNode;
When you add the first node, it's pointing to a null node (its next value is null).
Then, when you add a second node, as in the line above, your new head of the list points to the next node of the previous head, which is always null. So, your list always has only one node, the firstNode. To fix that, change the above lines as:
Node newNode = new Node(aData);
newNode.next = firstNode;
firstNode = newNode;
OR (using the second constructor):
Node newNode = new Node(aData,firstNode);
firstNode = newNode;
This will make your new node the head of the linked list, having as its next element the previous head of the list.
UPDATE:
Also, another problem is that numberOfEntries is always 0, since you do not increment it when you add the very first node. So, add a numberOfEntries++; inside the else block of the add() method, or just move the one that exists in the if block, outside the block.
Related
I found this code for adding an item to the front of the linked list, but since I have a last node, it doesn't work quite right, so I changed it a tiny bit:
public void moveToFront(String node) {
DoubleNode previous = first;
temp = first;
while (temp != null) {
if (node.equals(temp.item)) {
//Found the item
previous.next = temp.next;
temp.next = first;
first = temp;
if (last.next != null) {
last = last.prev;
last.prev = previous;
}
return;
}
previous = temp;
temp = temp.next;
}
The if (last.next != null) is asking if the original last was moved, and checking if the new last has the right links. Now I think it works properly for my code.
I'd like to implement this code, but for adding an item to the end. However, last just isn't right now. When calling last.prev it only gets the one item behind it, but last.prev.prev to infinity is the same item.
My idea was instead of working from first like in moveToFront(), I work from last, and step through each node backwards, but obviously that doesn't work when last doesn't work anymore.
public void moveToEnd(String node) {
DoubleNode previous = last;
temp = last;
System.out.println("last = " + last.prev.item);
System.out.println("temp = " + temp.item);
while (!temp.item.equals(first.item)) {
if(node.equals(temp.item)){
System.out.println("previous.prev = " + previous.prev.item);
}
previous = temp;
temp = temp.prev;
System.out.println("temp.prev = " + temp.prev.prev.prev.prev.prev.prev.prev.prev.prev.prev.prev.item);
}
Here's how I implement my linked list:
public class LinkedListDeque {
public DoubleNode first = new DoubleNode(null);
public DoubleNode last = new DoubleNode(null);
public DoubleNode temp;
public int N;
LinkedListDeque() {
first.next = last;
last.prev = first;
}
private class DoubleNode {
String item;
int counter = 0;
DoubleNode next;
DoubleNode prev;
DoubleNode(String i) {
this.item = i;
}
}
I found this example of a complete doubly linked list. It does not have an add to front method, but it is adding to the back of the linked list each time. Hopefully, it will help and give you a better idea of how this data structure is supposed to work and function. I would definitely test it first as it states in the readme for this GitHub that none of the code has been tested. Sorce
/*******************************************************
* DoublyLinkedList.java
* Created by Stephen Hall on 9/22/17.
* Copyright (c) 2017 Stephen Hall. All rights reserved.
* A Linked List implementation in Java
********************************************************/
package Lists.Doubly_Linked_List;
/**
* Doubly linked list class
* #param <T> Generic type
*/
public class DoublyLinkedList<T extends Comparable<T>> {
/**
* Node class for singly linked list
*/
public class Node{
/**
* private Members
*/
private T data;
private Node next;
private Node previous;
/**
* Node Class Constructor
* #param data Data to be held in the Node
*/
public Node(T data){
this.data = data;
next = previous = null;
}
}
/**
* Private Members
*/
private Node head;
private Node tail;
private int count;
/**
* Linked List Constructor
*/
public DoublyLinkedList(){
head = tail = null;
count = 0;
}
/**
* Adds a new node into the list with the given data
* #param data Data to add into the list
* #return Node added into the list
*/
public Node add(T data){
// No data to insert into list
if (data != null) {
Node node = new Node(data);
// The Linked list is empty
if (head == null) {
head = node;
tail = head;
count++;
return node;
}
// Add to the end of the list
tail.next = node;
node.previous = tail;
tail = node;
count++;
return node;
}
return null;
}
/**
* Removes the first node in the list matching the data
* #param data Data to remove from the list
* #return Node removed from the list
*/
public Node remove(T data){
// List is empty or no data to remove
if (head == null || data == null)
return null;
Node tmp = head;
// The data to remove what found in the first Node in the list
if(equalTo(tmp.data, data)) {
head = head.next;
count--;
return tmp;
}
// Try to find the node in the list
while (tmp.next != null) {
// Node was found, Remove it from the list
if (equalTo(tmp.next.data, data)) {
if(tmp.next == tail){
tail = tmp;
tmp = tmp.next;
tail.next = null;
count--;
return tmp;
}
else {
Node node = tmp.next;
tmp.next = tmp.next.next;
tmp.next.next.previous = tmp;
node.next = node.previous = null;
count--;
return node;
}
}
tmp = tmp.next;
}
// The data was not found in the list
return null;
}
/**
* Gets the first node that has the given data
* #param data Data to find in the list
* #return Node First node with matching data or null if no node was found
*/
public Node find(T data){
// No list or data to find
if (head == null || data == null)
return null;
Node tmp = head;
// Try to find the data in the list
while(tmp != null) {
// Data was found
if (equalTo(tmp.data, data))
return tmp;
tmp = tmp.next;
}
// Data was not found in the list
return null;
}
/**
* Gets the node at the given index
* #param index Index of the Node to get
* #return Node at passed in index
*/
public Node indexAt(int index){
//Index was negative or larger then the amount of Nodes in the list
if (index < 0 || index > size())
return null;
Node tmp = head;
// Move to index
for (int i = 0; i < index; i++)
tmp = tmp.next;
// return the node at the index position
return tmp;
}
/**
* Gets the current count of the array
* #return Number of items in the array
*/
public int size(){
return count;
}
/**
* Determines if a is equal to b
* #param a: generic type to test
* #param b: generic type to test
* #return boolean: true|false
*/
private boolean equalTo(T a, T b) {
return a.compareTo(b) == 0;
}
}
There is a problem with your moveToFront() method. It does not work for if node == first. If the node that needs to be moved is the first node, you end up setting first.next = first which is incorrect. The below code should work
public void moveToFront(DoubleNode node) {
DoubleNode previous = first;
temp = first;
while (temp != null && node != first) {
if (node.equals(temp.item)) {
//Found the item
previous.next = temp.next;
temp.next = first;
first = temp;
if (last.next != null) {
last = last.prev;
last.prev = previous;
}
return;
}
previous = temp;
temp = temp.next;
}
Now coming to moveToLast() the following code should work
public void moveToLast(DoubleNode node) {
DoubleNode temp = first;
DoubleNode prev = new DoubleNode(null); //dummy sentinel node
while (temp != null && node != last) {
if (temp == node) {
if (temp == first) first = temp.next;
prev.next = temp.next;
if (temp.next != null) temp.next.prev = prev;
last.next = temp;
temp.prev = last;
last = temp;
last.next = null;
break;
}
prev = temp;
temp = temp.next;
}
}
I created a list that goes {50, 10, 60, 30, 40} and want to return the index when I reach 30 in my Linked List, however my program always returns -1 (basically it is unable to increment and return my index). I am not sure about how to go about getting back the index in a different manor unless I am able to cast the anEntry object into an integer and equal it my index.
class MyLinkedList {
private Node firstNode; // index = 0
private int length;
public MyLinkedList() {
firstNode = null;
length = 0;
} // end default constructor
/** Task: Adds a new entry to the end of the list.
* #param newEntry the object to be added as a new entry
* #return true if the addition is successful, or false if not */
public boolean add(Object newEntry) {
Node newNode = new Node(newEntry);
if (isEmpty())
firstNode = newNode;
else {
Node lastNode = getNode(length-1);
lastNode.next = newNode;
}
length++;
return true;
} // end add
/** Task: Adds a new entry at a specified index
* #param newEntry the object to be added at the specified index
* #return true if successful, or false if not */
public boolean add(int index, Object newEntry) {
boolean isSuccessful = true;
if ((index >= 0) && (index <= length)) {
Node newNode = new Node(newEntry);
if (isEmpty() || (index == 0)) {
newNode.next = firstNode;
firstNode = newNode;
}
else {
Node nodeBefore = getNode(index - 1);
Node nodeAfter = nodeBefore.next;
newNode.next = nodeAfter;
nodeBefore.next = newNode;
}
length++;
}
else
isSuccessful = false;
return isSuccessful;
} // end add
/** Task: Determines whether the list contains a given entry.
* #param anEntry the object that is the desired entry
* #return true if the list contains anEntry, or false if not */
public boolean contains(Object anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.data))
found = true;
else
currentNode = currentNode.next;
} // end while
return found;
} // end contains
/** Task: Gets an entry in the list.
* #param the desired index
* #return the desired entry, or
* null if either the list is empty or index is invalid */
public Object getEntry(int index) {
Object result = null; // result to return
if (!isEmpty() && (index >= 0) && (index < length))
result = getNode(index).data;
return result;
} // end getEntry
/** Task: Gets index of an entry in the list.
* #param the desired entry
* #return index of the first occurrence of the specified entry
* in this list, or -1 if this list does not contain the entry */
public int getIndex(Object anEntry) {
Node currentNode = firstNode;
int index = 0; // result to return
while (anEntry != currentNode.data) {
currentNode = currentNode.next;
index++;
if (anEntry.equals(currentNode.data)){
break;
}
else {
return -1;
}
}
return index;
} // end getIndex
private Node getNode(int index) {
Node currentNode = firstNode;
// traverse the list to locate the desired node
for (int counter = 0; counter < index; counter++)
currentNode = currentNode.next;
return currentNode;
} // end getNode
private Node getNode(int index) {
Node currentNode = firstNode;
// traverse the list to locate the desired node
for (int counter = 0; counter < index; counter++)
currentNode = currentNode.next;
return currentNode;
} // end getNode
private class Node {
private Object data; // data portion
private Node next; // link to next node
private Node(Object dataPortion) {
data = dataPortion;
next = null;
} // end constructor
private Node(Object dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
private void setData(Object dataPortion) {
data = dataPortion;
} // end setData
private Object getData() {
return data;
} // end getData
private void setNextNode(Node nextNode) {
next = nextNode;
} // end setNextNode
private Node getNextNode() {
return next;
} // end getNextNode
} // end Node
} // end MyLinkedList
The problem is that your while loop never does more than one iteration.
if (anEntry.equals(currentNode.data)){
break;
}
else {
return -1;
}
If the current element matches, then I have found the item so we can stop. If it doesn't, then the list does not contain this item. It should be relatively clear that this logic is wrong. Just because the current element doesn't match does not mean that subsequent elements might not match.
You can demonstrate this further by observing that getIndex(50) actually does return the correct index: zero. Your statement "my program always returns -1" is actually incorrect.
We need to swap this logic around - only return -1 after we have already tried all the elements.
while (anEntry != currentNode.data) {
currentNode = currentNode.next;
index++;
if (anEntry.equals(currentNode.data)){
return index;
}
}
return -1;
You will still have a problem where your code will throw an exception if the element is not in the list. This can be trivially solved with a minor change to the above code, but I'll leave that up to you to figure out!
I created an Ordered Linked List consisting of Nodes which contain pointers, data, and the keys used to sort them. So far in my method named "add" I have got it to successfully add after a node continually. For example, adding A then B then C. But When I try to continually add in front of a node the first node doesn't seem to have anything pointing to it or the order is incorrect.
By process of elimination it looks like the second else in the add method is where I am incorrectly pointing my nodes. How can I get the add method to add D then C then B which would then be ordered BCD?
public class OrderedLinkedList<E>{
private int size;
private int index;
private KeyedNode currentNode;
private KeyedNode previousNode;
private KeyedNode head;
/**
* Constructor that creates the head node that points to null and initializes size to 0.
*/
public OrderedLinkedList(){
head = null;
this.size = 0;
}
/**
* Method that goes through the OrderedLinkedList and adds one to counter then returns counter as the size.
*
* #return Returns the int variable counter.
*/
public int size(){
return this.size;
}
/**
*
* #param key
* #param value
* #return
*/
public E add(String key, E value){
if(currentNode == null){//takes care of empty list
currentNode = new KeyedNode(key, value);
head = currentNode;
}
else{
if(key.compareToIgnoreCase(currentNode.getKey()) < 0){//add to left
KeyedNode newNode = new KeyedNode(key, value);
if(previousNode == null){
previousNode = currentNode;
newNode.setNext(currentNode);
currentNode = newNode;
head = currentNode;
}
else{
newNode.setNext(currentNode);
previousNode = currentNode;
currentNode = newNode;
}
}
else if(key.compareToIgnoreCase(currentNode.getKey()) > 0){//add to right
KeyedNode newNode = new KeyedNode(key, value);
previousNode = currentNode;
currentNode = newNode;
currentNode.setNext(previousNode.getNext());
previousNode.setNext(currentNode);
}
}
size++;
return null;
}
/**
* Method that looks through the keys of the OrderedLinkedList nodes and returns the data of the node.
* #param key The String key parameter used to search the OrderedLinkedList.
* #return Returns the data of the node with the matching key.
*/
public E find(String key){
KeyedNode current = head;
while(current != null){
if(current.getKey().compareToIgnoreCase(key) == 0){
return current.getData();
}
else if(key.compareToIgnoreCase(current.getKey()) < 0){
return null;
}
else{
current = current.getNext();
}
}
return null;
}
/**
* Method that creates an index for the OrderedLinkedList, then uses the index to find the position of a node to return.
*
* #param position Position is an int variable used to match with index to find a node.
* #return Returns the data of the node in the desired position.
*/
public E get(int position){
KeyedNode current = head;
index = 0;
if(position < 0 || position > size){
return null;
}
while(current != null){
if(index == position){
return current.getData();
}
else{
current = current.getNext();
index++;
}
}
return null;
}
/**
* Private inner class KeyedNode of OrderedLinkedList begins.
*/
private class KeyedNode{
private String key;
private E data;
private KeyedNode next;
public KeyedNode(String keyWord, E dataItem){
data = dataItem;
key = keyWord;
next = null;
}
//getters
public E getData(){
return data;
}
public KeyedNode getNext(){
return next;
}
public String getKey(){
return key;
}
//setters
#SuppressWarnings("unused")
public void setData(E nodeData){
data = nodeData;
}
public void setNext(KeyedNode nodeNext){
next = nodeNext;
}
#Override
public String toString(){
return "The nodes key is: " + key;
}
}
/**
* private class KeyedNode ends
*/
}
My program is meant to take a list of words and store each word under a letter reference in an array in ascending order. For example array of A-Z words apple, ape under a linked list under A referenced by 0, Zebra under Z referenced by 25. But when I use the standard first = new Node(word) I am not adding anything. I'm hopelessly lost.
import java.util.LinkedList;
public class ArrayLinkedList
{
/**
The Node class is used to implement the
linked list.
*/
private class Node
{
String value;
Node next;
/**
* Constructor
* #param val The element to store in the node
* #param n The reference to the successor node
*/
Node(String val, Node n)
{
value = val;
next = n;
}
Node(String val)
{
this(val, null);
}
}
private final int MAX = 26; // Number of nodes for letters
private Node first; // List head
private Node last; // Last element in the list
private LinkedList[] alpha; // Linked list of letter references
/**
* Constructor to construct empty array list
*/
public ArrayLinkedList()
{
first = null;
last = null;
alpha = new LinkedList[MAX];
for (int i = 0; i < MAX; i++)
{
alpha[i] = new LinkedList();
}
}
/**
* arrayIsEmpty method
* To check if a specified element is empty
*/
public boolean arrayIsEmpty(int index)
{
return (alpha[index].size() == 0);
}
/**
* The size method returns the length of the list
* #return The number of elements in the list
*/
public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count++;
p = p.next;
}
return count;
}
/**
* add method
* Adds the word to the first position in the linked list
*/
public void add(String e)
{
String word = e.toLowerCase(); // Put String to lowercase
char c = word.charAt(0); // to get first letter of string
int number = c - 'a'; // Index value of letter
// Find position of word and add it to list
if (arrayIsEmpty(number))
{
first = new Node(word);
first = last;
}
else
{
first = sort(first, word, number);
}
}
/**
* nodeSort method
* To sort lists
*/
private Node sort(Node node, String value, int number) {
if (node == null) // End of list
{
return getNode(value, number);
}
int comparison = node.value.compareTo(value);
if (comparison >= 0) // Or > 0 for stable sort.
{
Node newNode = getNode(value, number); // Insert in front.
newNode.next = node;
return newNode;
}
node.next = sort(node.next, value, number); // Insert in the rest.
return node;
}
private Node getNode(String value, int number)
{
return first.next;
}
/**
* get method
* to get each word value from the linked list and return it
* #return value
*/
public LinkedList get(int index)
{
return alpha[index];
}
public String toString()
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("Word and occurrence in ascending order\n\n");
Node p = first;
while (p != null)
{
sBuilder.append(p.value + "\n");
p = p.next;
}
return sBuilder.toString();
}
}
Is there a reason you are doing it this way.
I can think of an easier way: use Map which will map a character (e.g. "A") to a LinkedList of Words.
hello im trying to implement a Linked list in java.
As this is a homework assignment I am not allowed to use the built in LinkedList from java.
Currently I have implemented my Node class
public class WordNode
{
private String word;
private int freq;
private WordNode next;
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word, WordNode next )
{
this.word = word;
this.next = next;
freq = 1;
}
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word)
{
this(word, null);
}
/**
*
*/
public String getWord()
{
return word;
}
/**
*
*/
public int getFreq(String word)
{
return freq;
}
/**
*
*/
public WordNode getNext()
{
return next;
}
/**
*
*/
public void setNext(WordNode n)
{
next = n;
}
/**
*
*/
public void increment()
{
freq++;
}
}
and my "LinkedList"
public class Dictionary
{
private WordNode Link;
private int size;
/**
* Constructor for objects of class Dictionary
*/
public Dictionary(String L)
{
Link = new WordNode(L);
size = 1;
}
/**
* Return true if the list is empty, otherwise false
*
*
* #return
*/
public boolean isEmpty()
{
return Link == null;
}
/**
* Return the length of the list
*
*
* #return
*/
public int getSize()
{
return size;
}
/**
* Add a word to the list if it isn't already present. Otherwise
* increase the frequency of the occurrence of the word.
* #param word The word to add to the dictionary.
*/
public void add(String word)
{
Link.setNext(new WordNode(word, Link.getNext()) );
size++;
}
Im having trouble with implementing my add method correctly, as it has to check the list, for wether the word allready exists and if do not exists, add it to the list and store it alphabetic.
Ive been working on a while loop, but cant seem to get it to work.
Edit: Ive been trying to print the list but it wont print more than the first added word
public void print()
{
WordNode wn = Link;
int size = 0;
while(wn != null && size <= getSize()){
System.out.println(wn.getWord()+","+wn.getFreq(wn.getWord()));
size++;
}
}
Any help is appreciated
Your add method is wrong. You're taking the root node and setting its next value to the new node. So you will never have any more than 2 nodes. And if you ever have 0, it will probably crash due to a null pointer.
What you want to do is set a current value to the root node, then continue getting the next node until that node is null. Then set the node.
WordNode current = Link;
// Check if there's no root node
if (current == null) {
Link = new WordNode(word);
} else {
// Now that the edge case is gone, move to the rest of the list
while (current.getNext() != null) {
/* Additional checking of the current node would go here... */
current = current.getNext();
}
// At the last element, at then new word to the end of this node
current.setNext(new WordNode(word));
}
You need to keep the instance of the previous node so you can set the next value. Since this would cause a problem if there are no nodes to begin with, there needs to be some extra logic to handle a root node differently. If you'll never have 0 nodes, then you can remove that portion.
If you also need to check the values of the variables in order to see if it's there, you can add something to the while loop that looks at the current value and sees if it is equal to the current word you're looking for.
WordNode current = Link;
while (current != null) {
//check stuff on current word
current = current.getNext();
}
Without your loop code, it will be hard to help you with your specific problem. However, just to give you a bit of a hint, based on the instructions you don't actually have to search every node to find the word. This will give you the opportunity to optimize your code a bit because as soon as you hit a word that should come after the current word alphabetically, then you can stop looking and add the word to the list immediately before the current word.
For instance, if the word you're adding is "badger" and your words list are
apple-->avocado-->beehive-->...
You know that beehive should come after badger so you don't have to keep looking. What you will need to implement is a method that can do alphabetical comparison letter-by-letter, but I'll leave that to you ;-)
Assuming that you always insert in alphabetical order it should look something like this:
public void add(String word){
WordNode wn = Link;
WordNode prevNode = null;
while(wn != null){
if(word.equals(wn.getWord())){
//match found, increment frequency and finish
wn.increment();
return;
}else if(word.compareTo(wn.getWord) < 0){
//the word to add should come before the
//word in the current WordNode.
//Create new link for the new word,
//with current WordNode set as the next link
WordNode newNode = new WordNode(word, wn)
//Fix next link of the previous node to point
//to the new node
if(prevNode != null){
prevNode.setNext(newNode);
}else{
Link = newNode;
}
//increase list size and we are finished
size++;
return;
}else{
//try next node
prevNode = wn;
wn = wn.getNext();
}
}
//If we got here it means that the new word
//should be added to the end of the list.
WordNode newNode = new WordNode(word);
if(prevNode == null){
//if list was originally empty
Link = newNode;
}else{
//else append it to the end of existing list
prevNode.setNext(newNode);
}
//increment size and finish
size++;
}
Use this code :
class NodeClass {
static Node head;
static class Node {
int data;
Node next;
//constractor
Node(int d) {
data=d;
next=null;
}
}
static void printList(Node node ) {
System.out.println("Printing list ");
while(node!=null){
System.out.println(node.data);
node=node.next;
}
}
static void push(int data) {
Node node = new Node(data);
node.next = head;
head = node;
}
static void addInLast(int data) {
Node node = new Node(data);
Node n = head;
while(n.next!=null){
n= n.next;
}
n.next = node;
node.next = null;
}
public static void main (String[] args) {
NodeClass linklistShow = new NodeClass();
linklistShow.head = new Node(1);
Node f2 = new Node(2);
Node f3 = new Node(3);
linklistShow.head.next = f2;
f2.next =f3;
printList(linklistShow.head);
push(6);
addInLast(11);
printList(linklistShow.head);
}
}