日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线性表——链表

發布時間:2024/7/19 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线性表——链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#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年真題
思想:先整體逆置,在以循環右移位數在數組中的序列為界限兩邊逆置

/******************************************************************************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和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; }

總結

以上是生活随笔為你收集整理的线性表——链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。