Thread: Linked List

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

    Linked List

    Hello!
    I am trying hard to follow the Linked List concept.
    I was trying to work towards the adjacent list. I am using the function to make adjacent list but am not able to pass my data to it.
    Can someone help me with this?
    the code is
    Code:
    struct adj_node  /*the NODE which forms the part of the adjacency list; look at figure [B]*/
    {
    char vertex_val;
    struct adj_node *adj_next; /*red pointer*/
    };
    
    struct node /*the NODE which forms the part of the Graph; look at figure [A]*/
    {
    char vertex_val;
    struct adj_node *down;
    struct node *next;  /*black pointer*/
    };
    
    struct node *graph; 
    struct node *cur_vertex, *aux_ptr;
    struct adj_node *adj_cur, *new;
    
    LinkListrepresentation()
    {
    
    
    /*Step-1 of the algorithm is expected from you or refer linked LISTS*/ 
    /*Step-1 gives you a list, being pointed by 'graph' */ 
    
    cur_vertex = graph;   /*first node of the graph in consideration*/
    /*Step-2 starts here*/
    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)
                 {
                  printf("Is there any edge between %c and %c ",aux_ptr->vertex_val, cur_vertex->vertex_val);
                  scanf("%d", &edge);/*step-2: part-A*/
                  if( edge == 1) /*step-2: part - B*/
                     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;
         }
    }
    I want to pass the data to the above function for making the LL but i dont know how it shd be passed.
    I am reading the data from the file as
    Code:
    FILE *fp;
      int i, count = 0;
      char c;
      
      fp = fopen("data","r");    // reading the Edge file
      while((c=getc(fp))!=EOF) 
        {
          if(c=='\n')
    	count++;
        }
      printf("\tThe total number of entries: %d\n", count);   
      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(fp,"%d  %d %f \n", &C1[i], &C2[i], &C3[i]);
          //   printf("%d  %d %f  %c\n", C1[i], C2[i], pC3[i], C4[i]);   
          
    	}

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Is that all of the code? Sure doesn't seem like it. Most of your used variables are undeclared. And passing the root node to your function should be enough, checking for node->next != NULL

    Code:
    while(node){
        ...
        node = node->next
    }

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    I am reading my file in 3 columns. C1, C2, C3.
    I dont know how to put that in LL.
    C1 = Node1
    C2 = Node2
    C3 = Value.

  4. #4
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    it didnt work out for me
    I couldn't pass the input file to the above mentioned structure.
    Am I reading my data in some wrong format ????

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    This is rather confusing as we don't have the assignment you were given for this (nor figures A and b), and there is a bit more going on than just linked lists. Looks like what you need to do is populate the graph variable before calling the function.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM