2012-04-12 6 views
1

これは私のコードです...私の理解は、引数として関数をとる関数 "map"を作成するはずです。それは計画どおりに進まない。どんな助けも完全に素晴らしいだろう。 mainの中にあるものを構造体及び機能のすべては、あなたがC.移動を書くことになっているかいないの定義リンクリストのmap/reduce/filter ...マップで失敗しました! (C言語で)

#include <stdio.h> 
#include <stdlib.h> 
#include <stddef.h> 
int main(int argc, const char *argv[]) 
{ 
//it should be apparent that I am quite new to C, I have some java experience. 
struct linkedList { 
    int count; 
    int num; 
    struct linkedList *next; 
}; 
struct linkedList *head, *tail, *curr; 
int count1=0; 
int i=0; 

int square(int v) {return v=v*v;} 

void map(int (*func)(int v), struct linkedList){ 
    struct linkedList2 *head, *tail, *curr; 
    for(curr=head; curr!=NULL; curr=curr->next){ 
     curr->num=func(curr->num); 
     printList(); 
    } 
} 

void start(){ 
    printf("This program will ONLY accept integer input.\n"); 
    head=NULL; 
    for(i=1;i<=4;i++) { 
     count1++; 
     curr=malloc(sizeof(struct linkedList)); 
     curr->count=count1; 
     printf("Enter a number: "); 
     scanf("%d", &curr->num); 
     if(head==NULL) { head=curr; } 
     else { tail->next=curr; } 
     tail=curr; 
     tail->next=NULL; 
    } 
    printf("A list has been created.\n"); 
} 

void printList(){ 
    printf("The list now contains these numbers: "); 
    for(curr=head;curr!=NULL;curr=curr->next){ 
     printf("%d, ", curr->num); 
    } 
    printf("\n"); 
} 

start(); 
printList(); 

map(square, linkedList); 
printList(); 
system("PAUSE"); 
return 0; 
} 
+0

+1 Cでmap/reduceを使用しています。 – Mehrdad

答えて

1

:ここ

はコンパイル可能(うまくコンパイルではないが、縮小)コードのバージョンですint main(int argc, const char *argv[]) {printListの定義の直後に設定します。mainにはmainの実際のコードのみが含まれています。

また、mapの定義に未完成のプロトタイプがあるようです。 void map(int (*func)(int v), struct linkedList)の代わりに、2番目のパラメータが使用されていない場合は、void map(int (*func)(int v), struct linkedList* head)が必要です(そして、次の行でheadという宣言を取り除きます)。また、linkedList2linkedListに変更する必要があります。さらに、mainmapmap(square, linkedList)と呼ぶことは無意味です。 map(square, head)を使用します。

+0

これは私の最初の質問であり、回答のスピードと正確さが取り上げられました。本当にありがとう。チェックされた回答が、残念ながら私はupvoteに担当者を持っていない。再度、感謝します。 – break

関連する問題