Project Linked List: Implement problems 2, 3, and 4 from the end of the chapter
ID: 3598692 • Letter: P
Question
Project Linked List: Implement problems 2, 3, and 4 from the end of the chapter Programming Exercises. Copy the classes and add the new functions.
For your test program, present the user with the following menu of options. Your program stays in a loop and performs the function chosen, until the user enters 9 (to quit).
1- Add a new item to the list
2- Delete the first occurrence of a given item from the list
3 Delete all occurrences of a given item from the list
4- Delete the smallest item from the list
5- Print the kth item in the list
6- Delete the kth item from the list
7- Print the list in reverse
8- print the list
9- Quit
Explanation / Answer
#include<stdio.h>
#define arraySize 100
int main()
{
int list[arraySize], items=0, choice=0, x, i, j, loc, mItm, mItmLoc;
while(choice != 9)
{
printf(" ********* MENU *********");
printf(" 1- Add a new item to the list");
printf(" 2- Delete the first occurrence of a given item from the list");
printf(" 3- Delete all occurrences of a given item from the list");
printf(" 4- Delete the smallest item from the list");
printf(" 5- Print the kth item in the list");
printf(" 6- Delete the kth item from the list");
printf(" 7- Print the list in reverse");
printf(" 8- print the list");
printf(" 9- Quit *********");
printf(" Enter your choice: "); scanf("%d",&choice);
switch(choice)
{
case 1: printf(" Enter item: "); scanf("%d",&x); //Get the item
list[items]=x; items++; break; //and place it in the last. Increment numbe of items count
case 2: printf(" Enter item: "); scanf("%d",&x);//Get the item
loc=-1; //search for it. assuming it is not present
for(i=0; i<items; i++) if(list[i]==x) { loc=i; break; } //until you got it at loc
if(loc>-1) for(i=loc+1; i<items; i++) list[i-1]=list[i]; //shift all items after location loc one step down. This would delete item at location loc
items--; break; // number of items is now one less
case 3: printf(" Enter item: "); scanf("%d",&x); //same as case 2. here every time we see the item we go for deletion
for(i=0; i<items; i++) if(list[i]==x) {
for(j=i+1; j<items; j++) list[j-1]=list[j];
items--;
} break;
case 4: mItm=list[0]; mItmLoc=0; //Search for the smallest item from the list in incremental way. Let first item is amallest
for(i=0; i<items; i++) if(mItm>list[i]) { mItm=list[i]; mItmLoc=i; } // and then increase the size
for(i=mItmLoc+1; i<items; i++) list[i-1]=list[i]; // deletion is again shifting
items--; break;
case 5: printf(" Enter location: "); scanf("%d",&i); // get whic location and print item at that lication
if(i>0)if(i<=items) printf(" Item at location %d is %d",i,list[i-1]); break;
case 6: printf(" Enter location: "); scanf("%d",&loc); // deletion at a location is again same as case 2
if(loc>0)if(loc<=items) for(i=loc; i<items; i++) list[i-1]=list[i];
items--; break;
case 7: printf(" Reverse List: "); for(i=0; i<items; i++) printf("%d ",list[items-i-1]);break;
case 8: printf(" List: "); for(i=0; i<items; i++) printf("%d ",list[i]); break;
case 9: break;
default: printf(" [Error] Wrong choice! (enter 1-9) Please try again.");
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.