程序员面试金典-0305-栈排序

这篇博客详细介绍了如何实现一个栈排序算法,该算法在不使用额外的数据结构(如数组)的情况下,仅通过一个辅助栈就能保持栈内元素始终有序。文章提供了C语言的代码实现,包括push、pop、peek和isEmpty等操作,并通过示例说明了算法的正确性和效率。此外,还讨论了处理重复数据的问题以及栈空时的特殊情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序员面试金典-0305-栈排序

这个题目一直没pass,是因为测试数据有相同的数据,需要注意。

栈排序。 编写程序,对栈进行排序使最小元素位于栈顶。最多只能使用一个其他的临时栈存放数据,但不得将元素复制到别的数据结构(如数组)中。该栈支持如下操作:push、pop、peek 和 isEmpty。当栈为空时,peek 返回 -1。

示例1:

 输入:
["SortedStack", "push", "push", "peek", "pop", "peek"]
[[], [1], [2], [], [], []]
 输出:
[null,null,null,1,null,2]
示例2:

 输入: 
["SortedStack", "pop", "pop", "push", "pop", "isEmpty"]
[[], [], [], [1], [], []]
 输出:
[null,null,null,null,null,true]

struct list_node{
    int data;
    struct list_node * next;
};

typedef struct {
    struct list_node * head;
} SortedStack;

bool sortedStackIsEmpty(SortedStack * obj);
struct list_node * node_init(int val){
    struct list_node * t = malloc(sizeof(struct list_node));
    t->data = val;
    t->next = NULL;
    return t;
}

SortedStack* sortedStackCreate() {
    SortedStack * s = malloc(sizeof(SortedStack));
    struct list_node * h = node_init(-1);
    s->head = h;
    return s;    
}


void sortedStackPush(SortedStack* obj, int val) {
    struct list_node * h = obj->head;
    struct list_node * temp = node_init(val);
    if(h->next==NULL){
        h->next = temp;
    }
    else if(val<=(h->next->data)){
        struct list_node *first = h->next;
        temp->next=first;
        h->next=temp;}
    else{
        struct list_node * pre;
        struct list_node * post;
        pre = h->next;
        post = pre->next;
        while(post!=NULL){
            int t1 = pre->data;
            int t2 = post->data;
            if(t1<=val && val<t2){
                temp->next=post;
                pre->next=temp;
                return;
            }
            pre=post;
            post=pre->next;
        }
        pre->next=temp;
    }
    return;
}

void sortedStackPop(SortedStack* obj) {
    if(sortedStackIsEmpty(obj))
        return;
    struct list_node * h = obj->head;
    struct list_node *temp = h->next;
    h->next=temp->next;
    free(temp);
}

int sortedStackPeek(SortedStack* obj) {
    if(sortedStackIsEmpty(obj))
        return -1;
    struct list_node * h = obj->head;
    int ret = h->next->data;
    return ret;
}

bool sortedStackIsEmpty(SortedStack* obj) {
    if(obj->head->next==NULL)
        return true;
    else
       return false;
}

void sortedStackFree(SortedStack* obj) {
    struct list_node * h = obj->head;
    while(h->next!=NULL){
        struct list_node * temp = h->next;
        h->next=temp->next;
        free(temp);
    }
    free(h);
    free(obj);
}

/**
 * Your SortedStack struct will be instantiated and called as such:
 * SortedStack* obj = sortedStackCreate();
 * sortedStackPush(obj, val);
 
 * sortedStackPop(obj);
 
 * int param_3 = sortedStackPeek(obj);
 
 * bool param_4 = sortedStackIsEmpty(obj);
 
 * sortedStackFree(obj);
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值