我设法构建了一个链表,其中显示了我作为参数提供给它的int,但我无法实现一个显示命令行参数的链表。
例如/aout 20 40 60 80将作为输出[20] [40] [60] [80]
我认为列表的头应该变成argv[1],head->下一个应该是argv[2]等
这是我现在的代码:
#include <stdlib.h>
#include <stdio.h>
typedef struct s_list t_list;
struct s_list
{
int value;
t_list *next;
};
void add_node_end(t_list **head, int value);
void add_node_start(t_list **head, int value);
int remove_first(t_list **head);
int remove_last(t_list *head);
int remove_by_index(t_list **head, int n);
void print_list(t_list *head);
void add_node_end(t_list **head, int value)
{
t_list *current;
current = *head;
while (current->next != NULL)
current = current->next;
current->next = malloc(sizeof(t_list));
current->next->value = value;
current->next->next = NULL;
}
void add_node_start(t_list **head, int value)
{
t_list *new;
new = malloc(sizeof(t_list));
new->value = value;
new->next = *head;
*head = new;
}
int remove_first(t_list **head)
{
t_list *next_node;
int retvalue;
retvalue = -1;
next_node = NULL;
if (*head == NULL)
return (-1);
next_node = (*head)->next;
retvalue = (*head)->value;
free(*head);
*head = next_node;
return (retvalue);
}
int remove_last(t_list *head)
{
t_list *current;
int retvalue;
retvalue = 0;
if (head->next == NULL)
{
retvalue = head->value;
free(head);
return (retvalue);
}
current = head;
while (current->next->next != NULL)
current = current->next;
retvalue = current->next->value;
free(current->next);
current->next = NULL;
return(retvalue);
}
int remove_by_index(t_list **head, int n)
{
t_list *current;
t_list *temp_node;
int retvalue;
int i;
i = 0;
retvalue = -1;
if (n == 0)
return (remove_first(head));
while (i < n - 1)
{
if (current->next == NULL)
return (-1);
current = current->next;
i++;
}
temp_node = current->next;
retvalue = temp_node->value;
current->next = temp_node->next;
free(temp_node);
return(retvalue);
}
void print_list(t_list *head)
{
t_list *current;
current = head;
while (current != NULL)
{
printf("%dn", current->value);
current = current->next;
}
}
int main(void)
{
t_list *head;
head = NULL;
add_node_start(&head, 10);
add_node_end(&head, 20);
add_node_end(&head, 30);
add_node_end(&head, 40);
add_node_end(&head, 50);
print_list(head);
return (0);
}
你有主意吗?感谢
您需要更改main()
的定义,并且需要一种将字符串转换为整数(atoi()
、strtol()
等(的方法:
int main(int argc, char **argv) {
t_list *head = NULL;
if(argc > 1)
add_node_start(&head, atoi(argv[1]));
for(int i = 2; i < argc; i++)
add_node_end(&head, atoi(argv[i]));
print_list(head);
return 0
}
将add_node_start()
和add_node_end()
的实现组合成一个函数会很方便,这样调用代码更容易使用。作为奖励,add_node_end()
在head
指向NULL
指针的情况下调用时会做一些明智的事情(而不是崩溃(:
void add_node_end(t_list **head, int value) {
if(!*head) {
add_node_start(head, value);
return;
}
t_list *current = *head;
while (current->next != NULL)
current = current->next;
current->next = malloc(sizeof(t_list));
current->next->value = value;
current->next->next = NULL;
}
int main(int argc, char **argv) {
t_list *head = NULL;
for(int i = 1; i < argc; i++)
add_node_end(&head, atoi(argv[i]));
print_list(head);
return 0
}
有点偏离主题。Allan Wind的回答很好。
当涉及到简短的例程时,变量名可能会使环境变得混乱(可能会掩盖问题(
void print_list( t_list *p ) {
while( p ) {
printf( "%dn", p->value );
p = p->next;
}
}
如果您希望在链表中灵活地附加和预附加节点,可以通过传递第三个参数将这两个函数合并为一个函数。在本例中,当"atEnd"为0(false(时,代码将将新节点前置到列表的开头。(相反,如果"atEnd"为非零,则将"appending"添加到链表。(.
void add_node( int atEnd, t_list **head, int value ) {
t_list *new = malloc( sizeof *new );
/* omitting test for NULL */
new->value = value;
new->next = NULL;
if( atEnd ) {
for( t_list *p = *head; p->next; p = p->next )
;// just marching
p->next = new;
}
else {
new->next = *head;
*head = new;
}
}
更少的代码和更少的";特殊用途";函数减少了错误可能隐藏的行数。