void printList(Node *head) { Node *p = head; int pos = 1; do { printf("位置: %d, 名字: %s, 编号: %d\n", pos++, p->name, p->id); p = p->next; } while (p != head); }帮我优化一下
时间: 2024-01-10 08:03:07 浏览: 33
这段代码已经很简洁和高效了,不过有一些微小的优化可以进行:
1. 将循环条件改为 while 循环,这样可以避免刚进入循环时不必要的一次输出。
2. 将 printf 函数中的 pos++ 改为 ++pos,这样可以少用一个变量。
优化后的代码如下:
```c
void printList(Node *head) {
Node *p = head;
int pos = 0;
while (p != head || pos == 0) {
printf("位置: %d, 名字: %s, 编号: %d\n", ++pos, p->name, p->id);
p = p->next;
}
}
```
这个优化后的代码和原来的代码功能是完全相同的,但更加简洁和易读。
相关问题
以C结构体或C++的“类”代替“第2章中复杂数据类型”,实现“链式线性表”,编写下面6个接口函数:CreateList、ListPrint、GetElem、ListLength、ListInsert、ListDelete完整代码
### 回答1:
以下是使用C结构体实现链式线性表的完整代码:
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct ListNode {
int data;
struct ListNode* next;
} ListNode;
// 创建链表
ListNode* CreateList(int n) {
ListNode* head = NULL;
ListNode* tail = NULL;
int i, val;
for (i = 1; i <= n; i++) {
printf("请输入第%d个节点的值:", i);
scanf("%d", &val);
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = val;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
// 打印链表
void ListPrint(ListNode* head) {
ListNode* p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 获取链表中指定位置的节点值
int GetElem(ListNode* head, int i) {
int j = 1;
ListNode* p = head;
while (p != NULL && j < i) {
p = p->next;
j++;
}
if (p == NULL || j > i) {
printf("获取节点值失败\n");
return -1;
}
return p->data;
}
// 获取链表长度
int ListLength(ListNode* head) {
int len = 0;
ListNode* p = head;
while (p != NULL) {
len++;
p = p->next;
}
return len;
}
// 在链表中指定位置插入节点
ListNode* ListInsert(ListNode* head, int i, int val) {
int j = 1;
ListNode* p = head;
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = val;
node->next = NULL;
if (i == 1) {
node->next = head;
head = node;
return head;
}
while (p != NULL && j < i - 1) {
p = p->next;
j++;
}
if (p == NULL || j > i - 1) {
printf("插入节点失败\n");
return head;
}
node->next = p->next;
p->next = node;
return head;
}
// 在链表中删除指定位置的节点
ListNode* ListDelete(ListNode* head, int i) {
int j = 1;
ListNode* p = head;
ListNode* q = NULL;
if (i == 1) {
head = head->next;
free(p);
return head;
}
while (p != NULL && j < i) {
q = p;
p = p->next;
j++;
}
if (p == NULL || j > i) {
printf("删除节点失败\n");
return head;
}
q->next = p->next;
free(p);
return head;
}
int main() {
int n, i, val;
ListNode* head = NULL;
printf("请输入链表长度:");
scanf("%d", &n);
head = CreateList(n);
printf("创建的链表为:");
ListPrint(head);
printf("请输入要获取节点值的位置:");
scanf("%d", &i);
val = GetElem(head, i);
printf("第%d个节点的值为:%d\n", i, val);
printf("链表的长度为:%d\n", ListLength(head));
printf("请输入要在链表中插入节点的位置和值:");
scanf("%d %d", &i, &val);
head = ListInsert(head, i, val);
printf("插入节点后的链表为:");
ListPrint(head);
printf("请输入要删除节点的位置:");
scanf("%d", &i);
head = ListDelete(head, i);
printf("删除节点后的链表为:");
ListPrint(head);
return 0;
}
### 回答2:
下面是使用C结构体实现链式线性表的完整代码:
```c
#include<stdio.h>
#include<stdlib.h>
// 定义节点结构体
struct ListNode {
int data; // 数据域
struct ListNode* next; // 指针域
};
// 创建链式线性表
struct ListNode* CreateList(int num) {
struct ListNode* head = NULL;
// 创建头节点
head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next = NULL;
struct ListNode* curNode = head;
int i;
for (i = 1; i <= num; i++) {
// 创建新节点
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->data = i * 10;
newNode->next = NULL;
curNode->next = newNode;
curNode = curNode->next;
}
return head;
}
// 打印链式线性表
void ListPrint(struct ListNode* head) {
if (head == NULL) {
printf("链表为空\n");
return;
}
struct ListNode* curNode = head->next;
while (curNode != NULL) {
printf("%d ", curNode->data);
curNode = curNode->next;
}
printf("\n");
}
// 获取链式线性表指定位置的元素
int GetElem(struct ListNode* head, int pos) {
if (head == NULL || head->next == NULL) {
printf("链表为空\n");
return -1;
}
struct ListNode* curNode = head->next;
int i = 1;
while (curNode != NULL && i < pos) {
curNode = curNode->next;
i++;
}
if (curNode == NULL) {
printf("位置无效\n");
return -1;
}
return curNode->data;
}
// 获取链式线性表的长度
int ListLength(struct ListNode* head) {
if (head == NULL || head->next == NULL) {
printf("链表为空\n");
return 0;
}
int length = 0;
struct ListNode* curNode = head->next;
while (curNode != NULL) {
length++;
curNode = curNode->next;
}
return length;
}
// 在链式线性表指定位置插入元素
void ListInsert(struct ListNode* head, int pos, int data) {
if (head == NULL || head->next == NULL) {
printf("链表为空\n");
return;
}
struct ListNode* curNode = head;
int i = 0;
while (curNode != NULL && i < pos - 1) {
curNode = curNode->next;
i++;
}
if (curNode == NULL) {
printf("位置无效\n");
return;
}
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->data = data;
newNode->next = curNode->next;
curNode->next = newNode;
}
// 在链式线性表指定位置删除元素
void ListDelete(struct ListNode* head, int pos) {
if (head == NULL || head->next == NULL) {
printf("链表为空\n");
return;
}
struct ListNode* curNode = head;
int i = 0;
while (curNode->next != NULL && i < pos - 1) {
curNode = curNode->next;
i++;
}
if (curNode->next == NULL) {
printf("位置无效\n");
return;
}
struct ListNode* delNode = curNode->next;
curNode->next = delNode->next;
free(delNode);
}
int main() {
struct ListNode* myList = CreateList(5);
printf("链表内容:");
ListPrint(myList);
printf("链表长度:%d\n", ListLength(myList));
printf("第2个位置元素:%d\n", GetElem(myList, 2));
printf("在第3个位置插入元素20后的链表:");
ListInsert(myList, 3, 20);
ListPrint(myList);
printf("删除第4个位置的元素后的链表:");
ListDelete(myList, 4);
ListPrint(myList);
return 0;
}
```
这段代码中,我们使用了一个结构体`ListNode`来定义链表节点,节点包含一个`data`存储数据,和一个`next`指针指向下一个节点。接口函数`CreateList`用于创建一个包含指定数量节点的链表,接口函数`ListPrint`用于打印链表内容,接口函数`GetElem`用于获取链表指定位置的元素,接口函数`ListLength`用于获取链表的长度,接口函数`ListInsert`用于在链表指定位置插入元素,接口函数`ListDelete`用于删除链表指定位置的元素。在主函数中,我们创建一个包含5个节点的链表,并展示了这些接口函数的使用方法。
### 回答3:
下面是使用C结构体实现链式线性表的完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表结点结构体
typedef struct Node {
int data; // 数据域
struct Node* next; // 指针域
} Node;
// 创建链表
Node* CreateList(int n) {
Node* head = NULL;
Node* tail = NULL;
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;
} else {
tail->next = newNode;
}
tail = newNode;
}
return head;
}
// 打印链表
void ListPrint(Node* head) {
if (head == NULL) {
printf("链表为空!\n");
return;
}
Node* p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 获取第index个元素的值
int GetElem(Node* head, int index) {
if (head == NULL || index < 1) {
printf("链表为空或索引错误!\n");
return -1;
}
Node* p = head;
int count = 1;
while (p != NULL && count < index) {
p = p->next;
count++;
}
if (p == NULL) {
printf("索引超出链表长度!\n");
return -1;
}
return p->data;
}
// 获取链表的长度
int ListLength(Node* head) {
int len = 0;
Node* p = head;
while (p != NULL) {
p = p->next;
len++;
}
return len;
}
// 在第index个位置插入元素
void ListInsert(Node** head, int index, int value) {
if (index < 1 || index > ListLength(*head) + 1) {
printf("插入位置错误!\n");
return;
}
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (index == 1) {
newNode->next = *head;
*head = newNode;
} else {
Node* p = *head;
int count = 1;
while (p != NULL && count < index - 1) {
p = p->next;
count++;
}
newNode->next = p->next;
p->next = newNode;
}
}
// 删除第index个元素
void ListDelete(Node** head, int index) {
if (index < 1 || index > ListLength(*head)) {
printf("删除位置错误!\n");
return;
}
Node* p = *head;
if (index == 1) {
*head = p->next;
free(p);
} else {
int count = 1;
Node* q = p->next;
while (q != NULL && count < index - 1) {
p = p->next;
q = q->next;
count++;
}
p->next = q->next;
free(q);
}
}
int main() {
int n; // 链表长度
printf("请输入链表的长度:");
scanf("%d", &n);
Node* head = CreateList(n);
printf("创建的链表为:");
ListPrint(head);
int index;
printf("请输入要获取的元素的位置:");
scanf("%d", &index);
int value = GetElem(head, index);
printf("获取的元素值为:%d\n", value);
int length = ListLength(head);
printf("链表的长度为:%d\n", length);
int insertIndex, insertValue;
printf("请输入要插入的位置和值:");
scanf("%d%d", &insertIndex, &insertValue);
ListInsert(&head, insertIndex, insertValue);
printf("插入后的链表为:");
ListPrint(head);
int deleteIndex;
printf("请输入要删除的位置:");
scanf("%d", &deleteIndex);
ListDelete(&head, deleteIndex);
printf("删除后的链表为:");
ListPrint(head);
return 0;
}
```
注意:该代码使用了指针传递来修改链表的头指针,函数参数为`Node** head`。同时,在插入和删除操作中,需要对头指针进行特殊处理。
用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;
}
```