#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char date;
struct node *next;
}List;
void InitList(List*head,int n)
{
List *p=head,*q;
int i;char c;
for(i=0;i<n;i++)
{
q =(List*)malloc(sizeof(List));
scanf("%c",&c);
q->date = c;
p->next = q;
p=q;
}
p->next = NULL;
}
void Reverse(List*head)//逆置链表
{
List *p=head->next,*q;//一个指针指向第一个数据结点
head->next =NULL;//头结点断链
while(p!=NULL)
{
q=p->next;//q指针指向p指针的下一个
p->next = head->next;//画个图很好理解
head->next = p;
p=q;
}
}
void Print(List *head)
{
List* p =head->next;
while(p!=NULL)
{
printf("%c ",p->date);
p=p->next;
}
}
int main()
{
List*head =(List*)malloc(sizeof(List));
head->next =NULL;
int n;
scanf("%d",&n);
getchar();
InitList(head,n);
Reverse(head);
Print(head);
return 0;
}
SWUST OJ 957: 逆置单链表
最新推荐文章于 2023-02-03 22:15:27 发布