java链表的归并排序:堆栈溢出

发布于 2024-12-08 18:18:08 字数 3740 浏览 0 评论 0原文

我正在尝试实现链表合并排序。

这是我试图在其中进行合并排序的类。

/**
 * 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-12-15 18:18:08

检查这一点:

    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()); 
        }
    }

您没有从堆栈中删除条目,因此循环不会停止。

Check this:

    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()); 
        }
    }

You don't remove entries from the stacks, so the loop doesn't stop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文