实现特殊的栈,即保持栈元素的大小顺序

栈中使用两个指针链表,分别为栈顺序链表和大小顺序链表。

/**
Description:

We all know the famous link list. We can use these to hold data in a linear fashion. The link list can be used to implement a stack as well for example.
For this challenge you will need to develop a smart stack list. So what makes this link list so smart? This link list will behave like a stack but also maintain an ascending sorted order of the data as well. So our link list maintains 2 orders (stack and sorted)
In today's world link list data structures are part of many development platforms. For this challenge you will be implementing this and not using already pre-made frameworks/standard link lists code that you call. You have to implement it and all the features.
The Challenge will have 2 steps.
Implement your smart link list
Test your smart link list

Data:
The smart link list will hold integer data.

Methods:
The smart link list must support these methods or operations.
Push - Push a number on top of the stack (our link list is a stack)
Pop - Pop the number off the top of the stack
Size - how many numbers are on your stack
Remove Greater - remove all integers off the stack greater in value then the given number
Display Stack - shows the stack order of the list (the order they were pushed from recent to oldest)
Display Ordered - shows the integers sorted from lowest to highest.
Smart list:

One could make a stack and when you do the display ordered do a sort. But our smart list must have a way so that it takes O(n) to display the link list in stack order or ascending order. In other words our link list is always in stack and sorted order. How do you make this work? That is the real challenge.
Test your list:

Develop a quick program that uses your smart stack list.
Generate a random number between 1-40. Call this number n.
Generate n random numbers between -1000 to 1000 and push them on your list
Display stack and sorted order
Generate a random number between -1000 and 1000 can call it x
Remove greater X from your list. (all integers greater than x)
Display stack and sorted order
pop your list (size of list / 2) times (50% of your list is removed)
Display stack and sorted order
**/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>


typedef struct Node{
    void *owner;
    Node *pre;
    Node *next;
}Node;

typedef struct NodeList{
    Node *head;
    Node *tail;
};


void Insert(Node *p, Node *q){
    if (!p || !q){
        return;
    }
    p->next = q;
    p->pre = q->pre;

    if (q->pre){
        q->pre->next = p;
    }
    q->pre = p;
}

void Remove(Node *p){
    if (!p){
        return;
    }
    if (p->next){
        p->next->pre = p->pre;
    }
    if (p->pre){
        p->pre->next = p->next;
    }
    p->pre = NULL;
    p->next = NULL;
}


typedef struct StackNode{
    Node stackNode;
    Node orderNode;
    int val;
};
typedef struct Stack{
    NodeList orderList;
    NodeList stackList;
    int size;
};

int GetVal(Node *p){
    return ((StackNode *)(p->owner))->val;
}

void InsertByOrder(Node *p, Stack *stack){
    Node *q = stack->orderList.head;
    int val = GetVal(p);

    //处理空情况
    if (!q){
        stack->orderList.head = p;
        stack->orderList.tail = p;
        return;
    }

    while (q){
        int k = GetVal(q);
        if (val <= k){
            Insert(p, q);
            //处理最小情况
            if (stack->orderList.head == q){
                stack->orderList.head = p;
            }
            return;
        }
        else{
            q = q->next;
        }
    }

    //处理最大情况
    p->pre = stack->orderList.tail;
    stack->orderList.tail->next = p;
    stack->orderList.tail = p;
}

void Push(int val, Stack *stack){
    StackNode *p = (StackNode *)malloc(sizeof(StackNode));
    if (!p){
        exit(EXIT_FAILURE);
    }
    p->orderNode.pre = NULL;
    p->orderNode.next = NULL;
    p->orderNode.owner = p;

    p->stackNode.pre = NULL;
    p->stackNode.next = NULL;
    p->stackNode.owner = p;

    p->val = val;

    //空的stack
    if (stack->orderList.head == NULL){
        stack->orderList.head = &(p->orderNode);
        stack->orderList.tail = stack->orderList.head;

        stack->stackList.head = &(p->stackNode);
        stack->stackList.tail = stack->stackList.head;

        stack->size = 1;
        return;
    }

    //将p的stack节点插入合适位置
    Insert(&(p->stackNode), stack->stackList.head);
    stack->stackList.head = &(p->stackNode);

    //将p的order节点插入合适位置
    InsertByOrder(&(p->orderNode), stack);
    ++stack->size;
}

