1.代码实现
1 #include "stdio.h" 2 #include "stdlib.h" 3 #include "stdbool.h" 4 5 #define MAXSIZE 1024 6 #define ERROR 0 7 8 typedef int elemtype; 9 10 typedef struct{ 11 elemtype data[MAXSIZE]; 12 int last; 13 } SList; 14 15 16 17 //初始化线性表L并返回线性表 18 SList *Init_SList(){ 19 SList *L; 20 L = (SList *)malloc(sizeof(SList)); 21 L ->last = -1; 22 printf("初始化成功\n"); 23 return L; 24 } 25 26 27 //线性表的数据录入 28 SList *indata_SList(SList *L){ 29 elemtype x ; 30 scanf("%d" ,&x); 31 while(x != 0){ 32 L -> last += 1; 33 L -> data[L -> last] = x; 34 scanf("%d",&x); 35 } 36 return L; 37 } 38 39 40 //判断表是否为空 41 bool is_empty(SList *L){ 42 return (L -> last == -1); 43 } 44 45 46 //求线性表的表长 47 int SList_Length(SList *L){ 48 return(L -> last + 1); 49 } 50 51 52 //遍历线性表 53 void print_SList(SList *L){ 54 int i = 0; 55 for(i = 0; i < L -> last + 1; i++) 56 { 57 printf(" a[%d] = %d",i , L -> data[i]); 58 if(i%5 == 0) printf("\n"); 59 } 60 } 61 62 63 //插入数据于指定位置 64 SList *insert_SList(SList *L, int insert, int loc){ 65 if(loc < 0||loc > L -> last + 1){ 66 printf("请输入正确的插入位置\n"); 67 } 68 else{ 69 L -> last = L -> last + 1; 70 int i = 0; 71 for(i = L -> last; i > loc - 1; i--){ 72 L -> data[i] = L -> data[i-1]; 73 } 74 L -> data[loc] = insert; 75 } 76 return L; 77 } 78 79 80 //删除顺序表中指定位置的值非数组下标 81 SList *delete_SList(SList *L, int loc){ 82 int e = 0; 83 if(!(L -> data[loc - 1])||loc > L -> last + 2) 84 return ERROR; 85 else 86 { 87 int i = 0; 88 e = L -> data[loc - 1]; 89 for (i = loc ; i <= L -> last; i++) { 90 L -> data[i - 1] = L -> data[i]; 91 } 92 L -> last = L -> last - 1; 93 printf("您已成功删除数据%d",e); 94 return L; 95 } 96 } 97 98 99 //改动某一数据的值 100 SList *change_SList(SList *L, int e, int loc){ 101 if(!(L -> data[loc - 1])||loc > L -> last + 2) 102 return ERROR; 103 else{ 104 L -> data[loc] = e; 105 } 106 return L; 107 } 108 109 110 //查看某一元素是否在线性表中,若在,则返回其位置 111 int search_SList(SList *L, int e){ 112 int i = 0; 113 int loc = 0; 114 for(i = 0 ; i <= L -> last - 1; i++) 115 { 116 if(L -> data[i] == e) 117 loc = i; 118 119 } 120 if(loc < L -> last - 1) return loc; 121 else return 0; 122 } 123 124 125 //主函数 126 int main(){ 127 int e = 0; 128 int loc = 0; 129 SList * MyList = Init_SList(); 130 printf("请输入一组数据以插入进线性表,以0作为停止输入标志:"); 131 indata_SList(MyList); 132 is_empty(MyList); 133 int length = SList_Length(MyList); 134 printf("您输入的线性表的表长为%d",length); 135 print_SList(MyList); 136 printf("\n"); 137 printf("请输入您要插入的数据以及位置:"); 138 scanf("%d %d",&e,&loc); 139 MyList = insert_SList(MyList,e,loc); 140 print_SList(MyList); 141 printf("\n"); 142 printf("请选择需要删除的元素位置:"); 143 scanf("%d",&loc); 144 MyList = delete_SList(MyList, loc); 145 print_SList(MyList); 146 printf("\n"); 147 printf("请输入需要改动为新元素的值及其位置"); 148 scanf("%d %d",&e, &loc); 149 change_SList(MyList,e,loc); 150 print_SList(MyList); 151 printf("\n"); 152 printf("请输入要查找的元素:"); 153 scanf("%d",&e); 154 loc = search_SList(MyList, e); 155 if(loc != 0) 156 printf("您要查找的元素在位置%d",loc); 157 else printf("您要查找的元素不在顺序表内\n"); 158 printf("\n"); 159 }
时间: 2024-10-10 05:35:21