Algs4-1.3.47可连接的队列、栈或steque
1.3.47可连接的队列、栈或steque。为队列、栈或steque(请见练习1.3.32)添加一个能够(破坏性地)连接两个同类对象的额外操作catenation。
答:
Queue 代码:
import java.util.Iterator;
public class Queue<Item> implements Iterable<Item>
{
private int N;
private Node first;
private Node last;
private class Node
{
Item item;
Node next;
}
public Queue()
{
}
public Queue(Queue q)
{
for(Object i:q)
{
//
Node oldlast=last;
last=new Node();
last.item=(Item) i;
last.next=null;
if(isEmpty())
first=last;
else
oldlast.next=last;
N++;
}
}
public boolean isEmpty()
{return N==0;}
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 void catenation(Queue q)
{
while(!q.isEmpty())
{
this.enqueue((Item)q.dequeue());
}
}
public Iterator<Item> iterator() {return new ListIterator();}
private class ListIterator 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;
}//end next
}//end class ListIterator
public static void main(String[] args)
{
Queue<String> q1=new Queue<String>();
In in1=new In(args[0]);
while(!in1.isEmpty())
{
String item=in1.readString();
q1.enqueue(item);
}//end while
Queue<String> q2=new Queue<String>();
In in2=new In(args[0]);
while(!in2.isEmpty())
{
String item=in2.readString();
q2.enqueue(item);
}//end while
StdOut.println("---Queue q1 is:");
for(String item:q1)
StdOut.println(item+" ");
//
StdOut.println("---Queue q2 is:");
for(String item:q2)
StdOut.println(item+" ");
//
q1.catenation(q2);
StdOut.println("---After q1.catenation(q2) q1 is:");
for(String item:q1)
StdOut.println(item+" ");
//
StdOut.println("---After q1.catenation(q2) q2 is:");
for(String item:q2)
StdOut.println(item+" ");
}//end main
}//end class
Stack代码:
import java.util.Iterator;
public class Stack<Item> implements Iterable<Item>
{
private int N;
private Node first;
private class Node
{
Item item;
Node next;
}
public Stack()
{
}
public Stack(Stack s)
{
Node right=new Node();
Node oldright;
for(Object i:s)
{
oldright=right;
right=new Node();
right.item=(Item) i;
right.next=null;
if(isEmpty())
first=right;
else
oldright.next=right;
N++;
}
}
public boolean isEmpty()
{return N==0;}
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 Item peek()
{
Item item=first.item;
return item;
}
public void catenation(Stack s)
{
while(!s.isEmpty())
{
this.push((Item)s.pop());
}
}
public Iterator<Item> iterator()
{return new ListIterator();}
private class ListIterator 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;
}//end next
}//end class ListIterator
public static void main(String[] args)
{
Stack<String> s1=new Stack<String>();
In in1=new In(args[0]);
while(!in1.isEmpty())
{
String item=in1.readString();
s1.push(item);
}//end while
Stack<String> s2=new Stack<String>();
In in2=new In(args[0]);
while(!in2.isEmpty())
{
String item=in2.readString();
s2.push(item);
}//end while
StdOut.println("---Stack s1 is:");
for(String item:s1)
StdOut.println(item+" ");
//
StdOut.println("---Stack s2 is:");
for(String item:s2)
StdOut.println(item+" ");
//
s1.catenation(s2);
StdOut.println("---After s1.catenation(s2) s1 is:");
for(String item:s1)
StdOut.println(item+" ");
//
StdOut.println("---After s1.catenation(s2) s2 is:");
for(String item:s2)
StdOut.println(item+" ");
}
}
Steque代码:
import java.util.Iterator;
public class Steque<Item> implements Iterable<Item>
{
private int N;
private Node first;
private Node last;
private class Node
{
Item item;
Node next;
}
public boolean isEmpty()
{return N==0;}
public int size()
{return N;}
public void push(Item item)
{
Node oldfirst=first;
first=new Node();
first.item=item;
first.next=oldfirst;
if(isEmpty()) last=first;
N++;
}
public Item pop()
{
Item item=first.item;
first=first.next;
N--;
if(isEmpty()) last=null;
return item;
}
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;
N--;
if(isEmpty()) last=null;
return item;
}
public void catenation(Steque s)
{
while(!s.isEmpty())
{
this.push((Item)s.pop());
}
}
public Iterator<Item> iterator() {return new ListIterator();}
private class ListIterator 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;
}//end next
}//end class ListIterator
public static void main(String[] args)
{
Steque<String> sq1=new Steque<String>();
In in1=new In(args[0]);
while(!in1.isEmpty())
{
String item=in1.readString();
sq1.push(item);
}//end while
Steque<String> sq2=new Steque<String>();
In in2=new In(args[0]);
while(!in2.isEmpty())
{
String item=in2.readString();
sq2.push(item);
}//end while
StdOut.println("---Steque sq1 is:");
for(String item:sq1)
StdOut.println(item+" ");
//
StdOut.println("---Steque sq2 is:");
for(String item:sq2)
StdOut.println(item+" ");
//
sq1.catenation(sq2);
StdOut.println("---After sq1.catenation(sq2) sq1 is:");
for(String item:sq1)
StdOut.println(item+" ");
//
StdOut.println("---After sq1.catenation(sq2) sq2 is:");
for(String item:sq2)
StdOut.println(item+" ");
}//end main
}//end class