Question

JAVA - Circular Doubly Linked List

Does anybody could help me with this method below(previous)?

public E previous() {
//
Returns the previous Element
return null;
}

Explanation:

We have this class with these two implemented inferfaces:

New Java Class Java Class Create a new Java class. Source folder: cmoore/src Browse... Package: edu.ics 211.h06 Browse... Enc

The interfaces are:

package edu.ics211.h04;

/**
 * Interface for a List211.
 *
 * @author Cam Moore
 * @param  the generic type of the Lists.
 */
public interface IList211 {
  /**
   * Gets the item at the given index.
   * @param index the index.
   * @return the item at the given index.
   */
  E get(int index);

  /**
   * Sets the item at the given index.
   * @param index the index.
   * @param element the new element to set.
   * @return the old element at index.
   */
  E set(int index, E element);

  /**
   * Returns the index of the given obj or -1.
   * @param obj the object to find.
   * @return the index of the given obj or -1.
   */
  int indexOf(Object obj);

  /**
   * Returns the size of this list.
   * @return the size of this list.
   */
  int size();

  /**
   * Adds e to the end of the list.
   * @param e the item to add.
   * @return true if successful, false otherwise.
   */
  boolean add(E e);

  /**
   * Adds element to the list at the given index.
   * @param index the index.
   * @param element the element to add.
   */
  void add(int index, E element);

  /**
   * Removes the element at the given index.
   * @param index the index.
   * @return The element removed from the list.
   */
  E remove(int index);
}

Inside CircularDoublyLinkedList we have another class that implements

public interface ListIterator {
  void add(E e); // Inserts the specified element to the list. (Optional for this assignment)
  boolean hasNext(); // Returns true if this list iterator has more elements while traversing in the forward direction.
  boolean hasPrevious(); // Returns true if this list iterator has more elements while traversing in the reverse direction.
  E next(); // Returns the next Element.
  int nextIndex(); // Returns the index of the next element.
  E previous(); // Returns the previous Element
  int previousIndex(); // Returns the index of the previous element.
  void remove(); // Removes from the list the last element that was returned.
  void set(E e); // Replaces the last element returned. (Optional for this assignment)

}

Thank you very much for your help!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You can refer the below C code for finding the previous element of Circular Doubly Linked list:

#include <stdio.h>
#include <stdlib.h>
struct node
{   int val;
    struct node *next;
    struct node *prev;    
};
typedef struct node n;
void PreviousElement();
n *new, *ptr, *prev, *ptr_prev=NULL;
n *first = NULL, *last = NULL;
int number = 0;
void main()
{  
     n *previous;
     previous=PreviousElement();
}    
n PreviousElement()
{
    int key, i, f = 0;
    printf("\nEnter the value of which you want to find previous element:");
    scanf("%d", &key);
    if (first == last && first == NULL)
        printf("\nList is empty no elemnets in list to search");
    else
    {
        for (ptr = first,i = 0;i < number;i++,ptr = ptr->next)
        {   
            if (ptr->val == key)
            {   ptr_prev=ptr->prev;        
                f = 1;

return  ptr_prev;

            }    
        }
        if (f == 0)
            printf("\n the value is not found in linkedlist");
    }

return ptr_prev;

  }
 
Add a comment
Know the answer?
Add Answer to:
JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

    Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Generic Linked Lists ( Program should be written in Java language). Modify the linked list class...

    Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of...

    Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return an iterator over...

  • On Java: Problems Problem 1. (Deque) Hints: my Use a doubly-linked list Node to implement the...

    On Java: Problems Problem 1. (Deque) Hints: my Use a doubly-linked list Node to implement the API — each node in the list stores a generic item, and references next and prev to the next and previous nodes in the list null itemi → item2 item3 ... itemn null Instance variables wy Reference to the front of the deque, Node first my Reference to the back of the deque, Node last my Size of the deque, int n + LinkedDeque...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT