Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

creating a linked list

how to create a linked list with the add,remove & insert operations.i don't want the inbuilt utility of linked list class from java.util class.can someone give the code as to how to go about it.i ahve created it partly,but it's not running?
0
javatech
Asked:
javatech
  • 2
  • 2
  • 2
  • +4
1 Solution
 
javatechAuthor Commented:
it's important
0
 
Mayank SAssociate Director - Product EngineeringCommented:
There's no pointer in Java, so I guess you have to settle for the LinkedList class. Unless you want the linked-list to be something different from the normal linked-lists! Try in C ++, if you want to use pointers, or else, is it a compulsion to do it in Java without using the in-built class??

Mayank.

PS: Can you post a part of your code so that we can have an idea of your approach?
0
 
javatechAuthor Commented:
class SingleLinkedList{
//public static void main(String args[]) throws IOException{
Object value;
SingleLinkedList next;

SingleLinkedList()
{
     value=null;
}

SingleLinkedList(Object element)
{
     value=element;
}
SingleLinkedList(Object element,SingleLinkedList n)
{
     value=element;
     next=n;
}

Object getValue()
{
     return value;
}
SingleLinkedList getNext()
{
     return next;
}    

void setValue(Object element)
{
     value=element;
}
void setNext(SingleLinkedList next1)
{
     next=next1;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
Fine, what is the error. (Please close the class definition with a curly-brace } ).

Mayank.
0
 
Venci75Commented:
you can find the source of the LinkedList in the src.zip file in your jdk directory
0
 
TimYatesCommented:
class SingleLinkedList
{
Object value;
SingleLinkedList next;

SingleLinkedList()
{
    value=null;
}

SingleLinkedList(Object element)
{
    value=element;
}

SingleLinkedList(Object element,SingleLinkedList n)
{
    value=element;
    next=n;
}

Object getValue()
{
    return value;
}

SingleLinkedList getNext()
{
    return next;
}    

void setValue(Object element)
{
    value=element;
}

void setNext(SingleLinkedList next1)
{
    next=next1;
}

void insert( SingleLinkedList newNode )
{
  newNode.setNext( next ) ;
  setNext( newNode ) ;
}

SingleLinkedList removeNext()
{
  SingleLinkedList nxt = next ;
  next = next.getNext() ;
  return nxt ;
}
}
0
 
CEHJCommented:
Your linked list should be a collection of nodes, not of linked lists ;-)
0
 
bhayzoneCommented:
You can try using an array implementation of linked list.

Declare an array large enough to hold all your objects.

Your object has two fields 1] The data 2] an index to the next element in the array (optionally you can even have an index to the previous element).

Though this method completely contradicts the concept of a Linked List ... but maybe it mite work for u.
0
 
TimYatesCommented:
> Your linked list should be a collection of nodes, not of linked lists ;-)

What's in a name, la la la la ;-)
0
 
funnyveryfunnyCommented:
public class LinkedList {

  Node first, currentNode;
  static int id_master=0;

  public LinkedList() {
    first = new Node();
    currentNode = first;
    first.id = id_master;
    id_master++;
  }

  public LinkedList(Object d){
    this();
    first.data = d;
  }

  // Adding to the back a list
  public void add(){
    Node n = new Node();
    currentNode.next = n;
    n.prev = currentNode;
    currentNode = n;
    n.id = id_master;
    id_master++;
  }

  public void add(Object data){
    add();
    currentNode.data=data;
  }

  // Removing the last node
  public void removeLast(){
    currentNode.prev.next=null;
    currentNode = currentNode.prev;
  }

  // Removing a specific node
  private boolean remove(Node n){

    if((n.prev==null)&&(!n.equals(first))){
      return false;
    }

    else if(n.equals(first)){
      first = first.next;
      first.prev = null;
      return true;
    }

    else{
      n.prev.next = n.next;
      if(n.next!=null)
        n.next.prev = n.prev;
      return true;
    }

  }

  public boolean removeNode(int id){
    return remove(getNode(id));
  }

  // This will insert node _n_ after node _des_ position
  public boolean insert(Node n, Node des){

    //Testing the existen of node _des_ in a list
    if((des.prev==null)&&(!n.equals(first))){
      return false;
    }

    else{
      n.id = id_master;
      id_master++;
      n.next = des.next;
      des.next.prev = n;
      n.prev = des;
      des.next = n;
      return true;
    }

  }

  public Node getNode(int id){
    Node n = first;
    while(n.id!=id){
      n = n.next;
    }
    return n;
  }

  public void printList(){
    Node n = first;
    while(n!=null){
      System.out.println(n.data);
      n = n.next;
    }
  }

  public static void main(String[] args) {
    LinkedList l = new LinkedList(new String("testing"));
    System.out.println("=======Testing add() method========");
    l.add(new String("a"));
    l.add(new String("linked"));
    l.add(new String("list"));
    l.printList();

    System.out.println("========Testing insert() method=======");
    Node n = new Node();
    n.data = new String(">>>inserted here<<<");
    System.out.println(l.insert(n,l.getNode(2)));
    l.printList();

    System.out.println("========Testing removeNode() method=======");
    System.out.println("Node removed? "+l.removeNode(4));
    l.printList();
  }
}
class Node{
    Object data;
    Node prev,next;
    int id;

    Node(){
      prev = null;
      next = null;
      data = null;
    }
  }

This source code contains the basic methods that are needed to manupilate a LinkedList. But not all methods are provided here. It's now up to you to cover all the possiblities, remember this LinkedList is very flexible so it shouldn't be too hard.

Bye
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now