?
Solved

creating a linked list

Posted on 2003-03-13
10
Medium Priority
?
172 Views
Last Modified: 2010-03-31
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
Comment
Question by:javatech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
10 Comments
 

Author Comment

by:javatech
ID: 8134315
it's important
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8134399
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
 

Author Comment

by:javatech
ID: 8134428
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Mayank S
ID: 8134449
Fine, what is the error. (Please close the class definition with a curly-brace } ).

Mayank.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8134599
you can find the source of the LinkedList in the src.zip file in your jdk directory
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8134977
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135402
Your linked list should be a collection of nodes, not of linked lists ;-)
0
 

Expert Comment

by:bhayzone
ID: 8135443
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8135920
> Your linked list should be a collection of nodes, not of linked lists ;-)

What's in a name, la la la la ;-)
0
 
LVL 4

Accepted Solution

by:
funnyveryfunny earned 150 total points
ID: 8138428
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question