Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What\'re errors in my code? Pls directly see case 4 and case 5. Tks... Code: #in

ID: 3721843 • Letter: W

Question

What're errors in my code? Pls directly see case 4 and case 5. Tks...

Code:

#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <string>

using namespace std;

struct Node
{
Node *previous;
Node *next;
int number;
};

int main()
{
Node *First = NULL;
Node *Last = NULL;
Node *n;
int opt, element = 0, temp, number;
char ans = 'y';

while (ans == 'y')
{
system("cls");
cout << " Menu:"
<< " 1 - Insert a new data at the Begining of the list"
<< " 2 - Insert a new data at the End of the list"
<< " 3 - Find a Node via its data element"
<< " 4 - Traverse and Display list from Begining to End"
<< " 5 - Traverse and Display list in Reverse order"
<< " 6 - Change a value"
<< " 7 - Quit"
<< " Enter an option from 1 - 7: ";
cin >> opt;

switch (opt)
{

case 1:

system("cls");
n = new Node;//command 'new' create
element++;
cout << "Enter an integer value in the nodes: ";
cin >> temp;
n->previous = NULL;
n->number = temp;
n->next = First;
First = n;
if (element == 1)
{
Last = n;
}
if (element > 1)
{
n = n->next;
n->previous = First;
}
break;

case 2:

system("cls");
n = new Node;//comand 'new' create
element++;
if (element == 1)
{
First = NULL;
}
cout << "Enter an integer value in the nodes: ";
cin >> temp;
n->previous = Last;
n->number = temp;
n->next = NULL;
Last = n;
if (First == NULL)
{
First = n;
n->previous = NULL;
}
else
{
n = n->previous;
n->next = Last;
}
break;

case 3:

system("cls");
cout << "Enter the number you want to search: ";
cin >> temp;
n = First;
while (temp != NULL)
{
if (n->number == temp)
{
cout << " Number " << n->number << " Found, and total are " << element << " values.";
getch();
break;
}
else
{
// NOT Found, move to the NEXT Node.
n = n->next;
if ((n->next == NULL) && (n->number != temp))
{
cout << " " << temp << " is NOT in the LIST!";
getch();
break;
}
}
}
break;

case 4:

// Traverse and Display List Elements
if( element > 0 )
{
cout << "List has " << element << " elements(front to back) ";
n = First;
for (int i = 1; i<=element; i++)
{
cout << "Node #" << i << " " <<n->number << " ";
n = n->next;
}
cout << n->number << " (Null - End of list) "
<< " Hit Any Key to continue...";
}
else
cout << "List is empty!"
<< " Hit Any Key to continue...";
getch();
break;

case 5:

// Traverse and Display List Elements in REVERSE order
if( element > 0 )
{
cout << "List has " << element << " elements(back to front) ";
n = Last;
for (int i = 1; i<=element; i++)
{
cout << "Node #" << i << " " <<n->number << " ";
n = n->previous;
}
cout << n->number << " (Null - End of list) "
<< " Hit Any Key to continue...";
}
else
cout << "List is empty!"
<< " Hit Any Key to continue...";
getch();
break;

case 6:

system("cls");
cout << "Enter the number you want to change: ";
cin >> temp;
n = First;
while (temp != NULL)
{
if (n->number == temp)
{
cout << "Enter a new number: ";
cin >> n->number;
break;
}
else
{
n = n->next;
if ((n->next == NULL) && (n->number != temp))
{
cout << " " << temp << " is NOT in the LIST!";
getch();
break;
}
}
}
break;

case 7:

system("cls");
ans = false;
break;

default:

system("cls");
cout << opt << " is NOT a Valid value!";
cout << " Hit Any Key to continue...";
getch();

}
}
}

110 119 tempprevious->next -nenode: Traverse and Display List Elements if element >) 163 164 165 166 else cout?? "List has " ??element

Explanation / Answer

In case 4:

___________

case 4:

