第1关:建单向链表
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}Node;
// 请在此添加你的代码
/********** Begin **********/
Node *CreatList()
{
Node *head,*newone,*end;
int n;int i;int T=1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
newone=(Node*)malloc(sizeof(Node));
if(T==1)
{
head=newone;
T--;
}
else end->next=newone;
end=newone;
end->next=NULL;
scanf("%d",&end->data);
}
return head;
}
void ShowList(Node *head)
{
Node *end=head;
for(;end!=NULL;end=end->next)
{
printf("%d ",end->data);
}
}
/********** End **********/
int main(void)
{
Node *phead;
phead = CreatList();
ShowList(phead);
return 0;
}
第2关:统计单链表中的节点数
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}Node;
// 请在此添加你的代码
/********** Begin *********/
Node *CreatList()
{
Node *head,*newone,*end;
int n;int i;int T=1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
newone=(Node*)malloc(sizeof(Node));
if(T==1)
{
head=newone;
T--;
}
else end->next=newone;
end=newone;
end->next=NULL;
scanf("%d",&end->data);
}
return head;
}
void ShowList(Node *head)
{
Node *end=head;
for(;end!=NULL;end=end->next)
{
printf("%d ",end->data);
}
printf("\n");
}
int Length(Node *phead)
{
int a=0;
for(;phead!=NULL;phead=phead->next)
{
a=a+1;
}
return a;
}
/********** End **********/
int main(void)
{
Node *phead;
phead = CreatList();
ShowList(phead);
printf("%d", Length(phead));
return 0;
}
第3关:单链表逆置
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}Node;
// 请在此添加你的代码
/********** Begin *********/
Node *CreatList()
{
Node *head,*newone,*end;
int n;int i;int T=1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
newone=(Node*)malloc(sizeof(Node));
if(T==1)
{
head=newone;
T--;
}
else end->next=newone;
end=newone;
end->next=NULL;
scanf("%d",&end->data);
}
return head;
}
void ShowList(Node *head)
{
Node *end=head;
for(;end!=NULL;end=end->next)
{
printf("%d ",end->data);
}
printf("\n");
}
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)
{
Node *phead;
phead = CreatList();
printf("链表逆置前的数据:\n");
ShowList(phead);
phead = ReverseList(phead);
printf("链表逆置后的数据:\n");
ShowList(phead);
return 0;
}