以C结构体或C++的“类”代替“第2章中复杂数据类型”,实现“链式线性表”,编写下面6个接口函数:CreateList、ListPrint、GetElem、ListLength、ListInsert、ListDelete
时间: 2023-09-26 20:06:37 浏览: 72
### 回答1:
这里提供一种基于C结构体的链式线性表的实现方法:
```
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType; // 定义元素类型
typedef struct Node Node; // 前向声明结构体
struct Node {
ElementType data; // 数据域
Node *next; // 指针域,指向下一个结点
};
// 创建链表,返回头结点的指针
Node* CreateList()
{
Node *head = malloc(sizeof(Node)); // 创建头结点
head->next = NULL; // 头结点的指针域为空
return head;
}
// 打印链表
void ListPrint(Node *head)
{
Node *p = head->next; // 从第一个结点开始打印
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 获取第i个元素的值,返回-1表示索引非法
ElementType GetElem(Node *head, int i)
{
if (i < 1) {
return -1; // 索引非法
}
Node *p = head->next; // 从第一个结点开始查找
int j = 1;
while (p != NULL && j != i) {
p = p->next;
j++;
}
if (p == NULL) {
return -1; // 索引非法
}
return p->data;
}
// 获取链表长度
int ListLength(Node *head)
{
int length = 0;
Node *p = head->next; // 从第一个结点开始计算
while (p != NULL) {
length++;
p = p->next;
}
return length;
}
// 在第i个位置插入元素e,返回0表示成功,返回-1表示索引非法
int ListInsert(Node *head, int i, ElementType e)
{
if (i < 1) {
return -1; // 索引非法
}
Node *p = head; // 从头结点开始查找
int j = 0;
while (p != NULL && j != i - 1) {
p = p->next;
j++;
}
if (p == NULL) {
return -1; // 索引非法
}
Node *newNode = malloc(sizeof(Node)); // 创建新结点
newNode->data = e;
newNode->next = p->next;
p->next = newNode; // 插入新结点
return 0;
}
// 删除第i个元素,返回0表示成功,返回-1表示索引非法
int ListDelete(Node *head, int i)
{
if (i < 1) {
return -1; // 索引非法
}
Node *p = head; // 从头结点开始查找
int j = 0;
while (p->next != NULL && j != i - 1) {
p = p->next;
j++;
}
if (p->next == NULL) {
return -1; // 索引非法
}
Node *temp = p->next; // 记录要删除的结点
p->next = temp->next; // 删除结点
free(temp);
return 0;
}
// 测试
int main()
{
Node *head = CreateList(); // 创建链表
ListInsert(head, 1, 1);
ListInsert(head, 2, 3);
ListInsert(head, 3, 5);
ListInsert(head, 2, 2);
ListInsert(head, 4, 4);
printf("链表中的元素:");
ListPrint(head); // 打印链表
printf("第3个元素的值:%d\n", GetElem(head, 3)); // 获取元素值
printf("链表的长度:%d\n", ListLength(head)); // 获取链表长度
ListDelete(head, 2); // 删除元素
printf("链表中的元素:");
ListPrint(head); // 打印链表
return 0;
}
```
输出结果如下:
```
链表中的元素:1 2 3 4 5
第3个元素的值:3
链表的长度:5
链表中的元素:1 3 4 5
```
### 回答2:
链式线性表可以使用C结构体或C的“类”来实现。下面是6个接口函数的实现:
1. CreateList:创建链式线性表
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct List {
Node *head;
} List;
List* CreateList() {
List *list = (List*) malloc(sizeof(List));
list->head = NULL;
return list;
}
```
2. ListPrint:打印链式线性表
```c
void ListPrint(List *list) {
Node *current = list->head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
```
3. GetElem:获得指定位置的元素值
```c
int GetElem(List *list, int index) {
Node *current = list->head;
int i = 0;
while (current != NULL && i < index) {
current = current->next;
i++;
}
if (current == NULL) {
printf("Index out of range.\n");
return -1;
}
return current->data;
}
```
4. ListLength:获取链式线性表的长度
```c
int ListLength(List *list) {
int length = 0;
Node *current = list->head;
while (current != NULL) {
current = current->next;
length++;
}
return length;
}
```
5. ListInsert:在指定位置插入元素
```c
void ListInsert(List *list, int index, int data) {
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (index == 0) {
newNode->next = list->head;
list->head = newNode;
return;
}
Node *current = list->head;
int i = 0;
while (current != NULL && i < index - 1) {
current = current->next;
i++;
}
if (current == NULL) {
printf("Index out of range.\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}
```
6. ListDelete:删除指定位置的元素
```c
void ListDelete(List *list, int index) {
if (list->head == NULL) {
printf("Empty list.\n");
return;
}
Node *current = list->head;
if (index == 0) {
list->head = current->next;
free(current);
return;
}
int i = 0;
while (current != NULL && i < index - 1) {
current = current->next;
i++;
}
if (current == NULL || current->next == NULL) {
printf("Index out of range.\n");
return;
}
Node *nextNode = current->next->next;
free(current->next);
current->next = nextNode;
}
```
### 回答3:
链式线性表是一种基于链表结构的数据类型,可以用C语言的结构体或者C++的类来实现。下面是使用C语言的结构体来代替第2章中的复杂数据类型,并实现链式线性表的六个接口函数:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct ListNode {
int data; // 数据域
struct ListNode* next; // 指针域,指向下一个节点
} ListNode;
// 创建链表
ListNode* CreateList() {
ListNode* head = NULL; // 头指针初始化为空
ListNode* tail = NULL; // 尾指针初始化为空
// 利用循环动态生成链表节点
int num;
printf("请输入数据,输入-1结束:");
scanf("%d", &num);
while (num != -1) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = num;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
scanf("%d", &num);
}
return head;
}
// 打印链表
void ListPrint(ListNode* head) {
ListNode* p = head;
printf("链表的数据为:");
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 获取指定位置的元素
int GetElem(ListNode* head, int index) {
ListNode* p = head;
int i = 0;
while (p != NULL && i < index) {
p = p->next;
i++;
}
if (p == NULL) {
printf("指定位置不存在元素\n");
return -1;
}
return p->data;
}
// 获取链表长度
int ListLength(ListNode* head) {
ListNode* p = head;
int length = 0;
while (p != NULL) {
length++;
p = p->next;
}
return length;
}
// 在指定位置插入元素
void ListInsert(ListNode** head, int index, int data) {
ListNode* p = *head;
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = data;
newNode->next = NULL;
if (index == 0) { // 在表头插入
newNode->next = *head;
*head = newNode;
} else {
int i = 0;
while (p != NULL && i < index - 1) { // 找到要插入位置的前一个节点
p = p->next;
i++;
}
if (p == NULL) {
printf("插入位置无效\n");
return;
}
newNode->next = p->next;
p->next = newNode;
}
}
// 删除指定位置的元素
void ListDelete(ListNode** head, int index) {
ListNode* p = *head;
if (index == 0) { // 删除表头元素
*head = (*head)->next;
free(p);
} else {
int i = 0;
while (p != NULL && i < index - 1) { // 找到要删除位置的前一个节点
p = p->next;
i++;
}
if (p == NULL || p->next == NULL) {
printf("删除位置无效\n");
return;
}
ListNode* deleteNode = p->next;
p->next = deleteNode->next;
free(deleteNode);
}
}
int main() {
ListNode* head = CreateList();
ListPrint(head);
printf("链表长度为:%d\n", ListLength(head));
printf("第2个位置的元素为:%d\n", GetElem(head, 1));
printf("插入元素后:\n");
ListInsert(&head, 2, 100);
ListPrint(head);
printf("删除元素后:\n");
ListDelete(&head, 1);
ListPrint(head);
return 0;
}
```
以上是基于C语言结构体来实现链式线性表的六个接口函数。`CreateList`函数用于创建链表,`ListPrint`函数用于打印链表,`GetElem`函数用于获取指定位置的元素,`ListLength`函数用于获取链表的长度,`ListInsert`函数用于在指定位置插入元素,`ListDelete`函数用于删除指定位置的元素。