java 链表、栈、队列、树的实现

最近无意中翻到一本书,闲来无事写几行代码,实现几种常用的数据结构,以备后查。

一、线性表(链表)

1、节点定义

  1. /**链表节点定义 
  2.  * @author colonel 
  3.  * 
  4.  */  
  5. class Node {  
  6.     public int data;  
  7.     Node next=null;  
  8.     public Node(int data){  
  9.         this.data=data;  
  10.     }  
  11.       
  12. }  
/**链表节点定义
 * @author colonel
 *
 */
class Node {
	public int data;
	Node next=null;
	public Node(int data){
		this.data=data;
	}
	
}
2、链表操作类

  1. /**链表操作类 
  2.  * @author colonel 
  3.  * 
  4.  */  
  5. public class operateClass {  
  6.   
  7.     public Node headNode=null;  
  8.       
  9.     /*给链表添加界节点 
  10.      * @param data 链表节点数据 
  11.      */  
  12.     public Node addNode(int data){   
  13.         Node newNode=new Node(data);  
  14.         if (headNode==null) {  
  15.             headNode=newNode;  
  16.             newNode.next=null;  
  17.             return headNode;  
  18.         }  
  19.         Node tempNode=headNode;  
  20.         while (tempNode.next!=null) {  
  21.             //tempNode=headNode;  
  22.             tempNode=tempNode.next;  
  23.         }  
  24.         tempNode.next=newNode;  
  25.         return headNode;  
  26.     }  
  27.       
  28.     /**删除节点 
  29.      * @param 删除节点的位置 
  30.      *  
  31.      */  
  32.     public boolean delNode(int index){  
  33.           
  34.         if (index<1||index>length()) {  
  35.             return false;  
  36.         }  
  37.           
  38.         if (index==1) {  
  39.             headNode=headNode.next;  
  40.             return true;  
  41.         }  
  42.         Node preNode=headNode;  
  43.         Node curNode=preNode.next;  
  44.         int count=2;  
  45.         while (curNode!=null) {  
  46.             if (count==index) {  
  47.                 preNode.next=curNode.next;  
  48.                 return true;  
  49.             }  
  50.             preNode=curNode;  
  51.             curNode=curNode.next;  
  52.             count++;              
  53.         }         
  54.         return true;  
  55.     }  
  56.       
  57.     /**取链表的长度 
  58.      * @return返回链表的长度 
  59.      */  
  60.     public int length(){  
  61.         int length=0;  
  62.         Node temp=headNode;  
  63.         while (temp!=null) {  
  64.             length++;  
  65.             temp=temp.next;  
  66.         }         
  67.         return length;  
  68.     }  
  69.       
  70.       
  71.       
  72.     /**按照值域对链表数据排序 
  73.      * @return 返回排序后的链表头节点 
  74.      */  
  75.     public Node orderList(){  
  76.         Node nextNode=null;  
  77.         int temp=0;  
  78.         Node curNode=headNode;  
  79.         while (curNode.next!=null) {  
  80.             nextNode=curNode.next;  
  81.             while (nextNode!=null) {  
  82.                 if (curNode.data>nextNode.data) {  
  83.                 temp=curNode.data;  
  84.                 curNode.data=nextNode.data;  
  85.                 nextNode.data=temp;  
  86.               
  87.             }  
  88.             nextNode=nextNode.next;  
  89.             }  
  90.                 curNode=curNode.next;  
  91.           
  92.         }  
  93.             return headNode;  
  94.     }  
  95.       
  96.     /** 
  97.      * 去除链表中值域重复的元素 
  98.      */  
  99.     public void redRepeat(){  
  100.         if (length()<=1) {  
  101.             return;  
  102.         }  
  103.         Node curNode=headNode;    
  104.         while (curNode!=null) {  
  105.             Node insidNode=curNode.next;  
  106.             Node insidPreNode=insidNode;  
  107.             while (insidNode!=null) {  
  108.                 if (insidNode.data==curNode.data) {  
  109.                     insidPreNode.next=insidNode.next;                 
  110.                     //return;  
  111.                 }  
  112.                 insidPreNode=insidNode;  
  113.                 insidNode=insidNode.next;             
  114.             }  
  115.               
  116.             curNode=curNode.next;  
  117.   
  118.               
  119.         }     
  120.     }  
  121.       
  122.     /**倒序输出链表中所有的数据 
  123.      * @param hNode 链表头节点 
  124.      */  
  125.     public void reversePrint(Node hNode){  
  126.         if (hNode!=null) {  
  127.             reversePrint(hNode.next);  
  128.             System.out.println(hNode.data);  
  129.         }  
  130.     }  
  131.   
  132.     /** 
  133.      * 从头节点开始到为节点结尾打印出值 
  134.      */  
  135.     public void printList(){  
  136.         Node tmpNode=headNode;  
  137.         while (tmpNode!=null) {  
  138.             System.out.println(tmpNode.data);  
  139.             tmpNode=tmpNode.next;  
  140.         }  
  141.     }  
  142.               
  143.       
  144. }  
