帮我用c++写一个整型数顺序表包含六个选项并且使用菜单功能
时间: 2023-05-20 08:01:24 浏览: 58
当然可以,以下是代码示例:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 6
typedef struct {
int data[MAXSIZE];
int length;
} SeqList;
void initList(SeqList *L) {
L->length = 0;
}
void printList(SeqList L) {
int i;
for (i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
void insertList(SeqList *L, int pos, int value) {
int i;
if (pos < 1 || pos > L->length + 1) {
printf("Invalid position!\n");
return;
}
if (L->length == MAXSIZE) {
printf("List is full!\n");
return;
}
for (i = L->length - 1; i >= pos - 1; i--) {
L->data[i + 1] = L->data[i];
}
L->data[pos - 1] = value;
L->length++;
}
void deleteList(SeqList *L, int pos) {
int i;
if (pos < 1 || pos > L->length) {
printf("Invalid position!\n");
return;
}
for (i = pos; i < L->length; i++) {
L->data[i - 1] = L->data[i];
}
L->length--;
}
int main() {
SeqList L;
int choice, pos, value;
initList(&L);
while (1) {
printf("1. Print list\n");
printf("2. Insert element\n");
printf("3. Delete element\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printList(L);
break;
case 2:
printf("Enter position and value: ");
scanf("%d %d", &pos, &value);
insertList(&L, pos, value);
break;
case 3:
printf("Enter position: ");
scanf("%d", &pos);
deleteList(&L, pos);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)