线性表
单链表
**单链表操作**
#include<iostream>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void InitList(LinkList &L){
L=(LNode *)malloc(sizeof(LinkList));
L->next=NULL;
}
LinkList HeadInsert(LinkList &L){
InitList(L);
int x;
while(cin>>x){
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
}
return L;
}
LinkList TailInsert(LinkList &L){
InitList(L);
LNode *s,*r=L;
int x;
while(cin>>x){
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
r->next=s;
r=s;
}
r->next=NULL;
return L;
}
void PrintList(LinkList L){
LNode *p=L->next;
while(p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
}
int Length(LinkList L){
LNode *p = L->next;
int len = 0;
while(p!=NULL){
len++;
p = p->next;
}
return len;
}
LNode *LocateElem(LinkList L, int x){
LNode *p = L->next;
while(p!=NULL&&p->data!=x){
p = p->next;
}
return p;
}
LNode *GetElem(LinkList L, int i){
int j=1;
LNode *p = L->next;
if(i==0){
return L;
}
if(i<1){
return NULL;
}
while(p!=NULL&&j<i){
p = p->next;
j++;
}
return p;
}
void Empty(LinkList L){
if(L->next == NULL){
cout<<"L is null"<<endl;
}else{
cout<<"L is not null"<<endl;
}
}
void Insert(LinkList &L, int i, int x){
LNode *p = GetElem(L,i-1);
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = p->next;
p->next = s;
}
void Delete(LinkList &L, int i){
if(i<0||i>Length(L)){
cout<<"数据有误";
return ;
}
LNode *p = GetElem(L,i-1);
LNode *q = p->next;
p->next = q->next;
free(q);
}
int main(){
**自定义补充**
return 0;
}
双链表
循环链表
栈、队列、数组
树
图
查找
排序
插入排序
直接插入排序
#include<iostream>
using namespace std;
void InsertSort(int a[],int len){
int temp,i,j;
for(i=1;i<len;i++){
if(a[i]<a[i-1]){
temp=a[i];
for(j=i-1;temp<a[j];j--){
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
}
int main(){
int a[]={1,3,4,2,6,5,9,8,7};
int l=sizeof(a)/sizeof(int);
InsertSort(a,l);
for(int i=0;i<l;i++){
cout<<a[i]<<" ";
}
return 0;
}
折半插入排序
#include<iostream>
using namespace std;
void InsertSort(int a[],int len){
int temp,low,high,mid;
for(int i=1;i<len;i++){
temp=a[i];
low=0,high=i-1;
while(low<=high){
mid=(low+high)/2;
if(a[mid]>temp){
high=mid-1;
}else{
low=mid+1;
}
}
for(int j=i-1;j>=high+1;j--){
a[j+1]=a[j];
}
a[high+1]=temp;
}
}
int main(){
int a[]={1,3,4,2,6,5,9,8,7};
int l=sizeof(a)/sizeof(int);
InsertSort(a,l);
for(int i=0;i<l;i++){
cout<<a[i]<<" ";
}
return 0;
}
希尔排序
#include<iostream>
using namespace std;
void InsertSort(int a[],int len){
for(int d=len/2;d>=1;d=d/2){
int temp,i,j;
for(i=d+1;i<len;i++){
if(a[i]<a[i-d]){
temp=a[i];
for(j=i-d;j>0&&temp<a[j];j-=d){
a[j+d]=a[j];
}
a[j+d]=temp;
}
}
}
}
int main(){
int a[]={1,3,4,2,6,5,9,8,7};
int l=sizeof(a)/sizeof(int);
InsertSort(a,l);
for(int i=0;i<l;i++){
cout<<a[i]<<" ";
}
return 0;
}
交换排序
冒泡排序
#include<iostream>
using namespace std;
void BubbleSort(int a[],int len){
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i;j++){
if(a[j+1]<a[j]){
swap(a[j+1],a[j]);
}
}
}
}
int main(){
int a[]={1,3,4,2,6,5,9,8,7};
int l=sizeof(a)/sizeof(int);
BubbleSort(a,l);
for(int i=0;i<l;i++){
cout<<a[i]<<" ";
}
return 0;
}
void BubbleSort(int a[],int len){
for(int i=0;i<len-1;i++){
bool flag=false;
for(int j=0;j<len-i;j++){
if(a[j+1]<a[j]){
swap(a[j+1],a[j]);
flag=true;
}
}
if(flag==false){
return ;
}
}
}
快速排序
#include<iostream>
using namespace std;
void QuickSort(int a[],int low,int high){
int i=low,j=high,pivot=a[low];
if(low>=high){
return ;
}
while(i<j){
while(a[j]>=pivot&&i<j){
j--;
}
a[i]=a[j];
while(a[i]<=pivot&&i<j){
i++;
}
a[j]=a[i];
}
a[i]=pivot;
QuickSort(a,low,i-1);
QuickSort(a,i+1,high);
}
int main(){
int a[]={1,3,4,2,6,5,9,8,7};
int l=sizeof(a)/sizeof(int);
QuickSort(a,0,l-1);
for(int i=0;i<l;i++){
cout<<a[i]<<" ";
}
return 0;
}
**快速排序另一种写法:**
int Partition(int a[],int low,int high){
int pivot=a[low];
while(low<high){
while(a[high]>pivot&&low<high){
high--;
}
a[low]=a[high];
while(a[low]<pivot&&low<high){
low++;
}
a[high]=a[low];
}
a[low]=pivot;
return low;
}
void QuickSort(int a[],int low,int high){
if(low<high){
int pivotpos = Partition(a,low,high);
QuickSort(a,low,pivotpos-1);
QuickSort(a,pivotpos+1,high);
}
}
选择排序
简单选择排序
#include<iostream>
using namespace std;
void SelectSort(int a[],int len){
for(int i=0;i<len-1;i++){
int min=i;
for(int j=i+1;j<len;j++){
if(a[j]<a[min]){
min=j;
}
}
if(min!=i){
swap(a[i],a[min]);
}
}
}
int main(){
int a[]={1,3,4,2,6,5,9,8,7};
int l=sizeof(a)/sizeof(int);
SelectSort(a,l);
for(int i=0;i<l;i++){
cout<<a[i]<<" ";
}
return 0;
}
堆排序
#include<iostream>
using namespace std;
void HeapAdjust(int a[],int k,int len){
a[0]=a[k];
for(int i=2*k;i<=len;i*=2){
if(i<len&&a[i]<a[i+1]){
i++;
}
if(a[0]>=a[i]){
break;
}else{
a[k]=a[i];
k=i;
}
}
a[k]=a[0];
}
void BuildMaxHeap(int a[],int len){
for(int i=len/2;i>0;i--){
HeapAdjust(a,i,len);
}
}
void HeapSort(int a[],int len){
BuildMaxHeap(a,len);
for(int i=len;i>1;i--){
swap(a[i],a[1]);
HeapAdjust(a,1,i-1);
}
}
int main(){
int b[]={1,3,4,2,6,5,9,8,7};
int a[100],l=sizeof(b)/sizeof(int);
for(int i=0;i<l;i++){
a[i+1]=b[i];
}
HeapSort(a,l);
for(int i=1;i<=l;i++){
cout<<a[i]<<" ";
}
return 0;
}
归并排序

int b[high-low+1];
int i = low, j = mid + 1, k = 0;
while (i <= mid && j <= high) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
} else {
b[k++] = a[j++];
}
}








