zoukankan      html  css  js  c++  java
  • 数据结构之顺序存储

    
    

      线性表基本概念:由同类型数据元素构成有序序列的线性结构

        表中元素个数称为线性表的长度

         线性表没有元素时称为空表

         表起始位置称为表头,结束位置称为表尾

         位序i从1开始


    1
    #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define MAXSIZE 100//顺序表所能表达的最大长度 5 #define OVERFLOW 1 6 7 8 9 typedef int Status;//重命名,使Status和int有相同的作用 10 typedef int ElemType; 11 12 void Interrupt(void)//创建一个中断函数 13 { 14 while(1) //用于检测换行符,使函数脱离scanf的连续输出 15 if(getchar()==' ') 16 break; 17 } 18 19 typedef struct 20 { 21 ElemType *elem;//存储空间的基地址 22 int length;//当前长度 23 }Sqlist;//顺序表的结构类型为Sqlist 24 25 Status InitList(Sqlist &L)//建立一个空表 26 { 27 L.elem = new ElemType[MAXSIZE]; 28 if(!L.length) 29 { 30 exit(OVERFLOW); 31 } 32 L.length = 0; 33 return 0; 34 } 35 36 Status MyList(Sqlist &L)//在空表上输入数字,建立一个属于自己填写的表 37 { 38 int b,i=0; 39 printf("表的长度: "); 40 scanf("%d",&L.length); 41 Interrupt(); 42 printf("输入要填入表的数字: "); 43 for( ; i < L.length ; i++ ) 44 { 45 scanf("%d",&b); 46 L.elem[i] = b; 47 } 48 Interrupt(); 49 return 0; 50 } 51 52 Status TraverseList(Sqlist &L)//遍历 ,使现有表中的数据重新打印出来 53 { 54 int j; 55 if(L.length==0) 56 { 57 printf("空表 "); 58 return 0; 59 } 60 else 61 { 62 for(j=0 ; j < L.length ; j++)//建立一个for函数,读取顺序表中的数据 63 printf("%d ",L.elem[j]); 64 printf(" ");//最后换行 65 return 0; 66 } 67 } 68 69 Status GetElem(Sqlist &L,int i,int &e)//取值 。i:为要取的值得位置,&e:为i位置的接受变量,并且返回主函数 70 { 71 int c; 72 scanf("%d",&i); 73 Interrupt(); 74 if(i<0||i>L.length)//判断i的取值是否超范围 75 { 76 printf("ERROR ");//如果超范围,返回ERROR,并结束 77 return 0; 78 } 79 else 80 { 81 e = L.elem[i-1];//返回将第i位置的数据赋值给e,并输出 82 printf("%d ",e); 83 return 0; 84 } 85 86 } 87 88 Status DeleteElem(Sqlist &L,int i)//删除 。i:为要删除数据的位置 89 { 90 int j; 91 scanf("%d",&i);//输入要删除的第i位 92 Interrupt(); 93 if(i<0||i>L.length)//判断输入的i 是否超范围,如果超出,返回错误。 94 { 95 printf("ERROR "); 96 return 0; 97 } 98 else 99 { 100 for(j = i; j<=L.length;j++)//被删除元素之后的元素都向前移一位 101 L.elem[j-1] = L.elem[j]; 102 L.length = L.length-1;//表长减1 103 return 0; 104 } 105 } 106 107 Status ListInsert(Sqlist &L,int i,ElemType e)//顺序表的插入 i:为要插入的表的位置。e:要插入该位置的元素 108 { 109 int j; 110 scanf("%d",&i); 111 scanf("%d",&e); 112 Interrupt(); 113 if(i>L.length+1||L.length>=MAXSIZE||i<1)//判断要插入表的位置i是否超出范围 114 { 115 printf("ERROR "); 116 return 0; 117 } 118 else 119 { 120 for(j=L.length;j>=i;j--)//插入位置及之后的元素分别向后移一位 121 L.elem[j]=L.elem[j-1]; 122 L.elem[i-1]=e;//将新元素e放入第i个位置 123 ++L.length;//表长加1 124 return 0; 125 } 126 } 127 128 Status LocateElem(Sqlist &L,ElemType e)//查找。在顺序表L中查找值为e的数据元素 129 { 130 int j,i=0; 131 bool text=true;//利用bool类型,来判断表里有没有值为e的值 132 scanf("%d",&e);//输入e的值 133 Interrupt(); 134 for(j=0;j<L.length;j++)//遍历整个顺序表,和e值一一作比较, 135 { 136 if(L.elem[j]==e) 137 { 138 printf("第 %d 位 ",j+1); 139 i++; 140 text=false; 141 } 142 } 143 144 if(text)//bool类型的输出 145 printf("没有找到"); 146 return 0; 147 } 148 149 int main() 150 { 151 int a,i=0; 152 char c; 153 Sqlist L;//将L定义为Sqlist类型的变量,便于直接引用 154 InitList(L); 155 MyList(L); 156 TraverseList(L); 157 printf("操作输入序号选择: 1:遍历顺序表 2:顺序表取值 3:删除元素 4:插入元素 5:顺序表查找 6:表长 输入#退出 "); 158 while(1) 159 { 160 int f = 0; 161 printf("请选择:"); 162 scanf("%c",&c); 163 Interrupt(); 164 switch(c) 165 { 166 case '1': printf("遍历顺序表: ");TraverseList(L); break; 167 case '2': printf("顺序表取值(取值位置): ");GetElem(L, a , a ); break; 168 case '3': printf("删除元素(删除的位置): ");DeleteElem(L,a); break; 169 case '4': printf("插入元素(位置 插入的元素): ");ListInsert(L,a,a); break; 170 case '5': printf("顺序表查找(输入要查找的元素): ");LocateElem(L,a); break; 171 case '6': printf("表长: %d ",L.length); break; 172 case '#': f = 1; break; 173 default: printf("ERROR "); 174 } 175 if (f == 1) 176 { 177 printf("已正常退出! "); 178 break; 179 } 180 } 181 182 return 0; 183 }

     

  • 相关阅读:
    rabbitMQ学习(二)
    rabbitMQ学习(一)
    mysql自动添加最后修改时间
    git乱码问题解决
    tomcat manager配置
    linux下dos环境和unix环境转换
    Gson运用
    从一个复杂的json格式的String内获取某key的值
    spring quartz 定时器时间格式设置
    在spring框架中配置Quartz定时器发生错误: class org.springframework.scheduling.quartz.JobDetailBean has interface org.quartz.JobDetail as sup
  • 原文地址:https://www.cnblogs.com/GLory-LTF/p/15042485.html
Copyright © 2011-2022 走看看