Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi. Can someone explain to me the program, they're asking me to demonstrate the corresponding output and to give observation and analysis. Thank you! Direction:

Hi. Can someone explain to me the program, they're asking me to demonstrate the corresponding output and to give observation and analysis. Thank you!

"Direction: Simulate each line of the program. Demonstrate the corresponding output for each of the given program. Give your observation and analysis."

(PROGRAM)

#include

#include

using namespace std;

void doHousekeeping(void);

void doMainLoop(void);

void doFinishUp(void);

void traverse(void);

void insertFrontNode(void);

void insertBackNode(void);

void insertBetweenNodes(void);

void deleteFrontNode(void);

void deleteBackNode(void);

void deleteBetweenNodes(void);

struct node {

int item;

node *next;

};

node *head = NULL;

int main() {

// To hold user decision

char isQuit;

// Call housekeeping

//doHousekeeping();

cout<<"Press [1] to quit, any key to continue..."; isQuit = getch();

while (isQuit != '1') {

// Call main loop

doMainLoop();

// Decide

cout<<"Press [1] to quit, any key to

continue...";

isQuit = getch();

}

// Call finish up

//doFinishUp();

// Terminate program

getch();

return 0;

}

// Define main loop

void doMainLoop(void) {

char chosenOp;

cout << endl;

cout << "(i)nsert front node, i(n)sert back node, in(s)ert between nodes" << endl;

cout << "(d)elete front node, d(e)lete back node, de(l)ete between nodes" << endl;

cout << "(t)raverse, e(x)it" << endl;

cout << "Choose: ";

cin >> chosenOp;

cout << endl;

while (chosenOp != 'x') {

switch (chosenOp) {

case 'i':

insertFrontNode();

break;

case 'n':

insertBackNode();

break;

case 's':

insertBetweenNodes();

break;

case 'd':

deleteFrontNode();

break;

case 'e':

deleteBackNode();

break;

case 'l':

deleteBetweenNodes();

break;

case 't':

traverse();

break;

default:

cout << "Invalid input!" << endl;

}

cout << endl;

cout << "(i)nsert front node, i(n)sert back node, in(s)ert between nodes" << endl;

cout << "(d)elete front node, d(e)lete back node, de(l)ete between nodes" << endl;

cout << "(t)raverse, e(x)it" << endl;

cout << "Choose: ";

cin >> chosenOp;

cout << endl;

}

return;

}

void traverse(void) {

node *temp = new node;

temp = head;

while (temp != NULL) {

cout << "\t\t|" << temp->item << "|"<< endl; temp = temp->next;

}

return;

}

void insertFrontNode(void) {

node *head2 = new node;

cout << "Enter a numeric item: "; cin >> head2->item;

if (head == NULL) {

head2->next = NULL;

} else {

head2->next = head;

}

head = head2;

return;

}

void insertBackNode(void) {

node *tail = new node;

node *temp = new node;

cout << "Enter a numeric item: "; cin >> tail->item;

tail->next = NULL;

if (head == NULL) {

head = tail;

} else {

temp = head;

while (temp->next != NULL) {

temp = temp->next;

}

temp->next = tail;

}

return;

}

void insertBetweenNodes(void) {

node *body = new node;

node *temp = new node;

int position;

cout << "Enter a numeric item: "; cin >> body->item;

cout << "Insert at position: "; cin >> position;

if (position > 0) {

temp = head;

for (int i = 1; i < position; i++) {

temp = temp->next;

}

body->next = temp->next;

temp->next = body;

}

return;

}

void deleteFrontNode(void) {

node *temp = new node;

temp = head;

head = head->next;

delete temp;

return;

}

void deleteBackNode(void) {

node *tail = new node;

node *temp = new node;

temp = head;

while (temp->next != NULL) {

tail = temp;

temp = temp->next;

}

tail->next = NULL;

delete temp;

return;

}

void deleteBetweenNodes(void) {

node *body = new node;

node *temp = new node;

int position;

cout << "Delete at position: "; cin >> position;

if (position > 0) {

temp = head;

for (int i = 1; i < position; i++) {

body = temp;

temp = temp->next;

}

body->next = temp->next;

delete temp;

}

return;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions