题意:模拟游览器的前进和后退。
注重点:对数据结构的基本掌握,用双向链表实现的
代码来源:大萎的代码直接交的。
代码:
#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);
}