2012-10-21 5 views
1

포인터 삽입 알고리즘 :C - 나는 두 구조체 만든

typedef struct mainNode { 
    int theNode; 
    int visited; 
    struct mainNode *next; 
} node_rec, *node_ptr; 

typedef struct neighborNode { 
    struct neighborNode *next; 
} neighbor_node_rec, *neighbor_node_ptr; 

나는 목록에 삽입해야합니다

void insert(node_ptr head, int theNodeVal, int neighborNodeVal, int weight) { 

//if list is empty 
if (head == NULL) { 
    head = (node_ptr) malloc(sizeof (node_rec)); //create head of list 
    head->theNode = theNodeVal; //set head value to node value 
    head->next = NULL; //point to null  
} 

//while list is not pointing no null 
while (head != NULL) { 
    //if node IS NOT equal to node value 
    if (head->theNode != theNodeVal) { 
     head->theNode = theNodeVal; //set head value (new node) to node value 
     head->next = tail; //connect to next node 
     tail->next = NULL; //point to null 
    } 
    else 
    {   
     //if node IS equal to node value (the node already exists) 
     tail->next = head // head is the new tail 
     head->neighbor = n; //point at the neighbor of head (new tail) 
    } 
} 
} 

내가하려고 해요을 내가 구현 한 논리가 올바른지 확인하십시오. 이것이 내가 모든 라인에 주석을다는 이유이다. 페이지 상단의 링크를 참조하여 시각적으로 볼 수 있습니다.

+0

문제는 무엇인가 :

이 내가이 알고리즘에 대한 그래프를 구현하는 것이 어떻게? 너는 무엇에 붙어 있니? – nneonneo

답변

2

if (head->theNode = theNodeVal) {if (head->theNode == theNodeVal) {으로 바꿉니다. 도움이 될까요?

#include <limits.h> 
#include <string.h> 
#include <malloc.h> 
#include <stdio.h> 

struct Vertex { 
    struct Vertex *next; // The reference to the next vertex in the global list of vertices 
    int nodeId; 
    int distance; 
    struct Vertex *previous; // The reference to trace back the shortest path; 
    struct Edge *neighbor; // The reference to the list of edges of this 
}; 

struct Edge { 
    struct Edge *next; // The reference to the next edge of this vertex; 
    struct Vertex *node; // The reference to the vertex at other end of this edge; 
    int weight; 
}; 


struct Vertex *head = NULL; 

struct Vertex *newVertex(int nodeId) { 
    struct Vertex *p = malloc(sizeof(struct Vertex)); 
    memset(p,0,sizeof(struct Vertex)); 
    p->nodeId = nodeId; 
    p->distance=INT_MAX; 
    p->next = head; 
    head = p; 
    return p; 
} 

void addEdge(struct Vertex *v1, struct Vertex *v2, int weight) { 
    struct Edge *e = malloc(sizeof(struct Edge)); 
    e->next = v1->neighbor; 
    v1->neighbor = e; 
    e->node = v2; 
    e->weight = weight; 
} 

void insert(int node1, int node2, int weight) { 
    struct Vertex *current = head, *p1 = NULL, *p2 = NULL; 

    while (current != NULL) { 
     if (current->nodeId == node1) p1 = current; 
     if (current->nodeId == node2) p2 = current; 
     current = current->next; 
    } 

    if (p1 == NULL) p1 = newVertex(node1); 
    if (p2 == NULL) p2 = newVertex(node2); 

    addEdge(p1,p2,weight); 
    addEdge(p2,p1,weight); 
} 

void main(int argc, char **argv) { 
    int node1, node2, weight; 
    while (EOF != scanf("%d %d %d \n", &node1, &node2, &weight)) { 
     insert(node1, node2, weight); 
    } 
} 
+0

나는 세부 사항에 뛰어 들지 않았다. 그냥 명백한 점을 지적했다 ... – Serge

+0

논리에 관해서는, 나는 struct mainNode의 멤버'neighbor'가 neighbor_node_ptr로 선언되어야한다고 생각한다. 두 개의 if 문은 매우 유사합니다. 나는 일반적인 것들을 벗어나기를 제안 할 것이다. 그러면 더 이해하기 쉽고 읽을 수 있습니다. – Serge

+0

다시 같은 문제가 발생합니다. '='는 과제입니다! 나는 너의 포스트를 조금 고칠 것이다 – Serge