<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        AlgorithmPartI:ProgrammingAssignment(2)

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 14:44:58
        文檔

        AlgorithmPartI:ProgrammingAssignment(2)

        AlgorithmPartI:ProgrammingAssignment(2):問(wèn)題描述: Programming Assignment 2: Randomized Queues and Deques Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary
        推薦度:
        導(dǎo)讀AlgorithmPartI:ProgrammingAssignment(2):問(wèn)題描述: Programming Assignment 2: Randomized Queues and Deques Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary

        Throw a NullPointerException if the client attempts to add a null item; throw a java.util.NoSuchElementException if the client attempts to remove an item from an empty deque; throw an UnsupportedOperationException if the client calls the remove() method in the iterator; throw a java.util.NoSuchElementException if the client calls the next() method in the iterator and there are no more items to return.

        Your deque implementation must support each deque operation in constant worst-case time and use space proportional to the number of items currently in the deque. Additionally, your iterator implementation must support the operations next() and hasNext() (plus construction) in constant worst-case time and use a constant amount of extra space per iterator.

        Randomized queue. A randomized queue is similar to a stack or queue, except that the item removed is chosen uniformly at random from items in the data structure. Create a generic data type RandomizedQueue that implements the following API:

        public class RandomizedQueue implements Iterable {
         public RandomizedQueue() // construct an empty randomized queue
         public boolean isEmpty() // is the queue empty?
         public int size() // return the number of items on the queue
         public void enqueue(Item item) // add the item
         public Item dequeue() // delete and return a random item
         public Item sample() // return (but do not delete) a random item
         public Iterator iterator() // return an independent iterator over items in random order
         public static void main(String[] args) // unit testing
        }
        

        Throw a NullPointerException if the client attempts to add a null item; throw a java.util.NoSuchElementException if the client attempts to sample or dequeue an item from an empty randomized queue; throw anUnsupportedOperationException if the client calls the remove() method in the iterator; throw a java.util.NoSuchElementException if the client calls the next() method in the iterator and there are no more items to return.

        Your randomized queue implementation must support each randomized queue operation (besides creating an iterator) in constant amortized time and use space proportional to the number of items currently in the queue. That is, any sequence of M randomized queue operations (starting from an empty queue) should take at most cM steps in the worst case, for some constant c. Additionally, your iterator implementation must support construction in time linear in the number of items and it must support the operations next() and hasNext() in constant worst-case time; you may use a linear amount of extra memory per iterator. The order of two or more iterators to the same randomized queue should be mutually independent; each iterator must maintain its own random order.

        Subset client. Write a client program Subset.java that takes a command-line integer k; reads in a sequence of N strings from standard input using StdIn.readString(); and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 ≤ kN, where N is the number of string on standard input.

        % echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8
        C BB
        G AA
        A BB
         CC
        % echo A B C D E F G H I | java Subset 3 BB
        E BB
        F CC
        G BB
        
        The running time of Subset must be linear in the size of the input. You may use only a constant amount of memory plus either one Deque or RandomizedQueue object of maximum size at most N, where N is the number of strings on standard input. (For an extra challenge, use only one Deque or RandomizedQueue object of maximum size at most k.) It should have the following API.
        public class Subset {
         public static void main(String[] args)
        }
        

        Deliverables. Submit only Deque.java, RandomizedQueue.java, and Subset.java. We will supply stdlib.jar. You may not use any libraries other than those in stdlib.jar, java.lang, java.util.Iterator, and java.util.NoSuchElementException.

        代碼:

        Deque.java

        import java.util.Iterator;
        
        ;
        
        public class Deque implements Iterable {
        
        	private Node first;
        	private Node last;
        	private int length;
        
        	public Deque() {
        	first = null;
        	last = null;
        	length = 0;
        	}
        
        	public boolean isEmpty() {
        	return length == 0;
        	}
        
        	public int size() {
        	return length;
        	}
        
        	public void addFirst(Item item) {
        	if (item == null)
        	throw new NullPointerException();
        	if (length == 0) {
        	Node newNode = new Node();
        	newNode.i = item;
        	newNode.left = null;
        	newNode.right = null;
        	first = newNode;
        	last = newNode;
        	length++;
        	} else {
        	Node newNode = new Node();
        	newNode.i = item;
        	newNode.right = null;
        	newNode.left = first;
        	first.right = newNode;
        	first = newNode;
        	length++;
        	}
        	}
        
        	public void addLast(Item item) {
        	if (item == null)
        	throw new NullPointerException();
        	if (length == 0) {
        	Node newNode = new Node();
        	newNode.i = item;
        	newNode.left = null;
        	newNode.right = null;
        	first = newNode;
        	last = newNode;
        	length++;
        	} else {
        	Node newNode = new Node();
        	newNode.i = item;
        	newNode.right = last;
        	newNode.left = null;
        	last.left = newNode;
        	last = newNode;
        	length++;
        	}
        	}
        
        	public Item removeFirst() {
        	if (isEmpty())
        	throw new java.util.NoSuchElementException();
        	if (length == 1) {
        	Item item = first.i;
        	first = null;
        	last = null;
        	length--;
        	return item;
        	} else {
        	Item item = first.i;
        	Node temp = first.left;
        	first.left.right = null;
        	first.left = null;
        	first = temp;
        	length--;
        	return item;
        	}
        	}
        
        	public Item removeLast() {
        	if (isEmpty())
        	throw new java.util.NoSuchElementException();
        	if (length == 1) {
        	Item item = first.i;
        	first = null;
        	last = null;
        	length--;
        	return item;
        	} else {
        	Item item = last.i;
        	Node temp = last.right;
        	last.right.left = null;
        	last.right = null;
        	last = temp;
        	length--;
        	return item;
        	}
        	}
        
        	public static void main(String[] args) {
        	// TODO Auto-generated method stub
        	
        	}
        
        	@Override
        	public Iterator iterator() {
        	// TODO Auto-generated method stub
        	return new ListIterator();
        	}
        
        	private class Node {
        	private Node left;
        	private Node right;
        	private Item i;
        	}
        
        	private class ListIterator implements Iterator {
        	
        	private Node ptr;
        	private Item i;
        	
        	public ListIterator()
        	{
        	ptr = first;
        	}
        
        	@Override
        	public boolean hasNext() {
        	// TODO Auto-generated method stub
        	if (ptr == null)
        	return false;
        	else
        	return true;
        	}
        
        	@Override
        	public Item next() {
        	// TODO Auto-generated method stub
        	if (!hasNext())
        	throw new java.util.NoSuchElementException();
        	else {
        	i = ptr.i;
        	ptr = ptr.left;
        	return i;
        	}
        
        	}
        
        	public void remove() {
        	throw new UnsupportedOperationException();
        	}
        
        	}
        
        } 

        RandomizedQueue.java

        import java.util.Iterator;
        
        public class RandomizedQueue implements Iterable {
        
        	private Item items[];
        	private int length;
        
        	public RandomizedQueue() {
        	items = (Item[]) new Object[2];
        	length = 0;
        	}
        
        	public boolean isEmpty() {
        	return length == 0;
        	}
        
        	public int size() {
        	return length;
        	}
        
        	public void enqueue(Item item) {
        	if (item == null)
        	throw new NullPointerException();
        	if (length == items.length)
        	resize(items.length * 2);
        	items[length] = item;
        	length++;
        	}
        
        	public Item dequeue() {
        	if (isEmpty())
        	throw new java.util.NoSuchElementException();
        	int n = (int) (Math.random() * length);
        	Item i = items[n];
        	if (n != length - 1)
        	items[n] = items[length - 1];
        	length--;
        	if (length > 0 && length == items.length / 4)
        	resize(items.length / 2);
        	return i;
        	}
        
        	private void resize(int n) {
        	Item newItem[] = (Item[]) new Object[n];
        	for (int i = 0; i < length; i++)
        	newItem[i] = items[i];
        	items = newItem;
        	}
        
        	public Item sample() {
        	if (isEmpty())
        	throw new java.util.NoSuchElementException();
        	int n = (int) (Math.random() * length);
        	Item i = items[n];
        	return i;
        	}
        
        	@Override
        	public Iterator iterator() {
        	// TODO Auto-generated method stub
        	return new ListIterator();
        	}
        
        	private class ListIterator implements Iterator {
        	
        	int index;
        
        	public ListIterator() {
        	index = 0;
        	}
        
        	@Override
        	public boolean hasNext() {
        	// TODO Auto-generated method stub
        	return index <= length - 1;
        	}
        
        	@Override
        	public Object next() {
        	// TODO Auto-generated method stub
        	if (!hasNext())
        	throw new java.util.NoSuchElementException();
        	int n = (int) (Math.random()*(length-index));
        	Object item = items[n];
        	if(n != length-index-1)
        	items[n] = items[length-index-1];
        	index++;
        	return item;
        	}
        
        	public void remove() {
        	throw new UnsupportedOperationException();
        	}
        
        	}
        
        	public static void main(String[] args) {
        	// TODO Auto-generated method stub
        
        	}
        }
        
        Subset.java
        public class Subset {
        
        	public static void main(String[] args) {
        	// TODO Auto-generated method stub
        	RandomizedQueue rq = new RandomizedQueue();
        	int k = Integer.parseInt(args[0]);
        	while (!StdIn.isEmpty())
        	rq.enqueue(StdIn.readString());
        
        	for (int i = 0; i < k; i++)
        	StdOut.println(rq.dequeue());
        	}
        }
        

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        AlgorithmPartI:ProgrammingAssignment(2)

        AlgorithmPartI:ProgrammingAssignment(2):問(wèn)題描述: Programming Assignment 2: Randomized Queues and Deques Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary
        推薦度:
        標(biāo)簽: pr 2: algorithm
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 操美女视频免费网站| 1000部啪啪未满十八勿入免费| 女人与禽交视频免费看| 亚洲国产av高清无码| 免费能直接在线观看黄的视频| 亚洲精彩视频在线观看| 成人免费大片免费观看网站| 亚洲春色另类小说| 免费观看成人毛片a片2008| 亚洲 欧洲 自拍 另类 校园| 四虎成人免费网址在线| 激情小说亚洲图片| 亚洲人成人无码网www国产| 国产成人1024精品免费| 国产亚洲人成无码网在线观看| 国产午夜精品久久久久免费视 | 亚洲图片校园春色| 女人被免费视频网站| 香蕉视频在线观看免费| 亚洲中文字幕无码久久综合网| 免费在线中文日本| 亚洲人成电影在线观看网| 日韩免费视频观看| 中文字幕成人免费高清在线| 亚洲激情在线视频| 最好免费观看韩国+日本| 真人无码作爱免费视频| 国产av无码专区亚洲av果冻传媒| 久久久精品免费视频| 亚洲18在线天美| 337p日本欧洲亚洲大胆精品555588| 67194成手机免费观看| 亚洲精品国产情侣av在线| 曰曰鲁夜夜免费播放视频| 激情婷婷成人亚洲综合| 亚洲一区AV无码少妇电影☆| 亚洲一级毛片免费看| 日日摸夜夜添夜夜免费视频| 久久久久亚洲AV无码永不| 四虎永久在线精品免费观看地址| 黄色网站软件app在线观看免费|