• C--算法与数据结构--顺序表操作


    一.实验目的

    熟练掌握线性表的基本操作在顺序表存储结构上的实现。

    二.主要仪器及耗材:

    普通计算机

    三。实验内容:

    1.在有序表种插入一个元素并保持该表任然有序。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     5 
     6 #define DATATYPE int
     7 #define MAXSIZE 100
     8 typedef struct{
     9     DATATYPE datas[MAXSIZE];
    10     int last;
    11 }SEQUENLIST;
    12 main(){
    13     SEQUENLIST a;
    14     int i,k,m,x;
    15     printf("请输入顺序表元素,元素为整形量,用空格分开,-99为结束标志:");
    16     a.last=0;i=0;scanf("%d",&i);
    17     while(i!=-99){
    18         k=a.last;
    19         while((k>=1)&&(i<a.datas[k]))k--;
    20         for(m=a.last;m>=k+1;m--) a.datas[m+1]=a.datas[m];
    21         a.datas[k+1]=i;
    22         a.last++;
    23         scanf("%d",&i);
    24     } 
    25     printf("输入要插入的元素(整形)");
    26     scanf("%d",&x);
    27     printf("
    插入前有序表元素列表");
    28     for(i=1;i<=a.last;i++){
    29         printf("%4d",a.datas[i]);
    30     } 
    31     printf("
    ");
    32     i=a.last;
    33     while((i>=1)&&(x<a.datas[i]))i--;
    34     for(m=a.last;m>=i+1;m--){
    35         a.datas[m+1]=a.datas[m];
    36         }
    37     a.datas[i+1]=x;
    38     a.last++;
    39     printf("
    插入有序表元素列表");
    40     for(i=1;i<=a.last;i++){
    41         printf("%4d",a.datas[i]);
    42         
    43     } 
    44     printf("
    ");
    45         
    46     }
    1

    2.两个有序表的合并:按用户输入的数据建立两个有序表la,lb(元素值和按递增有序),合并成一个新的递增有序的顺序表lc。在lc中值相同的元素均保留,即lc的表长=la的表长+lb的表长。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     5 
     6 #define DATATYPE int 
     7 #define MAXSIZE 100
     8 typedef struct{
     9     DATATYPE datas[MAXSIZE];
    10     int last;
    11 }SEQUENLIST;
    12 void merge_sqlist(SEQUENLIST la,SEQUENLIST lb,SEQUENLIST *lc){
    13     int i=1,j=1,k=1;
    14     while(i<=la.last && j<=lb.last){
    15         if(la.datas[i]<=lb.datas[j]){
    16             lc->datas[k]=la.datas[i];
    17             k++;
    18             i++;
    19         }
    20         else{
    21             lc->datas[k]=lb.datas[j];
    22             k++;
    23             j++;
    24         }
    25     }
    26     while(i<=la.last){
    27         lc->datas[k]=la.datas[i];
    28         k++;
    29         i++;
    30     }
    31     
    32     while(j<=lb.last){
    33         lc->datas[k]=lb.datas[j];
    34         k++;
    35         j++;
    36     }
    37     lc->last=k-1;
    38     return ;
    39 }
    40 main(){
    41     SEQUENLIST la,lb,lc;
    42     int i,k,m;
    43     printf("请输入la顺序表元素,元素为整形量,用空格分开,-99为结束标志:");
    44     
    45     la.last=0;i=0;scanf("%d",&i);
    46     while(i!=-99){
    47         k=la.last;
    48         while((k>=1)&&(i<la.datas[k]))k--;
    49         for(m=la.last;m>=k+1;m--)
    50             la.datas[m+1]=la.datas[m];
    51         la.datas[k+1]=i;
    52         la.last++;
    53         scanf("%d",&i);
    54     }
    55     printf("请输入lb顺序表元素,元素为整形量,用空格分开,-99为结束标志:");
    56     
    57     lb.last=0;i=0;scanf("%d",&i);
    58     while(i!=-99){
    59         k=lb.last;
    60         while((k>=1)&&(i<lb.datas[k]))k--;
    61         for(m=lb.last;m>=k+1;m--) lb.datas[m+1]=lb.datas[m];
    62         lb.datas[k+1]=i;    
    63         lb.last++;
    64         scanf("%d",&i);
    65         }
    66     
    67     printf("
    la的有序表元素列表:"); 
    68     for(i=1;i<=la.last;i++) {
    69         printf("%4d",la.datas[i]); 
    70     }
    71     printf("
    ");
    72     
    73     printf("
    lb的有序表元素列表:"); 
    74     for(i=1;i<=lb.last;i++){
    75         printf("%4d",lb.datas[i]);
    76     }
    77     printf("
    ");
    78     
    79     merge_sqlist(la,lb,&lc);
    80     printf("
    合并后lc的有序表元素列表:"); 
    81     for(i=1;i<=lc.last;i++){
    82         printf(" %d",lc.datas[i]);    
    83     }
    84     printf("
    ");
    85     }
    2

    3.编程实现删除有序表某一指定元素。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     4 #define DATATYPE int
     5 #define MAXSIZE 100
     6 
     7 typedef struct{
     8     DATATYPE datas[MAXSIZE];
     9     int last;
    10 }SEQUENLIST;
    11 
    12 main(){
    13     SEQUENLIST a;
    14     int i,k,m,x;
    15     printf("请输入顺序表元素,元素为整形量,用空格分开,-99为结束标志:");
    16     a.last=0;i=0;scanf("%d",&i);
    17     while(i!=-99){
    18         k=a.last;
    19         while((k>=1)&&(i<a.datas[k]))k--;
    20         for(m=a.last;m>=k+1;m--) a.datas[m+1]=a.datas[m];
    21         a.datas[k+1]=i;
    22         a.last++;
    23         scanf("%d",&i);
    24     } 
    25     printf("输入要删除的元素(整形)");
    26     scanf("%d",&x);
    27     printf("
    删除前有序表元素列表");
    28     for(i=1;i<=a.last;i++){
    29         printf("%4d",a.datas[i]);
    30     } 
    31     printf("
    ");
    32     i=a.last;
    33     while((i>=1)&&(x<a.datas[i]))i--;
    34     for(m=i;m<a.last;m++){
    35         a.datas[m]=a.datas[m+1];
    36         }
    37     a.last--;
    38     
    39     printf("
    删除后有序表元素列表");
    40     for(i=1;i<=a.last;i++){
    41         printf("%4d",a.datas[i]);
    42         
    43     } 
    44     printf("
    ");        
    45     }
    3
  • 相关阅读:
    Canvas 五角星绘制
    Bob Waters
    自定义样式,使用浏览器阅读epub格式的电子书
    FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)
    【转载】什么样的妻子什么样的人生
    win10与Ubantu双系统:Linux下开启FTP服务器与创建无线热点(实现文件共享)
    这世上,没有完美的父母,也没有完美的孩子
    考研数学复习建议与资料
    eclipse中修改jsp文件中内容,但是刷新网页内容却不变问题
    JavaScript基础2
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/8795380.html
Copyright © 2020-2023  润新知