用结点实现链表LinkedList,用数组和结点实现栈Stack,用数组和结点链表实现队列Queue
生活随笔
收集整理的這篇文章主要介紹了
用结点实现链表LinkedList,用数组和结点实现栈Stack,用数组和结点链表实现队列Queue
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一,用結點實現鏈表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 stubMyQueue<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?*/ }}
總結
以上是生活随笔為你收集整理的用结点实现链表LinkedList,用数组和结点实现栈Stack,用数组和结点链表实现队列Queue的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Scanner的next,nextint
- 下一篇: 笔试总结