int createList(Node **head); void printList(Node *head); Node *searchNode(Node *head, int id); Node *deleteNode(Node *head, int pos);
时间: 2023-07-23 10:08:31 浏览: 41
这是一个 C 语言中的链表操作函数声明,具体说明如下:
- `int createList(Node **head);`:创建一个链表,并将头结点的地址存储在 head 指针所指向的地址中。返回值为链表长度。
- `void printList(Node *head);`:遍历链表,输出链表中所有节点的值。
- `Node *searchNode(Node *head, int id);`:在链表中查找节点值为 id 的节点,并返回该节点的地址。若未找到,返回 NULL。
- `Node *deleteNode(Node *head, int pos);`:删除链表中位置为 pos 的节点,并返回删除节点后的链表头节点地址。若 pos 位置不存在,返回原链表头节点地址。
需要注意的是,这里的 Node 是一个结构体类型,包含一个整型变量和一个指向下一节点的指针,具体实现可以参考以下代码:
```c
typedef struct Node {
int val;
struct Node *next;
} Node;
```
相关问题
用C语言再写一个读入数据生成链表并处理的程序:⚫从上述input.ifo文件读入人员信息,正向建立线性表,使用循环链表方式存储;⚫输出线性表各结点的值和在线性表的位置值到屏幕;⚫键盘输入编号值S,在线性表中查找到编号为S的结点并输出结点的值,包括名字和随机编号(需要测试查找到和查找不到两种情况);⚫键盘输入位置值M,从线性表中1号结点开始向后报数,将报M的结点从线性表中删除,并输出该结点的编号和初始位置;(提示:可以将按位置删除函数改造成一个新的删除函数,删除的同时返回指向下一个结点的指针值、被删除结点的值;初始位置的值,可以考虑在建立循环链表时保存到结点内)⚫从刚才被删除人的下一个人开始重复上述步骤,直至最后只剩下一个人为止;⚫输出最后剩余一个人的编号和在线性表的初始位置。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char name[20];
int id;
struct Node *next;
} Node;
int createList(Node **head);
void printList(Node *head);
Node *searchNode(Node *head, int id);
Node *deleteNode(Node *head, int pos);
int main() {
Node *head = NULL;
int s, m;
int len = createList(&head);
printf("The list is:\n");
printList(head);
printf("Please input a id to search: ");
scanf("%d", &s);
Node *node = searchNode(head, s);
if (node == NULL) {
printf("Can't find the node.\n");
} else {
printf("The node is: %s, %d\n", node->name, node->id);
}
printf("Please input a position to delete: ");
scanf("%d", &m);
while (len > 1) {
head = deleteNode(head, m);
len--;
printf("Deleted node is: %s, %d, initial pos: %d\n", head->name, head->id, m);
printf("The list is:\n");
printList(head);
printf("Please input a position to delete: ");
scanf("%d", &m);
}
printf("The last node is: %s, %d, initial pos: %d\n", head->name, head->id, 1);
free(head);
return 0;
}
int createList(Node **head) {
FILE *fp;
if ((fp = fopen("input.info", "r")) == NULL) {
printf("Can't open file.\n");
exit(1);
}
Node *p = (Node *)malloc(sizeof(Node));
*head = p;
int len = 0;
while (fscanf(fp, "%s %d", p->name, &p->id) != EOF) {
len++;
p->next = (Node *)malloc(sizeof(Node));
p = p->next;
}
free(p);
fclose(fp);
p->next = *head; // make it a circular linked list
return len;
}
void printList(Node *head) {
Node *p = head;
int pos = 1;
do {
printf("Pos: %d, Name: %s, ID: %d\n", pos++, p->name, p->id);
p = p->next;
} while (p != head);
}
Node *searchNode(Node *head, int id) {
Node *p = head;
do {
if (p->id == id) {
return p;
}
p = p->next;
} while (p != head);
return NULL;
}
Node *deleteNode(Node *head, int pos) {
Node *p = head;
Node *prev = NULL;
int i = 1;
while (i < pos) {
prev = p;
p = p->next;
i++;
}
if (p == head) {
head = p->next;
}
prev->next = p->next;
free(p);
return prev->next;
}
```
设计一组整数,完成链表(带表头结点)的基本操作。建立链表、插入、删除、查找、输出、求前驱、求后继、两个有序链表的合并操作。 其他基本操作还有销毁链表、将链表置为空表、求链表的长度、获取某位置结点的内容、搜索结点、销毁等。
链表基本操作的实现:
1. 建立链表
```c++
typedef struct node {
int data;
struct node *next;
}Node;
Node *createList() {
Node *head = (Node *)malloc(sizeof(Node));
head->next = NULL;
return head;
}
```
2. 插入
```c++
void insert(Node *head, int data) {
Node *p = (Node *)malloc(sizeof(Node));
p->data = data;
p->next = head->next;
head->next = p;
}
```
3. 删除
```c++
void remove(Node *head, int data) {
Node *p = head->next;
Node *pre = head;
while (p != NULL) {
if (p->data == data) {
pre->next = p->next;
free(p);
break;
}
pre = p;
p = p->next;
}
}
```
4. 查找
```c++
Node *find(Node *head, int data) {
Node *p = head->next;
while (p != NULL) {
if (p->data == data) {
return p;
}
p = p->next;
}
return NULL;
}
```
5. 输出
```c++
void printList(Node *head) {
Node *p = head->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
```
6. 求前驱
```c++
Node *getPre(Node *head, int data) {
Node *p = head->next;
Node *pre = head;
while (p != NULL) {
if (p->data == data) {
return pre;
}
pre = p;
p = p->next;
}
return NULL;
}
```
7. 求后继
```c++
Node *getNext(Node *head, int data) {
Node *p = head->next;
while (p != NULL && p->data != data) {
p = p->next;
}
if (p != NULL) {
return p->next;
}
return NULL;
}
```
8. 合并有序链表
```c++
Node *mergeList(Node *head1, Node *head2) {
Node *p1 = head1->next;
Node *p2 = head2->next;
Node *head = createList();
Node *p = head;
while (p1 != NULL && p2 != NULL) {
if (p1->data < p2->data) {
p->next = p1;
p1 = p1->next;
} else {
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
if (p1 != NULL) {
p->next = p1;
}
if (p2 != NULL) {
p->next = p2;
}
return head;
}
```
9. 销毁链表
```c++
void destroyList(Node *head) {
Node *p = head->next;
while (p != NULL) {
Node *tmp = p;
p = p->next;
free(tmp);
}
head->next = NULL;
}
```
10. 将链表置为空表
```c++
void clearList(Node *head) {
destroyList(head);
}
```
11. 求链表的长度
```c++
int length(Node *head) {
int len = 0;
Node *p = head->next;
while (p != NULL) {
len++;
p = p->next;
}
return len;
}
```
12. 获取某位置结点的内容
```c++
int get(Node *head, int index) {
Node *p = head->next;
int i = 0;
while (p != NULL && i < index) {
p = p->next;
i++;
}
if (p != NULL) {
return p->data;
}
return -1;
}
```
13. 搜索结点
```c++
bool search(Node *head, int data) {
Node *p = head->next;
while (p != NULL) {
if (p->data == data) {
return true;
}
p = p->next;
}
return false;
}
```
14. 销毁
```c++
void destroy(Node *head) {
free(head);
}
```