1. (a) Creating a dynamic linked list that is sorted online: Write a program that allows the user to enter a set of non-negative integers one by one via the keyboard (until the user enters a negative integer) and then ‘dynamically create a linked list’ of these integers which is sorted (in ascending order) ‘online’. By asking to ‘dynamically’ create a linked list, we mean that the memory/space for each new element of the list is allotted during runtime as and when the user inputs that element. Moreover, by asking to sort the list ‘online’, we mean that each new element given as input by the user is placed at an appropriate place in the list so that the list thus obtained is sorted. Hence, the list is always in a sorted state.
(b) Once the above list has been created, the program shall display a menu with the following options (these options are to be implemented using separate functions):-
i. Add a new element to the list such that the resultant list is again in the sorted state.
ii. Delete a specific element such that the resultant list is again in the sorted state.
iii. Search for an element in the list.
iv. Display the list.
v. Exit.
THIS IS THE PROGRAM
#include<stdio.h>
#include<stdlib.h>
typedef struct tag{
int a;
struct tag *next;
}data;
data *addele(data *head);
data *subele(data *head);
void search(data *head);
void display(data *head);
int main()
{
int tempo;
data *head,*temp,*prev,*next; head->next=NULL; int count=0; head=(data*) malloc(sizeof(data));
printf("enter the number you want to add in the list followed by a negative number to end this list \n"); scanf("%d",&tempo);
do
{ if(count==0)
{
scanf("%d",&tempo);
if(tempo>0){ head->a=tempo;count++;} else break;}
else scanf("%d",&temp);
if(tempo<head->a&&tempo>0) {
temp->next=head; head=temp;}
else
{
for(prev=head,next=prev->next;next!=NULL;prev=next,next=next->next)
{ temp=(data*) malloc(sizeof(data));
if(tempo>prev->a&&tempo<next->a)
{ temp->a=tempo;
prev->next=temp; temp->next=next;
}
if(next->next=NULL&&next->a<tempo)
{
temp->a=tempo;
next->next=temp; temp->next=NULL;
}
}
}}while(temp>0);
while(1)
{
printf("please enter any of the options by choosing from the menu:\n");
printf(" 1. Add a new element to the list such that the resultant list is again in the sorted state\n 2: Delete a specific element such that the resultant list is again in the sorted state\n 3. Search for an element in the list\n 4. Display the list.\n 5. Exit.");
int tempor;
scanf("%d",&tempor);
switch(tempor)
{
case 1:
{
head=addele(head);
break;
}
case 2:
{
head=subele(head);
break;
}
case 3:
{
search(head);
break;
}
case 4:
{
display(head);
break;
}
case 5:
{
exit(1);
break;
}
}}
return 0;
}
data *addele(data *head)
{ data *temp,*prev,*next;
int tempo;
printf("enter the number you want to add:\n");
scanf("%d",&temp);
if(tempo<head->a&&tempo>0) {
temp->next=head; head=temp;}
else
{
for(prev=head,next=prev->next;next!=NULL;prev=next,next=next->next)
{ temp=(data*) malloc(sizeof(data));
if(tempo>prev->a&&tempo<next->a)
{ temp->a=tempo;
prev->next=temp; temp->next=next;
}
if(next->next=NULL&&next->a<tempo)
{
temp->a=tempo;
next->next=temp; temp->next=NULL;
}
}
}
return head;
}
data *subele(data *head)
{ int tempo; data *temp,*prev,*next;
printf("enter the number which you want to subtract:\n");
scanf("%d",&temp->a); temp=head;
for(prev=head,prev->next=next;next->next!=NULL;prev=next,next->next=NULL)
{
if(temp->a==next->a)
{
prev->next=next->next;
free(next);
}
}
return head;
}
void search(data *head)
{
int tempo; data *temp;
printf("enter the integer you want to search :\n");
scanf("%d",&tempo);
for(temp=head;temp->next!=NULL;temp=temp->next)
{
if(temp->a==tempo)
printf("number found\n");
}
}
void display(data *head)
{
data *temp;
for(temp=head;temp->next!=NULL;temp=temp->next)
printf("%d\n",temp->a);
return;
}
[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]