C++

链表

创建

1
2
3
4
5
6
7
8
9
struct Node{
int data;
Node *next;
};
Node *head,*tail;
head=new Node;
head->data=0;
head->next=nullptr;
tail=head;

输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int num;
cin>>num;
Node *head,*pnew,*tail;
head=new Node;
head->data=num;
head->next=nullptr;
tail=head;
while(num--){
int data;
cin>>data;
pnew=new Node;
pnew->data=data;
pnew->next=nullptr;
tail->next=pnew;
tail=pnew;
}

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void show(Node *head);

show(head);

void show(Node *head){
//cout<<head->data<<' ';
while(head->next){
head=head->next;
cout<<head->data;
if(head->next)
cout<<' ';
}
cout<<endl;
}

销毁

1
2
3
4
5
6
7
8
9
10
11
12
13
void destroy(Node *head);

destroy(head);

void destroy(Node *head){
Node *p;
while(head->next){
p=head->next;
head->next=p->next;
delete p;
}
delete head;
}

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int deletenode(Node *H,int i);

deletenode(head,i);

int deletenode(Node *H,int i){
if(i>=1 && i<=H->data){
Node *q=H;
for(int j=1;j<i;j++){
q=q->next;
}
Node *p=q->next;
q->next=p->next;
H->data--;
return 1;
}
else
return 0;
}

插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int insert(Node *H,int i,int data);

insert(head,index,data);

int insert(Node *H,int i,int data){
if(i>=1 && i<=H->data+1){
Node *p=new Node;
Node *q=H;
for(int j=1;j<i;j++){
q=q->next;
}
p->data=data;
p->next=q->next;
q->next=p;
H->data++;
return 1;
} else
return 0;
}

指针

堆内存分配

一维

1
2
int *p=new int[10];
delete []p;

二维

1
2
3
4
5
6
7
8
int **p=new int*[m];
for(int i=0;i<m;i++){
p[i]=new int[n];
}
for(i=0;i<m;i++){
delete []p[i];
}
delete []a;

指针作函数参数

1
2
3
4
5
6
void swap2( int *px, int *py ){  
int t;
t = *px;
*px = *py;
*py = t;
}

字符串赋值

1
2
3
4
5
6
7
8
9
10
11
12
char buffer[10]=“hello”;//初始化,ok

char buffer[10];
buffer=“hello”; //数组名赋值,error

const char* pc;
pc="hello"; //字符指针赋值,ok
const char *cities[5]={"Beijing","Shenzhen","Hangzhou"};

char* pc;
pc="hello"; //error
char *cities[5]={"Beijing","Shenzhen","Hangzhou"};

指针函数

1
2
3
4
5
int  *add(int *x)
{
*x=*x+5;
return x;
}

const 指针

常量指针(指向常量的指针)

1
2
3
4
const int a=89,b=60;
const int *p=&a;
p=&b; //ok
*p=100; //error

指针常量

1
2
3
4
int a =89,b=60;    
int *const p = &a;
p=&b; //error
*p=100; //ok

常量指针常量 (指向常量的指针常量)

1
2
3
4
const int a =89,b;    
const int *const p = &a;
p=&b; //error
*p=100; //error

函数指针

1
2
3
4
5
6
7
8
9
10
void sort(int* a,int n);
int main(){
int a[10]={9,34,2,1,-5,4,23,67,-123,0};
void (*pfun)(int *a,int n); //声明函数指针
pfun=sort; //给函数指针赋值
pfun(a,10); //使用函数指针调用函数
}
void sort(int* a,int n){
...
}

常用库函数

最大最小值

1
2
3
#include<climits>
int min=INT_MAx;
int max=INT_MIN;

排序

1
2
3
4
#include <algorithm>
int a[10]={45,12,34,77,90,11,2,4,5,55};
sort(a,a+10);
sort(a,a+10,greater<>());

排序算法

选择排序

选择排序

1
2
3
4
5
6
7
8
9
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(p[j]>p[i]){
int t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}

冒泡排序

冒泡排序

1
2
3
4
5
6
7
8
9
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(p[j+1]>p[j]){
int t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}

更多排序

点击查看

Reward
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • © 2019-2020 Bingoyyj
  • Powered by Hexo Theme Ayer
  • PV: UV:

什么?这次一定?

支付宝
微信