链表,栈,队列,递归行为,哈希表,有序表

链表

1.单链表/双链表的反转

// 单向链表节点
struct Node{
    int value;
    Node* next;
    Node(int data)
    : value(data)
    , next(nullptr)
    {}
};
// 双向链表节点
struct DoubleNode{
    int value;
    DoubleNode* last;
    DoubleNode* next;

};

Node* reverseLinkedList(Node* head)
{
    Node* pre=nullptr;
    Node* next= nullptr;
    while(head!=nullptr)
    {
        next = head->next;
        head->next = pre;
        pre = head;
        head = next;
    }
    return pre;
}
DoubleNode* reverseDoubleList(DoubleNode* head)
{
    DoubleNode* pre = nullptr;
    DoubleNode* next = nullptr;
    while(head!=nullptr)
    {
        next = head->next;
        head->next = pre;
        head->last = next;
        pre = head ;
        head = next;
    }
    return pre;
}

2.删除链表中指定的值

Node* removeValue(Node* head,int num)
{
    // 删除的num可能出现在链表头部,我们最后需要返回头节点,
    // 因此删除头部元素会导致head野指针。
    Node* cur = head;
    while(cur!=nullptr)
    {
        if(cur->value!=num){
            break;
        }
        Node* next = cur->next;
        delete cur;
        cur = next;
    }
    head = cur;
    Node* pre = head;
    while(cur!=nullptr)
    {
        Node* next = cur->next;
        if(next->value == num)
        {
            pre->next = next;
            delete cur;
        }
        else
        {
            pre = cur;
        }
        cur=next;
    }
    return head;
}

队列

1.数组循环队列的实现

class MyQueue{
private:
    vector<int> arr;
    int pushi; // 该位置插入元素
    int polli; // 该位置拿出元素
    int size; // 有效元素个数
public:
    MyQueue(int length)
    :pushi(0)
    ,polli(0)
    ,size(0)
    ,arr(length){}
    int nextIndex(int index)
    {
        return (index+1) % arr.size();
    }
    // 插入队列
    void push(int value)
    {
        // 队列满了
        if(size==arr.size()){
            //扩容 或者 抛异常
        }
        size++;
        arr[pushi] = value;
        pushi = nextIndex(pushi);
    }
    int pop()
    {
        if(size == 0){
            // 队列为空,抛异常
        }
        size--;
        int ans = arr[polli];
        polli = nextIndex(polli);
        return ans;
    }

};

2. 双向链表实现双端队列

1.用数组实现栈

固定长度的数组(超过下标范围报错, 或者使用动态数组)+index 控制
index: 新进来的数要放的位置
一旦发现 index 超过了下标的范围给用户报错, 标记栈已经满了

在这里插入图片描述

栈和队列的面试题

1. 实现最小栈

leetcode

在这里插入图片描述

class MinStack {
public:
    MinStack() {
        
    }
    
    void push(int val) {
            st.push(val);
            if(minst.empty()||val<=minst.top())
            {
                minst.push(val);
            }
    }
    
    void pop() {
        if(!st.empty())
        {
	        if(st.top()==minst.top())
	        {
	            minst.pop();
	        }
        st.pop();
        }
    }
    
    int top() {
        return st.top();
    }
    
    int getMin() {
        return minst.top();
    }
    private:
    stack<int> st;
    stack<int> minst;
};

2. 两个栈实现一个队列

leetcode

3.两个队列实现一个栈

leetcode

4.用栈实现图的广度优先遍历

结合第2题

5.用队列实现图的深度优先遍历

结合第3题

递归

Master公式可以计算递归时间复杂度

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2023框框

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值