1. 以C结构体或C++的“类”代替“第2章中复杂数据类型”,实现“链式线性表”,编写下面6个接口函数:CreateList、ListPrint、GetElem、ListLength、ListInsert、ListDelete
时间: 2023-05-28 12:07:09 浏览: 98
基于C++实现(控制台)实现链表的插入、删除、打印等操作的函数(线程安全型数据结构)【100012475】
1. C结构体实现链式线性表
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int data;
struct ListNode* next;
} ListNode;
typedef struct LinkedList {
ListNode* head;
int length;
} LinkedList;
```
2. CreateList
```c
LinkedList* CreateList() {
LinkedList* L = (LinkedList*)malloc(sizeof(LinkedList));
L->head = NULL;
L->length = 0;
return L;
}
```
3. ListPrint
```c
void ListPrint(LinkedList* L) {
ListNode* p = L->head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
```
4. GetElem
```c
int GetElem(LinkedList* L, int index) {
if (index < 1 || index > L->length) {
printf("Error: Index out of range.\n");
return -1;
}
ListNode* p = L->head;
int i = 1;
while (i < index) {
p = p->next;
i++;
}
return p->data;
}
```
5. ListLength
```c
int ListLength(LinkedList* L) {
return L->length;
}
```
6. ListInsert
```c
void ListInsert(LinkedList* L, int index, int value) {
if (index < 1 || index > L->length + 1) {
printf("Error: Index out of range.\n");
return;
}
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = value;
newNode->next = NULL;
if (index == 1) {
newNode->next = L->head;
L->head = newNode;
} else {
ListNode* p = L->head;
int i = 1;
while (i < index - 1) {
p = p->next;
i++;
}
newNode->next = p->next;
p->next = newNode;
}
L->length++;
}
```
7. ListDelete
```c
void ListDelete(LinkedList* L, int index) {
if (index < 1 || index > L->length) {
printf("Error: Index out of range.\n");
return;
}
if (index == 1) {
ListNode* p = L->head;
L->head = L->head->next;
free(p);
} else {
ListNode* p = L->head;
int i = 1;
while (i < index - 1) {
p = p->next;
i++;
}
ListNode* q = p->next;
p->next = q->next;
free(q);
}
L->length--;
}
```
完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int data;
struct ListNode* next;
} ListNode;
typedef struct LinkedList {
ListNode* head;
int length;
} LinkedList;
LinkedList* CreateList() {
LinkedList* L = (LinkedList*)malloc(sizeof(LinkedList));
L->head = NULL;
L->length = 0;
return L;
}
void ListPrint(LinkedList* L) {
ListNode* p = L->head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int GetElem(LinkedList* L, int index) {
if (index < 1 || index > L->length) {
printf("Error: Index out of range.\n");
return -1;
}
ListNode* p = L->head;
int i = 1;
while (i < index) {
p = p->next;
i++;
}
return p->data;
}
int ListLength(LinkedList* L) {
return L->length;
}
void ListInsert(LinkedList* L, int index, int value) {
if (index < 1 || index > L->length + 1) {
printf("Error: Index out of range.\n");
return;
}
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = value;
newNode->next = NULL;
if (index == 1) {
newNode->next = L->head;
L->head = newNode;
} else {
ListNode* p = L->head;
int i = 1;
while (i < index - 1) {
p = p->next;
i++;
}
newNode->next = p->next;
p->next = newNode;
}
L->length++;
}
void ListDelete(LinkedList* L, int index) {
if (index < 1 || index > L->length) {
printf("Error: Index out of range.\n");
return;
}
if (index == 1) {
ListNode* p = L->head;
L->head = L->head->next;
free(p);
} else {
ListNode* p = L->head;
int i = 1;
while (i < index - 1) {
p = p->next;
i++;
}
ListNode* q = p->next;
p->next = q->next;
free(q);
}
L->length--;
}
int main() {
LinkedList* L = CreateList();
ListInsert(L, 1, 1);
ListInsert(L, 2, 2);
ListInsert(L, 3, 3);
ListInsert(L, 4, 4);
ListInsert(L, 5, 5);
printf("List: ");
ListPrint(L);
printf("Length: %d\n", ListLength(L));
printf("Element at index 3: %d\n", GetElem(L, 3));
ListDelete(L, 3);
printf("After deleting element at index 3: ");
ListPrint(L);
printf("Length: %d\n", ListLength(L));
return 0;
}
```
阅读全文