链表节点插入练习题 题目链接
#include <stdio.h>
#include <cstdlib>
using namespace std;
typedef int ElementType;
typedef struct node
{
ElementType x, y;
struct node * next;
} Lnode;
typedef Lnode * List;
void PrintL(List head)
{
List p = head;
int n = 0;
if(p)
{
while(p->next)
{
if(++n % 10 == 0)
printf("%d/%d\n", p->x, p->y);
else
printf("%d/%d\t", p->x, p->y);
p = p->next;
}
printf("%d/%d\n", p->x, p->y);
}
else
{
printf("This list is empty!\n");
}
}
int main()
{
int n;
scanf("%d", &n);
List head = (List) malloc( sizeof(Lnode) );
head->x = 0;
head->y = 1;
List rear = (List) malloc( sizeof(Lnode) );
head->next = rear;
rear->next = NULL;
rear->x = rear->y = 1;
int w = n-1;
while(w--)
{
List p = head;
while(p->next)
{
if(p->y + p->next->y <= n)
{
List q = (List) malloc( sizeof(Lnode) );
q->next = p->next;
q->x = p->x + p->next->x;
q->y = p->y + p->next->y;
p->next = q;
p = q->next;
continue;
}
p = p->next;
}
}
PrintL(head);
return 0;
}

本文深入探讨了链表节点插入算法的实现细节,通过一个具体的C语言代码示例,展示了如何在链表中根据特定条件插入新节点,并保持链表的有序性和有效性。文章详细解释了代码的运行流程,包括初始化链表、节点创建、插入逻辑以及最终的链表打印,为读者提供了一个理解链表操作的良好实践案例。

被折叠的 条评论
为什么被折叠?



