poj1028 Web Navigation

来源:互联网 发布:小米的域名花了多少钱 编辑:程序博客网 时间:2024/05/22 08:24

栈的简单模拟题,注意一定要读清楚题意,要不然你会后悔的!自己写的代码

#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;

#define N 80
#define M 105
typedef char elem[N];
typedef struct Stack
{
 elem *base;
 elem *top;
}staks;

staks forward;
staks backward;
void init(staks &s)
{
 s.base=(elem *)malloc(M*sizeof(elem));
 if(!s.base)
  return ;
 s.top=s.base;
}
void  push(staks &s,elem e)
{
 
 strcpy(*s.top,e);
 s.top++;
}

int pop(staks &s,elem &e)
{
 if(s.base==s.top)
  return 0;
 s.top--;
 strcpy(e,*s.top);
 return 1;
}
int empty(staks s)
{
 if(s.base==s.top)
  return 1;
 else
  return 0;
}

int main()
{
 char con[10],web[80];
 char current[80]="http://www.acm.org/";
    init(forward);
 init(backward);
 while(1)
 {
  scanf("%s",con);
  if(strcmp(con,"QUIT")==0)
   break;
  if(strcmp(con,"VISIT")==0)
  { 
   push(backward,current);
   scanf("%s",web);
   printf("%s/n",web);
   strcpy(current,web);
   forward.top=forward.base;
  }
  else if(strcmp(con,"BACK")==0)
  {
   
   if(empty(backward))
     printf("Ignored/n");
   else
   {
    push(forward,current);
    pop(backward,web);
    strcpy(current,web);
    printf("%s/n",web);
   }

  }
  else if(strcmp(con,"FORWARD")==0)
  {
   
   if(empty(forward))
    printf("Ignored/n");
   else
   {
    push(backward,current);
    pop(forward,web);
    strcpy(current,web);
    printf("%s/n",web);

   }
  }
 }
 return 0;
}

 

 

 

 

原创粉丝点击