#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int Elemtype;
//定义单链表
typedef struct Lnode{
Elemtype data;
struct Lnode *next;
}Lnode,*Linklist;
int a[4]={1,2,3,4};
int n=4;
//建立不带头节点的链表
void buildlist(Lnode *L){
Lnode *s,*r=L;
r->data=a[0];
if(n==1)r->next=NULL;
else for(int i=1;i<n;i++){
s=(Lnode*)malloc(sizeof(Lnode));
s->data=a[i];
r->next=s;
r=r->next;
}
L=(Linklist)malloc(sizeof(Lnode));
L->next=NULL;
}
//头插法建立单链表
Linklist list_headinsert(Linklist &L){
Lnode *s;
int x;
L=(Linklist)malloc(sizeof(Lnode));
L->next=NULL;
scanf("%d",&x);
while(x!=9999){
s=(Lnode*)malloc(sizeof(Lnode));
s->data=x;
s->next=L->next;
L->next=s;
scanf("%d",&x);
}
return L;
}
/**
*
*
*
**/
//尾插法建立单链表
Linklist List_TailInsert(Linklist &L){
int x;
L=(Linklist)malloc(sizeof(Linklist));
Lnode *s,*r=L;
scanf("%d",&x);
while(x!=9999){
s=(Lnode*)malloc(sizeof(Lnode));
s->data=x;
//和头插法不同
r->next=s;
r=s;
//
scanf("%d",&x);
}
r->next=NULL;
return L;
}
void disp(Lnode *L){
Lnode *s=L;
while(s){
cout<<(s->data)<<" ";
s=s->next;
}
cout<<endl;
}
void deletex(Lnode *&L,int x){
if(L==NULL)return;
Lnode *p;
if(L->data=x){
p=L;
L=L->next;
free(p);
deletex(L,x);
}
else
deletex(L->next,x);
}
int main() {
Lnode list;
Lnode *L=&list;
buildlist(L);
disp(L);
return 0;
}
2010年真题
思想:先整体逆置,在以循环右移位数在数组中的序列为界限两边逆置
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
int a[]={2,4,5,6,1,9,6};
int n=7;
void reverse(int a[],int l,int r){
for (int i=l;i<=(l+r)/2;i++) {
int flag=a[i];
a[i]=a[(l+r)-i];
a[(l+r)-i]=flag;
/* code */
}
}
void disp(int a[]){
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
}
void change(int a[],int l){
reverse(a,0,n-1);
reverse(a,0,l);
reverse(a,l+1,n-1);
disp(a);
}
int main()
{
change(a,3);
return 0;
}
代码思想:给两个数组s1,s2分别设置指针,记录当前未排入s3的值,每次进行比较,将较小的值排入s3
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
void disp(int a[],int length);
int a[]={2,4,5,6,1,9,6};
int s1[]={1,2,3,7};
int s2[]={4,5,8,9};
int length=4;
int n=7;
void reverse(int a[],int l,int r){
for (int i=l;i<=(l+r)/2;i++) {
int flag=a[i];
a[i]=a[(l+r)-i];
a[(l+r)-i]=flag;
/* code */
}
}
void merge(int s1[],int s2[]){
int s3[length*2];
int p1=0,p2=0;
for(int i=0;i<length*2;i++){
if(p1==4&&p2!=4){
s3[i]=s2[p2++];
continue;
}
if(p2==4&&p1!=4){
s3[i]=s1[p1++];
continue;
}
if(s1[p1]<s2[p2]){
s3[i]=s1[p1++];
}else{
s3[i]=s2[p2++];
}
}
disp(s3,2*length);
}
//循环的break和continue
void disp(int a[],int length){
for(int i=0;i<length;i++){
printf("%d ",a[i]);
}
}
void change(int a[],int l,int length){
reverse (a,0,length);
reverse(a,0,l);
reverse(a,l+1,length);
disp(a,length);
}
int main()
{
//change(a,3);
merge(s1,s2);
return 0;
}