| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:我写的怎么这么大,看别人的那么小。不过还是能AC的In Reply To:求代码? Posted by:mengsongshi at 2010-07-18 20:21:51 #include <stdio.h>
#include <string.h>
#define M 75
#define N 110
typedef struct stack
{
char bottom[N][M];
int top;
}STACK;//栈的顺序储存表示
void InitStack(STACK *S);
void Push(STACK *S, char *e);
void Pop(STACK *S, char *e);
void ClearStack(STACK *S);
int StackEmpty(STACK *S);
main()
{
STACK forward, backward; //定义forward, backward
char command[M], current[M];//定义字符一维数组command储存命令,current储存前页面.
int i = 0, j;
strcpy(current, "http://www.acm.org/");
InitStack(&forward);//构造一个空栈forward
InitStack(&backward);//构造一个空栈backward
do{
scanf("%s", command);
if (strcmp(command, "VISIT") == 0)
{
Push(&backward, current);
scanf("%s", current);
ClearStack(&forward);
printf("%s\n", current);
}
else if (strcmp(command, "BACK") == 0)
{
if (StackEmpty(&backward))
{
printf("Ignored\n");
}
else
{
Push(&forward, current);
Pop(&backward, current);
printf("%s\n", current);
}
}
else if (strcmp(command, "FORWARD") == 0)
{
if (StackEmpty(&forward))
{
printf("Ignored\n");
}
else
{
Push(&backward, current);
Pop(&forward, current);
printf("%s\n", current);
}
}
}while (strcmp(command, "QUIT") != 0);
return 0;
}
//构造空栈
void InitStack(STACK *S)
{
S->top = -1;
}
//进栈
void Push(STACK *S, char *e)
{
S->top++;
strcpy(S->bottom[S->top], e);
}
//出栈
void Pop(STACK *S, char *e)
{
strcpy(e, S->bottom[S->top]);
S->top--;
}
//将栈置空
void ClearStack(STACK *S)
{
S->top = -1;
}
//判断栈是否为空,为空则返回1,否则返回0
int StackEmpty(STACK *S)
{
if (S->top == -1)
{
return 1;
}
else return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator