首页 > 代码库 > 顺序表
顺序表
最近在补习本科时学的知识,之后会陆陆续续发一些自己补习时涉及到的知识点,都是基础知识,在这里写出来以来便于自己管理以及查看,二来有需要的同学也可以看看。这是我自己写的顺序表的取数据、插入数据、删除数据、显示数据以及查找数据操作代码,可直接运行。注释得很详细了,应该很容易看懂。
1 /*顺序表的取数据、插入数据、删除数据、显示数据以及查找数据操作*/ 2 #include <stdio.h> 3 #define ERROR -1 4 #define OK 1 5 #define TRUE 1 6 #define FALSE 0 7 #define MAXSIZE 100 8 //定义顺序表结构体 9 struct Sqlist 10 { 11 int data[MAXSIZE]; 12 int length; 13 }; 14 15 //获取表数据 16 int GetElement(struct Sqlist list,int index,int *elem) 17 { 18 int length=list.length; 19 //printf("length is %d\n",length); 20 if(length==0||index<0||index>=length) 21 return ERROR; 22 *elem=list.data[index]; 23 return OK; 24 } 25 26 //向表中插入数据 27 int Insert(struct Sqlist *list,int index,int elem) 28 { 29 int length=list->length; 30 //printf("length is %d\n",length); 31 if(length==0||index<0||index>length||length>=MAXSIZE) 32 return ERROR; 33 34 for (int i=length-1;i>=index;i--) 35 { 36 list->data[i+1]=list->data[i]; 37 } 38 list->data[index]=elem; 39 list->length++; 40 printf("length is %d\n",list->length); 41 return OK; 42 } 43 44 //删除表中数据 45 int Delete(struct Sqlist *list,int index) 46 { 47 int length=list->length; 48 //printf("length is %d\n",length); 49 if(length==0||index<0||index>length-1) 50 return ERROR; 51 for (int i=index;i<length-1;i++) 52 { 53 list->data[i]=list->data[i+1]; 54 } 55 list->data[length-1]=‘\0‘; 56 list->length--; 57 printf("length is %d\n",list->length); 58 return OK; 59 } 60 61 //显示数据 62 void ShowData(struct Sqlist *list) 63 { 64 printf("当前数据如下: \n"); 65 for (int i=0;i<list->length;i++) 66 { 67 printf("%d ",list->data[i]); 68 } 69 printf("\n"); 70 } 71 72 //找元素位置 73 void LocationElem(struct Sqlist list,int elem) 74 { 75 for (int i=0;i<list.length;i++) 76 if(elem==list.data[i]) 77 printf("该元素位置为:%d\n",i); 78 /*else 79 printf("没找到\n");*/ 80 } 81 82 //主函数 83 int main() 84 { 85 struct Sqlist list= 86 { 87 {1,33,43,2,21,34,65,112,32,19},10 88 }; 89 printf("length is %d\n",list.length); 90 //测试读数据 91 int *elem=0,test=11; 92 elem=&test; 93 if (OK==GetElement(list,2,elem)) 94 { 95 printf("list get 2:%d\n",*elem); 96 } 97 printf("\n"); 98 //测试插入数据 99 if (OK==Insert(&list,7,520))100 {101 printf("list insert 7 ok!\n");102 }103 if (OK==GetElement(list,7,elem))104 {105 printf("list get 7:%d\n",*elem);106 }107 ShowData(&list);108 printf("\n");109 if (OK==Insert(&list,3,520))110 {111 printf("list insert 3 ok!\n");112 }113 if (OK==GetElement(list,3,elem))114 {115 printf("list get 3:%d\n",*elem);116 }117 ShowData(&list);118 printf("\n");119 120 //测试删除数据121 /*if (OK==Delete(&list,3))122 {123 printf("list delete 3 ok!\n");124 }125 if (OK==GetElement(list,3,elem))126 {127 printf("list get 3:%d\n",*elem);128 }129 ShowData(&list);130 printf("\n");*/131 if (OK==Delete(&list,9))132 {133 printf("list delete 9 ok!\n");134 }135 if (OK==GetElement(list,9,elem))136 {137 printf("list get 9:%d\n",*elem);138 }139 ShowData(&list);140 printf("\n");141 142 /*else143 {144 printf("list get error!\n");145 }*/146 147 //int input;148 //scanf("please input the number you want to search:%d",&input);149 LocationElem(list,33);150 151 return OK;152 }
顺序表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。