一,用结点实现链表LinkedList,不用换JavaAPI的集合框架
import java.util.Scanner;
public class Main {
public static class Node {
int data;
Node next=null;
public Node(int data){this.data=data;};
}
public static class MyLinkedList {
Node head=null;
public MyLinkedList() {
}
//添加节点
public void addNode(int d){
Node newNode=new Node(d);
if(head==null){
head=newNode;
return ;
}
Node tmp=head;
while(tmp.next!=null){//找到结尾
tmp=tmp.next;
}
tmp.next=newNode;
}
//链表长度
public int size(Node head){
int count=0;
Node tmp=head;
while(tmp.next!=null){
count++;
tmp=tmp.next;
}
return count+1;
}
//删除节点
public boolean deleteNode(int d){
if(head.data==d){
head=head.next;
return true;
}
Node preNode=head;
Node curNode=head.next;
while(curNode!=null){
if(curNode.data==d){
preNode.next=curNode.next;
return true;
}
preNode=curNode;
curNode=curNode.next;
}
return false;
}
//对链表进行排序,返回排序后的头结点
public Node orderList(){
Node nextNode=null;
int temp=0;
Node curNode=head;
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;
}
//选择排序算法
/*while(curNode.next!=null){
nextNode=curNode.next;
Node k=curNode;
int tmp=curNode.data;
while(nextNode!=null){
if(nextNode.data<tmp){
tmp=nextNode.data;
k=nextNode;
}
nextNode=nextNode.next;
}
if(k!=curNode){
int t=k.data;
k.data=curNode.data;
curNode.data=t;
}
curNode=curNode.next;
}*/
return head;
}
//打印链表
public void printList(){
Node tmp=head;{
while(tmp!=null){
System.out.print(tmp.data+" ");
tmp=tmp.next;
}
}
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int[] nodes=new int[n];
for(int i=0;i<n;i++){
nodes[i]=in.nextInt();
}
int k=in.nextInt();
MyLinkedList list=new MyLinkedList();
for(int i=0;i<n;i++){
list.addNode(nodes[i]);
}
//LinkedList list2=new LinkedList();
Node p=FindKthToTail(list.head,k);
System.out.println(p.data);
}
//单链表找到倒数第k个数
public static Node FindKthToTail(Node head,int k){
if(k<=0||head==null){
return null;
}
Node p1=head;
Node p2=head;
for(int i=0;i<k+1;i++){//先走k步
p1=p1.next;
}
while(p1!=null){
p1=p1.next;
p2=p2.next;
}
return p2;
}
}
二,实现栈Stack
1,使用一般的数组Object[]实现栈
import java.util.Arrays;
public class Main {
public static class MyStack<E>{
private Object[] stack;//用数组实现栈
private int size;//数组的大小
public MyStack(){
stack=new Object[10];
}
public boolean isEmpty(){
return size==0;
}
public E peek(){
if(isEmpty()){
return null;
}
return (E) stack[size-1];
}
public E pop(){
E e=peek();
stack[size-1]=null;
size--;
return e;
}
public boolean push(E item){
ensureCapacity(size+1);
stack[size++]=item;
return true;
}
private void ensureCapacity(int size){
int len=stack.length;
if(len<size){
int newLen=10;
stack=Arrays.copyOf(stack, newLen);
}
}
}
public static void main(String[] args) {
MyStack<Integer> s=new MyStack<Integer>();
s.push(52);
s.push(20);
s.push(96);
System.out.println("栈中元素个数:"+s.size);
System.out.println("栈顶元素:"+s.pop());
System.out.println("栈顶元素:"+s.pop());
}
}
2,使用结点链表实现栈,这是一直在链表头插入,向左扩展。让我们先忽略JavaAPI提供的集合框架,咱主要讨论的是思想
public class Main {
//节点类
public static class Node<E>{
E data;
Node<E> next=null;
public Node(E data) {
this.data = data;
}
}
public static class MyStack<E>{
Node<E> top=null;//相当于链表头
public boolean push(E d){
Node<E> newNode=new Node<E>(d);
if(top!=null){
newNode.next=top;//一直在链表头插入,想左扩展
}
top=newNode;
return true;
}
public E peek(){
if(top==null)
return null;
return top.data;
}
public E pop(){
E e=peek();
top=top.next;
return e;
}
public boolean isEmpty(){
return top==null;
}
public int size(){
if(isEmpty()){
return 0;
}
Node<E> tmp=top;
int size=0;
while(tmp!=null){
size++;
tmp=tmp.next;
}
return size;
}
}
public static void main(String[] args) {
MyStack<Integer> s=new MyStack<Integer>();
s.push(52);
s.push(20);
s.push(96);
System.out.println("栈中元素个数:"+s.size());
System.out.println("栈顶元素:"+s.pop());
System.out.println("栈顶元素:"+s.pop());
}
}
三,实现队列Queue
1,用数组实现队列,这时用集合框架LinkedList是最好的方法
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
public static class MyQueue<E>{
private LinkedList<E> queue;//用的是Api的集合框架,为什么要用LinkedList。因为LinkedList有addlast和addFirst方法,可以在两端任意操作,使用ArrayList就比较麻烦
private int size=0;
public MyQueue(){
queue=new LinkedList<E>();
}
public boolean Enqueue(E e){
queue.addLast(e);
size++;
return true;
}
public E Dequeue(){
size--;
return queue.removeFirst();
}
public boolean Empty(){
return size==0;
}
public int size(){
return queue.size();
}
public void printQueue(){
System.out.println(queue.toString());
}
}
public static void main(String[] args) {
MyQueue<Integer> q=new MyQueue<Integer>();
q.Enqueue(52);
q.Enqueue(42);
q.Enqueue(71);
q.Enqueue(23);
q.printQueue();
q.Dequeue();
q.printQueue();
q.Dequeue();
q.printQueue();
q.Dequeue();
q.printQueue();
/*
* [52, 42, 71, 23]
[42, 71, 23]
[71, 23]
[23]
*
* */
}
}
2,用结点链表实现队列。
public class Main {
public static class Node<E>{
E data;
Node<E> next=null;
public Node(E data){
this.data=data;
}
}
public static class MyQueue<E>{
Node<E> head=null,tail=null;
public boolean Enqueue(E e){
Node<E> newNode=new Node<E>(e);
if(head==null&&tail==null){
head=newNode;
tail=newNode;
}
tail.next=newNode;
tail=newNode;
return true;
}
public E Dequeue(){
if(head==null){
return null;
}
E e=head.data;
head=head.next;
return e;
}
public boolean isEmpty(){
return head==tail;
}
public int size(){
Node<E> tmp=head;
int count=0;
while(tmp!=null){
count++;
tmp=tmp.next;
}
return count;
}
public void printQueue(){
Node<E> tmp=head;
while(tmp!=null){
System.out.print(tmp.data+" ");
tmp=tmp.next;
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyQueue<Integer> q=new MyQueue<Integer>();
q.Enqueue(52);
q.Enqueue(42);
q.Enqueue(71);
q.Enqueue(23);
q.printQueue();
q.Dequeue();
q.printQueue();
q.Dequeue();
q.printQueue();
q.Dequeue();
q.printQueue();
/*52 42 71 23
<span style="white-space:pre"> </span> *42 71 23
<span style="white-space:pre"> </span> *71 23
*23 */
}
}