#include<stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node* next; //这里定义了一个struct node类新的指针命名为next,
struct node* pre; //为什么他会把节点指向下一个,不清楚他的运作逻辑
}Node; //关于pre他在此结构的作用是什么呢
typedef struct _list {
Node* head;
Node* tail;
}List;
void add(List* pList, int number);
void print(List* pList);
void reprint(List* pList);
int main(int argc, char const* argv[])
{
List list;
list.head = NULL;
list.tail = NULL;
int number = 0;
do {
scanf_s("%d", &number);
if (number != -1)
{
add(&list, number);
}
} while (number != -1);
reprint(&list);
return 0;
}
void add(List* pList, int number)
{
Node* p = (Node*)malloc( sizeof(Node) );
p->value = number;
p->next = NULL;
if (pList->head)
{
p->pre = pList->tail;
pList->tail->next = p; //此时这个tail的作用是什么,他的运作逻辑又是怎样的呢
pList->tail = p;
}
else
{
pList->head = p;
pList->tail = p;
pList->tail->pre = NULL;
}
}
void print(List* pList) {
Node* p;
for (p = pList->head; p; p = p->next) {
printf("%d ", p->value);
}
printf("\n");
}
void reprint(List* pList) {
Node* p;
for (p = pList->tail; p; p = p->pre) {
printf("%d ", p->value);
}
printf("\n");
}