2017-08-27 12 views
0

私は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; 
    } 
    } 
    } 
+2

あなたのfix-my-codeに関する質問はここで話題にはなりません。すべての警告とデバッグ情報(例えば 'gcc -Wall -Wextra -g'と[GCC](http://gcc.gnu.org/)...)でコンパイルし、**デバッガを使用する**(例えば' gdb ')を実行してプログラムをステップバイステップで実行し、その状態を照会し、バグを理解してください。 –

+0

'ボイドpushFront(node_t *ノード);' - > 'node_t * pushFront(node_t *ノード);' 'pushFront(ヘッド);' - > 'ヘッド= pushFront(ヘッド);' – BLUEPIXY

+0

@BasileStarynkevitch、はい、 あなたが正しいです。私はこの手順に従います。 –

答えて

2

これが答えではないが、それはあなたにヒントを与える必要があります。

質問:

void Foo(int bar) 
{ 
    bar = bar * 2; 
} 

... 
int x = 3; 
Foo(x); 
// what's the value of x here ? 

コールFoo(x)xの値は何ですか?

よく、同じことがpushFrontpushBackで起こっています。

+0

関数呼び出し後は6になります。私はそれを持って、それを再考します。どうもありがとうございました。 –

+1

@HalilArslanいいえ、 'x'の値は' 3'です.BTW 'Foo(3)'を呼び出すとどうなりますか? –

+0

3のままであれば、関数の中にxをコピーしていますか?私はFoo(3)の結果は6だと思いますが、私は混乱しています。 –