1、顺序表
第i个位置是逻辑意义的位置,从1开始,在实际编程用数组,从0开始,对应数组i-1位置
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<string>
#define INF 0x3f3f3f3f
using namespace std;
class LinearList {
int* ele;
int lenth;
int size;
public:
LinearList() {
size = 1000;
int n;
cin >> n;
lenth = n;
ele = new int[size];
for (int i = 0; i < lenth; i++) {
cin >> n;
*(ele + i) = n;
}
show();
}
void insert(int idx, int val) {
if (lenth >= size || idx < 1 || idx > lenth + 1){
cout << "error" << endl;
return;
}
int* i;
for (i = ele + lenth - 1; i >= ele + idx - 1; i--) {
*(i + 1) = *i;
}
*(++i) = val;
lenth++;
show();
}
void erase(int idx) {
if (idx > lenth || idx < 1) {
cout << "error" << endl;
return;
}
int* i;
for (i = ele + idx - 1; i < ele + lenth - 1; i++) {
*i = *(i + 1);
}
lenth--;
show();
}
void check(int idx) {
if (idx > lenth || idx < 1) {
cout << "error" << endl;
return;
}
cout << *(ele + idx - 1) << endl;
}
void show() {
for (int* i = ele; i <= ele + lenth - 1; i++) {
cout << *i << " ";
}
cout << endl;
}
// 从下标i开始连续插入n个元素
void multiinsert(int i, int n, int item[]) {
if (lenth >= size || i < 1 || i > lenth + 1) {
cout << "error" << endl;
return;
}
int* j;
for (j = ele + lenth - 1; j >= ele + i - 1; j--) {
*(j + n) = *j;
}
int k;
for (j = ele + i + n - 2, k = n - 1; k >= 0; k--) {
*j = item[k];
j--;
}
lenth += n;
show();
}
// 从下标i开始连续删除n个元素
void multidel(int i, int n) {
if (i > lenth || i < 1) {
cout << "error" << endl;
return;
}
int* j;
for (j = ele + i - 1; j < ele + lenth - n ; j++) {
*j = *(j + n);
}
lenth -= n;
show();
}
// 移位操作,op = 0则左移,op=1则右移,n为移动次数
void move(int op, int n) {
if (op == 0) {
for (int* i = ele; i <= ele + n - 1; i++) {
*(i + lenth) = *i;
}
for (int* i = ele + n; i <= ele + n + lenth - 1; i++) {
*(i - n) = *i;
}
}
else {
for (int* i = ele + lenth -1; i >= ele; i--) {
*(i + n) = *i;
}
for (int* i = ele; i <= ele + n - 1; i++) {
*i = *(i + lenth);
}
}
show();
}
};
2、单链表
#include<iostream>
#include<algorithm>
#include<string>
#define ll long long
using namespace std;
class Node {
public:
int data;
Node* next;
Node() { next = NULL; }
Node(int data, Node* next) {
this->data = data;
this->next = next;
}
};
class Linearlist {
Node* head;
int size;
public:
Linearlist(){
int n;
cin >> n;
size = n;
head = new Node(size,NULL);
Node* p = head;
for (int i = 0; i < size; i++) {
cin >> n;
Node* q = new Node(n, NULL);
p->next = q;
p = q;
}
}
// 插入
void insert(int idx, int v) {
if (idx > size + 1 || idx < 1) {
cout << "error" << endl;
return;
}
Node* p = head;
for (int i = 1; i < idx; i++) {
p = p->next;
}
Node* q = new Node(v, p->next);
p->next = q;
size++;
print();
}
// 删除
void erease(int idx) {
if (idx > size || idx < 1) {
cout << "error" << endl;
return;
}
Node* p = head;
for (int i = 1; i < idx; i++) {
p = p->next;
}
Node* q = p->next;
p->next = q->next;
delete q;
size--;
print();
}
// 查询
void check(int idx) {
if (idx > size || idx < 1) {
cout << "error" << endl;
return;
}
Node* p = head;
for (int i = 1; i <= idx; i++) {
p = p->next;
}
cout << p->data;
}
void print() {
Node* p = head;
for (int i = 1; i <= size; i++) {
p = p->next;
cout << p->data << " ";
}
cout << endl;
}
// 交换链表中两节点的位置
void swap(int idx1, int idx2) {
if (idx1 > size || idx1 < 1 || idx2 > size || idx2 < 1) {
cout << "error" << endl;
return;
}
Node* p = head;
for (int i = 1; i <= idx1; i++) {
p = p->next;
}
Node* q = head;
for (int i = 1; i <= idx2; i++) {
q = q->next;
}
Node* tmp1 = p->next;
Node* front = head;
for (int i = 1; i < idx2; i++) {
front = front->next;
}
p->next = q->next;
front->next = p;
front = head;
for (int i = 1; i < idx1; i++) {
front = front->next;
}
q->next = tmp1;
front->next = q;
print();
}
// 按递增顺序合并两个递增的链表
void merge(Linearlist& list2) {
Node* p = head->next;
Node* q = list2.head->next;
Node* c = head;
while (p && q) {
if (p->data <= q->data) {
c->next = p;
c = p;
p = p->next;
}
else {
c->next = q;
c = q;
q = q->next;
}
}
c->next = p ? p : q;
p = head;
for (int i = 1; i <= size + list2.size; i++) {
p = p->next;
cout << p->data << " ";
}
cout << endl;
}
};