本文实例讲述了Java数据结构之链表、栈、队列、树的实现方法。分享给大家供大家参考,具体如下:
最近无意中翻到一本书,闲来无事写几行代码,实现几种常用的数据结构,以备后查。
一、线性表(链表)
1、节点定义
class Node {
public int data;
Node next=null;
public Node(int data){
this.data=data;
}
}
2、链表操作类
public class operateClass {
public Node headNode=null;
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;
}
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;
}
public int length(){
int length=0;
Node temp=headNode;
while (temp!=null) {
length++;
temp=temp.next;
}
return length;
}
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;
}
}
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、该栈使用数组实现,具体的栈操作类
class MyStack{ private Object[] stack; int top=-1; public MyStack(){ stack=new Object[10]; } public boolean isEmpty(){ return top==0; } public E peek(){ if (isEmpty()) { return null; } return (E) stack[top]; } public E pop(){ E e=peek(); stack[top]=null; top--; return e; } public E push(E item){ //ensureCapacity(top+1); stack[++top]=item; return item; } public void ensureCapacity(int size){ int len=stack.length; if (size>len) { int newLen=10; stack=Arrays.copyOf(stack, newLen); } } public E getTop(){ if (top==-1) { return null; } return (E) stack[top]; } }
三、队列
该队列使用链式实现
1、队节点定义
class queueNode{ queueNode nextNode=null; Elem data; public queueNode(Elem data){ this.data=data; } }
2、队列操作类
class MyQueue{ private queueNode headNode=null; private queueNode tailNode=null; private queueNode lastNode=null; public boolean isEmpty(){ return headNode==tailNode; } public void put(Elem data){ queueNode newNode=new queueNode (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; } public Elem pop(){ if (headNode==lastNode) { return null; } queueNode tempNode=headNode; Elem statElem=tempNode.data; headNode=tempNode.nextNode; return statElem; } public int size(){ if (isEmpty()) { return 0; } int length=0; queueNode temp=headNode; while (temp!=null) { length++; temp=temp.nextNode; } return length; } }
四、二叉树
1、节点定义
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、二叉树操作类
class OperateTree{
public TreeNode rootNode;
public OperateTree(){
rootNode=null;
}
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 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
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。



