C语言实现顺序表的插入、删除、查找运算
1 /* 2 *实现顺序表的插入、删除、查找运算 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <malloc.h> 8 9 #define MaxSize 20 10 typedef int ElemType; 11 12 typedef struct SeqList 13 { //线性表顺序存储结构定义 14 ElemType elem[MaxSize]; //存放顺序表数据元素 15 int length; //当前长度 16 }SeqList; //线性表顺序存储结构类型名 17 18 int Init_SeqList(SeqList &L) //初始化 19 { 20 L.length = 0; 21 return 1; 22 } 23 24 int Locate_SeqList(SeqList &L,int x) //查找 25 { 26 int i = 0; //i为扫描指示器 27 while(i < L.length && L.elem[i] != x) //顺序扫描直至找到等于给定值的元素或表尾 28 i ++; 29 if(i >= L.length) 30 { 31 printf("The sequence table does not exist for the element! "); 32 return 0; 33 } 34 else 35 return i + 1; 36 } 37 38 int Insert_SeqList(SeqList &L, int i, int x) //在第i个元素前插入新元素 39 { 40 int j; 41 if (L.length >= MaxSize) //L.length是最后一个元素的位置 42 { 43 printf("The sequence table is full and can not be inserted!"); 44 return 0; 45 } 46 if (i <= 0 || i > L.length+1) 47 { 48 printf("The insertion position is incorrect!"); 49 return 0; 50 } 51 for (j = L.length-1; j >= i - 1; j --) 52 { 53 L.elem[j + 1] = L.elem[j]; //元素右移 54 } 55 L.elem[i-1] = x; //插入新元素x 56 L.length++; //完成插入,表长增一 57 return 1; 58 } 59 60 int Delete_SeqList(SeqList &L, int i) //删除第i个元素 61 { 62 int j; 63 if (i < 1 || i > L.length) 64 { 65 printf("Delete location is incorrect!"); 66 return 0; 67 } 68 for (j = i; j < L.length; j++) 69 { 70 L.elem[j-1] = L.elem[j]; //元素左移 71 } 72 L.length --; //完成删除,表长减一 73 return 1; 74 } 75 76 int Display_SeqList(SeqList &L) 77 { 78 int i; 79 for (i = 0; i < L.length; i++) 80 printf("%d ",L.elem[i]); 81 return 1; 82 } 83 84 int main() 85 { 86 SeqList L; 87 ElemType e,x; 88 int i=1,k,j; 89 Init_SeqList(L); 90 printf("Initialization! The sequence table elements are as follows "); 91 Insert_SeqList(L,1,520); 92 Insert_SeqList(L,2,1314); 93 Insert_SeqList(L,3,4399); 94 Insert_SeqList(L,4,8888); 95 Insert_SeqList(L,5,215); 96 Insert_SeqList(L,6,222); 97 Display_SeqList(L); 98 printf(" "); 99 while (i < 3) 100 { 101 printf(" Main Menu "); 102 printf(" 1 Find the specified element "); 103 printf(" 2 Inert the element to the specified location "); 104 printf(" 3 Delete the specified location element "); 105 printf(" 4 Exit the problem "); 106 printf("--------------------------------------------------------------------- "); 107 printf("Please enter the menu number you want to select<1,2,3,4>: "); 108 scanf("%d",&i); 109 switch(i) 110 { 111 case 1: 112 printf("Please enter the search element: "); 113 scanf("%d",&x); 114 j = Locate_SeqList(L,x); 115 if (j != 0) 116 printf("The specified element location is: %d ",j); 117 break; 118 case 2: 119 printf("Please enter the location of the inserted element: "); 120 scanf("%d",&k); 121 printf("Please enter the value of the inserted element: "); 122 scanf("%d",&x); 123 j = Insert_SeqList(L,k,x); 124 if (j != 0) 125 { 126 printf("The sequence table after insertion is hown below "); 127 Display_SeqList(L); 128 printf(" "); 129 } 130 break; 131 case 3: 132 printf("Please enter the delete location: "); 133 scanf("%d",&k); 134 j = Delete_SeqList(L,k); 135 if (j != 0) 136 { 137 printf("The deleted sequence table is shown below "); 138 Display_SeqList(L); 139 printf(" "); 140 } 141 break; 142 case 0: 143 exit(0); 144 break; 145 default : 146 printf("Incorrect input!"); 147 } 148 } 149 }
结果运行截图