数组模拟数据结构

链表

  • 面试题中的链表一般用结构体来构造表示
struct Node {
	int val;
	Node *next;
};
new Node(); //非常慢
  • 笔试题中一般用数组模拟链表

用数组模拟单链表

  • 用邻接表来模拟
  • 邻接表一般用来存储树和图
  • 静态链表

单链表

//静态链表
//用数组模拟链表,速度快,同时可以做指针链表可以做的所有事情
//c++中动态链表生成需要用到new操作,这个操作很耗时
//算法可以不考虑内存泄漏问题

#include <iostream>

using namespace std;

const int N = 100010;

//head表示头节点的下标,也即头节点的下一个位置
//e[i]表示节点的值
//ne[i]表示节点的next指针
//idx表示存储当前已经用到了哪个位置(下标),可以理解为数组用掉了多少
int head, e[N], ne[N], idx;

//初始化
void init(){
    head = -1;//最开始链表为空,头节点指向空节点
    idx = 0;
}

//在头部插入节点
void add_to_head(int x){
    e[idx] = x;
    ne[idx] = head;
    head = idx ++; 
}

//将x插入下标为k的元素后面
void add(int k, int x) {
    e[idx] = x;
    ne[idx] = ne[k];
    ne[k] = idx ++;
}

//删除下标为k的元素后面的值
void remove(int k){
    ne[k] = ne[ne[k]];
}

int main() {
    int m;
    cin>>m;
    
    init();
    
    while(m --){
        int k, x;
        char op;
        
        cin >> op;
        if(op == 'H'){
            cin >> x;
            add_to_head(x);
        }
        else if(op == 'D'){
            cin >> k;
            if(!k) head = ne[head];
            remove(k - 1);
        }
        else{
            cin >> k >> x;
            add(k - 1, x);
        }
    }
    
    for(int i = head; i != -1; i = ne[i]) cout << e[i] << ' ';
    cout << endl;
    
    return 0;
}

用数组模拟双链表

  • 可以用来优化某些问题
  • 模拟思路类似单链表,只不过需要维护两个指针数组

双链表

#include <iostream>

using namespace std;

const int N = 100010;

int m;
int e[N], l[N], r[N], idx;

// 在节点a的右边插入一个数x
//在a的左边插入一个元素等价于在l[k]的右边插入该元素
void insert(int a, int x)
{
    e[idx] = x;
    l[idx] = a, r[idx] = r[a];
    l[r[a]] = idx, r[a] = idx ++ ;
}

// 删除节点a
void remove(int a)
{
    l[r[a]] = l[a];
    r[l[a]] = r[a];
}

int main()
{
    cin >> m;

    // 边界, 用0表示左端点,1表示右端点
    r[0] = 1, l[1] = 0;
    idx = 2;

    while (m -- )
    {
        string op;
        cin >> op;
        int k, x;
        if (op == "L")
        {
            cin >> x;
            insert(0, x);
        }
        else if (op == "R")
        {
            cin >> x;
            insert(l[1], x);
        }
        else if (op == "D")
        {
            cin >> k;
            remove(k + 1);
        }
        else if (op == "IL")
        {
            cin >> k >> x;
            insert(l[k + 1], x);
        }
        else
        {
            cin >> k >> x;
            insert(k + 1, x);
        }
    }

    for (int i = r[0]; i != 1; i = r[i]) cout << e[i] << ' ';
    cout << endl;

    return 0;
}

  • LIFO

模拟栈

#include <iostream>

using namespace std;

const int N = 100010;

int m;
int stk[N], tt;

int main()
{
    cin >> m;
    while (m -- )
    {
        string op;
        int x;

        cin >> op;
        if (op == "push")
        {
            cin >> x;
            stk[ ++ tt] = x;
        }
        else if (op == "pop") tt -- ;
        else if (op == "empty") cout << (tt ? "NO" : "YES") << endl;
        else cout << stk[tt] << endl;
    }

    return 0;
}

队列

  • FIFO

模拟队列

#include <iostream>

using namespace std;

const int N = 100010;

int m;
int q[N], hh, tt = -1;

int main()
{
    cin >> m;

    while (m -- )
    {
        string op;
        int x;

        cin >> op;
        if (op == "push")
        {
            cin >> x;
            q[ ++ tt] = x;
        }
        else if (op == "pop") hh ++ ;
        else if (op == "empty") cout << (hh <= tt ? "NO" : "YES") << endl;
        else cout << q[hh] << endl;
    }

    return 0;
}
原文地址:https://www.cnblogs.com/huhu555/p/14656769.html