/**链表操作类
 * @author colonel
 *
 */
public class operateClass {

	public Node headNode=null;
	
	/*给链表添加界节点
	 * @param data 链表节点数据
	 */
	public Node addNode(int data){ 
		Node newNode=new Node(data);
		if (headNode==null) {
			headNode=newNode;
			newNode.next=null;
			return headNode;
		}
		Node tempNode=headNode;
		while (tempNode.next!=null) {
			//tempNode=headNode;
			tempNode=tempNode.next;
		}
		tempNode.next=newNode;
		return headNode;
	}
	
	/**删除节点
	 * @param 删除节点的位置
	 * 
	 */
	public boolean delNode(int index){
		
		if (index<1||index>length()) {
			return false;
		}
		
		if (index==1) {
			headNode=headNode.next;
			return true;
		}
		Node preNode=headNode;
		Node curNode=preNode.next;
		int count=2;
		while (curNode!=null) {
			if (count==index) {
				preNode.next=curNode.next;
				return true;
			}
			preNode=curNode;
			curNode=curNode.next;
			count++;			
		}		
		return true;
	}
	
	/**取链表的长度
	 * @return返回链表的长度
	 */
	public int length(){
		int length=0;
		Node temp=headNode;
		while (temp!=null) {
			length++;
			temp=temp.next;
		}		
		return length;
	}
	
	
	
	/**按照值域对链表数据排序
	 * @return 返回排序后的链表头节点
	 */
	public Node orderList(){
		Node nextNode=null;
		int temp=0;
		Node curNode=headNode;
		while (curNode.next!=null) {
			nextNode=curNode.next;
			while (nextNode!=null) {
				if (curNode.data>nextNode.data) {
				temp=curNode.data;
				curNode.data=nextNode.data;
				nextNode.data=temp;
			
			}
			nextNode=nextNode.next;
			}
				curNode=curNode.next;
		
		}
			return headNode;
	}
	
	/**
	 * 去除链表中值域重复的元素
	 */
	public void redRepeat(){
		if (length()<=1) {
			return;
		}
		Node curNode=headNode;	
		while (curNode!=null) {
			Node insidNode=curNode.next;
			Node insidPreNode=insidNode;
			while (insidNode!=null) {
				if (insidNode.data==curNode.data) {
					insidPreNode.next=insidNode.next;				
					//return;
				}
				insidPreNode=insidNode;
				insidNode=insidNode.next;			
			}
			
			curNode=curNode.next;

			
		}	
	}
	
	/**倒序输出链表中所有的数据
	 * @param hNode 链表头节点
	 */
	public void reversePrint(Node hNode){
		if (hNode!=null) {
			reversePrint(hNode.next);
			System.out.println(hNode.data);
		}
	}

	/**
	 * 从头节点开始到为节点结尾打印出值
	 */
	public void printList(){
		Node tmpNode=headNode;
		while (tmpNode!=null) {
			System.out.println(tmpNode.data);
			tmpNode=tmpNode.next;
		}
	}
			
	
}

二、栈

