Jump to content

recursive function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
pretty.gal13

pretty.gal13

    Newbie

  • Members
  • Pip
  • 5 posts
hey guys pls can u gimme sm idea as to how to do recursion using linked lists?
Suppose if we have to print an array of integers line by line..with parameters as int size and int a[].

i,ve done d following thing but der r many errors in it!
int m,i,n;
struct node
{
int size,a[10];
struct node *next;
};

void main()
{
struct node *head=NULL,*temp;
clrscr();
printf("How many integers do u wanna enter?");
scanf("%d",&size);

printf("Enter the elements");
for(i=0;i<size;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%d",&temp->a[i]);
}
printf("The elements are %d",a[i]);
prob(*temp,a[]);
getch();
}
int prob(struct node *temp,int a[10])
{int size;
if(temp==NULL)
return a[0];
else
temp=a[0];

prob(temp->next,temp->a[i]);
printf("\n");

getch();
}

im not clear regarding the concepts of linked list and recursion.

pls help.

Edited by WingedPanther, 03 March 2009 - 06:54 PM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What is this code supposed to do?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
pretty.gal13

pretty.gal13

    Newbie

  • Members
  • Pip
  • 5 posts
it is supposed to print an array of integers line by line using the concept of linked list and recursive functions.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm attempting to analyze this as if it were C/C++, not C#, and hoping my comments hold true. Based on what I'm seeing, it looks like you don't understand how to use a linked list.
int m,i,n;

struct node
{
  int size,a[10];
  struct node *next;
};

void main()
{
  struct node *head=NULL,*temp; //head is never used, why is it declared?
  clrscr();
  printf("How many integers do u wanna enter?");
  scanf("%d",&size);  //size not declared!  this will not compile!
  
  printf("Enter the elements");
  for(i=0;i<size;i++)
  {
    temp=(struct node*)malloc(sizeof(struct node)); //this is leaking memory, you lose your previous node with this statement.
    scanf("%d",&temp->a[i]);
  }
  printf("The elements are %d",a[i]); //a not declared!  this will not compile!
  prob(*temp,a[]);
  getch();
}

int prob(struct node *temp,int a[10])
{
  int size;
  if(temp==NULL)
  return a[0];
  else
  temp=a[0];  //implicit cast from int to pointer.  This is VERY dangerous!
  
  prob(temp->next,temp->a[i]);
  printf("\n");
  
  getch();
}

Something that may help is stating the problem you've been given. What you appear to have here is a linked list of arrays. Each node of the linked list can have up to 10 ints in the array, and you can have an arbitrary number of nodes. It looks like you are trying to use it as a simple array... or a linked list of ints, however. My guess is that you don't have a recursion problem, you have a problem with understanding what your data needs to be. State the problem and I can try to help you understand the correct way to model your data.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
your first problem is the code you provided isn't even in C# it is very obviously plain C. It surprises me WP didn't pick up on the fact it was C.
Posted via CodeCall Mobile

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Sorry, but C# isn't on my list of languages. I knew it looked fishy, though.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog