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
ADVERTISEMENT
Apr 6, 2014
I am creating a josephus java program with a circular linked list.This is what the methods are suppose to do
a. size(): Returns the number of items in the list.
b. isEmpty(): Returns true if the list is empty and false otherwise
c. advance(): Moves the cursor from the current position to the next item. If the cursor is at the end (tail) of this list, calling advance() should make the cursor refer to the first element in the list. If the list is empty, this operation should have no effect.
d. getCurrent(): Return the String value in the node referenced by the cursor unless the CircularLL is empty in which case write an error message to System.err.
e. add(String data): Adds a new item to the list by creating a newNode and linking it to the end of the circular linked list. The last node should point to the new node and then you should make lastNodeInserted point to new node as well.) Please note that a circular linked list is always circular. The last node should point to the beginning node. When there is only one node in a CircularLL, its next should point to itself. This method updates currNode when the first node is added, and always updates lastNodeInserted and numNodes. Take care when adding the first node this is a special case and should be handled differently.
f. remove(): If the list is empty, write an error message to System.err indicating that an attempt was made to remove from an empty queue and return null. Assuming the list is't empty, this method removes the node currently referenced by the cursor, reassigns the cursor to point to the next node in the list, and returns the String value stored in that node. To keep the list linked, you'll need to locate the previous node to the one youre removing and connect its next to the node which follows the one youre removing. Create a local Node variable (prev) to find it. Consider and handle the special case when there is only one node and youre removing it. And make sure to update lastNodeInserted whenever you remove the node that it is pointing to.
g. toString(): This should return a String representation of the circular linked list, with the first value displayed being the data of the first node added (not the data in the last node).
public class CircularLL {
private class Node {
String data;
Node next;
public Node(String data) {
this.data = data;
this.next = null;
[code].....
View Replies
View Related
Oct 14, 2014
My homework is a Double Circular Link list and when i write implements Iterator it gives me a an error when I compile it the same for my subset method...
ERRORS :DoublyCircularList.java:55: error: DoublyCircularList.iterator is not abstract and does not override abstract method remove() in Iterator public class iterator implements Iterator<T>
^
Note: DoublyCircularList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
import java.util.Iterator;
public class DoublyCircularList<T extends Comparable<T>> extends LinkedList<T>implements Iterable<T>
{
Node<T> first;
int size;
public DoublyCircularList(){
first = null;
size = 0;
[code]....
View Replies
View Related
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
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
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
View Related
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
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
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
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
Oct 27, 2014
So I'm trying to build a queue, first in first out, (so add to the head remove from the end) using a linked list for use in another program, I'm having a problem dequeueing where the program seems to run indefinitely without giving an answer, so my suspicion is its caught in a while loop but how and why I can't figure out.
public class CircularQueuelist {
private Node head = null;
private int size = 0;
private class Node
{
int data;
Node next;
[Code] ....
My logic seems sound, I basically look for when the second node over from the current one I'm on is a reference to the head, and then skip the one in front of it using setting currents. next link to current.next = head, severing the link to the last node.
This is what my driver looks like, I enqueue items 1-10 and then use the iterator to make sure it worked out fine and check size, its when I dequeue that I run into a problem, the program runs indefinitely.
public class Queuetest {
public static void main(String[]args)
{
CircularQueuelist test = new CircularQueuelist();
for (int count = 0; count < 10; count ++)
[Code] ....
View Replies
View Related
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
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
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
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
Feb 6, 2014
Here is a small part of my program
Java Code:
// for every attribute not mentioned in the rule r
enumAtt = ruleE.enumerateAttributes();
while (enumAtt.hasMoreElements()) {
Attribute attr = (Attribute) enumAtt.nextElement();
if (isMentionedIn(attr, r.m_test)) {continue;}
int M = attr.numValues();
[Code] ....
It repeats only the last rule , why is that !!!!!!
View Replies
View Related
Feb 14, 2014
i have the following code to reverse the elements of the list but i only print the last element each time
public void reverse(OrderedList<T> lister) {
OrderedList<T> listerCopy = lister;
for(int i=0;i<5;++i) {
T removed=removeFromBack(listerCopy);
System.out.printf("%s ", removed);
[Code] .....
View Replies
View Related
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
Jan 8, 2015
Lets suppose that I pass to the sort method a list of 2 objects of the same class (which implements Comparable interface). I read (in head first java) that one object is compared relative to another with one object calling the CompareTo() while the other object being passed as a parameter to the same method. Now am I safe in assuming that the first object in the list calls the method with the second object being passed as a parameter.And also how does the CompareTo() work if there are more than 2 elements in the list. Which objcet calls the method and which is passed as a parameter?
View Replies
View Related
Dec 17, 2014
In my project I need to create a pizza ordering system. I have created a array list of pizza toppings and each pizza topping must have a cost property associated with it, then the user can pick the toppings they want and the cost of each topping will be added up and the total cost of the whole pizza will be displayed. But I don't know how to associate a cost with each topping. I have been told that I can access the cost through a get/set method and store it in a private member variable. But I don't know how to do this?
I've tried to do it, but it hasn't worked. This is the code
public void setCost( double cost){ //my get/set method
this.cost = cost;
}
public double getCost(){
double price = 0;
return price;
[Code] .....
But there is an error on each of the ingrediantCost method lines, saying
The primitive type double of cost does not have a field Pepperoni
...and the same for chicken, ham and onion.
View Replies
View Related
Sep 11, 2014
If you see in the code below where I print "Would you like to use previous key" I need to be able to save the previous key answers to be used again.. not sure how to go about doing this..
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] studentName = new String[20];
int[] studentAverage = new int[20];
Scanner input = new Scanner(System.in);
[Code] .....
View Replies
View Related
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
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
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
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
May 1, 2014
public void add(int d){
listNode l = new listNode (d, null);
l.next = first;
first= l;
}
public list Sum2List (list l1, list l2){
//variables
int sum;
[Code] .....
But I have a problem in my first listNode where it ll be pointing to null, thus in the sum2List method the program checks the while condition into false and doesn't go through the loop.
View Replies
View Related