1、该栈使用数组实现,具体的栈操作类

  1. class MyStack<E>{  
  2.     private Object[] stack;  
  3.      int top=-1;  
  4.     public MyStack(){  
  5.         stack=new Object[10];  
  6.     }  
  7.     public boolean isEmpty(){  
  8.         return top==0;  
  9.     }  
  10.       
  11.     /**弹出栈顶元素(不删除) 
  12.      * @return 
  13.      */  
  14.     public E peek(){  
  15.         if (isEmpty()) {  
  16.             return null;  
  17.         }  
  18.         return (E) stack[top];  
  19.     }  
  20.     /**出栈站顶元素 
  21.      * @return 栈顶元素 
  22.      */  
  23.     public E pop(){  
  24.         E e=peek();  
  25.         stack[top]=null;  
  26.         top--;  
  27.         return e;  
  28.     }  
  29.     /**压栈 
  30.      * @param item 待压元素 
  31.      * @return 返回待压元素 
  32.      */  
  33.     public E push(E item){  
  34.         //ensureCapacity(top+1);  
  35.         stack[++top]=item;  
  36.         return item;  
  37.     }  
  38.     /**栈满扩容 
  39.      * @param size 
  40.      */  
  41.     public void ensureCapacity(int size){  
  42.         int len=stack.length;  
  43.         if (size>len) {  
  44.             int newLen=10;  
  45.             stack=Arrays.copyOf(stack, newLen);  
  46.               
  47.         }  
  48.     }  
  49.     /**返回栈顶元素 
  50.      * @return 
  51.      */  
  52.     public E getTop(){  
  53.         if (top==-1) {  
  54.             return null;  
  55.         }  
  56.         return (E) stack[top];  
  57.     }  
  58.       
  59.   
  60. }  
class MyStack<E>{
	private Object[] stack;
	 int top=-1;
	public MyStack(){
		stack=new Object[10];
	}
	public boolean isEmpty(){
		return top==0;
	}
	
	/**弹出栈顶元素(不删除)
	 * @return
	 */
	public E peek(){
		if (isEmpty()) {
			return null;
		}
		return (E) stack[top];
	}
	/**出栈站顶元素
	 * @return 栈顶元素
	 */
	public E pop(){
		E e=peek();
		stack[top]=null;
		top--;
		return e;
	}
	/**压栈
	 * @param item 待压元素
	 * @return 返回待压元素
	 */
	public E push(E item){
		//ensureCapacity(top+1);
		stack[++top]=item;
		return item;
	}
	/**栈满扩容
	 * @param size
	 */
	public void ensureCapacity(int size){
		int len=stack.length;
		if (size>len) {
			int newLen=10;
			stack=Arrays.copyOf(stack, newLen);
			
		}
	}
	/**返回栈顶元素
	 * @return
	 */
	public E getTop(){
		if (top==-1) {
			return null;
		}
		return (E) stack[top];
	}
	

}



三、队列

该队列使用链式实现

1、队节点定义

  1. /** 
  2.  * @author colonel 
  3.  *队节点定义 
  4.  * @param <Elem> 
  5.  */  
  6. class queueNode<Elem>{  
  7.     queueNode<Elem> nextNode=null;  
  8.     Elem data;  
  9.     public queueNode(Elem data){  
  10.         this.data=data;  
  11.     }  
  12.       
  13. }  
/**
 * @author colonel
 *队节点定义
 * @param <Elem>
 */
class queueNode<Elem>{
	queueNode<Elem> nextNode=null;
	Elem data;
	public queueNode(Elem data){
		this.data=data;
	}
	
}
2、队列操作类、

  1. /** 
  2.  * @author colonel 
  3.  *队列操作类 
  4.  * @param <Elem> 
  5.  */  
  6. class MyQueue<Elem>{  
  7.     private queueNode<Elem> headNode=null;  
  8.     private queueNode<Elem> tailNode=null;  
  9.     private queueNode<Elem> lastNode=null;  
  10.       
  11.     /**判断该队列是否为空 
  12.      * @return 返回true or false 
  13.      */  
  14.     public boolean isEmpty(){  
  15.         return headNode==tailNode;  
  16.     }  
  17.     /**入队操作 
  18.      * @param data 节点元素值 
  19.      */  
  20.     public void put(Elem data){  
  21.         queueNode<Elem> newNode=new queueNode<Elem>(data);  
  22.         if (headNode==null&&tailNode==null) {  
  23.             headNode=tailNode=newNode;  
  24.             //tailNode=headNode.nextNode;  
  25.             lastNode=tailNode.nextNode;  
  26.             return;  
  27.         }  
  28.         tailNode.nextNode=newNode;  
  29.         tailNode=newNode;  
  30.         lastNode=tailNode.nextNode;  
  31.         //tailNode=tailNode.nextNode;  
  32.     }  
  33.     /**出队操作 
  34.      * @return 返回出队元素                                                                    
  35.      */  
  36.     public Elem pop(){  
  37.         if (headNode==lastNode) {  
  38.             return null;  
  39.         }  
  40.         queueNode<Elem> tempNode=headNode;  
  41.         Elem statElem=tempNode.data;  
  42.         headNode=tempNode.nextNode;  
  43.         return statElem;  
  44.     }  
  45.           
  46.     /**返回队列长度 
  47.      * @return 长度 
  48.      */  
  49.     public int size(){  
  50.         if (isEmpty()) {  
  51.             return 0;  
  52.         }  
  53.         int length=0;  
  54.         queueNode<Elem> temp=headNode;  
  55.         while (temp!=null) {  
  56.             length++;  
  57.             temp=temp.nextNode;  
  58.         }  
  59.         return length;  
  60.     }  
  61.       
  62.       
  63.       
  64. }  
