C指针插入算法

C指针插入算法,c,insert,C,Insert,我创建了两个结构: typedef struct mainNode { int theNode; int visited; struct mainNode *next; } node_rec, *node_ptr; 及 我需要将其插入列表中: void insert(node_ptr head, int theNodeVal, int neighborNodeVal, int weight) { //if list is empty if (head == NULL)

我创建了两个结构:

typedef struct mainNode {
    int theNode;
    int visited;
    struct mainNode *next;
} node_rec, *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)
    }
}
}
我想看看我实现的逻辑是否正确。这就是我评论每一行的原因。您可以参考页面顶部的链接以获得视觉效果。

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);
    }
}
#包括
#包括
#包括
#包括
结构顶点{
struct Vertex*next;//对全局顶点列表中下一个顶点的引用
int-nodeId;
整数距离;
struct Vertex*previous;//追溯最短路径的引用;
struct Edge*neighbor;//对该对象的边列表的引用
};
结构边{
struct Edge*next;//对该顶点下一条边的引用;
struct Vertex*node;//对该边另一端顶点的引用;
整数权重;
};
结构顶点*head=NULL;
结构顶点*新顶点(int nodeId){
结构顶点*p=malloc(sizeof(结构顶点));
memset(p,0,sizeof(struct-Vertex));
p->nodeId=nodeId;
p->distance=INT_MAX;
p->next=头部;
水头=p;
返回p;
}
void addEdge(结构顶点*v1,结构顶点*v2,整数权重){
结构边缘*e=malloc(sizeof(结构边缘));
e->next=v1->邻居;
v1->邻居=e;
e->node=v2;
e->重量=重量;
}
无效插入(int节点1、int节点2、int权重){
结构顶点*current=head,*p1=NULL,*p2=NULL;
while(当前!=NULL){
如果(当前->节点ID==节点1)p1=当前;
如果(当前->节点ID==节点2)p2=当前;
当前=当前->下一步;
}
如果(p1==NULL)p1=newVertex(node1);
如果(p2==NULL)p2=newVertex(node2);
附加值(p1、p2、重量);
附加值(p2、p1、重量);
}
void main(整型argc,字符**argv){
int节点1、节点2、权重;
while(EOF!=scanf(“%d%d%d\n”、&node1、&node2、&weight)){
插入(节点1、节点2、重量);
}
}

问题是什么?你在坚持什么?至于逻辑,我想
struct main node
的成员
neighbor
应该声明为neighbor\u node\ptr。if两个
语句的主体非常相似。我建议将其排除在普通事物之外。这样,它将更具可读性,更清晰再次遇到同样的问题。
=
是一个作业!我会稍微修改你的帖子+1我发誓我需要编写一个正则表达式,扫描一个文件,查看控制流语句表达式,然后进行一次符号运算,而不是相等性测试。我只能想象,用这样一只野兽回答一些SO问题的速度有多快。眼光真好。
#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);
    }
}