点击打开链接
/*
时间:2014.2.7
目的: 题目1511:从尾到头打印链表ac.jobdu.com/problem.php?pid=1511
*/
#include <stdio.h>
#include <malloc.h>
typedef struct LinkNode{
struct LinkNode*prior;
struct LinkNode*next;
int data;
}LinkNode,*LinkList;
int main()
{
int n;
LinkList plist = (LinkList)malloc(sizeof(LinkNode));
LinkNode *pNode,*rear;
rear = plist;
while(~scanf("%d", &n))
{
if(n == -1)
continue;
pNode = (LinkNode*)malloc(sizeof(LinkNode));
pNode->data = n;
rear->next = pNode;
pNode->prior = rear;
rear = pNode;
scanf("%d", &n);
while(n!=-1)
{
pNode = (LinkNode*)malloc(sizeof(LinkNode));
pNode->data = n;
plist->next = pNode;
pNode->prior = rear;
rear = pNode;
scanf("%d", &n);
}
rear->next = NULL;
while(rear!=plist)
{
printf("%d\n",rear->data);
rear = rear->prior;
}
}
return 0;
}
/*
-----------
1 思路:1.用的是双链表
2 2.用的是尾插法
3
4
5
-1
5
4
3
2
1
-----------
*/
//头插法
#include <stdio.h>
#include <malloc.h>
typedef struct LinkNode{
struct LinkNode*next;
int data;
}LinkNode,*LinkList;
int main()
{
int n;
LinkList plist = (LinkList)malloc(sizeof(LinkNode));
plist->next = NULL;
LinkNode *pNode,*rear;
while(~scanf("%d", &n))
{
if(n == -1)
continue;
pNode = (LinkNode*)malloc(sizeof(LinkNode));
pNode->data = n;
pNode->next = plist->next;
plist->next = pNode;
scanf("%d", &n);
while(n!=-1)
{
pNode = (LinkNode*)malloc(sizeof(LinkNode));
pNode->data = n;
pNode->next = plist->next;
plist->next = pNode;
scanf("%d", &n);
}
rear = plist->next;
while(rear!=NULL)
{
printf("%d\n",rear->data);
rear = rear->next;
}
}
return 0;
}
题目1511:从尾到头打印链表