顺序表的基本操作
来源:互联网 发布:js仿ios选择器 编辑:程序博客网 时间:2024/06/02 02:31
1.基本定义
- #include<stdio.h>
-
-
-
- #define LIST_INIT_SIZE 100
- typedef int ElemType;
-
- typedef struct SqList{
- ElemType data[LIST_INIT_SIZE];
- int len;
- }SqList;
2.创建线性表和打印输出线性表
-
- void createSqList(SqList &L, int n){
- printf("请输入%d个数:\n", n);
- for(int i = 0; i < n; i++){
- scanf("%d",&L.data[i]);
- }
- L.len = n;
-
- }
-
- void printSqList(SqList L){
- printf("打印线性表:");
- for(int i = 0; i < L.len; i++){
- printf("%d ",L.data[i]);
- }
- printf("\n");
- }
- void main(){
-
-
- SqList L;
- L.len=0;
-
- createSqList(L, 5);
- printSqList(L);
- }
3.插入操作
-
-
-
-
- void insertSqList(SqList &L,int i, ElemType e){
- if(i>L.len+1||i<1){
- printf("插入位置参数不合法");
- }else if(L.len >= LIST_INIT_SIZE){
- printf("表已满,无法差插入");
- }else{
- int j = L.len-1;
- for(j;j>=i-1;j--){
- L.data[j+1] = L.data[j];
- }
- L.data[i-1] = e;
- L.len++;
- }
- }
- void main(){
-
-
- SqList L;
- L.len=0;
-
- createSqList(L, 5);
- printSqList(L);
-
-
- printf("您要插入的数据和位置(1~%d)(如:在第2个位置插入10则输入:10,2):",L.len);
- int elem, pos;
- scanf("%d,%d",&elem,&pos);
- insertSqList(L,pos,elem);
- printSqList(L);
- }
[cpp] view plain copy
-
-
- void createUseInsertSqList(SqList &L, int n){
- printf("请输入%d个数:\n", n);
- int x;
- for(int i = 0; i < n; i++){
- scanf("%d", &x);
- insertSqList(L,i+1,x);
- }
- L.len = n;
- }
- void main(){
-
-
- SqList L;
- L.len=0;
-
-
-
-
-
- createUseInsertSqList(L, 5);
- printSqList(L);
- }
4.删除操作
-
-
- void delSqList(SqList &L, int i, ElemType &e){
- if(i>L.len||i<1){
- printf("删除位置参数不合法");
- }
- int j = i-1;
- e = L.data[j];
- printf("删除的第%d位置的数据是%d\n",i,e);
- for(j;j<L.len-1;j++){
- L.data[j] = L.data[j+1];
- }
- L.len--;
- void main(){
-
-
- SqList L;
- L.len=0;
-
- createSqList(L, 5);
- printSqList(L);
-
-
- printf("您要删除的数据的位置(如:要删除第2个位置的数据则输入(1~%d):2):",L.len);
- int pos;
- ElemType elem;
- scanf("%d",&pos);
- delSqList(L,pos,elem);
- printSqList(L);
- }
5.两个线性表的merge操作
-
-
-
-
- void mergeSqList(SqList La, SqList Lb, SqList &Lc){
- int i=0, j=0;
- Lc.len=La.len+Lb.len;
- int k =0;
- while(i<La.len&&j<Lb.len){
- if(La.data[i]<=Lb.data[j]){
- Lc.data[k]=La.data[i];
- i++;
- k++;
- }else{
- Lc.data[k]=Lb.data[j];
- j++;
- k++;
- }
- }
- while(i<La.len){
- Lc.data[k]=La.data[i];
- i++;
- k++;
- }
- while(j<Lb.len){
- Lc.data[k]=Lb.data[j];
- j++;
- k++;
- }
- }
- void main(){
-
- SqList La, Lb, Lc;
-
- La.len = Lb.len =Lc.len = 0;
- printf("创建线性表La:\n");
- createSqList(La, 4);
- printSqList(La);
- printf("创建线性表Lb:\n");
- createSqList(Lb, 7);
- printSqList(Lb);
- mergeSqList(La, Lb, Lc);
- printf("merge以后得到的Lc:");
- printSqList(Lc);
- }
6.两个线性表的union操作
-
-
- int locateElem(SqList L, ElemType e){
-
- int i = 1;
- while(i<=L.len&&L.data[i-1]!=e){
- i++;
- }
- if(i<=L.len)
- return i;
- else
- return 0;
- }
- void unionSqList(SqList &La, SqList Lb){
- for(int i = 0; i < Lb.len; i++){
- ElemType e = Lb.data[i];
- if(!locateElem(La, e)){
- insertSqList(La,La.len+1, e);
- }
- }
- }
- void main(){
-
- SqList La, Lb;
-
- La.len = Lb.len = 0;
- printf("创建线性表La:\n");
- createSqList(La, 4);
- printSqList(La);
- printf("创建线性表Lb:\n");
- createSqList(Lb, 7);
- printSqList(Lb);
- unionSqList(La, Lb);
- printSqList(La);
- printf("La的长度为%d\n",La.len);
注意:将merge第一个循环体中以switch语句代替if语句,即分出3种情况,当a=b时,只将两者之一插入到Lc中,则此时算法完成的操作和union完全相同,而时间复杂度却不同,其原因有二:
1.La,Lb中的元素都是依值递增(同一集合中元素不等),则对Lb中每个元素,不需要再La中从表头至表尾进行全程搜索
2.由于用新表Lc表示“并集”,则插入操作实际上借助“复制”完成,而无须因插入导致移动一系列元素
由此可见,若以线性表表示集合并进行集合的各种运算,应先对表中的元素进行排序,则可降低集合运算的时间复杂度