您好,欢迎访问一九零五行业门户网

JAVA 链表操作

一、单链表循环链表
package linklisttest; import java.util.hashmap; import java.util.map; public class singlecyclelinklist implements icommoperate { private snode head = new snode(head) ; // 公共头指针,声明之后不变 private int size = 0 ; public int getsize() { return this.size; } /* * 链表插入,每次往末端插入,判定末端的标准为next是否指向head * */ @override public boolean insertnode(snode node) { boolean flag = false ; initlinklist() ; // 初始化链表 if( this.size==0 ){ // 空链表 this.head.setnextnode(node) ; node.setnextnode(this.head) ; }else{ snode current = this.head ; while( current.getnextnode()!=this.head ){ // 找到末端节点 current = current.getnextnode() ; } current.setnextnode(node) ; node.setnextnode(this.head) ; // 循坏链表,尾节点指向head } this.size++ ; flag = true ; return flag; } /* * 插入链表指定位置pos,从1开始,而pos大于size则插入链表末端 * */ @override public boolean insertposnode(int pos, snode node) { boolean flag = true ; snode current = this.head.getnextnode() ; initlinklist() ;// 初始化链表 if( this.size==0 ){ // 链表为空 this.head.setnextnode(node) ; node.setnextnode(this.head) ;// 循坏链表,尾节点指向head this.size++ ; }else if( this.size0 && pos<=this.size ){ // 链表内节点 // 1、找到要插入pos位置节点和前节点,node将插入两个节点之间 int find = 0; snode prenode = this.head; // 前节点 snode currentnode = current; // 当前节点 while( find
其它类似信息

推荐信息