线性表3-双链表

来源:互联网 发布:php超链接传值 编辑:程序博客网 时间:2024/05/19 04:27

双链表的每个节点包含三个部分:该节点的数据域,节点的直接前驱,节点的直接后继。


插入:不管是在节点p前插入新节点s,还是在p节点后插入节点s,这里有个比较容易记和理解的地方:不管是哪种操作,都后处理p.prior和p.next就对了。

解释下,如果在p之前插入节点s,首先要做的部分是s.prior=p.prior,  s.next=p而后是p.prior.next=s p.prior=s 

此外还要考虑两点,在p之后插入时,p是否为尾节点;在p之前插入时,p是否为第一个节点。

删除:也要考虑删除点是否为尾节点和是否为第一个节点。

程序如下

package list;public class Student {private int id;private String name;private int age;private char gender;public Student(){}public Student(int id,String name){this.id=id;this.name=name;}public int getId(){return id;}public void setId(int id){this.id=id;}public String getName(){return name;}public void setName(String name){this.name=name;}}

package list;public class StuDouNode {private Student stu;private StuDouNode prior;private StuDouNode next;public StuDouNode() {// TODO Auto-generated constructor stub}public StuDouNode(Student stu){this.stu=stu;}public Student getStu() {return stu;}public void setStu(Student stu) {this.stu = stu;}public StuDouNode getPrior() {return prior;}public void setPrior(StuDouNode prior) {this.prior = prior;}public StuDouNode getNext() {return next;}public void setNext(StuDouNode next) {this.next = next;}}


package list;public class LinkList {private StuNode head;//链表头结点public LinkList(StuNode h){head=h;}public void insertAfter(StuNode p,StuNode s){//p节点后插入节点ss.setNext(p.getNext());p.setNext(s);}public void insertBefore(StuNode p,StuNode s){//p节点前插入sif(head==null) return;StuNode q=head;while(q.getNext()!=null){if(q.getNext()==p){q.setNext(s);s.setNext(p);break;}q=q.getNext();}}public void delete(StuNode p){//删除节点pif(head==null) return;StuNode q=head;while(q.getNext()!=null){if(q.getNext()==p){q.setNext(p.getNext());break;}q=q.getNext();}}public void converse(){//链表逆置StuNode p=head.getNext();//每次插入头结点后面的节点if(p==null)return;StuNode q=p.getNext();//保存后半部分链表,也是下一次插入时移动的节点if(q==null)return;p=q;q=p.getNext();//首次移动a1,插入head和a0之间,q=a2,p=a1p.setNext(head.getNext());head.setNext(p);p.getNext().setNext(null);//a0.next为空,将其职为尾节点while(q!=null){//q最后移动到尾节点p=q;q=p.getNext();p.setNext(head.getNext());head.setNext(p);}}public void print(){StuNode p=head.getNext();while(p!=null){System.out.println(p.getStu().getId()+"\t"+p.getStu().getName());p=p.getNext();}}public static void main(String[] args){StuNode head=new StuNode();//头结点,不存数据//head.setStu(hstu);head.setNext(null);LinkList linklist=new LinkList(head);Student stu1=new Student(1,"Alice");//链表中只有一个头结点StuNode a1=new StuNode();a1.setStu(stu1);Student stu2=new Student(2,"Bob");StuNode a2=new StuNode();a2.setStu(stu2);Student stu3=new Student(3,"Cindy");StuNode a3=new StuNode();a3.setStu(stu3);Student stu4=new Student(4,"Jack");StuNode a4=new StuNode();a4.setStu(stu4);Student stu5=new Student(5,"David");StuNode a5=new StuNode();a5.setStu(stu5);System.out.println("insert a1 a2 a3 after head");linklist.insertAfter(head,a1);linklist.insertAfter(a1, a2);linklist.insertAfter(a2,a3);linklist.print();System.out.println("insert a4 before a2");linklist.insertBefore(a2, a4);linklist.print();System.out.println("insert a5 before a1");linklist.insertBefore(a1, a5);linklist.print();System.out.println("converse");linklist.converse();linklist.print();System.out.println("delete a3");linklist.delete(a3);linklist.print();}}




结果:



0 0
原创粉丝点击