我有以下代码:
browser.h
#include <stdlib.h>
#include <string.h>
typedef struct webpage {
struct webpage *prev;
struct webpage *next;
char* url;
} Webpage;
Webpage *current = NULL;
void free_mem(Webpage *p) {
if (p->next != NULL) {
free_mem(p->next);
}
free(p);
}
char * add_url(char *url) {
Webpage *p = malloc(sizeof(Webpage));
p->url = url;
if (current != NULL) {
if (current->next != NULL) {
free_mem(current->next);
}
current->next = p;
p->prev = current;
} else {
p->prev = NULL;
}
p->next = NULL;
current = p;
}
void list_history() {
Webpage *temp = current;
printf("--- HISTORIAL ---\n");
while(temp != NULL) {
printf("%s\n", temp->url);
temp = temp->prev;
}
printf("--- FIN ---\n");
}
void nav_back() {
if (current->prev != NULL) {
current = current->prev;
} else {
printf("IGNORADO\n");
}
}
void nav_forward() {
if (current->next != NULL) {
current = current->next;
} else {
printf("IGNORADO\n");
}
}
的main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "browser.h"
int main() {
add_url("www.google.com");
add_url("www.something.com");
add_url("www.a.com");
add_url("www.b.com");
add_url("www.c.com");
add_url("www.d.com");
add_url("www.e.com");
list_history();
nav_back();
nav_back();
list_history();
nav_forward();
list_history();
add_url("www.new-url.com");
list_history();
system("pause"); // this line only works on Windows
return 0;
}
//int main (int argc, char *argv[]) {
// add_url(argv[1]);
// char *option = "Z";
// char input_text[2002];
// while (1) {
// printf("Ingrese comando(A,S,U,L,Q) (URL actual = %s): ", current->url);
// fgets(input_text, 2002, stdin);
// option = strtok(input_text, "\n ");
// if (strcmp(option, "A") == 0) {
// nav_back();
// } else if (strcmp(option, "S") == 0) {
// nav_forward();
// } else if (strcmp(option, "U") == 0) {
// char *url = strtok(NULL, "\n");
// printf("La URL es: %s\n", url);
// add_url(url);
// } else if (strcmp(option, "L") == 0) {
// list_history();
// } else if (strcmp(option, "Q") == 0) {
// break;
// } else {
// printf("OPCION NO VALIDA\n");
// }
// }
//}
第一个main
函数完美运行并且它完成了它必须做的所有事情,但第二个main
函数无法正常工作(即我添加了一些网址,它似乎工作得很好,但是当我尝试list_history时,没有任何内容出现)
我找不到第二个main
无效的原因。如果你能帮忙我会非常感激
示例执行:
Ingrese comando(A,S,U,L,Q) (URL actual = (null)): U a.com
La URL es: a.com
Ingrese comando(A,S,U,L,Q) (URL actual = a.com): U b.com
La URL es: b.com
Ingrese comando(A,S,U,L,Q) (URL actual = b.com): U c.com
La URL es: c.com
Ingrese comando(A,S,U,L,Q) (URL actual = c.com): U d.com
La URL es: d.com
Ingrese comando(A,S,U,L,Q) (URL actual = d.com): U e.com
La URL es: e.com
Ingrese comando(A,S,U,L,Q) (URL actual = e.com): U f.com
La URL es: f.com // works fine up to here
Ingrese comando(A,S,U,L,Q) (URL actual = f.com): L // L should show a list of all URLS written above
--- HISTORIAL ---
--- FIN --- // You can see that history has the space for the URLs but none was printed
Ingrese comando(A,S,U,L,Q) (URL actual = ): A // now it doesn't show current URL "URL actual" any more. Notice before it was showing current after I added one.
Ingrese comando(A,S,U,L,Q) (URL actual = ): Q
答案 0 :(得分:3)
将评论复制到答案中。
创建一个函数来打印给定起点的列表。用它来查看列表会发生什么。
您继续将相同的缓冲区传递给循环中的add_url()
,但它包含最近编写的内容。您的工作代码每次都会在不同的存储中传递不同的字符串。复制字符串,然后将其保存在列表中 - 查找strdup()
。