createList ( int n )
时间: 2024-01-24 07:17:11 浏览: 28
这是一个函数定义,用于创建一个包含 n 个元素的列表(或称为数组)。下面是一个可能的实现:
```c
int *createList(int n) {
int *list = malloc(n * sizeof(int));
return list;
}
```
这个函数分配了一个大小为 n 个 int 类型的内存块,然后将其转换为 int 类型指针并返回。调用者可以使用返回的指针来访问列表中的元素。请注意,这个函数仅仅是分配了内存,并没有初始化内存中的值,因此在使用列表之前需要初始化它们。
相关问题
CreateList_L(LinkList&L, int n)函数实现单链表的创建。
好的,我可以给您讲解一下CreateList_L函数的实现原理。
CreateList_L函数的作用是创建一个含有n个结点的单链表,并将该链表的头指针存放在L中。下面是CreateList_L函数的代码实现:
```
void CreateList_L(LinkList& L, int n) {
L = (LinkList)malloc(sizeof(LNode)); // 创建头结点
L->next = NULL; // 初始化链表为空
LinkList p, tail = L; // p指向当前结点,tail始终指向尾结点
for (int i = 0; i < n; i++) { // 循环创建n个结点
p = (LinkList)malloc(sizeof(LNode)); // 创建新结点
scanf("%d", &p->data); // 输入新结点的值
p->next = NULL; // 新结点指针域置为空
tail->next = p; // 尾结点指向新结点
tail = p; // 更新尾结点
}
}
```
以上是CreateList_L函数的完整代码实现,函数的主要流程如下:
1. 创建头结点,并将其存放在L中;
2. 循环创建n个结点,每个结点的值由用户输入;
3. 将新结点插入到链表的尾部,更新尾结点。
这样,CreateList_L函数就可以成功地创建一个含有n个结点的单链表,并将其头指针存放在L中。
//Create an array based list with n elements Status CreateList_Sq(SqList *L, int a[], int n) { //学生添加 }
Status CreateList_Sq(SqList *L, int a[], int n) {
if (n > MAXSIZE) {
return ERROR;
}
L->length = n;
for (int i = 0; i < n; i++) {
L->data[i] = a[i];
}
return OK;
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)