java链表的归并排序:堆栈溢出
我正在尝试实现链表合并排序。
这是我试图在其中进行合并排序的类。
/**
* CS 200 Colorado State University, Fall 2011
*/
public class Member {
private String userID;
private String first;
private String last;
private EdgeStack edgeStack;
public void sortScore(Member member){
// calling a helper method
// this greedy method takes all the creds
edgeStack = sortEdgeStack(member);
}
private EdgeStack sortEdgeStack(Member member)
{
// our temp stacks
EdgeStack tempEdgeStack_A = new EdgeStack();
EdgeStack tempEdgeStack_B = new EdgeStack();
// our return value
EdgeStack result = null;
// storing the size of the stack
int sizeOfStack = member.getEdgeStack().getSize();
// base case
if(sizeOfStack<0){
return null;
}
// our true base case
else if(sizeOfStack==1)
{
// init stack
EdgeStack base = new EdgeStack();
base.push(member.getEdgeStack().pop());
return base;
}
else
{
// pop and store
for(int i = 0; i < (sizeOfStack / 2); i++)
{
tempEdgeStack_A.push(member.getEdgeStack().pop());
}
// pop and store into b
for(int j = (sizeOfStack/2)+1; j < sizeOfStack; j++)
{
tempEdgeStack_B.push(member.getEdgeStack().pop());
}
tempEdgeStack_A = sortEdgeStack(member);
tempEdgeStack_B = sortEdgeStack(member);
result = merge(tempEdgeStack_A,tempEdgeStack_B);
return result;
}
}
private EdgeStack merge(EdgeStack tempEdgeStack_A, EdgeStack tempEdgeStack_B) {
EdgeStack result = new EdgeStack();
// while either or
while(tempEdgeStack_A.getSize()> 0 || tempEdgeStack_B.getSize() > 0)
{
// if both are bigger then 0
if(tempEdgeStack_A.getSize()> 0 && tempEdgeStack_B.getSize() > 0)
{
if(tempEdgeStack_A.peek().getEdgeRank()<=tempEdgeStack_B.peek().getEdgeRank())
{
// adds b to result
result.push(tempEdgeStack_A.pop());
}
else
{
result.push(tempEdgeStack_B.pop());
}
}
// these elses cover if A or B are > 0 but A or B is also less then or equal too 0;
else if(tempEdgeStack_A.getSize()> 0)
{
while(tempEdgeStack_A.iterator().hasNext())
{
result.push(tempEdgeStack_A.iterator().next());
}
}
else if(tempEdgeStack_B.getSize()> 0)
{
while(tempEdgeStack_B.iterator().hasNext())
{
result.push(tempEdgeStack_B.iterator().next());
}
}
}
return result;
}
}
这是堆栈类(实现链表)。为什么我会收到堆栈溢出错误?
import java.util.LinkedList;
import java.util.ListIterator;
/**
* CS 200 Colorado State University, Fall 2011
*/
public class EdgeStack {
private LinkedList<Edge> llist=new LinkedList<Edge>();
public EdgeStack(){
//add your code
}
public boolean isEmpty(){
return llist.isEmpty();
}
public void push(Edge e){
llist.add(e);
}
public Edge getIndexAt(int n){
return llist.get(n);
}
public Edge pop(){
return llist.remove();
}
public Edge peek(){
return llist.getLast();
}
public int getSize(){
return llist.size();
}
// public Edge peek(int n){
// LinkedList<Edge> temp=llist;
// return temp.peek();
// }
public LinkedList<Edge> popAll(){
LinkedList<Edge> temp=llist;
llist=null;
return temp; }
public ListIterator<Edge> iterator()
{
return llist.listIterator();
}
}
I'm trying to implement a linked list merge sort.
Here's the class I'm trying to do the merge sort in.
/**
* CS 200 Colorado State University, Fall 2011
*/
public class Member {
private String userID;
private String first;
private String last;
private EdgeStack edgeStack;
public void sortScore(Member member){
// calling a helper method
// this greedy method takes all the creds
edgeStack = sortEdgeStack(member);
}
private EdgeStack sortEdgeStack(Member member)
{
// our temp stacks
EdgeStack tempEdgeStack_A = new EdgeStack();
EdgeStack tempEdgeStack_B = new EdgeStack();
// our return value
EdgeStack result = null;
// storing the size of the stack
int sizeOfStack = member.getEdgeStack().getSize();
// base case
if(sizeOfStack<0){
return null;
}
// our true base case
else if(sizeOfStack==1)
{
// init stack
EdgeStack base = new EdgeStack();
base.push(member.getEdgeStack().pop());
return base;
}
else
{
// pop and store
for(int i = 0; i < (sizeOfStack / 2); i++)
{
tempEdgeStack_A.push(member.getEdgeStack().pop());
}
// pop and store into b
for(int j = (sizeOfStack/2)+1; j < sizeOfStack; j++)
{
tempEdgeStack_B.push(member.getEdgeStack().pop());
}
tempEdgeStack_A = sortEdgeStack(member);
tempEdgeStack_B = sortEdgeStack(member);
result = merge(tempEdgeStack_A,tempEdgeStack_B);
return result;
}
}
private EdgeStack merge(EdgeStack tempEdgeStack_A, EdgeStack tempEdgeStack_B) {
EdgeStack result = new EdgeStack();
// while either or
while(tempEdgeStack_A.getSize()> 0 || tempEdgeStack_B.getSize() > 0)
{
// if both are bigger then 0
if(tempEdgeStack_A.getSize()> 0 && tempEdgeStack_B.getSize() > 0)
{
if(tempEdgeStack_A.peek().getEdgeRank()<=tempEdgeStack_B.peek().getEdgeRank())
{
// adds b to result
result.push(tempEdgeStack_A.pop());
}
else
{
result.push(tempEdgeStack_B.pop());
}
}
// these elses cover if A or B are > 0 but A or B is also less then or equal too 0;
else if(tempEdgeStack_A.getSize()> 0)
{
while(tempEdgeStack_A.iterator().hasNext())
{
result.push(tempEdgeStack_A.iterator().next());
}
}
else if(tempEdgeStack_B.getSize()> 0)
{
while(tempEdgeStack_B.iterator().hasNext())
{
result.push(tempEdgeStack_B.iterator().next());
}
}
}
return result;
}
}
Here's the stack class (that implements a linked list). Why am I getting a stack overflow error?
import java.util.LinkedList;
import java.util.ListIterator;
/**
* CS 200 Colorado State University, Fall 2011
*/
public class EdgeStack {
private LinkedList<Edge> llist=new LinkedList<Edge>();
public EdgeStack(){
//add your code
}
public boolean isEmpty(){
return llist.isEmpty();
}
public void push(Edge e){
llist.add(e);
}
public Edge getIndexAt(int n){
return llist.get(n);
}
public Edge pop(){
return llist.remove();
}
public Edge peek(){
return llist.getLast();
}
public int getSize(){
return llist.size();
}
// public Edge peek(int n){
// LinkedList<Edge> temp=llist;
// return temp.peek();
// }
public LinkedList<Edge> popAll(){
LinkedList<Edge> temp=llist;
llist=null;
return temp; }
public ListIterator<Edge> iterator()
{
return llist.listIterator();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查这一点:
您没有从堆栈中删除条目,因此循环不会停止。
Check this:
You don't remove entries from the stacks, so the loop doesn't stop.