i have done a program to reverse the list..and here the users gives the input(i.e.,if i give ./a.out 3 my output will be like this orginal:1 2 3 and reverse:3 2 1 ......)
but how my program should take the list as input from a file
. (note:The name of the input file should be taken as a command line argument. This file would contain a line of white-space separated list of number. eg(19 1 4 3 18)
can any one help me out
......Code:
/**********reversing list***********
#include<stdio.h>
struct link {
int key;
struct link *next;
};
struct link * create_llist(int max)
{
struct link *head;
struct link *ptr;
int n = 0 ;
if(max<1)return NULL;
ptr=(struct link *)malloc(sizeof(struct link));
head=ptr;
while(n++ < max-1)
{
ptr->key=n;
ptr->next=(struct link *)malloc(sizeof(struct link));
ptr=ptr->next;
}
ptr->key=n;
ptr->next=NULL;
return head;
}
void reverse_llist(struct link *head,struct link **newHead)
{
struct link **ptr=&head;
if(*ptr == NULL || (*ptr)->next == NULL) return ;
if((*ptr)->next->next != NULL)
reverse_llist((*ptr)->next,newHead);
else
*newHead = (*ptr)->next ;
(*ptr)->next->next = *ptr ;
(*ptr)->next = NULL;
}
print_list(struct link *head)
{
struct link *a=head;
if(!a) return ;
while(a->next)
{
printf(" %d ", a->key);
a=a->next;
}
printf(" %d ", a->key);
printf("\n");
}
main(int argc, char*argv[])
{
struct link *head;
if(!argc) return ;
head=create_llist(atoi(argv[1]));
printf(“Original:\t”);
print_list(head);
printf(“Reversed:\t”);
reverse_llist(head,&head);
print_list(head);
}
