1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include "iostream"
using namespace std; const int N = 1e3 + 2; int a[N] = {1, 2, 5, 9}; int n = 4;
void add(int x) { int i; for (i = 0; i < n; i++) { if (a[i] >= x) break; } for (int j = n; j >= i; j--) a[j] = a[j - 1]; a[i] = x; n++; }
void print() { for (int i = 0; i < n; i++) cout << a[i] << ' '; cout << endl; }
int main() {
cout << "原序列为:" << endl; print(); cout << "请输入要插入的数:";
int x; cin >> x;
add(x); cout << "插入后序列:" << endl; print(); return 0; }
|
2
遍历一遍顺序表,不等于要删除的值的元素放回序列即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include "iostream" using namespace std; const int N = 1e3+3; int a[N] = {0,1,2,5,5,7,7,9}; int n; void print() { for(int i = 1;i<=n;i++) cout<<a[i]<< ' '; cout<<endl; }
void delete_x(int x) { int k = 0; for(int i = 1;i<=n;i++) { if(a[i]!=x) k++,a[k] = a[i];
} n = k; }
int main() { n = 7; int x; print(); cin>>x;
delete_x(x); print(); return 0; }
|
3
顺序表:
使用两个指针i,j,i指向头部,j指向尾部,每次交换a[i]和a[j]即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include "iostream" using namespace std; const int N = 1e2+9; int a[N]; int n; void print() { for(int i = 1;i<=n;i++) cout<<a[i]<< ' '; cout<<endl; }
void reverse() { for(int i = 1,j = n;i<j;i++,j--) swap(a[i],a[j]); }
int main() { n = 10; for(int i = 1;i<=n;i++) a[i] =i; cout<<"原顺序表:"<<endl; print(); reverse(); cout<<"逆序后的顺序表:"<<endl; print(); return 0; }
|
单链表
使用两个指针a,b,分别指向当前节点和当前节点的下一个节点,每次反转两者间的next指针,这里要用一个临时指针指向b的下一节点,不然后面就找不到了,反转操作如下:
1 2 3 4
| Node*temp = b->next; b->next = a; a = b; b= temp;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include "iostream"
using namespace std; const int N = 1e3 + 9; struct Node { int x; Node *next; }; int n; Node *first = new Node();
void add(int x) { Node *p = first; Node *s = new Node(); s->x = x; while (p->next != nullptr) { p = p->next; } s->next = p->next; p->next = s; n++; }
void print() { Node *p = first->next; while (p != nullptr) { cout << p->x << ' '; p = p->next; } cout << endl; }
void reserve() { Node *a = nullptr, *b; b = first->next; while(b!= nullptr) { Node*temp = b->next; b->next = a; a = b; b= temp; } first->next = a; }
int main() { first->next = nullptr;
for (int i = 1; i <= 5; i++) { add(i); } cout<<"反转前:"; print(); reserve(); cout<<"反转后:"; print(); return 0; }
|
4
判断时p指针指向当前节点,q指针指向p的下一节点,只要出现p节点的值大于等于q节点的值就返回false。
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
| bool check() { Node*p = first->next,*q;
while(p->next!= nullptr) { q = p->next; if(q->x<=p->x) return false; p = p->next; } return true; }
|
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include "iostream"
using namespace std; int n; struct Node { int x; Node *next; }; Node *first = new Node();
void add(int x) { Node *p = first; Node *s = new Node(); s->x = x; while (p->next != nullptr) { p = p->next; } s->next = p->next; p->next = s; n++; }
void print() { Node *p = first->next; while (p != nullptr) { cout << p->x << ' '; p = p->next; } cout << endl; }
bool check() { Node*p = first->next,*q;
while(p->next!= nullptr) { q = p->next; if(q->x<=p->x) return false; p = p->next; } return true; } int main() { first ->next = nullptr; int t; cout<<"请输入单链表元素(以-1结束):"; while(cin>>t,t!=-1) add(t); cout<<"单链表:"; print(); if(!check()) cout<<"不"; cout<<"递增有序。"<<endl; return 0; }
|
5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include "iostream" using namespace std; struct Node { int x; Node*next; }; Node*first = new Node();
void add(int x) { auto *p = first; while(p->next!= nullptr) { p = p->next; } Node *s = new Node(); s->x = x; s->next = p->next; p->next = s; }
void del(int x) { auto *p = first; while(p->next!= nullptr) { auto *q = p->next; if(q->x==x) { p->next = q->next; delete q; return; } p = p->next; }
}
void pop_min() { int current_min = 1e9; auto *p = first->next; while(p!= nullptr) { current_min = min(current_min,p->x); p = p->next; } cout<<current_min<<' '; del(current_min); } int main() { first ->next = nullptr; int t; cout<<"请输入单链表元素(以-1结束):"; int n = 0;
while(cin>>t,t!=-1) add(t),n++; cout<<endl;
while(n--) pop_min(); cout<<endl; return 0; }
|