Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I wrote a program and created my own class LINKED LIST. But I can't understand how to use positions. If you have any ideas, then please, help.
I want to use the insertAfter method, but i can't dedicate the position of a node.
With respect))))

Java
****************************************************************
package javaapplication9posled;// name of program
import java.io.*;
import javax.swing.text.Position;
import java.io.EOFException;

class Link implements Position {//class
private Object data;
public Link prev;
public Link next;
public Link(Object d)
    {this(null,null,d);}
public Link ( Link n, Link p,Object d)// constructor of class
    {
        
    next=n;
    prev=p;
    data = d;
}
public int getOffset()
    {
        return 0;
    }

 public Object d() throws InvalidPositionException
    {
        if((prev == null) && (next == null))
        {
            throw new InvalidPositionException("position is not in a list");
        }
        return data;
    }

    public void printLink()//print method
{
    System.out.println(data);}
    public class InvalidPositionException extends RuntimeException
    {public InvalidPositionException(String err){super(err);}}
    public Object data() throws InvalidPositionException {
    if ((prev==null)&&(next==null)) throw new InvalidPositionException("Position is not in a list");
    return data;
    
}
public Object getData() 
{
return data;
}
public Link getNext()
 {
return next;
}
public Link getPrev() {return prev;}
public void SetNext(Link newNext) {next=newNext;}
public void SetPrev(Link newPrev) {prev=newPrev;}


public void setData(Object ob) 
{
data=ob;
}
public void setNext(Link n)
{
next=n;
}
}


class LinkList{  //diclaration of linked list
private Link last; 
  public Link first;
    protected int numEl;
protected Link header,trailer;

    public LinkList()  //mathods of the linked list
{

first=null;numEl=0; 
    header=new Link(null,null,null);
    trailer=new Link(header,null,null);
    last=trailer.getPrev();
    header.SetNext(trailer);
    
}
   public Object lastP (Link n){int i; i=numEl; return i; }

     public   boolean isEmpty(){return first==null;}


     public Position insertLast(Object d)
{
         if(trailer.getPrev()==null)
         return insert(d);
         else {
         Link lin=trailer.getPrev();
         numEl++;
         Link link=new Link(lin,trailer,d);
         trailer.SetPrev(link);
         lin.SetNext(link);
         return link;

 }

     }
*********************************************************************

     public Object insertAfter( Link p,Object e) // there i have a problem in calling the position/
    { p=first.next.next;
       Link node=new Link(e);
      
      node.setData(e);
      node.SetPrev(p);
      node.SetNext(p.getNext());
      (p.getNext()).SetPrev(node);
      p.setNext(node);
      return node;


  }// i did'nt understand what position is. i know that it is a link. But how i showl 
get it? And how i can declare it? please, give some advice
*****************************************************************

    public Position insert(Object d)
    {
        numEl++;
        Link link=new Link (header, header.getNext(),d);
        link.next=first;
        first=link;
        header.getNext().SetPrev(link);
        header.SetNext(link);
        return link;
    }
************************************************************
public void printList()
{
    Link currentLink=first;
    System.out.println("List -");
    for(int i=0;i<numel;i++)>        currentLink.printLink();
    currentLink=currentLink.next;
}
    System.out.println("");
public class Main 
{

public static void main(String[] args) throws IOException 
{
     LinkList list= new LinkList();
     list.insertLast("The");
     list.insertLast("cat");
     list.insertLast("sits");
     list.insertLast("on");
     list.insertLast("the");
     list.insertLast("floor");
     list.printList();
     list.insertAfter("//have no idea what","black");//
}
}


[edit]Code block added to preserve formatting - OriginalGriff[/edit]
Posted
Updated 14-Feb-11 22:32pm
v3

1 solution

You could probably get some ideas from the Java LinkedList[^]. However, in your case you need to add some code to index through your list and find the item that you are trying to access by some equality test.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 22:21pm    
All right, 5.
However, I think school questions like that should addressed back to OP's school.
--SA
Richard MacCutchan 16-Feb-11 4:44am    
I don't think I gave much more than the OP's lecturer would offer.
Sergey Alexandrovich Kryukov 16-Feb-11 14:28pm    
Right, this is what I mean. I also mean it's not our job, there are much more important Questions to attend.
Thank you.
--SA
Richard MacCutchan 17-Feb-11 3:34am    
Well, that's a personal decision for everyone to make for themselves.
Sergey Alexandrovich Kryukov 17-Feb-11 4:00am    
Exactly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900