如何有效地对递归定义的堆栈进行排序?

时间:2014-09-12 06:28:55

标签: sorting recursion stack

它不是一个功课问题我只是想尝试实现一个递归定义的Stack。问题完全出于好奇。 除了从数组中的Stack复制元素,排序它们并再次将它们推入堆栈之外,是否有一种有效的方法?

如果您有任何建议,请提供算法或代码(我知道C / C ++,Java)。

堆栈定义:

class Stack<E>
{
    E element;
    Stack<E> topOfSubStack;
}

1 个答案:

答案 0 :(得分:0)

假设对堆栈的访问是顺序的(没有随机访问迭代器),那么类似于用于对列表进行排序的算法(例如std :: list :: sort)将起作用。示例C代码,它使用指向列表的全局静态数组(可以将其分配,然后作为参数传递)。对于C ++,数组和函数可以是类的一部分。

#define NUMLISTS 32                     /* number of lists */
typedef unsigned long long UI64;

typedef struct NODE_{
struct NODE_ * next;
UI64 data;
}NODE;

typedef struct LIST_{
NODE *first;
}LIST;

/*----------------------------------------------------------------------*/
/*      data                                                            */
/*----------------------------------------------------------------------*/
static LIST aList[NUMLISTS];            /* array of lists */

/*----------------------------------------------------------------------*/
/*      SortList                                                        */
/*----------------------------------------------------------------------*/
void SortList(LIST *pList)
{
NODE * pNode;
LIST mList;                             /* merged list */
int i;

    if(pList == NULL)                   /* check for null ptr */
        return;

    /* merge nodes into aList[] */
    while(NULL != (pNode = GetFirstNode(pList))){
        MergeNode(pNode);
    }

    /* find 1st non empty aList[] */
    for(i = 0; (i < NUMLISTS) && (aList[i].first == NULL); i++);
    if(i >= NUMLISTS){                  /* if all empty */
        pList->first = NULL;            /*   return null list */
        return;
    }

    pList->first = aList[i].first;      /* plist = aList[i] */
    aList[i].first = NULL;              /* clear aList[i] (optional) */

    for(i++; i < NUMLISTS; i++){        /* merge remaining aList[] */
        if(aList[i].first != NULL){
            MergeLists(&mList, &aList[i], pList);
            pList->first = mList.first;
        }
    }
}

/*----------------------------------------------------------------------*/
/*      MergeNode   merge a node into the array of lists                */
/*----------------------------------------------------------------------*/
void MergeNode(NODE *pNode)
{
LIST sList;                             /* source list */
LIST mList;                             /* merged list */
int i;

    if(pNode == NULL)                   /* return if null ptr */
        return;

    sList.first = pNode;                /* src list = node */

    /* merge into array */
    for(i = 0; (i < NUMLISTS) && (aList[i].first != NULL); i++){
        MergeLists(&mList, &aList[i], &sList);
        sList.first = mList.first;
    }
    if(i == NUMLISTS)                   /* update array */
        i--;
    aList[i].first = sList.first;
}

/*----------------------------------------------------------------------*/
/*      MergeLists  dst = merge(src1, src2)                             */
/*----------------------------------------------------------------------*/
void MergeLists(LIST *plDst, LIST *plSrc1, LIST *plSrc2)
{
NODE **ppDst, *pSrc1, *pSrc2;           /* node ptrs */

    ppDst = &(plDst->first);
    pSrc1 = GetFirstNode(plSrc1);       /* init ptrs to nodes */
    pSrc2 = GetFirstNode(plSrc2);

    while(1){
        if(pSrc2->data < pSrc1->data){  /* if src2 < src1 */
            *ppDst = pSrc2;
            ppDst = &(pSrc2->next);
            /* if end of src2, dst = rest of src1 */
            if(NULL == (pSrc2 = GetFirstNode(plSrc2))){
                pSrc1->next = plSrc1->first;
                *ppDst = pSrc1;
                plSrc1->first = NULL;
                break;
            }
        } else {                        /* src1 <= src2 */
            *ppDst = pSrc1;
            ppDst = &(pSrc1->next);
            /* if end of src1, dst = rest of src2 */
            if(NULL == (pSrc1 = GetFirstNode(plSrc1))){
                pSrc2->next = plSrc2->first;
                *ppDst = pSrc2;
                plSrc2->first = NULL;
                break;
            }
        }
    }
}