97国产精品视频人人做人人爱,久久精品亚洲日本,亚洲国产精品线播放,亚洲另类欧洲综合久久

      快報(bào):C語(yǔ)言實(shí)現(xiàn)順序表的基本操作
      2023-07-03 20:17:26 來(lái)源:博客園

      下面是使用C語(yǔ)言實(shí)現(xiàn)順序表基本操作的示例代碼:

      ```c#include #include


      (相關(guān)資料圖)

      #define MAX_SIZE 100

      typedef struct { int data[MAX_SIZE]; int length;} SeqList;

      void initList(SeqList* list) { list->length = 0;}

      void insert(SeqList* list, int position, int item) { if (position < 0 || position > list->length || list->length == MAX_SIZE) { printf("Invalid position or list is full\n"); return; }

      for (int i = list->length - 1; i >= position; i--) { list->data[i + 1] = list->data[i]; }

      list->data[position] = item; list->length++;}

      void delete(SeqList* list, int position) { if (position < 0 || position >= list->length) { printf("Invalid position\n"); return; }

      for (int i = position; i < list->length - 1; i++) { list->data[i] = list->data[i + 1]; }

      list->length--;}

      int search(SeqList* list, int key) { for (int i = 0; i < list->length; i++) { if (list->data[i] == key) { return i; } }

      return -1;}

      void display(SeqList* list) { printf("List elements: "); for (int i = 0; i < list->length; i++) { printf("%d ", list->data[i]); } printf("\n");}

      int main() { SeqList list; int choice, item, position, key, index;

      initList(&list);

      while (1) { printf("1. Insert\n"); printf("2. Delete\n"); printf("3. Search\n"); printf("4. Display\n"); printf("5. Exit\n");

      printf("Enter your choice: "); scanf("%d", &choice);

      switch (choice) { case 1: printf("Enter the element to be inserted: "); scanf("%d", &item); printf("Enter the position: "); scanf("%d", &position); insert(&list, position, item); break; case 2: printf("Enter the position: "); scanf("%d", &position); delete(&list, position); break; case 3: printf("Enter the key to search: "); scanf("%d", &key); index = search(&list, key); if (index != -1) { printf("Element found at position: %d\n", index); } else { printf("Element not found\n"); } break; case 4: display(&list); break; case 5: exit(0); default: printf("Invalid choice\n"); break; } }

      return 0;}```

      這段代碼實(shí)現(xiàn)了順序表的基本操作,包括初始化順序表、插入元素、刪除元素、查找元素和顯示順序表內(nèi)容。在`main`函數(shù)中,通過(guò)一個(gè)循環(huán)菜單,用戶可以選擇不同的操作。根據(jù)用戶選擇的操作,調(diào)用相應(yīng)的函數(shù)進(jìn)行處理。

      順序表的結(jié)構(gòu)體定義中,`data`數(shù)組用于存儲(chǔ)元素,`length`記錄當(dāng)前順序表的長(zhǎng)度。`initList`函數(shù)用于初始化順序表,`insert`函數(shù)用于在指定位置插入元素,`delete`函數(shù)用于刪除指定位置的元素,`search`函數(shù)用于查找指定元素的位置,`display`函數(shù)用于打印順序表的內(nèi)容。

      可以根據(jù)需要修改和擴(kuò)展這段代碼,以滿足特定的順序表操作要求。

      關(guān)鍵詞:

      相關(guān)閱讀
      分享到:
      版權(quán)和免責(zé)申明

      凡注有"實(shí)況網(wǎng)-重新發(fā)現(xiàn)生活"或電頭為"實(shí)況網(wǎng)-重新發(fā)現(xiàn)生活"的稿件,均為實(shí)況網(wǎng)-重新發(fā)現(xiàn)生活獨(dú)家版權(quán)所有,未經(jīng)許可不得轉(zhuǎn)載或鏡像;授權(quán)轉(zhuǎn)載必須注明來(lái)源為"實(shí)況網(wǎng)-重新發(fā)現(xiàn)生活",并保留"實(shí)況網(wǎng)-重新發(fā)現(xiàn)生活"的電頭。