Delete Method With Linked Lists?
Jul 19, 2014
Why is this method throwing a null pointer exception when I call it from main?
I'm trying to submit an integer to indicate the number of nodes in a linked list to delete and then delete them headfirst.
public boolean delete(int x){
if (x > count || head == null){
return false;
}else{ while (x>0){
head = head.getPrevious();
[Code] ....
View Replies
ADVERTISEMENT
Nov 27, 2014
I currently don't understand how the node's work.
private static class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType data, Node<AnyType> next)
{
this.data = data;
this.next = next;
}
}
Why do I make private Node<AnyType> next;And why do I have an inner class of Node for a linked list?I had the same topic in C, but there it was somehow easier than in java. Because there you have pointers.
View Replies
View Related
Apr 27, 2014
Write a Java function Sum2List that takes two lists L1 and L2 of the same size and returns list L that contains the sum of data inside the corresponding nodes of lists L1 and L2.
ex: L1 = {1,2,3}
L2 = {4,5,6}
L = {5,7,9}
I do not know how to iterate through two different lists >>
public LinkedList Sum2List(LinkedList l1, LinkedList l2){
LinkedList l3 = new LinkedList();
ListNode Current1 = L1.getFront();
ListNode Current2 = L2.getFront();
while(Current1 != null){
l3.Add(Current1.getItem() + Current2.getItem());
[Code] .....
This my attempt to write the code , but the dr. side that we have not to use the build in methods ( getNext()...etc)
in the function .
View Replies
View Related
Mar 10, 2014
I have an XML sheet and my project is top retrieve the required elements from XML sheet. So my format of XML was like follows:
<Class>
<Employees>
<EMPLOYEE>
<ENum> Abc123</ENum>
<Ename> John<?Ename>
<EType>Mathematics</EType>
[Code] ....
I have used unmarshalling concept to retrieve the data elements... I have to check whether the elements satisfy few regulations when compared with data in Database. So, i thought of grouping the employees depending on EType. I have created a Map with linkedlist of employees. Say Map<String, LinkedList<Employe>>EmpMap=new Map<String, LinkedList<Employe>>();
I have already created a class named Employee which has all the setter and getter methods for employee.
Here am going to take Etype(Employee type) as key and linkedlist(list of employees of certain type) as value. How to iterate these linked lists and place them in my Map.
View Replies
View Related
Feb 13, 2014
I have the following code that supposed to perfrom sorting on the linked list using method sort in order usind node concept of Linked List but seems to give inlogic results. the following code tests only the values lower than the first value but i can't manage to sort the data higher than the first entered value;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
public class ListNode<T extends Comparable> {
ListNode<T> nextNode;
T data;
public ListNode(T item)
[Code] .....
View Replies
View Related
Oct 25, 2014
I'm working with Doubly Linked Lists and using Java Generics..
My nodes looks like this:
class DNode<E> {
DNode<E> previous;
DNode<E> next;
E element;
//and all methods inside
}
My list of Nodes looks like this:
class DLL<E>{
private DNode<E> head;
private DNode<E> tail;
private int size;
[code]....
As you can see, as arguments they get "E o"...I need to write a program, which from the main function asks the users how long is the list, and after they type it's length, I ask them to start typing the elements (integers)...and this is how my main method is written, but I can't seem to make it work, specialy when I call the "insLast" method,I guess it's because the arguments i'm giving to the function...how to read the elements and write them into the list?
public static void main(String[] args) throws IOException {
DLL<Integer> lista=new DLL<Integer>();
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s = stdin.readLine();
int N = Integer.parseInt(s);
s = stdin.readLine();
String[] pomniza = s.split(" ");
for (int i = 0; i < N; i++) {
lista.instLast(Integer.parseInt(pomniza[i]));
}
}
View Replies
View Related
Apr 16, 2014
I am creating a chained hash table that uses my own LinkedListclass to handle collisons. Here is where my issue is occuring: I start with an array of LinkedLists, my "hash table", intially set to null. When i wish to add a word, if the desired location is null, I create a new LinkedList, place it in that array index. I then create a new LinkedListNode and set the head of the new LinkedList to the new node.
My issue is occuring here: Whenever I set the head of my new LinkedList to my new LinkedListNode all of my previously created linkedlists also have their head changed to the new node.
My debugger shows that I am truly creating new linkedlists every time. I was afraid each array index was just pointing to a single array, but that is evidently not the issue. Below is all the relevant code
public class SetOfStrings {
private int arraySize;
private LinkedList[] mainArray;
private int totalCount;
//other methods here
public boolean add(String toAdd) {
int hashToAdd = hash(toAdd);
[code]....
SUMMARY: Whenever I set the head of a Linked List to a new node, it changes the head of all my linked lists to point to the same new node
View Replies
View Related
Oct 29, 2014
Basically, the program is to be able to read information from a file like this;
11111 63.5
22222 59.9
33333 82.4
44444 73.1
55555 77.3
66666 99.5
77777 68.7
88888 53.2
99999 66.8
where the first number is student number and the second is their grade. I need to read this information from a .txt file and dynamically create a new node containing that student's number and mark, and insert it in the correct position in the linked list (in descending order based on grade). So I get that each Node needs to contain two data types, an Int for Student # and a Double for their grade, and I'm pretty sure I've done it correctly with my StudentNode class which can be found in the source code linked above.
But what I don't get it using that class to create Nodes in my main class and then sort them based on their Double grade value WHILE they are being sorted. I just don't understand where to actually put the methods and such that does these things. Apparently I'm supposed to have three classes.
One named StudentNode which is just the node info, which I have done.
The second is called StudentList which is apparently supposed to contain the head of LinkedList and the methods I need? I'm not sure how it ties into StudentNode though.
The third and final is just the main class which I'll use to test it.
Then after all that I need to print out the median mark through a recursive method that isn't allowed to use any loops or call and functions/methods that use loops. The function should return the node in the list which contains the median mark. Secondly, in order to find the median, you need to know how many items in total are in the list. Your recursive function must calculate that number (also recursively); you may not keep track of this count elsewhere your program. Your recursive solution should only examine each node only once, and the depth of the recursion should be equal to the number of nodes in the list.
Here's my current in-progress code.
import java.io.FileNotFoundException;
import java.util.Scanner;
class StudentNode {
private int studentNum;
private double grade;
private StudentNode next;
[Code] .....
I'm pretty sure the StudentNode class is fine, it's the StudentList class that I'm not sure about. I'm not sure how to add nodes since there isn't a getNext() method in the StudentList class.
View Replies
View Related
Oct 25, 2014
I'm not sure if my understanding of PriorityQueues is correct, so I'm trying to check if my reasoning is valid. I'm supposed to compare the Big-O for arrays and linked lists for the following instructions:
Insert 100 objects having the priorities 1, 2, 3, ... , 99, 100
Big-O for Array: __________
Big-O for Linked List: ___________
Insert 100 objects having the priorities 100, 99, 98, ... , 2, 1
Big-O for Array: __________
Big-O for Linked List(Assume no tail reference): ___________
If my understanding is correct, priority queues take in items randomly with no particular order, but they are removed according to the priority of each element. If what I've said is true, wouldn't that mean that inserting any number of objects would be O(1) for both linked lists and arrays? If the PriorityQueue has no particular order, then wouldnt each add() simply insert something to the next array index/linked list node?
View Replies
View Related
May 6, 2015
I am trying to implement product method below which returns the set representing the Cartesian product of the current set and a given set (the Cartesian product contains all ordered pairs (a, b) where a belongs to the current set, and b belongs to the given set). The product should be a ListSet <Tuple<E>> object where each ordered pair is a Tuple element. (I have a Tuple class which implements an ordered tuple)
What am I trying to do in the product method : Make 2 for loop and inside the for loop make an array of <E> then set the 2 elements of the tuple then again set tuple and add it to arrayList. how to set 2 elements of the tuple and set tuple ??
public class ListSet<E> implements Iterable<E>{
SinglyLinkedList<E> sl;
public ListSet(){
sl = new SinglyLinkedList<E>();
[code]....
View Replies
View Related
Jan 7, 2014
package test2;
import java.util.LinkedList;
public class Q {
public static void main(String[] args) {
LinkedList link = new LinkedList();
[Code] ....
So how to delete an element in the middle of the linked list?
View Replies
View Related
Nov 2, 2014
How to go through each link item in both lists, and directly link them into the new list in order without using insert()
class Link {
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long dd) // constructor
{ dData = dd; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
} // end class Link
[Code] ......
View Replies
View Related
Oct 21, 2011
i am trying to create an applet with drop-down lists. When I compile the program the following error message appears '. . .is not abstract and doesn't override abstract method action Performed. . . Here is my code . . .
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DavidApplet extends Applet implements ActionListener //class header {
Label fName=new Label("First Name");
[Code] .....
View Replies
View Related
Oct 4, 2014
Iva tried flipping the entire code upside down (expression) and No matter what I do the delete function returns a null pointer exception after my while loops takes the pointer right to where it should be.
package bp;
import java.time.LocalDate;
public class BinaryTree implements IBinaryTree {
private Node root = null;
private int sizeOfTree = 0;
[Code] ....
View Replies
View Related
Dec 30, 2014
Is there a particular implementation of a linked list you are referring to in your question?
View Replies
View Related
Apr 26, 2015
Implementing the add(int i, E o) method for a SinglyLinked List.
public void add(int i, E o)
// pre: 0<=i<= size()
// post: adds ith entry of list to o
View Replies
View Related
Jul 16, 2014
I'm still working with the singlylinkedlist data structure and trying to return a string of individual characters stored in each node. ( head--('J')---('A')---('V')---('A')---tail ) Hopefully this beautifully executed depiction to the left will clarify.
This is what I came up with after designing the algorithm w/ pen and paper. I know that I'm not accounting for the OutOfBound errors, an empty list, or an index < 0.... I will get to that.
Right now, I'm not sure why my assignment to the character array, ' chars[i] = cursor.getLink(getElement()); ' , is not working. The two methods getLink and getElement, type Node and T, respectively, exist in my Node class which is a private nested class in MySLList. Why would I be getting the following error: "The method getElement() is undefined for the type StringX" ? Is this a good design and implementation of the substring method?
public String substring(int index) {
char[] chars = new char[(getSize() - index)]; //getSize() defines the size of list in MySLList
Node cursor = null;
//Set the cursor to the node = index
if(cursor == head) {
[Code] ....
View Replies
View Related
Oct 21, 2014
So I have to write all the methods for a LinkedListQueue. I've got isEmpty, enqueue and dequeue working correctly (I think) but I'm having trouble with the toString method. I tried to do it recursively and it works if there is only one element in the list, but with multiple elements it throws a StackOverflowerror exception. I've tried it multiple different ways, but I can't seem to figure out how to print it out with out clearing everything. We haven't been taught StringBuilder or .append yet, which I saw a lot of as I was looking for solutions, so I can't use those.
public class LinkedQueue<T>
{
protected LLNode<T> front; // reference to the front of this queue
protected LLNode<T> rear; // reference to the rear of this queue
private T info;
public LinkedQueue()
{
front = null;
rear = null;
[Code] ....
and this is the ITD used with it, for some reason it has the "empty the queue" function as a choice but we weren't assigned that function, so just ignore it.
import java.util.Scanner;
public class ITDLinkedQueue
{
public static void displayMenu()
{
System.out.println("(1) display menu");
System.out.println("(2) check isEmpty");
System.out.println("(3) enqueue");
System.out.println("(4) dequeue");
[Code] ....
View Replies
View Related
Apr 9, 2014
I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass?
View Replies
View Related
Jun 7, 2014
I'm trying to write an indexOf() method that will return every time a value occurs in a linked list. I need to use my user-created linked list not the built in Java linked list. For example in a linked list of characters: "i, p, z, z, n, d, p, z" when I search for "z" it should return position variables for 3, 4, and 8. Currently what I have is obviously only returning the first instance.how I can return more than one instance?
public int indexOf(char input) {
LLNode currentNode = this.first;
int position =1;
boolean found = false;
[code]...
View Replies
View Related
Apr 23, 2015
I am trying out solving the question but i am stuck.The problem is to write a method that print data of single linked list backward using stack.The question is as follow
public class Stack{
public boolean isEmpty(){};
public void push(int n){};
public int peek(){};
public int pop(){};
}
public class node{
int data;
node next;
}
public class list{
node first;
}
View Replies
View Related
Oct 13, 2014
I am having a little trouble with a part of my Java assignment that needs to have linked lists in it.
I need to write a public method that takes an array of type object to load strings into a linked list.
View Replies
View Related
Aug 6, 2014
public void myFunc(MyNode n, ArrayList<MyNode> path) {
boolean hasChildren = false;
path.add(n);
int index = path.indexOf(n);
ArrayList<MyNode> statefulPath = new ArrayList<MyNode>();
[Code] ....
I have similar code that I stepped through in a debugger. After running the code I found that it built the desired tree, in this case a root node H with left child L and right child P. I want list of lists to contain all paths from root to leaf. I expected [H, L] and [H, P]. I discovered that statefulPath is not stateful; after a recursive stack frame pops, statefulPath still contains n! But that stack frame just popped! I expected to see statefulPath be [H] in the debugger and it was [H, L]! So I later have a list [H,L,P] which I don't want. How do I make the statefulPath list I want for my algorithm?
View Replies
View Related
Mar 25, 2015
List list1 = query1.list();
List list2 = query2.list();
for (Iterator itr = list1.iterator(); itr.hasNext();) {
Object[] row = (Object[]) itr.next();
}
View Replies
View Related
Jun 1, 2014
I have big problem with mergesort in Java. I can't figure out why it do not works. I need write it on lists using Comparable. Here is piece of code:
Java Code:
class MergeSort{
Comparator _comparator;
List lista = new ArrayList<>();
List list_temp = new ArrayList<>();
MergeSort(Comparator comparator){
[Code] ....
I have tried everything, still no results. It's return list with random placed numbers.
View Replies
View Related
Dec 6, 2014
What is going on here in the main class is a zoo that requires information to be read from and saved to a .txt file. I have made three arrayLists for each .txt file, I am getting errors for illegal start to an expression
import java.io.*;
import java.util.*;
public class ColumbusZoo
{
public static void addHelper(ArrayList<DomesticAnimal> a){
Scanner s = new Scanner(System.in);
System.out.println("What species");
[code].....
View Replies
View Related