私はCで初心者で、char変数で二重リンクリストを作成しようとしています。私は単一のリストで同じことをしました。しかし、同じことを二重リストにすると、リストが空であるかどうかをチェックする私のチェック機能は空であるため、リストは自分のプリント機能によって書き込まれません。私は私の問題に関連するいくつかの記事を読んで、それはポインタの参照についてだと思うが、問題を解決できなかった。これは私の最初の投稿であり、英語は母国語ではないので、私は時々不完全ですみませます。だから、投稿を正しく共有する私の間違いについて私に警告してください。どんな助けもありがとう。二重リンクリストが "List is empty"を返す
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char val;
struct node *next;
struct node *prev;
}node_t;
void pushFront(node_t *nodes);
int isEmpty(node_t *nodes);
void printList(node_t *nodes);
void pushBack(node_t *nodes);
int main(void)
{
node_t *head=NULL; //My head node
pushFront(head);
pushBack(head);
printList(head);
}
int isEmpty(node_t *nodes)//Checking whether node is empty or not.
{
node_t *current=nodes;
if(current==NULL)
{
return 1;
}
else
{
return 0;
}
}
void pushFront(node_t *nodes)
{
node_t *newNode;
char b;
int i;
do{
printf("Enter a char:");//Getting character from user
scanf(" %c",&b);
if(isEmpty(nodes))//If node is empty then create
{
newNode=malloc(sizeof(node_t));
newNode->val=b;
newNode->next=NULL;
newNode->prev=NULL;
nodes=newNode;
}
else//if it is not empty, get new first character
{
newNode=malloc(sizeof(node_t));
newNode->val=b;
nodes->prev=newNode;
newNode->next=nodes;
nodes=newNode;
}
printf("Enter a -1 to exit:");//If user wants to continue to add head
character
scanf("%d",&i);
}while(i!=-1);
}
void pushBack(node_t *nodes)//adding character to back of head
{
node_t *current=nodes;
char b;
int i;
do{
printf("Enter a char:");
scanf(" %c",&b);
if(isEmpty(nodes))//checking again empty or not
{
node_t *newNode=malloc(sizeof(node_t));
newNode->val=b;
newNode->next=NULL;
newNode->prev=NULL;
nodes=newNode;
}
else//if not empty
{
while(current->next!=NULL)
{
current=current->next;
}
current->next=malloc(sizeof(node_t));
current->next->val=b;
current->next->prev=current;
current->next->next=NULL;
}
printf("Enter -1 to exit:");
scanf("%d",&i);
}while(i!=-1);
}
void printList(node_t *nodes)//Printing all the characters
{
if(isEmpty(nodes))
{
printf("List is empty");
}
else
{
while(nodes!=NULL)
{
printf("%c",nodes->val);
nodes=nodes->next;
}
}
}
あなたのfix-my-codeに関する質問はここで話題にはなりません。すべての警告とデバッグ情報(例えば 'gcc -Wall -Wextra -g'と[GCC](http://gcc.gnu.org/)...)でコンパイルし、**デバッガを使用する**(例えば' gdb ')を実行してプログラムをステップバイステップで実行し、その状態を照会し、バグを理解してください。 –
'ボイドpushFront(node_t *ノード);' - > 'node_t * pushFront(node_t *ノード);' 'pushFront(ヘッド);' - > 'ヘッド= pushFront(ヘッド);' – BLUEPIXY
@BasileStarynkevitch、はい、 あなたが正しいです。私はこの手順に従います。 –