私が書いているプログラムでは、リンクされたリストが必要なので、かなり具体的な実装です。が必要だ:
- 最後にノードを追加する機能
- データが指定された値と一致するノードを削除する機能
データは cstring で、長さは 20 文字以下です。私は C の経験があまりなく、次の署名でエラーが発生していますvoid addToEnd(llist root, char entery[51])
。に置き換えllist
てみましたnode
が、エラーは「不明な型名ノード」です。どうすればこれを取り除くことができますか?
これがコードです
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct node
{
char entery[51];
struct node* next;
} llist;
/*may be losing root address permanently*/
void addToEnd(llist root, char entery[51])
{
while(root->next != NULL)
root = root->next;
node last = malloc(sizeof(struct node));
root->next = last;
strcpy(last, entery);
}
int main()
{
struct node *root = malloc(sizeof(struct node));
root->next = NULL;
strcpy(root->entery, "Hello");
struct node *conductor = root;//points to a node while traversing the list
if(conductor != 0)
while(conductor->next != 0)
conductor = conductor->next;
/* Creates a node at the end of the list */
conductor->next = malloc(sizeof(struct node));
conductor = conductor->next;
if (conductor == NULL)
{
printf( "Out of memory" );
return EXIT_SUCCESS;
}
/* initialize the new memory */
conductor->next = NULL;
strcpy(conductor->entery, " world\n");
addToEnd(root, " at the");
addToEnd(root, " end");
/*print everything in list*/
conductor = root;
if(conductor != NULL)
{
while(conductor->next != NULL)
{
printf("%s", conductor->entery);
conductor = conductor->next;
}
printf("%s", conductor->entery);
}
return EXIT_SUCCESS;
}
私が不明なことの1つは、私が見たすべての例で、それらが struct を typedef していることです。なんで?詳しく説明させてください。または だけを渡したいかどうかはどうすればわかりますnode
かstruct node
。また、私はその点を本当に理解していませんstruct node
.1つのtypedefされた名前よりもそれほど長くはありません.