A list of key values is maintained in non-decreasing order of their key values.
ID: 3561011 • Letter: A
Question
A list of key values is maintained in non-decreasing order of their key values. You may assume that all key values are different. In other words, the list is in increasing order. Implement your solution using a singly linked-list data structure. There are three functions to write:
1. insert: the insert function will add a given key value into the proper position of your list.
2. delete: the delete function will remove a given key value from the list. Print an appropriate warning message if the key value does not exist in the list.
3. print: the print function will output the whole list of keys in non-decreasing order.
To test the functions, write a main program which will read in an initial list of key values. Each time a new key is read, echo-print the key value and use the insert function to insert it into the list, and then use the print function to show the content of the whole list after the insertion.
Use the following list to test your implementation of insert: 8, 25, 3, 15, 7, 4, 28, 2
After the whole list has been inserted, perform the deletions of the following keys in the given order: 15, 5, 2, 28, 7, 25
For each key to delete, you may also want to read in the key value, print the key value to be removed, use the delete function, and then use the print function to show the new list of keys.
ill add more points
Explanation / Answer
#include<stdio.h>
#include<conio.h>
typedef struct node{
int data;
struct node *next;
}nodee;
typedef nodee *list;
int main()
{
list head,temp;
int n,op,i;
head =NULL;
while(1)
{
printf("Choose the operation ");
printf("1. INSERT ");
printf("2. DELETE ");
printf("3. TRAVERSE ");
printf("4. EXIT ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("Enter the item ");
scanf("%d",&n);
temp = (list)malloc(sizeof(nodee));
if(head==NULL)
{
temp->data=n;
head=temp;
temp->next=NULL;
}
else
{
temp->data=n;
temp->next=head;
head=temp;
printf("Insertion is completed ");
}
break;
case 2:
temp=head;
if(temp==NULL)
{
printf("Underflow ");
}
else
{
i=temp->data;
printf("The deleted item is %d ",i);
temp=head->next;
head=temp;
}
break;
case 3:
temp=head;
if(temp==NULL)
{
printf("The list is empty. ");
}
else
{
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf(" ");
}
break;
case 4:
exit(0);
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.