#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
//struct node*
void even_odd(struct node *current)
{
struct node* even=NULL;
struct node* odd=NULL;
struct node* oddstart=NULL;
//struct node* current=*head;
while(current)//partition linked list in two part
{
if((current->data)&1)//%2!=0)
{
if(odd)
odd->next=current;
else
oddstart=current;
odd=current;
}
else
{
if(even)
even->next=current;
even=current;
}
current=current->next;
}
if(even)
even->next=oddstart;// append to odd-starting position
if(odd)
odd->next=NULL;////make odd last equal to null;
}
/* Utility function to print a linked list */
void printList(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
push(&head, 12);
push(&head, 4);
push(&head, 12);
push(&head, 3);
push(&head, 16);
push(&head, 13);
push(&head, 8);
push(&head, 16);
push(&head, 15);
push(&head, 5);
even_odd(head);
printList(head);
}
Run Here
No comments :
Post a Comment