// Traverse and Display List Elements
if( element > 0 )
{
cout << "List has " << element << " elements(front to back) ";
n = First;
for (int i = 1; i<=element; i++)
{
cout << "Node #" << i << " " <<n->number << " ";
n = n->next;
}
cout << n->number << " (Null - End of list) "
<< " Hit Any Key to continue...";

The above cout << n->number is will access the next element in the list which is null as the for loop used above this cout:

"for (int i = 1; i<=element; i++), where element = total number of elements in the list."

has traversed the complete list till the end of list and listed all the elements in the list, Hence the above cout statement should be avoided to make it work.

Same with case 5 as well. Therefore the modified code to rectify the error is:

#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <string>

using namespace std;

struct Node
{
Node *previous;
Node *next;
int number;
};

int main()
{
Node *First = NULL;
Node *Last = NULL;
Node *n;
int opt, element = 0, temp, number;
char ans = 'y';

while (ans == 'y')
{
system("cls");
cout << " Menu:"
<< " 1 - Insert a new data at the Begining of the list"
<< " 2 - Insert a new data at the End of the list"
<< " 3 - Find a Node via its data element"
<< " 4 - Traverse and Display list from Begining to End"
<< " 5 - Traverse and Display list in Reverse order"
<< " 6 - Change a value"
<< " 7 - Quit"
<< " Enter an option from 1 - 7: ";
cin >> opt;

switch (opt)
{

case 1:

system("cls");
n = new Node;//command 'new' create
element++;
cout << "Enter an integer value in the nodes: ";
cin >> temp;
n->previous = NULL;
n->number = temp;
n->next = First;
First = n;
if (element == 1)
{
Last = n;
}
if (element > 1)
{
n = n->next;
n->previous = First;
}
break;

case 2:

system("cls");
n = new Node;//comand 'new' create
element++;
if (element == 1)
{
First = NULL;
}
cout << "Enter an integer value in the nodes: ";
cin >> temp;
n->previous = Last;
n->number = temp;
n->next = NULL;
Last = n;
if (First == NULL)
{
First = n;
n->previous = NULL;
}
else
{
n = n->previous;
n->next = Last;
}
break;

case 3:

system("cls");
cout << "Enter the number you want to search: ";
cin >> temp;
n = First;
while (temp != NULL)
{
if (n->number == temp)
{
cout << " Number " << n->number << " Found, and total are " << element << " values.";
getch();
break;
}
else
{
// NOT Found, move to the NEXT Node.
n = n->next;
if ((n->next == NULL) && (n->number != temp))
{
cout << " " << temp << " is NOT in the LIST!";
getch();
break;
}
}
}
break;

case 4:

// Traverse and Display List Elements
if( element > 0 )
{
cout << "List has " << element << " elements(front to back) ";
n = First;
for (int i = 1; i<=element; i++)
{
cout << "Node #" << i << " " <<n->number << " ";
n = n->next;
}
cout << " (Null - End of list) "
<< " Hit Any Key to continue...";
}
else
cout << "List is empty!"
<< " Hit Any Key to continue...";
getch();
break;

case 5:

// Traverse and Display List Elements in REVERSE order
if( element > 0 )
{
cout << "List has " << element << " elements(back to front) ";
n = Last;
for (int i = 1; i<=element; i++)
{
cout << "Node #" << i << " " <<n->number << " ";
n = n->previous;
}
cout << " (Null - End of list) "
<< " Hit Any Key to continue...";
}
else
cout << "List is empty!"
<< " Hit Any Key to continue...";
getch();
break;

case 6:

system("cls");
cout << "Enter the number you want to change: ";
cin >> temp;
n = First;
while (temp != NULL)
{
if (n->number == temp)
{
cout << "Enter a new number: ";
cin >> n->number;
break;
}
else
{
n = n->next;
if ((n->next == NULL) && (n->number != temp))
{
cout << " " << temp << " is NOT in the LIST!";
getch();
break;
}
}
}
break;

case 7:

system("cls");
ans = false;
break;

default:

system("cls");
cout << opt << " is NOT a Valid value!";
cout << " Hit Any Key to continue...";
getch();

}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote