Thread: Getting Worse with LL

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Getting Worse with LL

    Hello everybody,
    It been a while now that I am working with Linked lists.
    But still get stuck very easily.
    I was wondering if some1 can comment on the following code.
    I had put some part of the code before too......
    I am trying to make a adjacency list from my input data.I am not able to compile it yet....
    I have already spent a lot of time on it.Your help is appreciated.
    Thanks..
    Code:
    #include <stdio.h>  // I/O functions
    #include <stdlib.h> // malloc, realloc...
    #include <string.h> // string functions
    int edge, *pC1,*pC2;
    float *pC3;
    struct adj_node  /*the NODE which forms the part of the adjacency list; */
    {
      char vertex_val;
      struct adj_node *adj_next; 
    };
    
    struct node /*the NODE which forms the part of the Graph; */
    {
      int *vertex_val;
      int *vertex_val1;
      float* prob;
      struct adj_node *down;
      struct node *next; 
    };
    
    struct node *graph; 
    struct node *cur_vertex, *aux_ptr;
    
    
    void LinkListrepresentation(graph )
    {
      struct adj_node *adj_cur, *new;
      cur_vertex = graph;   /*first node of the graph in consideration*/
      while ( cur_vertex != NULL)
        {
          aux_ptr = graph; /*an auxiliary pointer to the graph*/
          adj_cur = NULL; /*an auxiliary pointer to keep track of the adjacency lists*/
          cur_vertex->down = adj_cur; 
          while (aux_ptr != NULL)
    	{
    	  *aux_ptr->prob == edge;
    	  if( edge != 0) 
    	    if( adj_cur == NULL)
    	      {
    		adj_cur = (struct adj_node*)malloc(sizeof(adj_node));
    		adj_cur ->vertex_val = *aux_ptr->vertex_val;
    		adj_cur -> adj_next = NULL;
    	      } 
    	    else
    	      {
    		new = (struct adj_node *)malloc(sizeof(adj_node));
    		new -> vertex_val = *aux_ptr ->vertex_val;
    		adj_cur ->adj_next = new;
    		adj_cur = new; 
    		adj_cur ->adj_next = NULL;
    	      }  
    	  aux_ptr = aux_ptr ->next;
    	}
          cur_vertex = cur_vertex ->next;
        }
      printf("%d value\n", *cur_vertex);
    }
    int main ()
    {
      int *c1; // reading first column
      int *c2; //reading second column
      float *c3; // reading third column
      FILE *fp2;  
      int i;
      int edges = 0;
      char c;
      
      fp2 = fopen("data.txt","r");    // Reading the Edge file
      while((c=getc(fp2))!=EOF) 
        {
          if(c=='\n')
    	edges = edges + 1;
        }
      printf("\tThe total number of edges: %d\n", edges);   // number of edges in the network
      c1 = (int*) calloc(edges, sizeof(int));
      c2 = (int*) calloc(edges, sizeof(int));
      c3 = (float*) calloc(edges, sizeof(float));
      rewind(fp2);
      for (i=0;i<edges;i++)
        {
          fscanf(fp2,"%d  %d %f \n", &c1[i], &c2[i], &c3[i]);
        }
      struct node * graph, * head;
      head = NULL;
      for(i=0;i<edges;i++) {
        graph = (struct node*)malloc(sizeof(struct node));
            graph->vertex_val = &c1[i];
    	graph->vertex_val1 = &c2[i];
    	graph->prob = &c3[i];
    	graph->next  = head;
    	head = graph;
      }
      graph = head;
      LinkListrepresentation(graph );
      return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I am not able to compile it yet....
    Does that mean you haven't attempted to compile it yet because the code is incomplete? Or, does it mean you have attempted to compile it but could not resolve the errors? If you have compiled it what are the errors?

    At a glance:


    #1
    Code:
    struct node *graph; 
    struct node *cur_vertex, *aux_ptr;
    
    void LinkListrepresentation(graph )
    {
      struct adj_node *adj_cur, *new;
      cur_vertex = graph;   /*first node of the graph in consideration*/
    I think maybe you meant struct node * graph instead of just simply graph.



    #2
    Code:
    *aux_ptr->prob == edge;
    This code has no effect. Did you mean to use = vs. ==?



    #3
    Code:
    if( adj_cur == NULL)
    {
        adj_cur = (struct adj_node*)malloc(sizeof(adj_node));
    
    ...
    
    else
    {
        new = (struct adj_node *)malloc(sizeof(adj_node));
    
    ...
    
    printf("\tThe total number of edges: %d\n", edges);   // number of edges in the network
    c1 = (int*) calloc(edges, sizeof(int));
    c2 = (int*) calloc(edges, sizeof(int));
    c3 = (float*) calloc(edges, sizeof(float));
    
    ...
    
    for(i=0;i<edges;i++) {
        graph = (struct node*)malloc(sizeof(struct node));
    Avoid casting the return value of those malloc/calloc calls. The return type is void* and they can be safely assigned without the cast.



    #4
    Code:
    char c;
    
    ...
    
    fp2 = fopen("data.txt","r");    // Reading the Edge file
    while((c=getc(fp2))!=EOF) 
    {
        if(c=='\n')
            edges = edges + 1;
    }
    getc returns an int, not a char.


    That's all I saw at a quick glance. I haven't tried to go through the logic of your program at all.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    thanks...
    i tried compiling but i get good amt of errors
    Comment1: I tried that way too. Just help much.
    Comment 2:*aux_ptr->prob == edge;
    .U are right, i shd be =.
    Comment 3: I didnt get it. Also i compile my program, it says...
    adj_node : Undeclared, though it is already declared in the structure.
    Comment4: point taken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LL Search Function
    By mikeman in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2010, 02:19 PM
  2. compare backward ll ints
    By Soulzityr in forum C Programming
    Replies: 20
    Last Post: 02-28-2010, 07:19 AM
  3. Dynamic LL && inheritance, help?
    By Else in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2009, 02:46 AM
  4. Doubt on Linked lists and doubly LL!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 06-28-2006, 10:29 PM