- 单链表数据结构
- 单链表的插入删除
- 头插法:
- 头删除法
-
某个节点后添加新节点
- 删除某个节点
- 头插法:
- 代码
- 链表节点
-
public interface Node { public Object data = null; public Node next=null; public void display(); }
-
public class LinkNode implements Node{ public int iData;//data public double dData;//data public LinkNode next; //reference to next link //constructor public LinkNode(int id,double dd){ iData = id; dData = dd; } public void display(){ //display itself System.out.println ("iData : "+iData+" dData:"+dData); } }//end class LinkNode
-
- 链表
-
public interface class List {//声明 Node first = null; //链表是否为空 public boolean isEmpty() ; // 头插法 public void insertFirst(Object o1) ; // 在指定节点后插入新节点 public boolean insertAfter(Object o1, Object o2) ; // 删除头节点 public Node deleteFirst() ; // 显示链表 public void displayList(); // 找到指定节点 public Node findNode(Object o) ; // 删除指定节点 public Node deleteNode(Object o) ; }
-
/* * 查找效率:O(N) * 删除指定节点效率:O(N) * 增加/删除效率:O(1) * */ public class LinkList extends List{ private LinkNode first; public LinkNode getFirst() { return first; } public void LinkList(){ first = null; // constructor : no items on list yet } public boolean isEmpty(){ return (first == null); } /*头插法 时间效率 O(1)*/ public void insertFirst(int id,double dd){ LinkNode newLink = new LinkNode(id,dd); newLink.next = first; first = newLink; } /*头删法 时间效率 O(1)*/ public LinkNode deleteFirst(){ //如果链表为空 抛出自定义异常 if(first==null) throw new RuntimeException("链表为空 没的可删除了"); LinkNode temp = first; first = first.next; return temp; } public void displayList(){ System.out.println(" List (first-->last): "); LinkNode current = first; while (current!=null){ current.display(); current = current.next; } System.out.println(""); } /*查找指定的节点 * *需要 O(N)次比较*/ public LinkNode findNode(double value){ LinkNode current = first; while (current!=null){ // current.displayLink(); if(current.dData ==value){ return current; } current = current.next; } return null; } /*删除指定的节点 *需要 O(N)次比较* * */ public LinkNode deleteNode(LinkNode l){ LinkNode current = first; LinkNode pre = current; while (current!=null){//the loop end condition:traverse to the last node of the link if(current.dData == l.dData && current.iData==l.iData){//find the node if(current == first) first = first.next; pre.next = current.next; return current; }else{//go on to find it pre = current; current = pre.next; } } return null; } //在某个特定节点node1后插入一个节点node2 /*O[N]*/ public boolean insertAfter(LinkNode node1,LinkNode node2){ //找到这个特定节点 LinkNode target = findNode(node1.dData); System.out.println("target "); target.display(); if (target!=null){ node2.next = target.next; target.next= node2; return true; } return false; } }
-
public class LinkListApp { public static void main(String[] args) { //make a new List LinkList theList = new LinkList(); //insert nodes theList.insertFirst(11, 11.11); theList.insertFirst(22, 22.22); theList.insertFirst(33, 33.33); theList.insertFirst(44, 44.44); theList.displayList(); theList.deleteNode( new LinkNode(44,44.44)); theList.displayList(); theList.displayList();*/ } }
-
- 链表节点
- 单链表的应用
- 栈
- 栈: First in last out
- 栈与单链表:
- 栈 先进 后出
- 链表 insertFirst(int id,double dd) LinkNode deleteFirst()
- 代码
-
public interface Stack { public void push(LinkNode i); public Object pop(); public boolean isEmpty(); public void displayStack(); }
-
public class LinkStack implements Stack{ private LinkList theList; public LinkStack(){ theList = new LinkList(); } public void push(LinkNode node){ theList.insertFirst(node.iData, node.dData); } public LinkNode pop(){ return theList.deleteFirst(); } public boolean isEmpty(){ return theList.isEmpty(); } public void displayStack(){ System.out.println("Stack (top--->bottom):"); theList.displayList(); } }
-
- 有序链表 -->见下篇博客
- 循环链表
- 栈
转载于:https://www.cnblogs.com/cici-new/archive/2012/11/12/2766978.html