单链表的输出上不希望有的零 [英] Undesirable Zero on the Output of Single Linked-list
问题描述
我试图做一个简单的链表,以便在程序调用时打印作为参数插入的数字.但是,它在输出的最后打印出不希望的零.我猜它是打印出来的NULL,但是我不知道如何摆脱它.我仍然了解链接列表的基础.谢谢.
I am trying to do a simple linked-list in order to print the numbers inserted as arguments on the call of the program. However, it prints an undesirable zero on the final of the output. I guess it is a NULL that is printed, but I don't know how to get rid of it. I am still understanding the basics of linked-lists. Thank you.
/* */
#include <stdio.h>
#include <stdlib.h>
/* */
#define NUMERO_DE_ARGUMENTOS_MINIMO 3
#define EOS '\0'
/* */
#define OK 0
#define ARGUMENTO_NULO 1
#define ARGUMENTO_VAZIO 2
#define PONTEIRO_NULO 3
#define NUMERO_DE_ARGUMENTOS_INVALIDO 101
/* */
typedef struct estruturaNumeros
{
unsigned numero;
struct estruturaNumeros *proximaEstrutura;
} tipoNumeros;
/* */
int
main(int argc, char **argv)
{
/* */
tipoNumeros *numeroInicial, *proximoNumero;
char *validacao;
unsigned indiceArgumento;
/* */
numeroInicial = (tipoNumeros *) malloc(sizeof(tipoNumeros));
/* */
if (argc < NUMERO_DE_ARGUMENTOS_MINIMO)
{
printf("\n\n\nNumero de argumentos invalido.\n\n\n\n");
exit(NUMERO_DE_ARGUMENTOS_INVALIDO); /* Programa abortado. */
} /* if */
/* */
if (!numeroInicial)
{
printf("\n\n\nPonteiro nulo.\n\n\n\n");
exit(PONTEIRO_NULO); /* Programa abortado. */
} /* if */
/* */
proximoNumero = numeroInicial;
/* */
for (indiceArgumento = 1; indiceArgumento < argc; indiceArgumento++)
{
proximoNumero->numero = strtoul(*(argv + indiceArgumento), &validacao, 10);
proximoNumero->proximaEstrutura = (tipoNumeros *) malloc(sizeof(tipoNumeros));
proximoNumero = proximoNumero->proximaEstrutura;
} /* for */
/* */
proximoNumero->proximaEstrutura = NULL;
proximoNumero = numeroInicial;
/* */
printf("\n\n\n");
/* */
while (proximoNumero != NULL)
{
printf("%u\n", proximoNumero->numero);
proximoNumero = proximoNumero->proximaEstrutura;
} /* while */
/* */
printf("\n\n\n");
return OK; /* Codigo retornado com sucesso. */
} /* main */
/* output */
UBUNTU 05 --> ./exemplo_lista_encadeada_004 1 2 3
1
2
3
0
推荐答案
while测试应该测试结构指针proximaEstrutura
.
The while test should be testing the structure pointer proximaEstrutura
.
您的代码使用最终(或终端)节点.终端节点的proximaEstrutura成员被初始化为NULL.
Your code uses a final (or terminal) node. It is the terminal node's proximaEstrutura member that is initialised to NULL.
while (proximoNumero->proximaEstrutura != NULL)
{
printf("%u\n", proximoNumero->numero);
proximoNumero = proximoNumero->proximaEstrutura;
}
这篇关于单链表的输出上不希望有的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!