首页 > 代码库 > 数据结构简单实现

数据结构简单实现

1、栈(能动态调整数组大小的实现)

 

import java.util.Iterator ;import java.util.Scanner ;public class ResizingArrayStack<Item> implements Iterable<Item>{	private Item[] a = (Item[]) new Object[1] ;	private int N = 0 ;		public boolean isEmpty(){		return N == 0 ;	}		public int size(){		return N ;	}	private void resize(int max){		Item[] temp = (Item[]) new Object[max] ;		for (int i=0;i<N;i++) {			temp[i] = a[i] ;		}		a = temp ;	}	public void push(Item item){		if(N==a.length){			resize(2*a.length) ;		}		a[N] = item ;		N++ ;	}	public Item pop(){		N-- ;		Item item = a[N] ;		a[N] = null ;		if (N>0 && N==a.length/4) {			resize(a.length/2) ;		}		return item ;	}	public Iterator<Item> iterator(){		return new ReverseArrayIterator() ;	}	private class ReverseArrayIterator implements Iterator<Item>{		private int i = N ;		public boolean hasNext(){			return i>0 ;		}		public Item next(){			i-- ;			return a[i] ;		}		public void remove(){		}	}	public static void main(String[] args) {		ResizingArrayStack<Integer> stack = new ResizingArrayStack<Integer>() ;		System.out.println("please input the number of items:") ;		Scanner sc = new Scanner(System.in) ;		int n = sc.nextInt() ;		for(int i=0;i<n;i++){			stack.push(sc.nextInt()) ;		}		for(int i:stack){			System.out.print(i + " ") ;		}		System.out.println() ;	}}

  

2、栈(链表实现)

 

import java.util.Iterator ;import java.util.Scanner ;public class ListStack<Item> implements Iterable<Item>{	private Node first ;	private int N ;	private class Node{		Item item ;		Node next ;	}		public boolean isEmpty(){		return first == null ;	}		public int size(){		return N ;	}	public void push(Item item){		Node oldfirst = first ;		first = new Node() ;		first.item = item ;		first.next = oldfirst ;		N++ ;	}	public Item pop(){		Item item = first.item ;		first = first.next ;		N-- ;		return item ;	}	public Iterator<Item> iterator(){		return new ListIterator() ;	}	private class ListIterator implements Iterator<Item>{		private Node current = first ;		public boolean hasNext(){			return current != null ;		}		public Item next(){			Item item = current.item ;			current = current.next ;			return item ;		}		public void remove(){		}	}	public static void main(String[] args) {		ListStack<Integer> stack = new ListStack<Integer>() ;		System.out.println("please input the number of items:") ;		Scanner sc = new Scanner(System.in) ;		int n = sc.nextInt() ;		for(int i=0;i<n;i++){			stack.push(sc.nextInt()) ;		}		for(int i:stack){			System.out.print(i + " ") ;		}		System.out.println() ;	}}

  

3、队列

 

import java.util.Iterator ;import java.util.Scanner ;public class Queue<Item> implements Iterable<Item>{	private Node first ;	private Node last ;	private int N ;		private class Node{		Item item ;		Node next ;	}		public boolean isEmpty(){		return first == null ;	}		public int size(){		return N ;	}		public void enqueue(Item item){		Node oldlast = last ;		last = new Node() ;		last.item = item ;		last.next = null ;		if (isEmpty()) {			first = last ;		}else{			oldlast.next = last ;		}		N++ ;	}		public Item dequeue(){		Item item = first.item ;		first = first.next ;		if (isEmpty()) {			last = null ;		}		N-- ;		return item ;	}	public Iterator<Item> iterator(){		return new QueueIterator() ;	}	private class QueueIterator implements Iterator<Item>{		private Node current = first ;		public boolean hasNext(){			return current!=null ;		}		public void remove(){		}		public Item next(){			Item item = current.item ;			current = current.next ;			return item ;		}	}	public static void main(String[] args) {		Queue<String> q = new Queue<String>() ;		System.out.println("please input the number of items:") ;		Scanner sc = new Scanner(System.in) ;		int n = sc.nextInt() ;		for (int i=0;i<n;i++) {			q.enqueue(sc.next()) ;		}		for (String s : q) {			System.out.print(s + " ") ;		}		System.out.println() ;	}}

  

4、背包

 

import java.util.Iterator ;import java.util.Scanner ;public class Bag<Item> implements Iterable<Item>{	private Node first ;	private int N = 0 ;	private class Node{		Item item ;		Node next ;		}	public void add(Item item){		Node oldfirst = first ;		first = new Node() ;		first.item = item ;		first.next = oldfirst ;		N++ ;	}	public boolean isEmpty(){		return first == null ;	}	public int size(){		return N ;	}	public Iterator<Item> iterator(){		return new ListIterator() ;	}	private class ListIterator implements Iterator<Item>{		private Node current = first ;		public boolean hasNext(){			return current != null ;		}		public Item next(){			Item item = current.item ;			current = current.next ;			return item ;		}		public void remove(){		}	}	public static void main(String[] args) {		Bag<Double> numbers = new Bag<Double>() ;		System.out.println("please input the number of items:") ;		Scanner sc = new Scanner(System.in) ;		int n = sc.nextInt() ;		for(int i=0;i<n;i++){			numbers.add(sc.nextDouble()) ;		}		System.out.println("size(): " + numbers.size()) ;		for (double d : numbers) {			System.out.print(d + " ") ;		}		System.out.println() ;	}}

  

数据结构简单实现