/**
 * @author colonel
 *队列操作类
 * @param <Elem>
 */
class MyQueue<Elem>{
	private queueNode<Elem> headNode=null;
	private queueNode<Elem> tailNode=null;
	private queueNode<Elem> lastNode=null;
	
	/**判断该队列是否为空
	 * @return 返回true or false
	 */
	public boolean isEmpty(){
		return headNode==tailNode;
	}
	/**入队操作
	 * @param data 节点元素值
	 */
	public void put(Elem data){
		queueNode<Elem> newNode=new queueNode<Elem>(data);
		if (headNode==null&&tailNode==null) {
			headNode=tailNode=newNode;
			//tailNode=headNode.nextNode;
			lastNode=tailNode.nextNode;
			return;
		}
		tailNode.nextNode=newNode;
		tailNode=newNode;
		lastNode=tailNode.nextNode;
		//tailNode=tailNode.nextNode;
	}
	/**出队操作
	 * @return 返回出队元素                                                                   
	 */
	public Elem pop(){
		if (headNode==lastNode) {
			return null;
		}
		queueNode<Elem> tempNode=headNode;
		Elem statElem=tempNode.data;
		headNode=tempNode.nextNode;
		return statElem;
	}
		
	/**返回队列长度
	 * @return 长度
	 */
	public int size(){
		if (isEmpty()) {
			return 0;
		}
		int length=0;
		queueNode<Elem> temp=headNode;
		while (temp!=null) {
			length++;
			temp=temp.nextNode;
		}
		return length;
	}
	
	
	
}


四、二叉树

1、节点定义

  1. /**树节点定义 
  2.  * @author colonel 
  3.  * 
  4.  */  
  5. class TreeNode{  
  6.     public int data;  
  7.     public TreeNode leftNode;  
  8.     public TreeNode rightNode;  
  9.     public TreeNode(int data){  
  10.         this.data=data;  
  11.         this.leftNode=null;  
  12.         this.rightNode=null;  
  13.     }  
  14.       
  15. }  
/**树节点定义
 * @author colonel
 *
 */
class TreeNode{
	public int data;
	public TreeNode leftNode;
	public TreeNode rightNode;
	public TreeNode(int data){
		this.data=data;
		this.leftNode=null;
		this.rightNode=null;
	}
	
}

