C语言编程怎么设计?
C代码如下:
运行结果如下:
输出符合题意,望采纳~
附源码:
#include <stdio.h>
#include <malloc.h>
typedef struct _LinkNode { // 链表结点
int val;
struct _LinkNode *next;
} LinkNode;
LinkNode *initNode(int x) { // 新建一个链表结点
LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));
node->val = x;
node->next = NULL;
return node;
}
void printLink(LinkNode *head) { // 打印链表
if (!head) return;
printf("%d", head->val);
head = head->next;
while (head != NULL) {
printf("->%d", head->val);
head = head->next;
}
printf("\n");
}
int main() {
int num, x;
LinkNode *dummyHead, *curNode;
dummyHead = initNode(0); // 伪头结点
curNode = dummyHead;
while (scanf("%d", &num)) { // 输入一组整数存储在链表
curNode->next = initNode(num);
curNode = curNode->next;
if (getchar() == '\n') // 换行终止输入
break;
}
printf("生成的链表为:");
printLink(dummyHead->next);
scanf("%d", &x); // 读入单独的x
num = 0;
curNode = dummyHead->next;
while (curNode != NULL) {
if (curNode->val <= x)
num++;
curNode = curNode->next;
}
printf("不大于%d的结点个数为%d\n", x, num);
return 0;
}