/*
开发者:慢蜗牛 开发时间:2020.6.11
程序功能:顺序输出和逆序输出
*/
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct L)
void print(struct L* head);
void _print(struct L* head);
struct L//结构体
{
int a;
struct L* next;
};
struct L* creat()//建立无头节点的链表
{
struct L* head;
struct L* p1, * p2; int n = 0;
p1 = p2 = (struct L*)malloc(LEN);
scanf_s("%d", &p1->a);
head = NULL;
while (p1->a != -1)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (struct L*)malloc(LEN);
scanf_s("%d", &p1->a);
}
p2->next = NULL;
return(head);
}
void print(struct L* head)//顺序输出链表函数
{
struct L* p;
p = head;
do
{
printf("--%d", p->a);
p = p->next;
} while (p != NULL);
}
void _print(struct L* head)//反转链表并输出
{
struct L* p1, * p2, * p3, * p;
p3 = p1 = p2 = (struct L*)malloc(LEN);
p1->next = head; head = p1;
p1 = head->next;
do//反转
{
p3=head->next; p2 = p1->next;
head->next = p2; p1->next = p2->next;
p2->next = p3;
} while (p1->next!=NULL);
p = head->next;
do//输出头节点链表
{
printf("--%d", p->a);
p = p->next;
} while (p != NULL);
}
void main()//主函数
{
struct L* LA;
LA = creat();//调用函数建立链表
print(LA);//输出顺序链表
printf("\n");
print(LA);//输出逆序链表
}