A Peaceful Tree 2020-07-18 00:14 采纳率: 50%
浏览 62
已采纳

Stack with operations on middle element

希望有哪位大佬可以帮助我解决这道题!

需要实现的功能:

1) push() 将元素添加到堆栈顶部。

2) pop() 从堆栈顶部删除一个元素。

3) findMiddle() 将返回堆栈的中间元素。

4)deleteMiddle() 将删除堆栈的中间元素。

那么,用于实现此特殊堆栈的数据结构是什么?

使用**双链表**(DLL)。我们可以通过保持mid指针来在O(1)时间中找到中间元素。我们可以使用上一个和下一个指针在两个方向上移动中间指针。

图片说明

不要修改源代码,并完成push()、pop()、 findMiddle()、deleteMiddle()四个元素

//-----Include required headers here-----
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//-----End of headers-----


//-----Add new functions here(if any)-----

//-----New functions end here-----

//-----A Doubly Linked List Node-----
// DO NOT MODIFY IT
struct DLLNode {
    struct DLLNode *prev;
    int data;
    struct DLLNode *next;
};

/* Representation of the stack data structure that supports findMiddle(),
   deleteMiddle() in O(1) time.  The Stack is implemented using Doubly 
   Linked List. It maintains pointer to head node, pointer to middle 
   node and count of nodes */
// DO NOT MODIFY IT
struct myStack {
    struct DLLNode *head;
    struct DLLNode *mid;
    int count;
};

//-----Function to create the stack data structure-----
// DO NOT uncomment the below function. It is for your reference purpose only.
// We will use this function to create the stack.
/*struct myStack *createMyStack() {
    struct myStack *ms = (struct myStack*) malloc(sizeof(struct myStack));
    ms->count = 0;
    ms->head = NULL;
    ms->mid = NULL;
    return ms;
}; */

//-----Function to push an element to the stack-----
void push(struct myStack *ms, int new_data) {
    /* allocate DLLNode and put in data */
    struct DLLNode* new_DLLNode = (struct DLLNode*) malloc(sizeof(struct DLLNode));
    new_DLLNode->data  = new_data;

    //-----Appropriately update new_DLLNode and ms to get the functionality-----


    //-----code ends here-----
}

//-----Function to pop an element from stack. Return the popped element-----
//-----Will not be called for empty stack-----
int pop(struct myStack *ms) {
    struct DLLNode *head = ms->head;
    int item = head->data;

    //-----Appropriately update ms to get the required functionality-----


    //-----code ends here-----

    free(head);

    return item;
}

//-----Function for finding middle of the stack-----
//-----Will not be called for empty stack-----
int findMiddle(struct myStack *ms) {
    //-----code begins here-----


    //-----code ends here-----
}

//-----Function for deleting middle of the stack-----
//-----Return the value deleted, will not be called for empty stack-----
int deleteMiddle(struct myStack *ms) {
    //-----code begins here-----


    //-----code ends here-----
}

  • 写回答

1条回答 默认 最新

  • 关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA