Dequeue Of Queue Implemented Using Circular Singly Linked List With No Tail Reference

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


ADVERTISEMENT

Sort Singly Linked List

Oct 30, 2014

I searched a lot but can't seem to understand the sorting of a SLLNode... I noticed a method called Bubble Sort, I understand how it works, but can't think of a way to implement it to my code..

View Replies View Related

Implementing Singly Linked List

Apr 17, 2015

I am trying to implement a singly linked list. My singly linked list, where I implemented a class named linkedlist, that I defined (this implementation of linkedlist is not the java class linkedlist, it is my own version of linkedlist):

Java Code:

public class SinglyLinkedList<E> implements LinkedList<E> {
public class Node<E> {
public Node<E> next;
public E element;
public Node(E element) {

this.element = element;
}

public Node (E element, Node<E> next) {

[code]...

But when I go to my main method to add into my SinglyLinkedList, it doesn't add anything.

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

Josephus Circular Linked List Java

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

Implement Add Method For Singly Linked List

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

Singly Linked List Not Printing Correctly

Sep 30, 2014

Its a program that creates a singly linked list that stores names and high scores and prints them. For some reason it is printing an entry extra times. Also my remove function is not working properly.

package project;
public class GameEntry implements Comparable<GameEntry> {
private String name;
private int score;
public GameEntry(String n, int s) {
name = n;
score = s;

[code]...

View Replies View Related

Substring Method And Singly Linked List

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

Singly-linked List Program Challenges

Oct 22, 2014

read a text file that has some polynomials in a specific format and perform an arithmetic expression. This needs to be done using a singly-linked list to store the coefficients of the polynomials. There needs to be two methods, one that adds the two polynomials and one to multiply them. So for example suppose i had the following set of polynomials in the text file:

2+x2-3x3 and 1-x-2x2+3x3

these are represented by 2, 0, 1, -3 and 1, -1, -2, 3 respectively. The of the two polynomials is: 3-x-x2 which is represented as: 3, -1, -1

The product of the two polynomials is: 2-2x-3x2+2x3+x4+9x5-9x6

My code is just echoing the data in the text file and not actually doing any arithmetic.

import java.io.*;
import java.util.Scanner;
class Node {
public int digi1;
public int digi2;
public Node nxt;

[code]....

View Replies View Related

Double Circular Linked List - Implementing Iterator

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

Linked List Queue ToString Method

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

Create Program Which Inherit Stack And Queue From A Linked List Class

Nov 18, 2014

I want to create java program in which i want to inherit stack and queue from a linked list class and also make infix to postfix inherit from stack and periority queue from queue class.Ho can i make this program.

View Replies View Related

Implement A Set Abstract Data Type Using Singly Linked Lists

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

Queue Operations In Java - Using Reference As Pointer

May 28, 2014

I am trying to run the queue operations in java such as enqueue, dequeue using pointers ... How to use references as pointer in java.

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

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

Can't Get GUI Program To Print Out Queue List

Apr 11, 2014

I have been working on this Java Gui program and i cant get it to print to the textbox correctly.i originally had it displayed in a dialog window but it would print one integer a time in a seperate window.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import java.io.*;

[code]....

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

EnQueue DeQueue With Gui?

Mar 29, 2015

I am doing a project from my book, basically you type into the textfield and what you type gets added to the queue and displayed in the text area...if you click the dequeue, it takes it out of the queue. It has to be in a gui(which is where I am stuck.)... Here is my classes and code:

Driver:

public class QueueGui {
public static void main(String[] args) {
JFrame frame = new JFrame("Queue Gui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

[Code]....

Basically everything is done, but making the buttons work with the queue. I have a majority of the logic done but cannot get the information to list in the text area and take away...so that is where I am stuck; I am not sure how I would do that with a gui.

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

Building Linked List Whose Nodes Data Is The Sum Of Nodes Of Other List

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

Add Or Remove Will Be More Efficient By A Linked List

Jan 31, 2015

if one address point on another address. so set and get methods will be less efficient then an array, but add or remove will be more efficient by a linked list ? a linked list also inherit from queue so if i add an elemnt via "addFirst" function . where its adding the element ? to the right most or left most ?
if i have for example :

here [ ] --> [ ] --> [ ] --> [ ] -->[ ] or here

linked list its FIFO so the head will be the right most ?

Edit : its confused me a little bit but i understood now .so it will be at the left most. its actually ordered . not like the stack which is LIFO.

View Replies View Related

Insert Method Of Linked List

Dec 30, 2014

Is there a particular implementation of a linked list you are referring to in your question?

View Replies View Related







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