5.以C结构体或C++的“类”代替“第2章中复杂数据类型”,实现“链式线性表”,编写下面6个接口函数:CreateList、ListPrint、GetElem、ListLength、ListInsert、ListDelete
时间: 2023-08-31 07:33:45 浏览: 52
### 回答1:
以下是使用C结构体实现链式线性表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct List {
Node* head;
int length;
} List;
void CreateList(List* list) {
list->head = NULL;
list->length = 0;
}
void ListPrint(List* list) {
Node* p = list->head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int GetElem(List* list, int i) {
if (i < 1 || i > list->length) {
printf("Error: Index out of range\n");
return -1;
}
Node* p = list->head;
for (int j = 1; j < i; j++) {
p = p->next;
}
return p->data;
}
int ListLength(List* list) {
return list->length;
}
void ListInsert(List* list, int i, int data) {
if (i < 1 || i > list->length + 1) {
printf("Error: Index out of range\n");
return;
}
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
if (i == 1) {
newNode->next = list->head;
list->head = newNode;
} else {
Node* p = list->head;
for (int j = 1; j < i - 1; j++) {
p = p->next;
}
newNode->next = p->next;
p->next = newNode;
}
list->length++;
}
void ListDelete(List* list, int i) {
if (i < 1 || i > list->length) {
printf("Error: Index out of range\n");
return;
}
if (i == 1) {
Node* p = list->head;
list->head = p->next;
free(p);
} else {
Node* p = list->head;
for (int j = 1; j < i - 1; j++) {
p = p->next;
}
Node* q = p->next;
p->next = q->next;
free(q);
}
list->length--;
}
int main() {
List list;
CreateList(&list);
ListInsert(&list, 1, 1);
ListInsert(&list, 2, 2);
ListInsert(&list, 3, 3);
ListPrint(&list); // output: 1 2 3
printf("Length: %d\n", ListLength(&list)); // output: 3
printf("First element: %d\n", GetElem(&list, 1)); // output: 1
ListDelete(&list, 2);
ListPrint(&list); // output: 1 3
printf("Length: %d\n", ListLength(&list)); // output: 2
return 0;
}
```
其中,CreateList函数用于创建空的链表,ListPrint函数用于打印链表中的元素,GetElem函数用于获取链表中第i个元素的值,ListLength函数用于返回链表长度,ListInsert函数用于在链表的第i个位置插入一个元素,ListDelete函数用于删除链表中第i个元素。在这些函数中,链表的头结点保存在List结构体中,每个元素保存在一个Node结构体中,Node结构体包含了一个data成员表示元素的值,以及一个next指针表示指向下一个元素的指针。
### 回答2:
C语言中没有类的概念,但可以使用C结构体来代替第2章中复杂数据类型,实现链式线性表。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链式线性表的节点结构体
typedef struct Node {
int data; // 节点数据
struct Node* next; // 指向下一个节点的指针
} Node;
// 创建链式线性表
Node* CreateList() {
Node* head = NULL; // 头指针
Node* tail = NULL; // 尾指针
int n;
printf("请输入链表元素个数: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
// 创建新节点
Node* newNode = (Node*)malloc(sizeof(Node));
printf("请输入第%d个元素: ", i + 1);
scanf("%d", &(newNode->data));
newNode->next = NULL;
// 将新节点添加到链表末尾
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
// 打印链表
void ListPrint(Node* head) {
Node* p = head;
printf("链表元素: ");
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 获取指定位置的元素值
int GetElem(Node* head, int pos) {
Node* p = head;
int count = 1;
// 遍历链表找到指定位置的节点
while (p != NULL && count < pos) {
p = p->next;
count++;
}
if (p == NULL || count > pos) {
printf("无效的位置\n");
return -1;
} else {
return p->data;
}
}
// 获取链表长度
int ListLength(Node* head) {
Node* p = head;
int count = 0;
// 遍历链表计算长度
while (p != NULL) {
p = p->next;
count++;
}
return count;
}
// 在指定位置插入元素
void ListInsert(Node** head, int pos, int value) {
if (pos <= 0 || pos > ListLength(*head) + 1) {
printf("无效的位置\n");
return;
}
// 创建新节点
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
if (pos == 1) {
// 插入到链表头部
newNode->next = *head;
*head = newNode;
} else {
// 遍历链表找到插入位置前一个节点
Node* p = *head;
int count = 1;
while (p != NULL && count < pos - 1) {
p = p->next;
count++;
}
newNode->next = p->next;
p->next = newNode;
}
}
// 删除指定位置的元素
void ListDelete(Node** head, int pos) {
if (pos <= 0 || pos > ListLength(*head)) {
printf("无效的位置\n");
return;
}
if (pos == 1) {
// 删除链表头部元素
Node* temp = *head;
*head = (*head)->next;
free(temp);
} else {
// 遍历链表找到待删除节点的前一个节点
Node* p = *head;
int count = 1;
while (p != NULL && count < pos - 1) {
p = p->next;
count++;
}
Node* temp = p->next;
p->next = temp->next;
free(temp);
}
}
int main() {
Node* head = CreateList();
ListPrint(head);
printf("链表长度: %d\n", ListLength(head));
int value, pos;
printf("请输入要插入的元素和位置: ");
scanf("%d %d", &value, &pos);
ListInsert(&head, pos, value);
ListPrint(head);
printf("请输入要删除的位置: ");
scanf("%d", &pos);
ListDelete(&head, pos);
ListPrint(head);
return 0;
}
```
这个程序中,我们使用C结构体来定义节点结构体`Node`。`CreateList`函数用于创建链表,`ListPrint`函数用于打印链表,`GetElem`函数用于获取指定位置的元素值,`ListLength`函数用于获取链表长度,`ListInsert`函数用于在指定位置插入元素,`ListDelete`函数用于删除指定位置的元素。
### 回答3:
以下是使用C结构体和C的“类”代替第2章中复杂数据类型实现链式线性表的6个接口函数:
1. CreateList: 用于创建链式线性表。可以输入一个数组作为初始化数据,创建一个包含这些数据的链式线性表。
2. ListPrint: 用于打印链式线性表中的所有元素。遍历链表,依次打印每个节点的值。
3. GetElem: 用于获取链式线性表中指定位置的元素。输入一个位置索引,遍历链表,找到对应位置的节点,并返回节点的值。
4. ListLength: 用于获取链式线性表的长度。遍历链表,统计节点的个数,并返回节点个数。
5. ListInsert: 用于在链式线性表的指定位置插入一个元素。输入一个位置索引和一个要插入的值,遍历链表,找到对应位置的节点,将新节点插入到该位置。
6. ListDelete: 用于删除链式线性表中指定位置的元素。输入一个位置索引,遍历链表,找到对应位置的节点,删除该节点。
以上是用C结构体或C的“类”代替第2章中复杂数据类型实现链式线性表的6个接口函数。这些接口函数可以操作链表,完成链表的创建、遍历、获取元素、获取长度、插入元素和删除元素的功能,实现了链式线性表的基本操作。