第1关:建单向链表
编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:
键盘输入一组元素,建立一个带头结点的单向链表(无序)。
要求:
输入整数的长度以及整数;
输出无序的单向链表。
效果如下:
输入:
5
1 23 4 8 9
输出:1 23 4 8 9。
代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}Node;
// 请在此添加你的代码
/********** Begin **********/
Node *CreatList()
{
int val,i,n;
Node *phead,*p,*q;
phead=(Node *)malloc(sizeof(Node));
q=phead;
q->next=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&val);
p=(Node *)malloc(sizeof(Node));
p->data=val;
q->next=p;
p->next=NULL;
q=p;
}
return phead;
}
void ShowList(Node *phead)
{
Node *p;
p=phead->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
/********** End **********/
int main(void)
{
Node *phead;
phead = CreatList();
ShowList(phead);
return 0;
}
第2关:统计单链表中的节点数
编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:
编写程序,从键盘输入一串整数以及整数的个数,以单链表形式存储起来,计算单链表中结点的个数,输出单链表的数据及结点的个数。
效果如下:
输入:
8
12367802
输出:
12367802
8
测试说明
平台会对您的代码进行运行测试,如果实际输出与预期输出相同,则算通关。
代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}Node;
// 请在此添加你的代码
/********** Begin *********/
Node * CreatList()
{
Node *phead = (Node *)malloc(sizeof(Node));
phead->next=NULL;
return phead;
}
Node * CreatNode(Node * phead)
{
int n;
scanf("%d",&n);
while(n--){
Node * newPoint = (Node *)malloc(sizeof(Node));
if(newPoint == 0)
{
printf("内存分配错误\n");
break;
}
scanf("%d", &newPoint->data);
phead->next = newPoint;
phead = newPoint;
}
phead -> next = NULL;
return 0;
}
void ShowList(struct node * headNode)
{
struct node * pMove = headNode->next;
while(pMove)
{
printf("%d ",pMove->data);
pMove=pMove->next;
}
}
int Length(Node * phead)
{
int i=0;
struct node * pMove = phead->next;
while(pMove)
{
i++;
pMove=pMove->next;
}
return i;
}
/********** End **********/
int main(void)
{
Node *phead;
phead = CreatList();
CreatNode(phead);
ShowList(phead);
printf("\n%d", Length(phead));
return 0;
}
第3关:单链表逆置
编程要求
按程序提示输入并创建一个单链表,带有头结点;
可自定义链表的长度,可自定义链表储存的数据类型,注意更改相应的输入输出方式;
实现单链表的逆置,直观地输出结果。
效果如下:
输入:
6
1 212 7 8 0 2
输出:
链表逆置前的数据:
1 212 7 8 0 2
链表逆置后的数据:
2 0 8 7 212 1
测试说明
平台会对您的代码进行运行测试,如果实际输出与预期输出相同,则算通关。
代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}Node;
// 请在此添加你的代码
/********** Begin *********/
Node * CreatList()
{
Node *phead = (Node *)malloc(sizeof(Node));
phead->next=NULL;
return phead;
}
Node * CreatNode(Node * phead,int n)
{
while(n--){
Node * newPoint = (Node *)malloc(sizeof(Node));
if(newPoint == 0)
{
printf("内存分配错误\n");
break;
}
scanf("%d", &newPoint->data);
phead->next = newPoint;
phead = newPoint;
}
phead -> next = NULL;
return 0;
}
void ShowList(struct node * headNode)
{
struct node * pMove = headNode ->next;
while(pMove)
{
printf("%d ",pMove->data);
pMove=pMove->next;
}
}
void ShowList2(struct node * headNode,int n)
{
struct node * pMove = headNode;
while(pMove != NULL && n--)
{
printf("%d ",pMove->data);
pMove=pMove->next;
}
}
Node* ReverseList(Node *head)
{
Node *newHead;
if(head==NULL||head->next==NULL)
return head;
newHead=ReverseList(head->next);
head->next->next=head;
head->next=NULL;
return newHead;
}
/********** End **********/
int main(void)
{
int n;
scanf("%d",&n);
Node *phead;
phead = CreatList();
CreatNode(phead,n);
printf("链表逆置前的数据:\n");
ShowList(phead);
phead = ReverseList(phead);
printf("\n链表逆置后的数据:\n");
ShowList2(phead,n);
return 0;
}