用c程序给出“链式线性表”的创建 Create、显示 Display、插 入 Insert、删除 Delete、合并 Merge 接口函数
时间: 2024-02-01 18:04:34 浏览: 67
链式线性表是一种数据结构,使用链表来实现。下面是用C语言实现链式线性表的创建、显示、插入、删除和合并的接口函数。
首先,需要定义链表的节点结构体:
```c
typedef struct node {
int data;
struct node *next;
} Node;
```
接下来,实现创建函数 Create。Create 函数接受一个整数数组和数组长度,返回一个指向链表头节点的指针。函数遍历整个数组,为每个数组元素创建一个新的节点,然后将节点按照数组元素顺序连接起来。
```c
Node *Create(int *arr, int length) {
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < length; i++) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = arr[i];
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
```
接下来,实现显示函数 Display。Display 函数接受一个指向链表头节点的指针,遍历整个链表,依次输出每个节点的数据。
```c
void Display(Node *head) {
Node *node = head;
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
```
接下来,实现插入函数 Insert。Insert 函数接受一个指向链表头节点的指针、要插入的位置和要插入的数据,将新节点插入到指定位置。
```c
void Insert(Node *head, int pos, int data) {
Node *node = head;
for (int i = 0; i < pos - 1; i++) {
node = node->next;
if (node == NULL) {
printf("Error: index out of range\n");
return;
}
}
Node *new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = node->next;
node->next = new_node;
}
```
接下来,实现删除函数 Delete。Delete 函数接受一个指向链表头节点的指针和要删除的位置,将指定位置的节点从链表中删除。
```c
void Delete(Node *head, int pos) {
Node *node = head;
for (int i = 0; i < pos - 1; i++) {
node = node->next;
if (node == NULL) {
printf("Error: index out of range\n");
return;
}
}
Node *delete_node = node->next;
node->next = delete_node->next;
free(delete_node);
}
```
最后,实现合并函数 Merge。Merge 函数接受两个指向链表头节点的指针,将第二个链表合并到第一个链表的末尾。
```c
void Merge(Node *head1, Node *head2) {
Node *node = head1;
while (node->next != NULL) {
node = node->next;
}
node->next = head2;
}
```
完整代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
Node *Create(int *arr, int length) {
Node *head = NULL;
Node *tail = NULL;
for (int i = 0; i < length; i++) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = arr[i];
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
void Display(Node *head) {
Node *node = head;
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
void Insert(Node *head, int pos, int data) {
Node *node = head;
for (int i = 0; i < pos - 1; i++) {
node = node->next;
if (node == NULL) {
printf("Error: index out of range\n");
return;
}
}
Node *new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = node->next;
node->next = new_node;
}
void Delete(Node *head, int pos) {
Node *node = head;
for (int i = 0; i < pos - 1; i++) {
node = node->next;
if (node == NULL) {
printf("Error: index out of range\n");
return;
}
}
Node *delete_node = node->next;
node->next = delete_node->next;
free(delete_node);
}
void Merge(Node *head1, Node *head2) {
Node *node = head1;
while (node->next != NULL) {
node = node->next;
}
node->next = head2;
}
int main() {
int arr1[] = {1, 2, 3};
int arr2[] = {4, 5, 6};
Node *head1 = Create(arr1, sizeof(arr1) / sizeof(int));
Node *head2 = Create(arr2, sizeof(arr2) / sizeof(int));
Display(head1);
Display(head2);
Merge(head1, head2);
Display(head1);
Insert(head1, 2, 7);
Display(head1);
Delete(head1, 4);
Display(head1);
return 0;
}
```
阅读全文