http://acm.pku.edu.cn/JudgeOnline/problem?id=1028
模拟浏览器的前进后退功能:
#include <stdio.h>
#include <string.h>
typedef struct link
{
char Address[70];
struct link *prior;
struct link *next;
}Link;
Link* Head;
Link* V;
void InitLink()
{
Head = new(Link);
strcpy(Head->Address,"http://www.acm.org/");
Head->prior = NULL;
Head->next =NULL;
V = Head;
}
void Insert()
{
Link* q;
q = new(Link);
scanf("%s",q->Address);
q->next = NULL;
q->prior =NULL;
if(Head->next == NULL)
{
Head->next = q;
q->prior = Head;
V = q;
}
else
{
if(V->next != NULL)
V->next->prior = q;
V->next = q;
q->prior = V;
V = q;
}
}
int main()
{
InitLink();
char Call[10];
scanf("%s",Call);
while(strcmp(Call,"QUIT") != 0)
{
if(strcmp(Call,"VISIT") == 0)
{
Insert();
printf("%s/n",V->Address);
}
if(strcmp(Call,"BACK") == 0)
{
if(V->prior == NULL)
printf("Ignored/n");
else
{
V = V->prior;
printf("%s/n",V->Address);
}
}
if(strcmp(Call,"FORWARD") == 0)
{
if(V->next == NULL)
printf("Ignored/n");
else
{
V = V->next;
printf("%s/n",V->Address);
}
}
if(strcmp(Call,"QUIT") == 0)
break;
scanf("%s",Call);
}
return (0);
}
/*
VISIT http://acm.ashland.edu/
VISIT http://acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored
*/