dimanche 29 mars 2015

creating printing and counting linked lists using recursion



i tried to create linked list as follows but the output comes a fixed list of two elements and count to be 2



#include<stdio.h>
#define null 0
struct list
{
int num;
struct list *next;
};

typedef struct list node;

int create(node *list)
{ int n;
printf("enter the element to end the list finish it with -999\n");
scanf("%d",&n);
if(n==-999)return 0;
else {
list=(node *)malloc(sizeof(node *));
list->num=n;
if(!(create(list->next)))list->next=null;
return 1}
}
void printlist(node * list) {
if(list->next==null)
{printf("%d->",list->num);return;}
else
{printf("%d->",list->num);printlist(list->next);}
return;
}

int count(node *list) {
if(list->next==null)return 1;
else return(1+count(list->next));
}

void main() {
node *list;
create(list);
printlist(list);
printf("\n%d",count(list));
}


is there any problem with passing pointer to the function.




Aucun commentaire:

Enregistrer un commentaire