顺序表查删插以及合并
1 #pragma GCC optimize(2)//O2 2 #include<bits/stdc++.h> 3 using namespace std; 4 const int M=10000;//最大多少个数组 5 typedef struct node 6 { 7 int a[M];//数据域 8 int Last;//末下标 9 }*List; 10 int n; 11 List MakeEmpty(List L)//建立空表 12 { 13 L=(List)malloc(sizeof(struct node)); 14 L->Last=-1; 15 return L; 16 } 17 int Find(List L,int x)//查找函数 18 { 19 int i=0; 20 while(i<=L->Last&&L->a[i]!=x) 21 i++; 22 if(i>L->Last) 23 return -1; 24 else 25 return i; 26 } 27 bool Insert(List L,int p,int i)//插入函数 28 { 29 int j; 30 if(L->Last==M-1) 31 { 32 cout<<"表已经满了"<<endl; 33 return false; 34 } 35 if(i<1||i>L->Last+2) 36 { 37 cout<<"位置不合法"<<endl; 38 return false; 39 } 40 for(j=L->Last;j>=i-1;j--) 41 { 42 L->a[j+1]=L->a[j]; 43 } 44 L->a[i-1]=p; 45 L->Last++; 46 return true; 47 48 } 49 bool Delete(List L,int r)//删除函数 50 { 51 int j; 52 if(r<1||r>L->Last+1) 53 { 54 cout<<"要删除的元素不存在"<<endl; 55 return false; 56 } 57 for(j=r;j<=L->Last;j++) 58 { 59 L->a[j-1]=L->a[j]; 60 } 61 L->Last--; 62 return true; 63 } 64 void merge(List A, List B, List& C)//合并算法 65 { 66 int i=0, j=0, k=0; 67 while (i < A->Last && j < B->Last) 68 { 69 if(A->a[i]<B->a[j]) 70 { 71 C->a[k] = A->a[i]; 72 i++; k++; 73 } 74 else if (A->a[i] >B->a[j]) 75 { 76 C->a[k] = B->a[j]; 77 j++; k++; 78 } 79 else 80 { 81 C->a[k] = B->a[j]; 82 j++; k++; 83 C->a[k] = A->a[i]; 84 i++; k++; 85 } 86 } 87 while(i<A->Last) 88 { 89 C->a[k] = A->a[i]; 90 i++; k++; 91 } 92 while (j < B->Last) 93 { 94 C->a[k] = B->a[j]; 95 j++; k++; 96 } 97 C->Last = k; 98 } 99 bool cmp(node v,node t) 100 { 101 return (v.a<t.a); 102 } 103 int main() 104 { 105 ios::sync_with_stdio(false);//快输 106 List L=MakeEmpty(L);//建表 107 cin>>n; 108 for(register int i=0;i<n;i++) 109 { 110 cin>>L->a[i]; 111 } 112 L->Last=n-1;//把末下标确定下来 113 int x; 114 cin>>x; 115 if(Find(L,x)==-1)//判断条件,分找到和没找到 116 cout<<"没有找到您所要找的元素"<<endl; 117 else 118 { 119 cout<<Find(L,x)<<endl;//如果查找到了就输出那个下表就可以了 120 } 121 int i; 122 int p; 123 cin>>p>>i; 124 Insert(L,p,i);//开始插入 125 cout<<"插入之后的元素是:"<<' '; 126 for(register int j=0;j<=L->Last;j++)//输出插入之后的元素都有那些 127 cout<<L->a[j]<<' '; 128 cout<<endl; 129 int r; 130 cin>>r; 131 Delete(L,r);//开始删除 132 cout<<"删除之后的元素是:"<<' '; 133 for(register int j=0;j<=L->Last;j++)//输出删除之后的元素都有那些 134 { 135 cout<<L->a[j]<<' '; 136 } 137 cout<<endl;//换行好看显美观 138 List sqA, sqB,sqC; 139 int Adata,Bdata; 140 cout << "请输入顺序表A中的元素个数(不超过100个)"; 141 cin>>Adata; 142 sqA=MakeEmpty(sqA); 143 cout << "下面请按元素值递增顺序输入元素" << endl; 144 for (int i = 0; i < Adata; i++) 145 { 146 cout << "顺序表中A第" << i << "个元素是:"; 147 cin >> sqA->a[i]; 148 } 149 sqA->Last = Adata; 150 cout << "顺序表A的长度为:" << sqA->Last << endl; 151 cout << "请输入顺序表B中的元素个数(不超过100个)"; 152 cin >> Bdata; 153 sqB=MakeEmpty(sqB); 154 for (int i = 0; i < Bdata; i++) 155 { 156 cout << "顺序表B中第" << i << "个元素是:"; 157 cin >> sqB->a[i]; 158 } 159 sqB->Last = Bdata; 160 cout << "顺序表B的长度为:" << sqB->Last << endl; 161 sqC=MakeEmpty(sqC); 162 merge(sqA, sqB, sqC); 163 int o=sqC->Last; 164 cout << "顺序表C的长度为:" << sqC->Last << endl; 165 sort(sqC->a,sqC->a+o); 166 cout << "顺序表C中的元素依次为:"; 167 for (int i = 0; i < sqC->Last; i++) 168 { 169 cout <<sqC->a[i]<<setw(6); 170 } 171 172 return 0; 173 }
还有一种思路,我虽然不承认但是不得不承认(被迫承认,狗头保命sly不要揍我)
:
1 //顺序表 2 #pragma GCC optimize(3) 3 #include<bits/stdc++.h> 4 using namespace std; 5 const int M=10000; 6 int n; 7 typedef struct node{ 8 int a[M]; 9 int Last; 10 }*List; 11 List InitList(List L) 12 { 13 L=(List)malloc(sizeof(struct node)); 14 L->Last=-1; 15 return L; 16 } 17 //查找 18 int Find(List L,int x) 19 { 20 int i=0; 21 while(i<=L->Last&&L->a[i]!=x) 22 i++; 23 if(i>L->Last) return -1; 24 else return i; 25 } 26 //插入 27 bool Insert(List L,int i,int e) 28 { 29 if(L->Last==M-1) return false; 30 if(i<1||i>L->Last+2) return false; 31 for(int j=L->Last;j>=i-1;j--) 32 L->a[j+1]=L->a[j]; 33 L->a[i-1]=e; 34 L->Last++; 35 return true; 36 } 37 //删除 38 bool Delect(List L,int r) 39 { 40 if(r<1||r>L->Last+1) 41 { 42 cout<<"该数不存在"; 43 return false; 44 } 45 for(int j=r;j<=L->Last;j++) 46 { 47 L->a[j-1]=L->a[j]; 48 } 49 L->Last--; 50 return true; 51 } 52 //合并 53 void merge(List A,List B,List &C) 54 { 55 int i=0,j=0,k=0; 56 int s=A->Last+B->Last+1; 57 for(i=0,k=0;i<A->Last;) 58 { 59 C->a[k]=A->a[i]; 60 i++;k++;j++; 61 } 62 for(j=0;j<B->Last;) 63 { 64 C->a[k]=B->a[j]; 65 k++;j++; 66 } 67 C->Last=k; 68 sort(C->a,C->a+k); 69 } 70 int main() 71 { 72 List L=InitList(L); 73 cout<<"请输入表的元素个数:"; 74 cin>>n; 75 cout<<"请输入表中元素:"; 76 for(register int i=0;i<n;i++) 77 cin>>L->a[i]; 78 L->Last=n-1;//确定末下标 79 //查找 80 int x; 81 cout<<"要查找的元素是:"; 82 cin>>x; 83 if(Find(L,x)==-1) 84 cout<<"没有要查找的元素"<<endl; 85 else 86 cout<<"要查找的元素在表中的位置为:"<<Find(L,x)<<endl; 87 //插入 88 int i,e; 89 cout<<"要插入的位置和元素分别是:"; 90 cin>>i>>e; 91 Insert(L,i,e); 92 cout<<"插入之后的顺序表为:"<<' '; 93 for(int j=0;j<=L->Last;j++) 94 cout<<L->a[j]<<' '; 95 cout<<endl; 96 //删除 97 int r; 98 cout<<"要删除的元素的位置是:"; 99 cin>>r; 100 Delect(L,r); 101 cout<<"删除之后的顺序表为"; 102 for(register int j=0;j<=L->Last;j++) 103 cout<<L->a[j]<<" "; 104 cout<<endl; 105 //合并 106 List sqA,sqB,sqC; 107 sqA=InitList(sqA); 108 sqB=InitList(sqB); 109 sqC=InitList(sqC); 110 int Adata,Bdata; 111 cout<<"请输入A表的元素个数并输入A表:"; 112 cin>>Adata; 113 for(int i=0;i<Adata;i++) 114 cin>>sqA->a[i]; 115 sqA->Last = Adata; 116 cout<<"请输入B表的元素个数并输入B表:"; 117 cin>>Bdata; 118 for(int j=0;j<Bdata;j++) 119 cin>>sqB->a[j]; 120 sqB->Last = Bdata; 121 merge(sqA,sqB,sqC); 122 cout<<"A表B表合并后为:"; 123 for(int k=0;k<sqC->Last;k++) 124 cout<<sqC->a[k]; 125 return 0; 126 }
本文来自博客园,作者:江上舟摇,转载请注明原文链接:https://www.cnblogs.com/LQS-blog/p/16014133.html