如何使用我自己创建的“链表"中的职位?如何在某个位置后插入节点? [英] How to use the positions in the Linked List which was made by myself? How to insert a node after some positon?

查看:145
本文介绍了如何使用我自己创建的“链表"中的职位?如何在某个位置后插入节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我编写了一个程序,并创建了自己的类链接列表".但是我不明白如何使用职位.如果您有任何想法,请帮忙.
我想使用insertAfter方法,但是我无法指定节点的位置.
关于))))

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))))

****************************************************************
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]添加了代码块以保留格式-OriginalGriff [/edit]



[edit]Code block added to preserve formatting - OriginalGriff[/edit]

推荐答案

您可能可以从Java LinkedList [
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.


这篇关于如何使用我自己创建的“链表"中的职位?如何在某个位置后插入节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