Skip to content

Commit

Permalink
完成数据结构作业
Browse files Browse the repository at this point in the history
  • Loading branch information
gongxun committed Apr 26, 2017
1 parent f622e29 commit 417d7ca
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
30 changes: 21 additions & 9 deletions group17/785396327/4.23/queue/QueueWithTwoStacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,39 @@ public QueueWithTwoStacks() {
}




public boolean isEmpty() {
return false;
return stack1.size() == 0;
}



public int size() {
return -1;
return stack1.size();
}



public void enQueue(E item) {

Stack<E> temp = new Stack<E>();
stack1.push(item);
while (!stack2.isEmpty())
temp.push(stack2.pop());
temp.push(item);
while (!temp.isEmpty())
stack2.push(temp.pop());
}

public E deQueue() {
return null;
Stack<E> temp = new Stack<E>();
E ele = stack2.pop();
while (!stack1.isEmpty())
temp.push(stack1.pop());
temp.pop();
while (!temp.isEmpty())
stack1.push(temp.pop());
return ele;
}


@Override
public String toString() {
return stack1.toString() + "\n" + stack2.toString();
}
}
37 changes: 37 additions & 0 deletions group17/785396327/4.23/queue/QueueWithTwoStacksTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package queue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Created by gongxun on 2017/4/26.
*/
public class QueueWithTwoStacksTest {

private QueueWithTwoStacks queue;

@Before
public void startUp() {
queue = new QueueWithTwoStacks();
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
}

@After
public void tearDown() {

}

@Test
public void enQueueTest() {
System.out.println(queue);
}

@Test
public void deQueueTest() {
queue.deQueue();
System.out.println(queue);
}
}

0 comments on commit 417d7ca

Please sign in to comment.