Question: I need help correcting the code to a logical order instead of a physical order deletion. Please help. This is the prompt Make a linked

I need help correcting the code to a logical order instead of a physical order deletion. Please help. "This is the prompt Make a linked list using arrays and no pointers consisting of the following items, D, G,J,M,R,W. Show the list before the link list is created. Next show me the list pointer, the array list and the link list next to each item using the table below as an example.
Next write the code to add B and delete M. Make the code so when I run the program, I can add and delete an item."
#include
using namespace std;
const int N =100; // Maximum number of elements
char list[N];// Array to store elements
int link[N];// Array to store the index of the next element
int first =0;// Index of the first element
void initializeList(){
list[0]='D'; link[0]=1;
list[1]='G'; link[1]=2;
list[2]='J'; link[2]=3;
list[3]='M'; link[3]=4;
list[4]='R'; link[4]=5;
list[5]='W'; link[5]=99;
}
void add(char item){
int i = first;
while (link[i]!=99){
i = link[i];
}
for (int j =0; j N; j++){
if (list[j]=='\0'){
list[j]= item;
link[i]= j;
link[j]=99;
break;
}
}
}
void deleteElement(char item){
int i = first;
int prev =99;
while (i !=99){
if (list[i]== item){
if (prev !=99){
link[prev]= link[i];
} else {
first = link[i];
}
list[i]='\0';
break;
}
prev = i;
i = link[i];
}
}
void displayTable(){
cout "Position | List | Link
";
cout "----------------------
";
for (int i =0; i N; i++){
if (list[i]!='\0'){
cout i "|" list[i]"|" link[i] endl;
}
}
}
int main(){
initializeList();
cout "Initial table:
";
displayTable();
int choice;
do {
cout "
Menu:
1. Add element
2. Delete element
3. Exit
Enter your choice: ";
cin >> choice;
if (choice ==1){
char element;
cout "Enter element to add: ";
cin >> element;
add((int)element);
displayTable();
} else if (choice ==2){
char element;
cout "Enter element to delete: ";
cin >> element;
deleteElement((int)element);
displayTable();
}
} while (choice !=3);
return 0;
}
I need help correcting the code to a logical

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!