线性表——链表
#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);}elsedeletex(L->next,x);}int main() {Lnode list;Lnode *L=&list;buildlist(L);disp(L);return 0;
}
2010年真題
思想:先整體逆置,在以循環右移位數在數組中的序列為界限兩邊逆置
代碼思想:給兩個數組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和continuevoid 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; }總結
- 上一篇: 开通博客园
- 下一篇: MFC基于对话框的商场交易软件实现