Removing A Node From Doubly Linked List

Apr 11, 2014

Im not sure why the nodes are not being deleted. Is a part of my logic wrong?

public void delete(String s) {
Node temp = find(s);
if (temp==null) {
return;
}
Node prevTemp = temp.prev;
Node nextTemp = temp.next;

[Code] .....

View Replies


ADVERTISEMENT

Dummy Node In Doubly Linked List - Cannot Find Symbol Variable Error

Jun 5, 2012

Im running into some problems with the Java compiler. This is my code:

public class DoublyLinkedList<T> implements SimpleList<T> {
protected Node dummy;
protected int n;
public DoublyLinkedList(){

dummy = new Node();
dummy.next = dummy;
dummy.pre = dummy;

n = 0;

[Code] ....

I want to use a dummy node in my implementation of the Doubly Linked List, so it will be easier to code all the methods I need to write. I keep on getting a problem with line 14 and 15:

dummy.next = dummy;

dummy.pre = dummy;

// cannot find symbol variable next (compiler error)

// cannot find symbol variable pre

View Replies View Related

How To Move Along A Doubly Linked List

Dec 11, 2014

I'm having some trouble with figuring out how to move along a doubly linked list for an assignment. The program is supposed to be a simple board game simulation. Here is what I have so far:

Space.java:

public class Space
{
private String name;
public Space next;
public Space previous;
public Space(String name)
{
this.name = name;

[Code]...

I seem to have been able to get all the other methods working properly, but I am pretty stuck on how to do the movePlayer. Specifically because it is passing an integer, but my objects are of type Space and Boardgame.

View Replies View Related

Elements Of Circular Doubly Linked List

Feb 12, 2015

For the program I have to create, I need to be able to work with the elements of a circular doubly linked list to do the following:

Find an element in the list (specified as an argument)
Insert an element after a specified element (specified as an argument)
Delete an element (specified as an argument)
Display the elements in the list

I can insert elements and display the elements, but I can't figure out how to search for an element, insert an element after a specified element, or delete an element.

What I am having trouble with is passing the elements in the list as arguments for searching, deleting, inserting after an element.

public class doublyLinkedList {
private Node pointer;
private Node dLL;
private int count;

[Code] .....

View Replies View Related

How To Insert New Item In Doubly Linked List

Oct 26, 2014

How can I insert a new item at the middle of a BookList . I have also got a Book class represting Book objects and a inner class BookNode referencing them.

public void add(Book newBook)
{ BookNode newNode = new BookNode(newBook);
if (firstNode == null){
// no nodes in the list so add newNode at start
firstNode = newNode;
tempNode = newNode ; }

[Code] ....

View Replies View Related

Doubly Linked List - Swapping Two Sub-parts

Oct 30, 2014

I have a couple more (2or3) and I believe I'll be ready to go 8-)This one is about an Army and a list of warriors... for example 1,2,3,4,5,6,7,9,10 .... and the user inputs points for two sequences, for example 1, 5 and 6,10 .... That means I have to take the array from the 1st element, up to the 5th one, and swap it with the elements from 6to10....

The nest list should be:

6 7 8 9 10 1 2 3 4 5

Things to keep in mind: The list will always have at least two warriors. The intervals will never interfere, and will at least contain ONE warrior..

It says: be careful when the intervals are next to each other, and when be careful when an interval starts with the first warrior, or finishes with the last warrior.

View Replies View Related

Doubly Linked List Check Palindrome

Oct 29, 2014

I cant figure out something in my code. I have to check whether given doubly linked list is palindrome or not. If it is palindrome it should print 1 , if not -1. My code has no errors but it always prints -1. I tried debugging but first comparisongives always false and function cant reach to else statement.

Are there any logical errors or i cant do right way of comparison?

import java.util.Scanner;
class DLLNode<E> {
protected E element;
protected DLLNode<E> pred, succ;
public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
this.element = elem;
this.pred = pred;
this.succ = succ;

[Code] ....

View Replies View Related

Huge Numbers And Doubly Linked List

Jul 15, 2014

I am trying to create doubly linked list that can hold huge numbers (i.e. 123456789) and add them together. I have seen some examples on how to do this for linked list, but none really for doubly linked list.

Here is my test driver:

public class HugeNumberDriver
{
/**
* Main method with some test code
*/
public static void main(String[] args)
{
// Create a HugeNumber that is 123456789
HugeNumber h1 = new HugeNumber();
for (int i=9; i>=1; i--)

[Code] ....

Output:

h1 is 987654321
h2 is 88888888885555555555
Exception in thread "main" java.lang.NullPointerException
at HugeNumber.<init>(HugeNumber.java:52)
at HugeNumberDriver.main(HugeNumberDriver.java:29)

As you can see the numbers are displayed incorrectly and the rest of the program does not run. I have a feeling that it has to do with my deep copy constructor or my addDigit() method

View Replies View Related

Swing/AWT/SWT :: How To Show Doubly Linked List In Table

Nov 15, 2014

I have a doubly linked list and i want to show the data in a table using java gui.

View Replies View Related

How To Advance To Next Node In Linked List

Apr 7, 2014

I am trying to advance to the next node in my linkedList. Below is what i have so far.

/**
* Move forward, so that the current element is now the next element in this sequence.
* @param - none
* @precondition:
* isCurrent() returns true.
* @postcondition:
* If the current element was already the end element of this sequence (with nothing after it), then there is no longer any current element.
* Otherwise, the new element is the element immediately after the original current element.
* @exception IllegalStateException
* Indicates that there is no current element, so advance may not be called.
**/

public void advance( ) {
// student implements
if(!isCurrent())
throw new IllegalStateException();
else{
while(cursor != null){
precursor = cursor;
cursor = cursor.getLink();
}
}
}

View Replies View Related

Quicksort With Single Linked List / 1 Node Parameter

Nov 12, 2014

I have a custom linkedList(single) class that uses the provided node class. Now I have another class to QuickSort this.(left out for brevity, i just wanna focus on editing the L.head). However, instead of passing the quicksort method the entire linkedList, I want to pass it just the head from the linkedlist.

My problem is accessing this head node and changing it from the quckSort method/class, and I dont want to delete it or simply just change the element value

Main:

public class TestLinkedList {
public static <E extends Comparable<E>> void main(String[] args) {
MyLinkedList<Integer> L = new MyLinkedList<Integer>();
L.add(3);
L.add(1);
L.add(2);
System.out.println("Initial=" + L);
MySort.quickSort(L.head);
System.out.println("After ="+L);
}
}

QuickSort:

public class MySort {
public static <E extends Comparable<E>> void quickSort(MyNode<E> list) {
list = list.next;
}

Node Class:

public class MyNode<E extends Comparable<E>> {
E element;
MyNode<E> next;
public MyNode(E item) {
element = item;
next = null;

[code]....

View Replies View Related

Traverse Linked List From First To Last Node And Print Data Value

Jan 1, 2015

AddItemToFront(Item p) : This method will create a new Node with the Item object as its data value and then add the newly created node to the front of the linked list.

DisplayItems(): This method will traverse the linked list from first node to last node and print data value ( i.e., id, name, type and price of the Item object) of each node.

RemoveItemAtPosition(int n): This method will remove the node at position n in the linked list. Assume that the first node of the linked list has a position number of 1 and the second node has a position number of 2 and so on.

This is my Code

AddItemToFront
public void AddItemtoFront(Item p)
{
Node newNode = new Node(p);
newNode.setLink(head);
head = newNode;

[Code] ....

I don't know what am I suppose to do inside the remove method

View Replies View Related

Finding And Printing Median Node In Linked List Using Recursion

Nov 3, 2014

The only problem I have now is getting a method to return the median element of a LinkedList without using loops of any kind or by using a global counter anywhere.

I've pretty easily figured out how to get the index value for the median number (there is some lee way allowed. If the list has an even size, any of the middle values are accepted) but I can't figure out how to print it without loops.

I'm sure I need to make a method that finds an element at the given index value, but I don't know how to do it without loops.

Here's all of my code. Inside is my Assignment3 class I use for testing, StudentList which contains the LinkedList head and other List methods, and StudentNode which is obviously, the Node class. Also I've attached the first test1.txt file as well.

import java.io.FileNotFoundException;
import java.util.*;
public class Assignment3 {
public static void main (String []args){
StudentList<StudentNode> myList = new StudentList<StudentNode>();

[Code] .....

I tried making a method that basically counts up the list recursively then a second method that counts down recursively and is supposed to stop once it hits the middle number, then print that node.

Attached File(s) : test1.txt (106bytes)

View Replies View Related

Searching For Object In Linked List Then Removing The Object

Nov 19, 2014

I have just started working with linked lists. I have a linked list of Objects and I want to be able to search for a specific object. But currently my code continues to return false. Also how would I go about removing the first index of the linked list.

public static void main(String[] args) {
LinkedList<Cookies> ml = new LinkedList<>();
int choice = 0;
while (choice >= 0) {
choice = menu();

[Code] ....

View Replies View Related

Doubly Linked Lists And Using Java Generics

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

Linked Binary Search Tree - Creating Node Object

Apr 5, 2015

I'm trying to use LinkedBinarySearchTree but a lot of the variables are protected in BinaryTreeNode. I am creating a BinaryTreeNode object but it still isn't allowing me to use them. The variables I am trying to use are element, left, and right.

import ch11.exceptions.*;
import ch10.LinkedBinaryTree;
import ch10.BinaryTreeNode;
/**
* LinkedBinarySearchTree implements the BinarySearchTreeADT interface with links.

*/
public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T>
implements BinarySearchTreeADT<T>

[Code] ....

View Replies View Related

Sort Linked List Through The Nodes Of List - Data Of Calling Object

Feb 14, 2014

I have some class called sorted to sort the linked list through the nodes of the list. and other class to test this ability, i made object of the sort class called "list1" and insert the values to the linked list.

If i make other object called "list2" and want to merge those two lists by using method merge in sort class. And wrote code of

list1.merge(list2);

How can the merge method in sort class know the values of list1 that called it as this object is created in other class.

View Replies View Related

Removing Duplicates In Array List

Sep 18, 2014

I am stuck on this exercise and I don't know what exactly is wrong. I think it's something with the .remove and the for each loop, but I am not sure.

public class seven {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unni");

[Code] ....

This is what i get

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at seven.removeDuplicates(seven.java:24)
at seven.main(seven.java:18)

View Replies View Related

Removing (Integer) From A Generic List?

Oct 3, 2014

I am working on a java program that is called OrderedVector which is basically a storage or list that grows and shrinks depending on the amount of data is put in. Most of the methods in my code are correct and working, the only real issue I have lies with either the remove(E obj) method or remove(int index) method. This is the driver I am currently using to test my remove method,

public class Tester {
public static void main(String[] args) {
OrderedListADT<Integer> v;
v = new OrderedVector<Integer>();
for(int i = 0 ; i <= 9; i++){
v.insert(i);

[code]....

the output I am receiving is

Removing 0
Size of data structure is 9
Removing 1
Size of data structure is 8
Removing 2
Size of data structure is 7

[code]....

As you can see, when I am calling the second for loop, none of the elements are being removed by my methods but the first for loop is working just fine.

Here is my code for the OrderedVector

package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class OrderedVector<E> implements OrderedListADT<E>{
private int currentSize, maxSize;
private E[] storage;
public OrderedVector(){
currentSize = 0;

[code]....

So overall, my remove method should implement binary search and remove elements using either an index or the object value type.

View Replies View Related

Removing Brackets From Array List Printout

Apr 10, 2009

When you "system.out.print(arraylist)" an arraylist, it will give you something like [item1, item2].

Im wondering how I can remove the "[" and "]" brackets fromt he printout, or even if i pass it in as another variable.

View Replies View Related

Linked List Implementation Of List Interface?

Oct 5, 2013

So we have an assignment regarding a linked list implementation of a given list interface.

In my list interface, the method contains(T anEntry) is defined.

In the LList implementation, contains is already implemented as part of getting the core methods in.

Now I am being tasked with the following:

Provide a second implementation of the method contains2(T anEntry) that calls a private recursive method

Private boolean contains (T anEntry, Node startNode) that returns whether the list that starts at startNode contains the entry anEntry.

I've written the private recursive method already. That's not an issue (at least not right now).

But what I don't understand is how startNode is supposed to be populated when this private contains method is called from the public contains2 method? contains2 only takes one parameter: anEntry. the private method takes two parameters: anEntry and startNode. How am i supposed to provide startNode when I am calling contains2?

View Replies View Related

List Interface Class That Has Operations For Linked List And LList Class

Oct 6, 2014

I have this ListInterface class that has operations for my linked list and a LList class. The Llist and ListInterface classes are perfect. My job is to create a driver, or a demo class that showcases these operations. That being said, heres the driver so far:

import java.util.*;
public abstract class DriverWilson implements ListInterface
{
public static void main(String[] args)
{

LList a = new LList();

[code]....

View Replies View Related

Inserting A Set Into Linked List?

Mar 21, 2014

What I'm supposed to do is make a method to insert a set of Tiles to the list,i.e.,a detour(make sure that the inserted detouris compatible with thecurrent path so that the resultingpathdoesnot have any gaps). But I'm confused on how to go about doing it. I was thinking of maybe just adding 1 to the current Node.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
public class Path {
static Tile startTile;

[code].....

View Replies View Related

Dynamic Linked List

Jan 30, 2014

I'm trying to implement an Office class that contains an inner class: WorkerNode. The WorkerNode class has a name attribute (String) and WorkerNode attributes for boss, peer and subordinate. The attributes of Office are manager and current which are WorkerNode references. The manager refers to the entry point of the structure and current is the current node in the structure. For simplicity, i'm going to try to limit it to 3 levels and assume that the names are unique. I've put together a Office class that containing main and provided the code I've worked on so far.

public class Office {
public static void main(String[] args) {
String name=Input.getString("input the manager's name: ");
Office office=new Office(name);
int option;

[code]....

View Replies View Related

Linked List Sorting

Apr 20, 2014

I have made a node class and im trying to implement a sorting method. I must use a selection sort but with specific instructions: "Your method should not need to use the new operator since it is just moving nodes from one list to another( not creating new nodes)

this is my current implementation ..but i am instantiating new object..

public class NodeInt
{
private int data;
private NodeInt next = null;
public NodeInt(){}
//precondition:
//postcondition:
public NodeInt(int data, NodeInt next)
{
this.data = data;
this.next = next;

[code]....

edit: this is the part that worked but i had it commented out so i have the previous and current declared above but didnt copy.

View Replies View Related

Searching Linked List

Apr 30, 2014

Ok here I have a code that generates 1 million random values then converts them to a string then hashcode. I then insert into a linked list and then I want to run through each hash and find it in the linked list timing each run then averaging out the time at the end.

It works great for smaller amounts of numbers it is searching for (fine under 50 thousand searches for the for loop starting at line 24 LinkedListTest.java) but when I try to do the full million searches it gives me "a Exception in thread "main" java.lang.StackOverflowError" at line 158 in List.java. Maybe im getting tired but I cannot figure out why.

// class to represent one node in a list
class ListNode< T >
{
// package access members; List can access these directly
T data; // data for this node
ListNode< T > nextNode; // reference to the next node in the list

[code]....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved