线性表操作

来源:互联网 发布:淘宝网十二伏鹰眼灯 编辑:程序博客网 时间:2024/05/16 15:44
/***name: create a seqlistreceive: seqlist addressreturn: int***/int creat(Seqlist* L){int len;L->length = 0;printf("Enter the length of the seqlist:");scanf("%d", &len);if (len <= 100){for (int i = 0; i < len; i++){printf("Enter the %d element of the seqlist:", (i + 1));scanf("%d", &(L->data[i]));L->length++;}return 0;}else{printf("the length overflow!");return -1;}}/***name: print the seqlistreceive: seqlistreturn: int***/int show(Seqlist L){if (L.length > 0){for (int i = 0; i < (L.length); i++)printf("%d ", L.data[i]);printf("\n");return 0;}else{printf("The seqlist is empty!");return -1;}}/***name: get a element of the seqlistreveive: seqlist, positionreturen: int***/int getelement(Seqlist L, int locate){if (locate <= L.length)return L.data[locate - 1];return -1;}/***name: insert a data to the seqlistreceive: seqlist address, insert position, insert datareturn: int***/int insert(Seqlist *L, int locate, int element){if ((L->length) < 100){for (int i = (L->length); i>locate; i--)L->data[i] = L->data[i - 1];L->data[locate] = element;L->length++;return 0;}else{printf("seqlist is full!");return -1;}}/***name: delete a element from the seqlistreceive: seqlist address, elementreturn int***/int delete_by_element(Seqlist* L, int element){for (int i = 0; i < L->length; i++){if (L->data[i] == element){for (int j = i; j < L->length; j++){L->data[j] = L->data[j + 1];}}}L->length--;return 0;}/***name: delete a element from the seqlistreceive: seqlist address, positionreturn: int***/int delete_by_position(Seqlist* L, int locate){if (locate < L->length){for (int i = (locate-1); i < L->length; i++){L->data[i] = L->data[i + 1];}}L->length--;return 0;}

0 0
原创粉丝点击