2017-06-16 3 views
1

i einen neuen Knoten machen will, das eine Kopie von Node_1 zu Knoten_2 verbunden ist , das Problem ist, dass ich Elemenets in jedem Knoten wählen muß, die eine bestimmte Bedingung akzeptieren thhat i in der Verbindungsfunktion einfügen. zum Beispiel, wenn ich zwei Knoten habe, die ich miteinander verbinden möchte (die zweite am Ende der ersten), aber ich möchte die Elemente in jedem Knoten, die zum Beispiel ungerade sind wählen! (zum Beispiel: erste verknüpfte Liste hat die folgenden Elemente (1 2 3), und die zweite verknüpfte Liste hat die folgenden Elemente (4 5 6) dann möchte ich eine neue verknüpfte Liste haben> die die folgenden Elemente hat: (1 3 5) jetzt mein Hauptproblem ist, dass ich mit Zeigern auf Funktionen arbeiten muß, weil jedes Mal, wenn ich der Funktion unterschiedliche Bedingungen geben will.Wie verbindet man einen Strukturknoten (Linked Lists)?

i schrieb diese Funktion mit der Annahme, dass ich habe eine ConditionFunction , aber eigentlich ich ein bisschen fest auf, wie man eine ConditionFunction in der Hauptfunktion, die tatsächlich tun kann, was ich will:/(zum Beispiel nur die ungeraden Zahlen)

i schrieb diese Funktion die beiden verknüpften Listen verbinden:

// the struct: 
typedef struct node_t* Node; 
struct node_t { 
Element element; 
Node next; 
}; 

// ConditionNode a pointer to a function that has condition 
// CopyNode a pointer to a function that copy's the node 

Node concatLists(Node Node_1,Node Node_2,ConditionNode ConditionFunction,CopyNode copyFunction,void* condition){ 
    Node currentNode=NULL; 
    Node* New_Node=NULL; 
    Node head=NULL; 
    while(Node_1!=NULL){ 
     if(ConditionFunction(Node_1->element,condition)==0){ 
      Node_Result=create_node(&New_Node); 
      if(head==NULL){ 
       head=New_Node; 
       currentNode=New_Node; 
      } 
      currentNode->next=New_Node; 
      currentNode=New_Node; 
      Node_1=GetNextNode(Node_1); 
     } 
     else{ 
      Node_1=GetNextNode(Node_1); 
     } 
    } 


    while(Node_2!=NULL){ 
     if(CmpFunction(Node_2->element,condition)!=0){ 
      if(head==NULL){ 
       head=New_Node; 
       currentNode=New_Node; 
      } 
      currentNode->next=New_Node; 
      currentNode=New_Node; 
      Node_2=GetNextNode(Node_2); 
     } else { 
      Node_1=GetNextNode(Node_1); 
     } 
    } 
    return head; 
} 


Node_Result create_node(Node* CreatedNode) { 
Node newNode = malloc(sizeof(*newNode)); 
if(!newNode) { 
    return MEM_PROBLEM; 
} 
newNode->element =0; 
newNode->next = NULL; 
*CreatedNode=newNode; 
return NODE_SUCCESS; 
} 



Node GetNextNode(Node node){ 
    if(node==NULL){ 
    return NULL; 
    } 
    return node->next; 
} 

i schrieb ein Beispiel, aber ich denke, dass es falsch ist: \

int main(){ 
    int array_1[3]={1,2,3}; 
    int array_2[4]={4,5,6,7}; 

    Node head_1=createAllNode(array_1,3); 
    Node head_2=createAllNode(array_2,4); 
    int num=2; 
    Node oddhead=concatLists(head_1,head_2,&copyInt,&checkIfOdd,&num); 
    printIntElements(oddhead); 

    return 0; 
} 





static Node createAllNode(int* array,int len){ 
    Node head; 
    Node_Result result=create_node(&head); 
    if(result!=NODE_SUCCESS){ 
     return NULL; 
    } 
    Node new_node=NULL; 
    int j=0; 
    while(len){ 
     /*int *num=malloc(sizeof(*num)); 
     if(num==NULL){ 
      return NULL; 
     } */ 
     int element=array[j]; 
     head->element=*(int *)element; 

     if(j != len-1){ 
      result=create_node(&new_node); 
     } 
     if(Node_Result!=NODE_SUCCESS){ 
        return NULL; 
     } 
     head->next=new_node; 
     head=new_node; 
     new_node=GetNextNode(new_node); 
     j++; 
     len--; 
     } 
    return head; 
} 


static void* copyInt(void* num){ 
    int* newInt=malloc(sizeof(*newInt)); 
    *newInt=*(int*)num; 
    return newInt; 
} 

/* 
static bool PrimaryIntNode(void*num1,void* num2){ 

} 
*/ 

static void printIntElements(Node head){ 
    while(head!=NULL){ 
     printf("%d",(int*) head->element); 
     head=GetNextNode(head); 
    } 
} 


static bool checkIfOdd(Element num1,int num2){ 
    int num=*(int *)num1; 
    if(num<0){ 
     num *=-1; 
    } 
    return num % num2 != 0; 

} 

und i Rufen Sie die Coonect-List-Funktion wie folgt in der Hauptfunktion auf: Node oddhead=concatLists(head_1,head_2,&copyNode,&checkifIdd,&num);

kann mir jemand nur ein korrektes Beispiel zeigen, oh, wie tatsächlich eine Funktion wie diese in Haupt verwenden !! weil ich alle Arten von Fehlern in Eklipse ..

+1

Vor allem nie typedef Zeiger. Zweitens, verwenden Sie nicht '_t' Suffix in Ihren Typen. Es steht im Widerspruch zu POSIX. – 0andriy

+0

Wenn Sie jeweils nur einen Filter benötigen, können Sie einfach 'struct node_t * filter' zum' struct node_t' hinzufügen und anstatt Daten zu kopieren, einfach eine neue * filtered * -Liste erstellen. – 0andriy

+0

@kayan Ich verstehe nicht, wie Sie die Seltsamkeit überprüfen. –

Antwort

0

Ich werde nicht Ihre Definitionen verwenden, weil sie verwirrend sind. Ich werde nur zeigen, wie zwei Listen verkettet werden können, indem Knoten ausgewählt werden, die eine bestimmte Bedingung erfüllen. Sie können das vorgestellte Demonstrationsprogramm als Grundlage für Ihre eigene Listenimplementierung verwenden.

#include <stdio.h> 
#include <stdlib.h> 

struct node 
{ 
    int data; 
    struct node *next; 
}; 

void insert(struct node **head, const int a[], size_t n) 
{ 
    if (*head != NULL) head = &(*head)->next; 

    for (size_t i = 0; i < n; i++) 
    { 
     struct node *tmp = malloc(sizeof(struct node)); 
     tmp->data = a[i]; 
     tmp->next = *head; 
     *head = tmp; 
     head = &(*head)->next; 
    }   
}  


struct node * concatLists(struct node *head1, struct node *head2, int cmp(struct node *)) 
{ 
    struct node *head = NULL; 
    struct node **current = &head; 

    for (; head1 != NULL; head1 = head1->next) 
    { 
     if (cmp(head1)) 
     { 
      *current = malloc(sizeof(struct node)); 
      (*current)->data = head1->data; 
      (*current)->next = NULL; 
      current = &(*current)->next; 
     } 
    } 

    for (; head2 != NULL; head2 = head2->next) 
    { 
     if (cmp(head2)) 
     { 
      *current = malloc(sizeof(struct node)); 
      (*current)->data = head2->data; 
      (*current)->next = NULL; 
      current = &(*current)->next; 
     } 
    } 

    return head; 
} 

void output(struct node *head) 
{ 
    for (; head != NULL; head= head->next) 
    { 
     printf("%d ", head->data); 
    } 
} 

int odd(struct node *n) 
{ 
    return n->data % 2; 
} 

int main(void) 
{ 
    struct node *head1 = NULL; 
    struct node *head2 = NULL; 
    int a1[] = { 1, 2, 3 }; 
    int a2[] = { 4, 5, 6 }; 
    const size_t N1 = sizeof(a1)/sizeof(*a1); 
    const size_t N2 = sizeof(a2)/sizeof(*a2); 

    insert(&head1, a1, N1); 
    insert(&head2, a2, N2); 

    struct node *head3 = concatLists(head1, head2, odd); 

    output(head1); 
    putchar('\n'); 
    output(head2); 
    putchar('\n'); 
    output(head3); 
    putchar('\n'); 

    return 0; 
} 

Die Programmausgabe ist

1 2 3 
4 5 6 
1 3 5 
+0

danke so sehr! das ist, was ich suchte .. ein Beispiel dafür, wie ich vielleicht so etwas tun kann! Du hast mir sehr geholfen :) – kayan

+0

@kayan Nein. Sie sind willkommen. :) –

+0

Hallo, ich bin wieder .. – kayan

Verwandte Themen