Special Mantra with C: A Saint thought of teaching his disciples a special mantra to vanish things to one of his disciple. So he asks his disciples to collect some stones with different symbols on it. The task is to arrange the stones in ascending and descending order based on the symbols in stone. Once the Disciples are ready, saint will draw two symbols, the stones between those symbols should be arranged in ascending order and the remaining stones should be arranged in descending order, one who does the trick first will be thought the mantra.
Include function
void append (struct special_mantra**q,int n);
void displayData(struct special_mantra*q);
void sort(struct special_mantra**q, int l, int h);
append — append a new symbol of stone
displayData — display the symbol from each stone
sort — sort the symbols in between the start and end position
Input and Output Format:
If the list is empty, print “List is empty” and when an invalid start or end position is given print “List is empty or Invalid index”. Refer sample input output for further details.
Sample Input and Output 1:
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
1
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
7
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
2
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
5
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
8
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
3
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
6
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
2
The elements in the linked list are 1 7 2 5 8 3 6
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
3
Enter the starting position
1
Enter the ending position
3
The elements in the linked list are 1 2 7 8 6 5 3
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
4
Exit
Sample Input and Output 2:
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
1
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
7
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
2
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
5
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
8
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
3
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
1
Enter the value
6
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
2
The elements in the linked list are 1 7 2 5 8 3 6
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
3
Enter the starting position
4
Enter the ending position
7
The elements in the linked list are 7 2 1 3 5 6 8
1.Insert
2.Display
3.Sort
4.Exit
Enter the choice
4
Exit
Define a structure
struct special_mantra
{
int data ;
struct special_mantra* link ;
} ;
Solution
#include<stdio.h>
#include<stdlib.h>
struct special_mantra
{
int data;
int position;
struct special_mantra* link;
};
void append (struct special_mantra**q,int n);
void displayData(struct special_mantra*q);
void sort(struct special_mantra**q, int l, int h);
int main()
{
int n,choice=0,start,end;
struct special_mantra *list;
list = NULL;
while(choice!=4){
printf("1.Insert\n2.Display\n3.Sort\n4.Exit\n");
printf("Enter the choice\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the value\n");
scanf("%d",&n);
append(&list,n);
break;
case 2:
displayData(list);
break;
case 3:
printf("Enter the starting position\n");
scanf("%d",&start);
printf("Enter the ending position\n");
scanf("%d",&end);
sort(&list,start,end);
displayData(list);
break;
case 4:
printf("Exit\n");
break;
}
}
return 0;
}
void append (struct special_mantra **q,int n)
{
static int pos = 0;
struct special_mantra *nn = (struct special_mantra*) malloc(sizeof(struct special_mantra));
struct special_mantra *temp = *q;
nn->link = NULL;
nn->data = n;
nn->position = ++pos;
if(!(*q)){
*q = nn;
return;
}
while(temp->link){
temp = temp->link;
}
temp->link = nn;
}
void displayData(struct special_mantra *q){
struct special_mantra *h = q;
printf("The elements in the linked list are ");
while(q){
printf("%d ",q->data);
q=q->link;
}
printf("\n");
if(!h)
printf("List is empty\n");
}
struct special_mantra* max_index(struct special_mantra *q, int l, int h){
int max_ = -99999;
struct special_mantra *hh;
while(q){
if(((q->position)<l || (q->position)>h) && (max_ < q->data)){
max_ = q->data;
hh = q;
}
q = q->link;
}
return hh;
}
struct special_mantra* min_index(struct special_mantra *q, int l, int h){
int min_ = 99999;
struct special_mantra *hh;
while(q){
if(((q->position)>=l) && ((q->position)<=h) && (min_ > q->data)){
min_ = q->data;
hh = q;
}
q = q->link;
}
return hh;
}
int isValid(struct special_mantra *q, int l, int h){
int res = (l > 0) && (h>0);
do{
q = q->link;
}while(q->link);
return (l<= q->position && (h <= (q->position)) && res);
}
void sort(struct special_mantra **qq, int l, int h){
struct special_mantra *q = *qq;
if((!q) || (l>=h) || !isValid(q,l,h)){
printf("List is empty or Invalid index\n");
return;
}
if(!(q->link))
return;
int t, i = 1;
struct special_mantra* temp;
while(q){
if(i<l || i>h)
temp = max_index(q,l,h);
else
temp = min_index(q,l,h);
t = temp->data;
temp->data = q->data;
q->data = t;
q= q->link;
i++;
}
}
Happy Learning – If you require any further information, feel free to contact me.