while (i <= mid){
b[k++] = a[i++];
}
while (j <= high){
b[k++] = a[j++];
}
while (i <= mid && j <= high) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
} else {
b[k++] = a[j++];
}
}
for (int i = low; i <= hight; i++) {
a[i] = b[k++];
}
delete[]b;
void MergeSort(int a[ ],int low,int high){
if(low<high){
int mid=(low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
Merge(a,low,mid,high);
}
}
**完整代码**
#include<iostream>
using namespace std;
void Merge(int a[],int low,int mid,int high){
int *b=new int[high-low+1];
int i = low, j = mid + 1, k = 0;
while (i <= mid && j <= high){
if (a[i] <= a[j]){
b[k++] = a[i++];
}else{
b[k++] = a[j++];
}
}
while (i <= mid){
b[k++] = a[i++];
}
while (j <= high){
b[k++] = a[j++];
}
k = 0;
for (int i = low; i <= high; i++){
a[i] = b[k++];
}
delete []b;
}
void MergeSort(int a[],int low,int high){
if(low<high){
int mid=(low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
Merge(a,low,mid,high);
}
}
int main(){
int a[]={1,3,4,2,6,5,9,8,7};
int l=sizeof(a)/sizeof(int);
MergeSort(a,0,l-1);
for(int i=0;i<l;i++){
cout<<a[i]<<" ";
}
return 0;
}
基数排序