#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct test_struct
{
int val;
struct test_struct * next ;
};
struct test_struct * head = NULL;
struct test_struct * curr = NULL;
struct test_struct * create_list( int val )
{
struct test_struct * ptr = (struct test_struct*) malloc( sizeof (struct test_struct ));
if ( NULL == ptr )
{
printf("\n Node creation failed ");
return NULL;
}
ptr -> val = val;
ptr -> next = NULL;
head = curr = ptr;
return ptr;
}
struct test_struct * add_list(int val, bool add_to_end )
{
if ( NULL == head )
{
head = create_list(val);
return head;
}
struct test_struct * ptr = ( struct test_struct *)malloc( sizeof(struct test_struct ));
if ( ptr == NULL )
{
printf("\n node failed ");
return NULL;
}
ptr -> val = val;
ptr -> next = NULL;
if( add_to_end )
{
curr -> next = ptr;
curr = ptr;
}
else
{
ptr -> next = head;
head = ptr;
}
return ptr;
}
void print_list ()
{
struct test_struct * ptr = head;
printf("\n\n Printing the list ");
while ( ptr != NULL )
{
printf("\n %d ", ptr->val);
ptr = ptr-> next;
}
}
int main()
{
int i=0, ret = 0;
struct test_struct * ptr = NULL;
//print_list();
printf("\n Adding to list ");
for ( i = 10; i< 20; i++)
add_list(i, true);
print_list();
printf("\n Adding to list ");
for ( i = 0 ; i< 30; i++)
add_list(i, false );
print_list();
}
No comments:
Post a Comment