1. 程式人生 > >Problem D: 逆置鏈式鏈表(線性表)

Problem D: 逆置鏈式鏈表(線性表)

arp 鏈式存儲 out stdio.h tput div eat truct ask

Problem D: 逆置鏈式鏈表(線性表)

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 594 Solved: 346
[Submit][Status][Web Board]

Description

本題只需要提交填寫部分的代碼 (線性表)試編寫算法將線性表就地逆置,以鏈式存儲結構實現。 代碼: #include <stdio.h>
#include <malloc.h>
struct Num
{
int n;
struct Num *next;
}num;
struct Num *createlist(struct Num *head);
void print(struct Num *head);
void destroy(struct Num *head);
void destroy(struct Num *head)
{
struct Num *p;
while(head!=NULL)
{
p=head->next;
delete(head);
head=p;
}
}
int main()
{
struct Num *head=NULL;
head=createlist(head); //建立
print(head);//輸出
destroy(head);
return 0;
}
struct Num *createlist(struct Num *head) //頭插法建立鏈表
{
struct Num *p;
p=head=(struct Num*)malloc(sizeof(struct Num));
head=NULL;
p=(struct Num*)malloc(sizeof(struct Num)); //p建立新結點
while(scanf("%d",&p->n)!=EOF) //將新結點插到開頭的位置
{
/***************/ 添加代碼 /*****************/ p=(struct Num*)malloc(sizeof(struct Num)); //p每次建立新結點
}
return head;
} void print(struct Num *head)
{
struct Num *current=head;
while(current!=NULL)
{
printf("%d ",current->n);
current=current->next;
}
}

Input

1 2 3 4 5 6 7 8 9

Output

9 8 7 6 5 4 3 2 1

Sample Input

10 23 56 89 11

Sample Output

11 89 56 23 10 
        p->next=head;
        head=p;

  

 

Problem D: 逆置鏈式鏈表(線性表)