单链表视频中版本:
#include<bits/stdc++.h>
using namespace std;
struct Data_type{
char name[15];
int score;
void out(){
printf("(%s:%d)",name,score);
}
};
bool operator ==(Data_type l, Data_type r){
if(l.score!=r.score) return 0;
if(strlen(l.name)!=strlen(r.name))return 0;
int n=strlen(l.name);
for(int i=0;i<n;i++){
if(l.name[i]!=r.name[i])return 0;
}
return 1;
}
bool operator <(Data_type l, Data_type r){
if(l.score!=r.score) return l.score>r.score;
int n=max(strlen(l.name), strlen(r.name));
for(int i=0;i<n;i++){
if(l.name[i]!=r.name[i])return l.name[i]<r.name[i];
}
return 0;
}
struct Node{
Data_type data;
Node* next;
};
struct link_list{
Node* head;
int SIZE;
void bulid(){
head= new Node;
head->next= NULL;
SIZE=0;
}
int size(){
return SIZE;
}
bool push(Data_type x, int k){
if(size()<k) return 0;
Node* now= head;
while(k--){
now= now->next;
}
Node* temp= new Node;
temp->data= x;
temp->next= now->next;
now->next= temp;
SIZE++;
return 1;
}
void push_front(Data_type data){
push(data, 0);
}
void push_back(Data_type data){
push(data, size());
}
Node* find(int pos){
if(size()<pos) return NULL;
Node* ret= head;
while(pos--){
ret= ret->next;
}
return ret;
}
vector<int> query(Data_type data){
vector<int>ret;
int cnt=0;
Node* now= head;
while(now->next!=NULL){
now= now->next;
cnt++;
if(now->data==data){
ret.push_back(cnt);
}
}
return ret;
}
void pop(int pos){
if(pos<1||pos>size()){
printf("error\n");
return ;
}
Node* now= head;
pos--;
while(pos--){
now= now->next;
}
Node* temp= now->next;
now->next= now->next->next;
delete(temp);
SIZE--;
}
void pop_front(){
pop(1);
}
void pop_back(){
pop(size());
}
void print(Node* x){
while(x!=NULL){
printf("-->");
x->data.out();
x= x->next;
}
}
void print(){
Node* now= head;
while(now->next!=NULL){
now= now->next;
printf("--> ");
now->data.out();
}
printf("\n");
}
void clear(Node* x){
if(x==NULL) return;
clear(x->next);
delete(x);
SIZE--;
}
void clear(){
clear(head);
}
Node* find_inv(int pos){
if(pos>size()) return NULL;
Node* ret= head, *now= head;
while(pos--){
now= now->next;
}
while(now!=NULL){
now= now->next;
ret= ret->next;
}
return ret;
}
void reverse(){
if(size()<=0) return ;
Node* now= head->next;
while(now->next!= NULL){
Node* temp= now->next;
now->next= now->next->next;
temp->next= head->next;
head->next= temp;
}
}
Node* Get_mid(Node* x){
Node* mid=x, *p=x;
while(p->next!=NULL&&p->next->next!=NULL){
p= p->next->next;
mid= mid->next;
}
Node* ret= mid->next;
mid->next= NULL;
return ret;
}
Node* merge(Node* x, Node* y){
Node* ret=NULL, *tail=NULL;
while(x!=NULL&&y!=NULL){
if(x->data < y->data){
Node* temp= x->next;
if(ret==NULL){
ret= tail= x;
ret->next= NULL;
}
else {
tail->next= x;
tail= tail->next;
tail->next= NULL;
}
x=temp;
}
else{
Node* temp= y->next;
if(ret==NULL){
ret= tail= y;
ret->next= NULL;
}
else {
tail->next= y;
tail= tail->next;
tail->next= NULL;
}
y=temp;
}
}
if(x!=NULL){
tail->next= x;
}
else{
tail->next= y;
}
return ret;
}
Node* sort(Node* x){
if(x->next==NULL) return x;
Node* mid= Get_mid(x);
Node* p1= sort(x);
Node* p2= sort(mid);
return merge(p1, p2);
}
void sort(){
head->next= sort(head->next);
}
};
int main(){
link_list ans;
ans.bulid();
Data_type temp;
temp.name[0]='s';
temp.name[1]='a';
temp.name[2]='m';
temp.name[3]='y';
temp.name[4]='1';
temp.name[5]='\0';
temp.score=5;
ans.push_front(temp);
temp.name[4]='2';
temp.score=15;
ans.push_front(temp);
temp.name[4]='3';
temp.score=150;
ans.push_back(temp);
ans.print();
ans.reverse();
ans.sort();
ans.print();
return 0;
}
数组归并排序:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10002;
int a[maxn],n,temp[maxn];
void sort(int l, int r){
if(r-l==1){
return ;
}
int mid=(l+r)/2;
sort(l,mid);
sort(mid,r);
int pl=l, pr=mid, pos=l;
while(pl<mid&&pr<r){
if(a[pl]<a[pr]){
temp[pos]= a[pl];
pos++, pl++;
}
else{
temp[pos]= a[pr];
pos++, pr++;
}
}
while(pl<mid){
temp[pos]= a[pl];
pos++, pl++;
}
while(pr<r){
temp[pos]= a[pr];
pos++, pr++;
}
for(int i=l;i<r;i++){
a[i]=temp[i];
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(0,n);
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
}
单链表附加版本:
#include<bits/stdc++.h>
using namespace std;
struct Data_type{
int score;
char s[1];
void print(){
printf("( %s: %d )",s,score);
}
};
bool operator ==(Data_type l, Data_type r){
if(l.score!=r.score|| strlen(l.s)!=strlen(r.s))return 0;
int n=strlen(l.s);
for(int i=0;i<n;i++){
if(l.s[i]!=r.s[i])return 0;
}
return 1;
}
bool operator <(Data_type l, Data_type r){
if(l.score!= r.score)return l.score>r.score;
int n= max( strlen(l.s), strlen(r.s) );
for(int i=0;i<n;i++){
if(l.s[i]!=r.s[i])return l.s[i]<r.s[i];
}
return 0;
}
struct Node{
Data_type data;
Node* next;
};
struct link_list{
Node* head;
int SIZE;
void build(){
head=new Node;
head->next= NULL;
SIZE=0;
}
int size(){
return SIZE;
}
void push_front(Data_type data){
Node* now=new Node;
now->data= data;
now->next= head->next;
head->next= now;
SIZE++;
}
void push_back(Data_type data){
Node* now=head;
while(now->next!=NULL){
now=now->next;
}
now->next= new Node;
now= now->next;
now->next= NULL;
now->data= data;
SIZE++;
}
bool push(Data_type data, int pos){
if(SIZE<pos)return 0;
Node* now= head;
while(pos--){
now=now->next;
}
Node* temp=now->next;
now->next= new Node;
now= now->next;
now->next= temp;
now->data= data;
SIZE++;
return 1;
}
Node* find(int pos){
if(pos>size()) return NULL;
Node* ret=head;
while(pos--){
ret= ret->next;
}
return ret;
}
Node* find_inv(int pos){
if(pos>size()) return NULL;
Node* ret=head;
Node* now=head;
while(pos--){
now= now->next;
}
while(now!=NULL){
now= now->next;
ret= ret->next;
}
return ret;
}
void print(){
Node* now= head;
while(now->next!= NULL){
now=now->next;
printf("-->");
now->data.print();
}
printf("\n");
}
void print(int pos){
Node* now= find(pos);
if(now==NULL){
printf("error\n");
return;
}
printf("%d:",pos);
now->data.print();
printf("\n");
}
void print(Node* x){
while(x!=NULL){
printf("%d ",x->data.score);
x=x->next;
}
printf("\n");
}
void pop(int pos){
if(pos<1||pos>size()){
printf("error\n");
return;
}
Node* now= head;
pos--;
while(pos--){
now= now->next;
}
Node* temp= now->next;
now->next= now->next->next;
delete(temp);
SIZE--;
}
void pop_front(){
pop(1);
}
void pop_back(){
pop(size());
}
void clear(Node* x){
if(x==NULL) return;
clear(x->next);
delete(x);
SIZE--;
}
void clear(){
clear(head);
}
vector<int> query(Data_type data){
vector<int>ret;
Node* now= head;
int cnt=0;
while(now->next!= NULL){
now= now->next;
cnt++;
if(now->data== data){
ret.push_back(cnt);
}
}
return ret;
}
void link(link_list &x){
Node* now= head;
while(now->next!= NULL){
now= now->next;
}
Node* temp= x.head;
now->next= temp->next;
delete(temp);
x.head= NULL;
}
void reverse(){
if(size()==0)return ;
Node* now= head->next;
while(now->next!=NULL){
Node* temp= now->next;
now->next= now->next->next;
temp->next= head->next;
head->next= temp;
}
}
Node* Get_mid(Node* x){
Node* mid=x, *p=x;
while(p->next!=NULL&& p->next->next!= NULL){
p= p->next->next;
mid= mid->next;
}
Node* ret= mid->next;
mid->next= NULL;
Node* test=x;
return ret;
}
Node* merge(Node* x, Node* y){
Node* ret=NULL, *tail=NULL;
while(x!=NULL&& y!=NULL){
if(x->data < y->data ){
Node* temp= x->next;
if(ret==NULL){
ret= tail= x;
ret->next= NULL;
}
else{
tail->next= x;
tail= tail->next;
tail->next= NULL;
}
x=temp;
}
else{
Node* temp= y->next;
if(ret==NULL){
ret= tail= y;
ret->next= NULL;
}
else{
tail->next= y;
tail= tail->next;
tail->next= NULL;
}
y=temp;
}
}
if(x!=NULL){
tail->next= x;
}
if(y!=NULL){
tail->next= y;
}
return ret;
}
Node* sort(Node* x){
if(x->next== NULL) return x;
Node* mid= Get_mid(x);
Node* p1=sort(x);
Node* p2=sort(mid);
return merge(p1, p2);
}
void sort(){
head->next= sort(head->next);
}
};
void test(){
while(getchar()){
int T=rand();
int m=T;
link_list ans;
ans.build();
Data_type add;
while(T--){
add.score=rand();
ans.push_front(add);
}
ans.sort();
int pre;
Node* now=ans.head->next;
for(int i=0;i<m;i++){
if(i!=0){
if(pre< now->data.score){
while(1)printf("error");
}
}
pre=now->data.score;
now=now->next;
}
printf("%d ok\n",m);
}
}
int main(){
test();
return 0;
}