栈
public class MyStack
{
private long arr[];
private int top;
public long arrQueue[];
public MyStack()
{
arr=new long[10];
arrQueue=new long[10];
top=-1;
}
public MyStack(int maxsize)
{
arr=new long[maxsize];
arrQueue=new long[maxsize];
top=-1;
}
public void push(long value)
{
arr[++top]=value;
}
public long pop()
{
return arr[top--];
}
public long peek()
{
return arr[top];
}
public boolean isEmpty()
{
return top==-1;
}
public boolean isFull()
{
return top==arr.length-1;
}
}
测试
public class TestMyStack
{
public static void main(String[] args)
{
MyStack ms=new MyStack(5);
ms.push(12);
ms.push(13);
ms.push(16);
ms.push(23);
ms.push(121);
System.out.print(ms.isEmpty()+"\n");
System.out.print(ms.isFull()+"\n");
// while(!ms.isEmpty())
// {
// System.out.print(ms.pop()+",");
// }
System.out.print("\n");
System.out.print(ms.isEmpty()+"\n");
MyStack mq=new MyStack(5);
// mq.push(ms.pop());
// mq.push(ms.pop());
// mq.push(ms.pop());
// mq.push(ms.pop());
// mq.push(ms.pop());
for(int i=0;i<5;i++)
{
mq.push(ms.pop());
}
System.out.print(mq.isEmpty()+"\n");
System.out.print(mq.isFull()+"\n");
while(!mq.isEmpty())
{
System.out.print(mq.pop()+",");
}
}
}
队列
public class MyQueue
{
private long arr[];
private int elements;
private int front;
private int end;
public MyQueue()
{
arr=new long[10];
elements=0;
front=0;
end=-1;
}
public MyQueue(int maxsize)
{
arr=new long[maxsize];
elements=0;
front=0;
end=-1;
}
public void insert(int value)
{
arr[++end]=value;
elements++;
}
public long remove()
{
return arr[front++];
}
public long peek()
{
return arr[front];
}
public boolean isEmpty()
{
return elements==0;
}
public boolean isFull()
{
return elements==arr.length;
}
}
测试
public class TestMyQueue
{
public static void main(String[] args)
{
MyQueue mq=new MyQueue(4);
mq.insert(11);
mq.insert(15);
mq.insert(35);
mq.insert(153);
System.out.println(mq.isEmpty());
System.out.println(mq.isFull());
while(!mq.isEmpty())
{
System.out.println(mq.remove()+",");
}
}
}
二叉树:
Node.java
public class Node
{
public long data;
public String sData;
public Node leftChild;
public Node rightChild;
public Node(long data,String sData)
{
this.data=data;
this.sData=sData;
}
}
tree.java
import com.sun.istack.internal.NotNull;
import com.sun.scenario.effect.impl.prism.ps.PPSBlend_ADDPeer;
import sun.launcher.resources.launcher;
public class Tree
{
public Node root;
public void insert(long value,String sData)
{
Node newnode=new Node(value,sData);
Node current=root;
Node parent;
if(root==null)
{
root=newnode;
return;
}
else
{
while(true)
{
parent=current;
if(current.data>value)
{
current=current.leftChild;
if(current==null)
{
parent.leftChild=newnode;
return;
}
}
else
{
current=current.rightChild;
if(current==null)
{
parent.rightChild=newnode;
return;
}
}
}
}
}
public Node find(long value)
{
Node current=root;
while(current.data!=value)
{
if(current.data>value)
{
current=current.leftChild;
}
else
{
current=current.rightChild;
}
if(current==null)
{
return null;
}
}
return current ;
}
public boolean delete(long value)
{
Node current=root;
Node parent=root;
boolean isLeftChild=true;
while(current.data!=value)
{
parent=current;
if(current.data>value)
{
current=current.leftChild;
isLeftChild=true;
}
else
{
current=current.rightChild;
isLeftChild=false;
}
if(current==null)
{
return false;
}
}
if(current.leftChild==null&¤t.rightChild==null)
{
if(current==root)
{
root=null;
}
else if(isLeftChild)
{
parent.leftChild=null;
}
else
{
parent.rightChild=null;
}
}
else if(current.rightChild==null)
{
if(current==root)
{
root=current.leftChild;
}
else if(isLeftChild)
{
parent.leftChild=current.leftChild;
}
else
{
parent.rightChild=current.leftChild;
}
}
else if (current.leftChild==null)
{
if(current==root)
{
root=current.rightChild;
}
else if(isLeftChild)
{
parent.leftChild=current.rightChild;
}
else
{
parent.rightChild=current.rightChild;
}
}
else
{
Node sucessor=getSuccessor(current);
if(current==root)
{
root=sucessor;
}
else if(isLeftChild)
{
parent.leftChild=sucessor;
}
else
{
parent.rightChild=sucessor;
}
sucessor.leftChild=current.leftChild;
}
return true;
}
public Node getSuccessor(Node delNode)
{
Node sucessor=delNode;
Node sucessorParent=delNode;
Node current=delNode.rightChild;
while(current!=null)
{
sucessorParent=sucessor;
sucessor=current;
current=current.leftChild;
}
if(sucessor!=delNode.rightChild)
{
sucessorParent.leftChild=sucessor.rightChild;
sucessor.rightChild=delNode.rightChild;
}
return sucessor;
}
public void frontOrder(Node localNode)
{
if(localNode!=null)
{
System.out.println(localNode.data+","+localNode.sData);
frontOrder(localNode.leftChild);
frontOrder(localNode.rightChild);
}
}
public void inOrder(Node localNode)
{
if(localNode!=null)
{
inOrder(localNode.leftChild);
System.out.println(localNode.data+","+localNode.sData);
inOrder(localNode.rightChild);
}
}
public void afterOrder(Node localNode)
{
if(localNode!=null)
{
afterOrder(localNode.leftChild);
afterOrder(localNode.rightChild);
System.out.println(localNode.data+","+localNode.sData);
}
}
}
测试:
public class TestTree
{
public static void main(String[] args)
{
Tree tree=new Tree();
tree.insert(10,"zhong");
tree.insert(20,"jun");
tree.insert(15,"peng");
tree.insert(3,"lixiaoxiao");
tree.insert(4,"xiaoxiao");
tree.insert(5,"xiao");
// System.out.println(tree.root.data);
// System.out.println(tree.root.rightChild.data);
// System.out.println(tree.root.rightChild.leftChild.data);
// System.out.println(tree.root.leftChild.data);
//
// Node node=tree.find(20);
// System.out.println(node.data+","+node.sData);
// tree.frontOrder(tree.root);
// tree.inOrder(tree.root);
// tree.afterOrder(tree.root);
tree.delete(10);
tree.inOrder(tree.root);
}
}
注:本人小白,如果你们觉得不错就打赏下吧