帮我用数据结构写一个整型数顺序表并使用菜单功能
时间: 2023-05-20 17:01:02 浏览: 90
数据结构实验一顺序表的实现.doc
好的,以下是一个简单的整型数顺序表的实现,同时也包含了菜单功能:
```c
#include <stdio.h>
#include <stdlib.h>
#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 value) {
if (list->length >= MAX_SIZE) {
printf("List is full!\n");
return;
}
list->data[list->length++] = value;
}
void delete(SeqList *list, int index) {
if (index < 0 || index >= list->length) {
printf("Invalid index!\n");
return;
}
for (int i = index; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->length--;
}
void printList(SeqList *list) {
printf("List: ");
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
int main() {
SeqList list;
initList(&list);
int choice, value, index;
while (1) {
printf("Menu:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Print\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
insert(&list, value);
break;
case 2:
printf("Enter the index to delete: ");
scanf("%d", &index);
delete(&list, index);
break;
case 3:
printList(&list);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
希望这个实现能够满足您的需求。
阅读全文