用c++写一个数据结构里的顺序表的创建和赋值
时间: 2024-06-05 20:06:02 浏览: 39
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int length;
} SeqList;
void create(SeqList *L, int n) {
if(n > MAX_SIZE) {
printf("Error: the size of list exceeds the maximum size.\n");
exit(1);
}
printf("Please input %d integers:\n", n);
for(int i = 0; i < n; i++) {
scanf("%d", &L->data[i]);
}
L->length = n;
}
void printList(SeqList L) {
for(int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
int main() {
SeqList L;
int n;
printf("Please input the size of list: ");
scanf("%d", &n);
create(&L, n);
printf("The list is: ");
printList(L);
return 0;
}
阅读全文