# 线性表
链表
单链表
实现一个单链表,链表初始为空,支持三种操作:
<1>向链表头插入一个数;
<2>删除第 k 个插入的数后面的数;
<3>在第 k 个插入的数后插入一个数。
现在要对该链表进行 M次操作,进行完所有操作后,从头到尾输出整个链表。
指针构造
#include <iostream>
using namespace std;
struct ListNode{
int val;
ListNode* next;
ListNode(int x) : val(x),next(NULL){}
};
void insertAtHead(ListNode*& head, int val){
ListNode* newNode = new ListNode(val);
newNode -> next = head;
head = newNode;
}
void deleteAfterk(ListNode* head, int k){
int i = 1;
while (head != NULL && i < k){
head = head -> next;
i ++;
}
if (head != NULL && head -> next != NULL){
ListNode* temp = head -> next;
head -> next = temp -> next;
delete temp;
}
}
void insertAfterk(){
int i = 1;
while (head != NULL && i < k){
head = head -> next;
i ++;
}
if (head != NULL){
ListNode* newNode = new ListNode(val);
newNode -> next = new ListNode(val);
head -> next = newNode;
}
}
int main()
{
int M;
cin >> M;
ListNode* head = NULL;
for (int i = 0; i < M; i ++) {
int op, k, val;
cin >> op;
if (op == 1) {
cin >> val;
insertAtHead(head, val);
} else if (op == 2) {
cin >> k;
} else if (op == 3) {
cin >> k >> val;
insertAfterk(head, k, val);
}
}
while (head != NULL) {
cout << head -> val << " ";
head = head -> next;
}
cout << endl;
return 0;
}
指针法实现单链表需要频繁的new和delete操作,这两个操作大大增加了常数时间
new的底层涉及内存分配,调用构造函数,指针转换等多种复杂且费时的操作。一秒大概能new1w次左右。
在平时的工程代码中,不会涉及上万次的new操作,所以这种结构是一种 见代码知意 的好结构。
但是在算法比赛中,经常碰到操作在10w级别的链表操作,如果使用结构体这种操作,是无法在算法规定时间完成的。
所以,在算法比赛这种有严格的时间要求的环境中,不能频繁使用new操作。也就不能使用结构体来实现数组。
数组模拟
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int e[N], ne[N], idx = 1;
int head;
void init()
{
head = 0,idx = 1;
}
void add_k(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx ++;
}
void del_k(int k)
{
ne[k] = ne[ne[k]];
}
int main()
{
int n;
cin >> n;
char op;
while (n -- )
{
cin >> op;
int k, x;
if (op == 'H')
{
cin >> x;
add_k(0, x);
}
else if (op == 'I')
{
cin >> k >> x;
add_k(k, x);
}
else if (op == 'D')
{
cin >> k;
del_k(k);
}
}
for (int i = ne[head]; i; i = ne[i])
{
cout << e[i] << " ";
} //ne作为了读取e[ide]的桥梁,理解了这里就理解了数组怎么做到模拟的
return 0;
}
数组模拟链表实际上是利用e[N],ne[N]和数组下标实现的:
e[N]存放链表数据,
ne[N]存放e[idx]指向的下一个数据的数组下标 ( 代码中的 i = ne [i] ) ,
读取时根据ne[N]指向顺序读取e[N]。
双链表
指针构造
数组模拟
队列
队列:最前面叫队头,最后面叫队尾。前端删除,后端插入,先进先出
实现一个队列,队列初始为空,支持四种操作:
<1>push x – 向队尾插入一个数 x;
<2>pop – 从队头弹出一个数;
<3>empty – 判断队列是否为空;
<4>query – 查询队头元素。
数组模拟*
#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;
}
栈
栈:元素在表尾(栈顶)进行插入和删除,先进后出
数组模拟
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
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;
}