#include <stdio.h>
#include <stdlib.h>
typedef struct List
{
int data;
struct List *next;
}List;
List * list_Create(int n)
{
List * head, * tail, *p;
int i = 1;
head = (List *)malloc(sizeof(List));
head->next = NULL;
tail = head;
while(n)
{
p = (List *)malloc(sizeof(List));
p->data = i;
tail->next = p;
tail = p;
i++;
n--;
}
tail->next = NULL;
return head;
}
List *list_Reverse(List * head)
{
List *p, *q, *r;
p = head;
q = p->next;
while(q != NULL)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = NULL;
head = p;
return head;
}
int main()
{
int n;
List *head, *p;
printf("Please enter the number of the List:\n");
scanf("%d", &n);
head = list_Create(n);
p = head->next;
printf("\nThe List is :\n");
for(p=head->next;p!=NULL;p=p->next)
{
printf("--%d--", p->data);
}
head = list_Reverse(head);
printf("\nThe Reverse of the List:\n");
for(p = head;p->next != NULL;p = p->next)
{
printf("--%d--", p->data);
}
printf("\n");
return 0;
}
单链表的创建和逆转
最新推荐文章于 2022-02-21 09:42:48 发布