说明:用函数的形式刷完数据结构链表的专题,整个专题整合成了一篇代码,只要在main函数里根据题目做相应变换即可,但是没有完全按照数据结构算法的要求严格书写(并非所有函数都有输出,偷了个懒)使用C++完成
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
using namespace std;
const int maxn = 1e5 + 10;
const double PI = acos(-1.0);
struct node {
int data;
node *next;
};
struct farey {
int a, b;
farey *next;
};
struct shuang
{
shuang *next;
int data;
shuang *front;
};
node *creat1(int n) {
node *p, *tail, *head;
head = new node;
head->next = NULL;
tail = head;
for (int i = 0; i < n; i++) {
p = new node;
cin >> p->data;
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
node *creat2(int n) {
node *p, *head;
head = new node;
head->next = NULL;
for (int i = 0; i < n; ++i) {
p = new node;
cin >> p->data;
p->next = head->next;
head->next = p;
}
return head;
}
shuang *creat3(int n){
shuang *p,*head, *tail;
head = new shuang;
head->front = head->next = NULL;
tail = head;
for(int i = 0; i < n; i ++) {
p = new shuang;
cin >> p->data;
p->next = NULL;
tail->next = p;
p->front = tail;
tail = p;
}
return head;
}
void query(shuang *root, int x) {
shuang *p = root->next;
while (p->data != x)
{
p=p->next;
}
int flag = 0;
if(p->front !=root) {
flag = 1;
cout << p->front->data;
}
if(flag == 1) cout << ' ';
if(p->next != NULL)
cout << p->next->data;
cout << endl;
}
void pri(node *root) {
node *p = root->next;
while (p) {
printf("%d%c", p->data, p->next == NULL ? '\n' : ' ');
p = p->next;
}
}
void reserve(node *root) {
node *p = root->next;
node *q = p->next;
root->next = NULL;
while (p) {
p->next = root->next;
root->next = p;
p = q;
if (q) {
q = q->next;
}
}
}
node *marge(node *root1, node *root2) {
node *root, *p, *q, *tail;
p = root1->next;
q = root2->next;
root = new node;
root->next = NULL;
tail = root;
while (p && q) {
if (p->data < q->data) {
tail->next = p;
tail = p;
p = p->next;
} else {
tail->next = q;
tail = q;
q = q->next;
}
}
if (p) {
tail->next = p;
tail = p;
}
if (q) {
tail->next = q;
tail = q;
}
return root;
}
void Sort(node *root) {
node *p, *q, *qq;
p = root->next;
qq = p->next;
root->next = NULL;
while (p) {
q = root;
while (q->next) {
if (q->next->data > p->data) {
break;
}
q = q->next;
}
p->next = q->next;
q->next = p;
p = qq;
if (qq) qq = qq->next;
}
}
node *chai(node *root, int &ou) {
ou = 0;
node *head, *p, *q, *tail1, *tail2;
head = new node;
head->next = NULL;
tail1 = head;
p = root->next;
q = p->next;
root->next = NULL;
tail2 = root;
while (p) {
if (p->data % 2 == 0) {
ou++;
p->next = NULL;
tail1->next = p;
tail1 = p;
} else {
p->next = NULL;
tail2->next = p;
tail2 = p;
}
p = q;
if (q) q = q->next;
}
return head;
}
void erase(node *root, int &n) {
node *p, *q, *qq, *tail;
p = root->next;
qq = p->next;
root->next = NULL;
tail = root;
while (p) {
q = root->next;
int flag = 0;
while (q) {
if (q->data == p->data) {
n--;
flag = 1;
break;
}
q = q->next;
}
if (flag == 0) {
p->next = NULL;
tail->next = p;
tail = p;
}
p = qq;
if (qq) qq = qq->next;
}
}
farey *Farey(int n) {
farey *head, *p, *q;
head = new farey;
head->next = new farey;
head->next->a = 0;
head->next->b = 1;
head->next->next = new farey;
head->next->next->a = 1;
head->next->next->b = 1;
head->next->next->next = NULL;
for (int i = 2; i <= n; i++) {
p = head->next;
while (p->next) {
if (p->b + p->next->b <= i) {
q = new farey;
q->b = p->b + p->next->b;
q->a = p->a + p->next->a;
q->next = p->next;
p->next = q;
}
p = p->next;
}
}
return head;
}
void pri_farey(farey *root) {
farey *p = root->next;
int con = 0;
while (p) {
con++;
printf("%d/%d", p->a, p->b);
if(p->next != NULL && con%10 !=0) cout << '\t';
if (con % 10 == 0) cout << '\n';
p = p->next;
}
}
int main(int argc, char const *argv[]) {
int n, m;
cin >> n >> m;
shuang *root;
root = creat3(n);
while (m--)
{
int x;
cin >> x;
query(root,x);
}
return 0;
}