数据结构的几道题,有点难,能做出一道就采纳,可以加财富值。谢谢了
1个回答
展开全部
给你做一题吧。
11.2.5 浏览器那题
#include <stdio.h>
#define STACK_SIZE 100
#define URL_SIZE 100
typedef struct sitem
{
char url[URL_SIZE];
}URL_I;
typedef struct url_stack
{
URL_I urls[STACK_SIZE];
URL_I *top;
}URL_S;
URL_S bw, fw;
void stack_init(URL_S *s)
{
s->top = s->urls;
}
int stack_is_empty(URL_S *s)
{
return s->top <= s->urls;
}
int stack_is_full(URL_S *s)
{
return s->top >= s->urls + STACK_SIZE -1;
}
int stack_push(URL_S *s, URL_I *u)
{
if(stack_is_full(s))
return -1;
strcpy((++s->top)->url, u->url);
return 0;
}
int stack_pop(URL_S *s, URL_I *u)
{
if(stack_is_empty(s))
return -1;
strcpy(u->url, (s->top--)->url);
return 0;
}
int main(void)
{
char cmd[20];
URL_I u;
int f;
stack_init(&bw);
stack_init(&fw);
u.url[0] = '\0';
while(1)
{
gets(cmd);
f = 1;
if(strncmp(cmd, "VISIT ", 6) == 0)
{
if(strlen(cmd) < 7)
{
printf("Ignored\n");
continue;
}
if(strlen(u.url) > 0)
{
if(stack_push(&bw, &u) != 0)
{
printf("BACK FULL STACK!\n");
printf("Ignored\n");
continue;
}
}
strcpy(u.url, cmd + 6);
stack_init(&fw);
}
else if(strcmp(cmd, "BACK") == 0)
{
if(stack_push(&fw, &u) != 0)
{
printf("FORWARD FULL STACK!\n");
printf("Ignored\n");
continue;
}
if(stack_pop(&bw, &u) != 0)
{
printf("Ignored\n");
stack_pop(&fw, &u);
f = 0;
}
}
else if(strcmp(cmd, "FORWARD") == 0)
{
if(stack_push(&bw, &u) != 0)
{
printf("BACK FULL STACK!\n");
printf("Ignored\n");
continue;
}
if(stack_pop(&fw, &u) != 0)
{
printf("Ignored\n");
stack_pop(&bw, &u);
f = 0;
}
}
else if(strcmp(cmd, "QUIT") == 0)
{
break;
}
else
{
printf("Wrong cmd, Ignored\n");
f = 0;
}
if(f)
printf("%s\n", u.url);
}
return 0;
}
其实这一题用一个栈就能实现,不过还是按照题意给你做了一下。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询