7.Starting from picture C, draw the pictures that show step by step what is need
ID: 3588516 • Letter: 7
Question
7.Starting from picture C, draw the pictures that show step by step what is needed to get to picture D. We are assuming the part of the linked list shown is somewhere in the middle of a long linked list.
C D
Using your pictures, write the code to delete a node from the middle of the linked list starting from picture C. Now insert before it the code that will move currentPtr and previousPtr through the list until it finds a particular value if it exists (look at the find function from above). The previousPtr should start out as NULL and always be one node behind the currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to a linked list and a value, and if the value is in the linked list, it is deleted and the function returns true. If it is not in the list, the function returns false.
8. Expand your menu to test your three new functions. When choosing to test the find or findAndDelete functions, the user will be first asked to enter the value to find or to find and delete. Depending on whether the functions returned true or false, the main program should write an appropriate message to the user.
9. Finally write a function called deleteList that will delete all nodes in the linked list and return the parameter head set to nullptr. You can add it to the menu for testing, but also put it at the end of the program to release all dynamic memory you used.
18 8 NULL NULL currentPtr previousPt c currentPtr previousPtrExplanation / Answer
Ans 7.
#include<stdio.h>
#include<stdbool.h>
#include<malloc.h>
bool findAndDelete(int);
void insertnode();
void show();
int flag=0;
bool b;
struct N
{
int info;
struct N *next;
}*node,*head,*ptr,*previousPtr,*currentPtr;
void main()
{
int s;
insertnode();
show();
printf("Enter your option: ");
printf("Press 1 to find the element ");
printf("Press 2 to find and delete the element ");
scanf("%d",&s);
b = findAndDelete(s);
if((s==1)&&(b==true))
printf("Element found in list ");
if((s==2)&&(b==true))
printf("Element is found and deleted ");
show();
}
void insertnode()
{
int value[3]={9,13,8},x=1,i=0;
do{
node = (struct N*)malloc(sizeof(struct N));
if(node == '')
{
printf("Memory is full ");
return 0;
}
else
{
node->info=value[i];
node->next='';
if(head=='')
head = node;
else
{
ptr=head;
while(ptr->next!='')
ptr = ptr->next;
ptr->next = node;
}
i++;
x++;
}
}while(x!=4);
}
bool findAndDelete(int s)
{
int y;
printf("Enter a element to be find in linked list ");
scanf("%d",&y);
if(s==1)
{
currentPtr = head;
while(currentPtr)
{
if(y==currentPtr->info){
flag=1;
break;
}
else
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
}
}
else{
currentPtr = head;
while(currentPtr)
{
if(y==currentPtr->info){
previousPtr->next = currentPtr->next;
flag = 1;
break;
}
else
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
}
}
if(flag == 1)
return true;
else
return false;
}
void show()
{
ptr = head;
while(ptr)
{
printf("%d ",ptr->info);
ptr = ptr->next;
}
}
Ans 8 and 9.
#include<stdio.h>
#include<stdbool.h>
#include<malloc.h>
bool findAndDelete(int);
void insertnode();
void show();
void test();
void deleteall();
int flag=0;
bool b;
struct N
{
int info;
struct N *next;
}*node,*head,*ptr,*previousPtr,*currentPtr;
void main()
{
int s;
insertnode();
show();
printf(" Enter your option: ");
printf("Press 1 to find the element ");
printf("Press 2 to find and delete the element ");
printf("Press 3 to test the functions ");
printf("Press 4 to delete all elements ");
scanf("%d",&s);
if((s==1)||(s==2))
b = findAndDelete(s);
else if(s==3)
test();
else if(s==4)
deleteall();
if((s==1)&&(b==true))
printf("Element found in list ");
if((s==2)&&(b==true))
printf("Element is found and deleted ");
show();
}
void insertnode()
{
int value[3]={9,13,8},x=1,i=0;
do{
node = (struct N*)malloc(sizeof(struct N));
if(node == '')
{
printf("Memory is full ");
return 0;
}
else
{
node->info=value[i];
node->next='';
if(head=='')
head = node;
else
{
ptr=head;
while(ptr->next!='')
ptr = ptr->next;
ptr->next = node;
}
i++;
x++;
}
}while(x!=4);
}
bool findAndDelete(int s)
{
int y;
printf("Enter a element to be find in linked list ");
scanf("%d",&y);
if(s==1)
{
currentPtr = head;
while(currentPtr)
{
if(y==currentPtr->info){
flag=1;
break;
}
else
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
}
}
else{
currentPtr = head;
while(currentPtr)
{
if(y==currentPtr->info){
previousPtr->next = currentPtr->next;
flag = 1;
break;
}
else
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
}
}
if(flag == 1)
return true;
else
return false;
}
void test()
{
int s;
printf("you want to find an element then press 1 ");
printf("Else press 2 to find and delete the element ");
scanf("%d",&s);
b=findAndDelete(s);
}
void deleteall()
{
head = NULL;
printf("List empty ");
}
void show()
{
ptr = head;
while(ptr)
{
printf("%d ",ptr->info);
ptr = ptr->next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.