#include#define MAXSIZE 100 //线性表可能达到的最大长度typedef struct { int last;//线性表最后一个元素在数组中的位置 int elem[MAXSIZE];}SeqList;void InitList(SeqList *list);int FindList(const SeqList *list, int e);int GetValue(const SeqList *list, int n);int Insert(SeqList *list, int n, int e);int DelList(SeqList *list, int n);int main(int argc, char* argv[]){ int position; SeqList *list; list = (SeqList *)malloc(sizeof(SeqList *)); InitList(list); Insert(list, 1,5); Insert(list, 2,4); Insert(list, 3,7); Insert(list, 4,4); position = FindList(list,5); if (position != -1) { printf("找到元素5在线性表中的位置:%d\n",position); }else printf("没有找到!\n"); printf("%d\n", GetValue(list,1)); for(int i = 0;i last; i++) printf("%d ",list->elem[i]); printf("\n"); DelList(list,1); for(int j = 0;j last; j++) printf("%d ",list->elem[j]); printf("\n"); return 0;}void InitList(SeqList *list){ list->last = 0;//初始化一个线性表,0表示为空}/*查找 *在线性表中查找与e相等的元素,返回其在数组中的位置,若找不到返回-1 */int FindList(const SeqList *list, int e){ int i = 0; for (; i <= list->last; i++) { if(list->elem[i] == e) return i+1; } return -1;}/*返回线性表中第n个元素*/int GetValue(const SeqList *list, int n){ if( (n<0) || (n > list->last+1)) return -1; return list->elem[n-1];}/**插入:在线性表指定位置n,插入元素e*/int Insert(SeqList *list, int n, int e){ int k ; if ((n<1)|| (n > list->last+2)) { printf("插入位置不合法!\n"); return -1; } if(list->last == MAXSIZE - 1) { printf("线性表已满,无法插入!\n"); return -1; } for (k = list->last; k >= n - 1 ; k--) list->elem[k+1] = list->elem[k]; list->elem[n-1] = e; list->last++; return 1;}/**删除在线性表指定位置的元素*/int DelList(SeqList *list, int n){ int k ; if ((n<1)|| (n > list->last+1)) { printf("删除位置不合法!\n"); return -1; } for (k = n; k < list->last+1; k++) list->elem[k-1] = list->elem[k]; list->last--; return 1;}