Don't remember exact problem description but following is the behaviour expected and program should be written using linked list.
Example 1:
Input:
8 5
Output:
5 4 3 2 1 6 7 8
Example 2:
Input:
8 1
Output:
1 2 3 4 5 6 7 8
Example 3:
Input:
8 3
Output:
3 2 1 6 5 4 7 8
Following is the C program.
#include<stdio.h>
#include<malloc.h>
typedef struct ll{
int i;
struct ll *next;
}ll;
ll *head;
ll *tail;
void insert_data(int data)
{
if(head == NULL)
{
head = (ll*)malloc(sizeof(ll));
head->i = data;
head->next = NULL;
tail = head;
//printf("address %x\n",head);
}
else
{
tail->next = (ll*)malloc(sizeof(ll));
tail = tail->next;
tail->i = data;
tail->next = NULL;
//printf("address %x\n",tail);
}
}
void delete_node(ll *node)
{
printf("%d ",node->i);
//printf("address delete %x\n",node);
free(node);
}
ll* recursive_delete(int K,ll *node)
{
ll *temp;
if((K==0) || (node == NULL))
{
//printf("line %d\n",__LINE__);
return NULL;
}
temp = recursive_delete(K-1,node->next);
if((temp ==NULL) && (K ==1))
{
//printf("line %d\n",__LINE__);
head = node->next;
delete_node(node);
return head;
}
else if((K>1) && (temp == NULL))
{
if(head == NULL)
{
delete_node(node);
}
//printf("line %d\n",__LINE__);
return NULL;
}
else
{
//printf("line %d\n",__LINE__);
delete_node(node);
return temp;
}
}
int main()
{
ll* temp;
int N,K;
int i;
printf("enter number of items and size of sub segment:");
scanf("%d %d",&N,&K);
for(i = 1;i<=N;i++)
{
insert_data(i);
}
for(i = 1;i<=N;i +=K)
{
if(recursive_delete(K,head) == NULL)
{
while(head != NULL)
{
temp = head;
head = head->next;
delete_node(temp);
}
}
}
printf("\n");
return 0;
}
Example 1:
Input:
8 5
Output:
5 4 3 2 1 6 7 8
Example 2:
Input:
8 1
Output:
1 2 3 4 5 6 7 8
Example 3:
Input:
8 3
Output:
3 2 1 6 5 4 7 8
Following is the C program.
#include<stdio.h>
#include<malloc.h>
typedef struct ll{
int i;
struct ll *next;
}ll;
ll *head;
ll *tail;
void insert_data(int data)
{
if(head == NULL)
{
head = (ll*)malloc(sizeof(ll));
head->i = data;
head->next = NULL;
tail = head;
//printf("address %x\n",head);
}
else
{
tail->next = (ll*)malloc(sizeof(ll));
tail = tail->next;
tail->i = data;
tail->next = NULL;
//printf("address %x\n",tail);
}
}
void delete_node(ll *node)
{
printf("%d ",node->i);
//printf("address delete %x\n",node);
free(node);
}
ll* recursive_delete(int K,ll *node)
{
ll *temp;
if((K==0) || (node == NULL))
{
//printf("line %d\n",__LINE__);
return NULL;
}
temp = recursive_delete(K-1,node->next);
if((temp ==NULL) && (K ==1))
{
//printf("line %d\n",__LINE__);
head = node->next;
delete_node(node);
return head;
}
else if((K>1) && (temp == NULL))
{
if(head == NULL)
{
delete_node(node);
}
//printf("line %d\n",__LINE__);
return NULL;
}
else
{
//printf("line %d\n",__LINE__);
delete_node(node);
return temp;
}
}
int main()
{
ll* temp;
int N,K;
int i;
printf("enter number of items and size of sub segment:");
scanf("%d %d",&N,&K);
for(i = 1;i<=N;i++)
{
insert_data(i);
}
for(i = 1;i<=N;i +=K)
{
if(recursive_delete(K,head) == NULL)
{
while(head != NULL)
{
temp = head;
head = head->next;
delete_node(temp);
}
}
}
printf("\n");
return 0;
}
No comments:
Post a Comment
తెలుగులో వ్రాయడానికి http://www.google.com/ime/transliteration/ ఉపయోగించండి.