Problem in Adjancey list

This is a discussion on Problem in Adjancey list within the C Programming forums, part of the General Programming Boards category; I have made program for an adjancy list. First column is dynamically allocated and it's next nodes are allocated through ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Problem in Adjancey list

    I have made program for an adjancy list. First column is dynamically allocated and it's next nodes are allocated through linked list. I am not getting proper output.Pls help

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    
    struct node
    {
    int info;
    struct node *next;
    }*temp;
    
    struct fcol
    {
    int info;
    struct node *next;
    }*firstcol;
    void createlist(int*,int);
    
    int main()
    {
    int *a,num,i,j;
    clrscr();
    printf("Enter the number of nodes...");
    scanf("%d",&num);
    
    a=(int *)malloc(num*num*sizeof(int));
    
    printf("Enter the adjacency matrix....");
    for(i=0;i<=num-1;i++)
    {
      for(j=0;j<=num-1;j++)
          scanf("%d",&a[i*num+j]);
    }
    
    createlist(a,num);
    for(i=0;i<=num-1;i++)
    {
      printf("node%d",firstcol[i].info);
      for(temp=firstcol[i].next;temp!=NULL;temp=temp->next)
         printf("->node%d",temp->info);
      printf("\n");
    }
    getch();
    return(0);
    }
    
    
    void createlist(int a[],int num)
    {
    int *row,i,j;
    temp=NULL;
    firstcol=NULL;
    //allocates 1d array for first col
    firstcol=(struct fcol*)malloc(num*sizeof(struct fcol));
    
    for(i=0;i<=num-1;i++)
    {
      firstcol[i].info=i+1;
      firstcol[i].next=NULL;
    }
    
    for(i=0;i<=num-1;i++)
    {
      for(j=0;j<=num-1;j++)
      {
        if(a[i*num+j]==1)
        {
           temp=firstcol[i].next;
           while(temp->next!=NULL)
    	 temp=temp->next;
    
           temp->next=(struct node*)malloc(sizeof(struct node));
           temp=temp->next;
           temp->info=j+1;
           printf("\t%d",temp->info);
           temp->next=NULL;
        }
      }
    }
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I am not getting proper output.Pls help
    My advice would be to change your program, so it gives "proper" output.






    You could also ask a smart question ( see sig ).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
      firstcol[i].next=NULL;
      temp=firstcol[i].next;
     while(temp->next!=NULL)
    temp->next=(struct node*)malloc(sizeof(struct node));
    Now can you see some thing.

    some others.
    Code:
    a=(int *)malloc(num*num*sizeof(int));
    Casting malloc.read faq.

    Code:
    void createlist(int*,int);
    remember array are not pointers although this is not giving error
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    #include <malloc.h>
    Most compilers don't need that header -- if yours does, get a new compiler.

    Code:
    void createlist(int*,int);
    
    void createlist(int a[],int num)
    As cbastard said, your function prototype and definitions are different.

    Why do you have a struct fcol?
    Code:
    struct node
    {
    int info;
    struct node *next;
    }*temp;
    
    struct fcol
    {
    int info;
    struct node *next;
    }*firstcol;
    ->
    Code:
    struct node
    {
    int info;
    struct node *next;
    } *firstcol, *temp;
    Unless you're planning to make them different.

    And you're casting malloc. And you're not freeing your malloc()ed data.

    I am not getting proper output.
    What output are you getting, then? And what is "proper" output?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks
    Code:
    void createlist(int*,int);
    
    void createlist(int a[],int num)
    As cbastard said, your function prototype and definitions are different.
    Actually, it's not really true. Arrays degrade into a pointer to their first element when passed. Any time you pass a single dimension array, it degrades to a pointer of that type. Thus, passing an "int[]", ends up a "int*". So it really doesn't matter in this case. After all, this is perfectly legal:
    Code:
    int array[] = { 1, 2, 3 };
    int *ptr = array;
    
    ptr[0] = 5; /* <--- Perfectly fine to do. */
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Okay, thanks. I always knew they had the same result, but I didn't know they were compatible.

    Well, at least you can't say that vaibhav is freeing his memory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 03:45 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21