question:
Delete kth element. Implement a class that supports the following API:
public clas GeneralizedQueue<Item> GeneralizedQueue() //create an empty queue boolean isEmpty() //is the queue empty? void insert(Item x) //add an item Item delete(int k) //delete and return the kth least recently inserted itemFirst, develop an implementation that an array implementation, and then develop one that uses a linked-list implementation. Note: the algorithms and data structures that we introduce in CHAPTER 3 make it possible to develop an implementation that can guarantee that both insert() and delete() take time prortional to the logarithm of number of items in the queue----see EXERCISE 3.5.27.
answer:
//题目要求是要用数组可链表分别实现,我这就只用链表实现了一下
import edu.princeton.cs.algs4.*; public class GeneralizedQueue<Item> { private class Node { Item item; Node next; } private Node head; private Node tail; private int N; public GeneralizedQueue() { head = null; tail = null; N = 0; } public boolean isEmpty() { return N == 0; } public void show()//遍历 { Node current = head; while(current != null) { StdOut.print(current.item + "\t"); current = current.next; } StdOut.print("\n"); } public void insert(Item x) { Node node = new Node(); node.item = x; node.next = null; if(this.isEmpty()) { head = tail = node; N++; return; } tail.next = node; tail = node; N++; } public Item delete(int k)//删除第k项(从第0项开始),并返回最近插入的一项 { if(k < 0 || k > N) { return null; } if(k == 0) { head = head.next; N--; return tail.item; } Node current = head.next; Node before_current = head; for(int i = 0; i < k; i++) { current = current.next; before_current = before_current.next; } before_current.next = current.next; N--; Node p = head; while(p.next != null) { p = p.next; } tail = p; return tail.item; } public static void main(String[] args) { GeneralizedQueue<Integer> queue = new GeneralizedQueue<Integer>(); for(int i = 0; i < 10; i++) { queue.insert(i); } queue.show(); StdOut.println(queue.delete(3)); queue.show(); } }
import edu.princeton.cs.algs4.*;
public class GeneralizedQueue<Item>
{
private class Node
{
Item item;
Node next;
}
private Node head;
private Node tail;
private int N;
public GeneralizedQueue()
{
head = null;
tail = null;
N = 0;
}
public boolean isEmpty()
{
return N == 0;
}
public void show()//遍历
{
Node current = head;
while(current != null)
{
StdOut.print(current.item + "\t");
current = current.next;
}
StdOut.print("\n");
}
public void insert(Item x)
{
Node node = new Node();
node.item = x;
node.next = null;
if(this.isEmpty())
{
head = tail = node;
N++;
return;
}
tail.next = node;
tail = node;
N++;
}
public Item delete(int k)//删除第k项,并返回最近插入的一项,从第0项开始
{
if(k < 0 || k > N)
{
return null;
}
if(k == 0)
{
head = head.next;
N--;
return tail.item;
}
Node current = head.next;
Node before_current = head;
for(int i = 0; i < k; i++)
{
current = current.next;
before_current = before_current.next;
}
before_current.next = current.next;
N--;
Node p = head;
while(p.next != null)
{
p = p.next;
}
tail = p;
return tail.item;
}
public static void main(String[] args)
{
GeneralizedQueue<Integer> queue = new GeneralizedQueue<Integer>();
for(int i = 0; i < 10; i++)
{
queue.insert(i);
}
queue.show();
StdOut.println(queue.delete(3));
queue.show();
}
}