void Pop(Stack *stack){
    //stack 不可以为空
    assert(stack->orderList.head);

    --stack->size;

    //最后一个节点的情况
    if (stack->orderList.head == stack->orderList.tail){
        StackNode *p = (StackNode *)(stack->orderList.head->owner);
        free(p);
        stack->orderList.head = NULL;
        stack->orderList.tail = NULL;

        stack->stackList.head = NULL;
        stack->stackList.tail = NULL;

        return;
    }

    StackNode *p = (StackNode *)(stack->stackList.head);
    //更新orderlist
    if ((StackNode *)(stack->orderList.head->owner) == p){
        stack->orderList.head = p->orderNode.next;
    }
    //更新stackList
    stack->stackList.head = p->stackNode.next;

    Remove(&(p->orderNode));
    Remove(&(p->stackNode));
    free(p);
}

int Size(Stack* stack){
    return stack->size;
}

void RemoveGreater(int num, Stack *stack){
    StackNode *p = (StackNode *)(stack->orderList.head->owner);

    while (p){
        int val = GetVal(&(p->orderNode));
        if (val > num){
            break;
        }
        else{
            if (p->orderNode.next){
                p = (StackNode *)(p->orderNode.next->owner);
            }
            else{
                p = NULL;
            }
        }
    }

    while (p){
        if ((StackNode *)(stack->orderList.head->owner) == p){
            stack->orderList.head = p->orderNode.next;
        }
        if ((StackNode *)(stack->orderList.tail->owner) == p){
            stack->orderList.tail = p->orderNode.pre;
        }
        if ((StackNode *)(stack->stackList.head->owner) == p){
            stack->stackList.head = p->stackNode.next;
        }
        if ((StackNode *)(stack->stackList.tail->owner) == p){
            stack->stackList.tail = p->stackNode.pre;
        }

        StackNode *q = NULL;
        if (p->orderNode.next){
            q = (StackNode *)(p->orderNode.next->owner);
        }

        Remove(&(p->orderNode));
        Remove(&(p->stackNode));
        free(p);
        p = q;
        --stack->size;
        assert(stack->size >= 0);
    }
}


void DisplayByStack(Stack *stack){
    if (stack->orderList.head == NULL){
        printf("empty stack\n");
        return;
    }
    StackNode *p = (StackNode *)(stack->stackList.head->owner);
    printf("display by stack:\n");
    while (p){
        printf("%d ", p->val);
        if (p->stackNode.next){
            p = (StackNode *)(p->stackNode.next->owner);
        }
        else{
            p = NULL;
        }
    }
    printf("\n");
}

void DisplayByOrder(Stack *stack){
    if (stack->orderList.head == NULL){
        printf("empty stack\n");
        return;
    }
    StackNode *p = (StackNode *)(stack->orderList.head->owner);
    printf("display by order:\n");
    while (p){
        printf("%d ", p->val);
        if (p->orderNode.next){
            p = (StackNode *)(p->orderNode.next->owner);
        }
        else{
            p = NULL;
        }
    }
    printf("\n");
}

//Generate a random number between 1 - 40. Call this number n.
//Generate n random numbers between - 1000 to 1000 and push them on your list
//Display stack and sorted order
//Generate a random number between - 1000 and 1000 can call it x
//Remove greater X from your list. (all integers greater than x)
//Display stack and sorted order
//pop your list(size of list / 2) times(50 % of your list is removed)
//Display stack and sorted order




int GenerateRandom(int low, int hight){
    double k = (double)rand();
    while (k > 1){
        k = k / 10;
    }

    return (k * (hight - low)) + low;
}
int main(){
    srand((unsigned)time(NULL));
    for (int i = 1000; i >= 0; --i){
        Stack stack;
        stack.orderList.head = NULL;
        stack.orderList.tail = NULL;
        stack.stackList.head = NULL;
        stack.stackList.tail = NULL;
        stack.size = 0;

        int n = GenerateRandom(1, 40);
        for (int i = 0; i < n; ++i){
            int k = GenerateRandom(-1000, 1000);
            printf("push number :%d\n", k);
            Push(k, &stack);
        }
        DisplayByStack(&stack);
        DisplayByOrder(&stack);
        printf("\n");

        int x = GenerateRandom(-1000, 1000);
        printf("remove greater than :%d\n", x);
        RemoveGreater(x, &stack);
        DisplayByStack(&stack);
        DisplayByOrder(&stack);
        printf("\n");

        int m = Size(&stack);
        for (int i = 0; i < m / 2; ++i){
            Pop(&stack);
        }

        printf("pop half of stack size\n");
        DisplayByStack(&stack);
        DisplayByOrder(&stack);

    }
 
    return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容