标签:nod def scanf lis 成功 基本操作 eof malloc tty
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MaxSize 10 4 typedef int ElementType; 5 struct SqList { 6 ElementType elem[MaxSize]; 7 int Length; 8 }; 9 10 typedef struct SqList *PtrNode; 11 typedef PtrNode List; 12 13 List InitList(); 14 int InSert(List L, int i, ElementType x) ; 15 int Delete(List L, int i); 16 int GetElem(List L, int i); 17 int Print(List L); 18 19 int main() { 20 int a,b; 21 ElementType x; 22 List list; 23 list=InitList(); 24 InSert(list, 1, 1); 25 InSert(list, 2, 2); 26 InSert(list, 3, 3); 27 Print(list); 28 printf("第一处的元素为:%d\n",GetElem(list,1)); 29 printf("要删除第几处的数据"); 30 scanf("%d", &a); 31 Delete(list, a); 32 Print(list); 33 } 34 List InitList() { //初始化 35 List L; 36 L = (List)malloc(sizeof(struct SqList)); 37 L->Length = 0; 38 printf("初始化成功\n"); 39 return L; 40 } 41 //插入 42 int InSert(List L, int i, ElementType x) { 43 int j; 44 if (i<1 || i>L->Length + 1) { 45 printf("越界"); return 0; 46 } 47 for (j = L->Length; j > i; j--) { 48 L->elem[j + 1] = L->elem[j]; 49 } 50 L->elem[i - 1] = x; //第i处,因为是数组所以减一 51 L->Length++; 52 return 1; 53 } 54 //删除 55 int Delete(List L, int i) { 56 int j; 57 if (i<1 || i>L->Length) { 58 printf("越界"); return 0; 59 } 60 for (j = i - 1; j < L->Length-1; j++) 61 L->elem[j] = L->elem[j+1]; 62 L->Length--; 63 return 1; 64 65 } 66 //查找第i处的数据 67 int GetElem(List L, int i) { 68 if (i<1 || i>L->Length) { 69 printf("越界"); return 0; 70 } 71 return L->elem[i - 1]; 72 } 73 //遍历输出 74 int Print(List L) { 75 int i = 0; 76 for (i; i < L->Length; i++) 77 printf("%d\n", L->elem[i]); 78 }
标签:nod def scanf lis 成功 基本操作 eof malloc tty
原文地址:https://www.cnblogs.com/wy0526/p/11788468.html