C语言实现:【问题描述】 用带头结点的单链表存储一帧数据,计算该帧数据的累加值作为校验码,倒序输出链表,并将校验码附加在数据帧尾部 【输入形式】 5 9 3 8 4 1 【输出形式】 1 4 8 3 9 5 25 6 1 4 8 3 9 25 【样例输入】 5 9 3 8 4 1 【样例输出】 1 4 8 3 9 5 25 6 1 4 8 3 9 25
时间: 2023-12-31 15:05:37 浏览: 63
好的,对于你的问题,这里提供一份C语言的实现代码,实现了倒序输出链表并将校验码附加在数据帧尾部的功能:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node, *LinkedList;
LinkedList createList(int n) {
LinkedList head = (LinkedList) malloc(sizeof(Node));
head->next = NULL;
LinkedList p = head;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
LinkedList newNode = (LinkedList) malloc(sizeof(Node));
newNode->data = x;
newNode->next = NULL;
p->next = newNode;
p = newNode;
}
return head;
}
int calcChecksum(LinkedList list) {
int sum = 0;
LinkedList p = list->next;
while (p != NULL) {
sum += p->data;
p = p->next;
}
return sum;
}
void appendChecksum(LinkedList list, int checksum) {
LinkedList p = list;
while (p->next != NULL) {
p = p->next;
}
LinkedList newNode = (LinkedList) malloc(sizeof(Node));
newNode->data = checksum;
newNode->next = NULL;
p->next = newNode;
}
void reversePrintList(LinkedList list) {
if (list->next != NULL) {
reversePrintList(list->next);
printf("%d ", list->next->data);
}
}
void printList(LinkedList list) {
LinkedList p = list->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
}
int main() {
int n, checksum;
scanf("%d", &n);
LinkedList list = createList(n);
checksum = calcChecksum(list);
appendChecksum(list, checksum);
printf("%d ", n + 1);
reversePrintList(list);
printf("%d\n", checksum);
return 0;
}
```
以上代码实现了根据输入的数据,创建带头结点的单链表,并计算该帧数据的累加值作为校验码。然后,将校验码附加在数据帧尾部,并倒序输出链表。希望可以帮助到你!
阅读全文