2、二叉树操作类

  1. /**二叉排序树操作类 
  2.  * @author colonel 
  3.  * 
  4.  */  
  5. class OperateTree{  
  6.     public  TreeNode rootNode;  
  7.     public OperateTree(){  
  8.         rootNode=null;  
  9.           
  10.     }  
  11.     /**元素插入二叉排序树 
  12.      * @param data 待插节点数据 
  13.      */  
  14.     public void insert(int data){  
  15.         TreeNode newNode=new TreeNode(data);  
  16.         if (rootNode==null) {  
  17.             rootNode=newNode;  
  18.         }else {  
  19.                 TreeNode current=rootNode;  
  20.                 TreeNode parent;  
  21.             while (true) {  
  22.                 parent=current;  
  23.                 if (data<current.data) {  
  24.                     current=current.leftNode;  
  25.                     if (current==null) {  
  26.                         parent.leftNode=newNode;  
  27.                         return;  
  28.                     }  
  29.                 } else {  
  30.                     current=current.rightNode;  
  31.                     if (current==null) {  
  32.                         parent.rightNode=newNode;  
  33.                         return;  
  34.                     }  
  35.                 }  
  36.             }         
  37.         }  
  38.     }  
  39.       
  40.     /**构建二叉排序树 
  41.      * @param item 元素数组 
  42.      */  
  43.     public void buildTree(int[] item){  
  44.         for (int i = 0; i < item.length; i++) {  
  45.             insert(item[i]);  
  46.         }  
  47.     }  
  48.       
  49.     /** 
  50.      * 先序遍历二叉树 
  51.      */  
  52.     public void preOrder(TreeNode root){  
  53.         if (root!=null) {  
  54.             System.out.println(root.data);  
  55.             preOrder(root.leftNode);  
  56.             preOrder(root.rightNode);     
  57.         }  
  58.   
  59.           
  60.     }  
  61.     /**中序遍历 
  62.      * @param root 
  63.      */  
  64.     public void inOrder(TreeNode root){  
  65.         if (root!=null) {  
  66.             inOrder(root.leftNode);  
  67.             System.out.println(root.data);  
  68.             inOrder(root.rightNode);  
  69.         }  
  70.   
  71.     }  
  72.     /**后序遍历 
  73.      * @param root 
  74.      */  
  75.     public void afterOrder(TreeNode root){  
  76.         if (root!=null) {  
  77.             afterOrder(root.leftNode);  
  78.             afterOrder(root.rightNode);  
  79.             System.out.println(root.data);  
  80.               
  81.         }  
  82.     }  
  83.     /** 
  84.      * 层序遍历二叉排序树 
  85.      */  
  86.     public void layerTrave(){  
  87.         if (this.rootNode==null) {  
  88.             return;  
  89.         }  
  90.         Queue<TreeNode> myQueue=new LinkedList<>();  
  91.         myQueue.add(rootNode);  
  92.         while (!myQueue.isEmpty()) {  
  93.             TreeNode tempNode=myQueue.poll();  
  94.             System.out.println(tempNode.data);  
  95.             if (tempNode.leftNode!=null) {  
  96.                 myQueue.add(tempNode.leftNode);  
  97.             }  
  98.             if (tempNode.rightNode!=null) {  
  99.                 myQueue.add(tempNode.rightNode);  
  100.             }  
  101.         }  
  102.     }  
  103.       
/**二叉排序树操作类
 * @author colonel
 *
 */
class OperateTree{
	public  TreeNode rootNode;
	public OperateTree(){
		rootNode=null;
		
	}
	/**元素插入二叉排序树
	 * @param data 待插节点数据
	 */
	public void insert(int data){
		TreeNode newNode=new TreeNode(data);
		if (rootNode==null) {
			rootNode=newNode;
		}else {
				TreeNode current=rootNode;
				TreeNode parent;
			while (true) {
				parent=current;
				if (data<current.data) {
					current=current.leftNode;
					if (current==null) {
						parent.leftNode=newNode;
						return;
					}
				} else {
					current=current.rightNode;
					if (current==null) {
						parent.rightNode=newNode;
						return;
					}
				}
			}		
		}
	}
	
	/**构建二叉排序树
	 * @param item 元素数组
	 */
	public void buildTree(int[] item){
		for (int i = 0; i < item.length; i++) {
			insert(item[i]);
		}
	}
	
	/**
	 * 先序遍历二叉树
	 */
	public void preOrder(TreeNode root){
		if (root!=null) {
			System.out.println(root.data);
			preOrder(root.leftNode);
			preOrder(root.rightNode);	
		}

		
	}
	/**中序遍历
	 * @param root
	 */
	public void inOrder(TreeNode root){
		if (root!=null) {
			inOrder(root.leftNode);
			System.out.println(root.data);
			inOrder(root.rightNode);
		}

	}
	/**后序遍历
	 * @param root
	 */
	public void afterOrder(TreeNode root){
		if (root!=null) {
			afterOrder(root.leftNode);
			afterOrder(root.rightNode);
			System.out.println(root.data);
			
		}
	}
	/**
	 * 层序遍历二叉排序树
	 */
	public void layerTrave(){
		if (this.rootNode==null) {
			return;
		}
		Queue<TreeNode> myQueue=new LinkedList<>();
		myQueue.add(rootNode);
		while (!myQueue.isEmpty()) {
			TreeNode tempNode=myQueue.poll();
			System.out.println(tempNode.data);
			if (tempNode.leftNode!=null) {
				myQueue.add(tempNode.leftNode);
			}
			if (tempNode.rightNode!=null) {
				myQueue.add(tempNode.rightNode);
			}
		}
	}
	

五、总结

  更好的理解数据结构为何物,还需继续探索,谨记。by:colonel








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值