typedef struct node
{
int data;
struct node *next;
} NODE;
NODE * reverse( NODE *);
void print_list(NODE *);
NODE *insert_node(NODE *);
main()
{
NODE * front = NULL;
char ch;
printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
while(1)
{
scanf("%c",&ch);
switch(ch)
{
case '1':front=insert_node(front);
printf("\n====Node is inserted===\n");
printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
break;
case '2':print_list(front);
printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
break;
case '3':front = reverse(front);
printf("\n====Node is reversed===\n");
printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
break;
case '4':free(front);
return(0);
};
}
}
NODE * insert_node(NODE * ptr)
{
NODE * newnode ;
int newnode_data;
NODE *temp=ptr;
NODE *front=NULL;
/* INSERT FROM FRONT*/
if(ptr == NULL)
{
ptr = (NODE *)malloc(sizeof(NODE *));
printf("\n=====Enter the data for the first newnode ======\n");
scanf("%d",&newnode_data);
ptr->data = newnode_data;
ptr->next = NULL;
front = ptr;
}
else
{
newnode = (NODE *)malloc(sizeof(NODE *));
printf("\n=====Enter the data for the newnode ======\n");
scanf("%d",&newnode_data);
newnode->data = newnode_data;
newnode->next = ptr;
front = newnode;
}
return front;
}
void print_list(NODE *ptr)
{
NODE *temp_ptr=ptr;
int count=0;
if(temp_ptr == NULL)
{
printf("\nNo list to print \n");
return;
}
while(temp_ptr != NULL)
{
printf("\nnode [%d] : data [%d]\n",count,temp_ptr->data);
temp_ptr=temp_ptr->next;
count++;
}
return;
}
NODE * reverse( NODE * root )
{
NODE * ret_val;
if ( root == NULL || root->next == NULL )
return root;
ret_val = reverse(root->next);
(root -> next)->next = root;
root->next = NULL;
return ret_val;
}
No comments:
Post a Comment