#include<iostream> using namespace std; struct LinkedList { // todo: some variables here void initialize() { int n; scanf("%d", &n); int *a = new int[n]; for(int i = 0; i < n; i++) scanf("%d", &a[i]); // todo delete[] a; } void insert(int i, int x) { // todo } void erase(int i) { // todo } void swap() { // todo } void moveback(int i, int x) { // todo } int query(int i) { // todo } void printAll() { // todo } void ClearMemory() { // todo } }; int main() { LinkedList List; int m, op, i, x; List.initialize(); scanf("%d", &m); while(m--) { scanf("%d", &op); if(op == 1) { scanf("%d%d", &i, &x); List.insert(i, x); } else if(op == 2) { scanf("%d", &i); List.erase(i); } else if(op == 3) { List.swap(); } else if(op == 4) { scanf("%d%d",&i, &x); List.moveback(i, x); } else if(op == 5) { scanf("%d", &i); printf("%d\n", List.query(i)); } else if(op == 6) { List.printAll(); } } List.ClearMemory(); return 0; }
时间: 2024-02-14 19:09:36 浏览: 65
抱歉,上述代码并没有实现您的要求,只是一个模版代码。以下是根据您的要求修改后的代码实现单链表:
```c++
#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class LinkedList {
private:
ListNode *head, *tail;
int size;
public:
LinkedList(): head(NULL), tail(NULL), size(0) {}
void initialize() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
ListNode *node = new ListNode(x);
if(head == NULL) {
head = tail = node;
}
else {
tail->next = node;
tail = node;
}
size++;
}
}
void insert(int i, int x) {
if(i < 0 || i > size) return;
ListNode *node = new ListNode(x);
if(i == 0) {
node->next = head;
head = node;
if(tail == NULL) tail = head;
}
else if(i == size) {
tail->next = node;
tail = node;
}
else {
ListNode *cur = head;
for(int j = 0; j < i - 1; j++) {
cur = cur->next;
}
node->next = cur->next;
cur->next = node;
}
size++;
}
void erase(int i) {
if(i < 0 || i >= size) return;
ListNode *cur = head, *pre = NULL;
for(int j = 0; j < i; j++) {
pre = cur;
cur = cur->next;
}
if(pre == NULL) {
head = head->next;
if(head == NULL) tail = NULL;
}
else {
pre->next = cur->next;
if(pre->next == NULL) tail = pre;
}
delete cur;
size--;
}
void swap() {
if(size <= 2) return;
ListNode *cur = head, *pre = NULL;
while(cur && cur->next) {
ListNode *tmp = cur;
cur = cur->next;
tmp->next = cur->next;
cur->next = tmp;
if(pre) pre->next = cur;
pre = tmp;
cur = tmp->next;
}
if(size % 2 == 0) tail = pre;
}
void moveback(int i, int x) {
if(i <= 0 || i > size) return;
ListNode *cur = head, *pre = NULL;
for(int j = 0; j < i - 1; j++) {
pre = cur;
cur = cur->next;
}
for(int j = 0; j < x; j++) {
if(cur == NULL) cur = head;
ListNode *tmp = cur->next;
if(pre == NULL) {
head = tmp;
tail->next = cur;
cur->next = NULL;
tail = cur;
}
else if(tmp == NULL) {
pre->next = NULL;
tail = pre;
cur->next = head;
head = cur;
}
else {
pre->next = tmp;
cur->next = tmp->next;
tmp->next = cur;
}
pre = cur;
cur = cur->next;
}
}
int query(int i) {
if(i < 1 || i > size) return -1;
ListNode *cur = head;
for(int j = 1; j < i; j++) {
cur = cur->next;
}
return cur->val;
}
void printAll() {
ListNode *cur = head;
while(cur) {
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
void clearMemory() {
ListNode *cur = head;
while(cur) {
ListNode *tmp = cur;
cur = cur->next;
delete tmp;
}
head = tail = NULL;
size = 0;
}
};
int main() {
LinkedList list;
int m, op, i, x;
list.initialize();
scanf("%d", &m);
while(m--) {
scanf("%d", &op);
if(op == 1) {
scanf("%d%d", &i, &x);
list.insert(i, x);
}
else if(op == 2) {
scanf("%d", &i);
list.erase(i);
}
else if(op == 3) {
list.swap();
}
else if(op == 4) {
scanf("%d%d", &i, &x);
list.moveback(i, x);
}
else if(op == 5) {
scanf("%d", &i);
printf("%d\n", list.query(i));
}
else if(op == 6) {
list.printAll();
}
}
list.clearMemory();
return 0;
}
```
阅读全文