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

How would you solve this problem? The text book I am using is Data Structures Us

ID: 3789524 • Letter: H

Question

How would you solve this problem? The text book I am using is Data Structures Using C++ Author: D.S. Malik Publisher: Course Technology. Please help I'm really confused on the assignment, I'm using this linked list below #include <iostream> #include <cstdlib> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType *first, *last; int num; createList(first, last); int choice; while(true) { cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Exit. "; cout<<"Enter your choice: "; cin>>choice; switch(choice) { case 1: insertFront(first); break; case 2: insertBack(last); break; case 3: deleteFirst(first); break; case 4: deleteLast(last, first); break; case 5: printList(first); break; case 6: return 0; default: cout<<"Invalid menu option. Try again."<<endl; } } system("PAUSE"); return 0; } void createList(nodeType*& first, nodeType*& last) { int number; nodeType *newNode; first = NULL; last = NULL; cout<<"Enter an integer (-999 to stop): "; cin>>number; while (number != -999) { newNode = new nodeType; // create new node newNode->info = number; newNode->link = NULL; if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } cout<<"Enter an integer (-999 to stop): "; cin>>number; } // end of while-loop } // end of build list function void deleteFirst(nodeType*& first) { nodeType *temp; temp= first; first= temp->link; delete temp; return; } void deleteLast(nodeType*& last, nodeType* current) { nodeType *temp; while(current->link != NULL) { temp=current; current=current->link; } temp=last; current->link=NULL; delete temp; last = current; return; } void insertFront(nodeType*& front) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= front; front= newNode; return; } void insertBack(nodeType*& last) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= NULL; last->link= newNode; last = newNode; return; } void printList(nodeType*& first) { cout<<"Inside printList...printing linked list... "<<endl; nodeType *current; current = new nodeType; current = first; while (current != NULL) { cout << current->info << " "; current = current->link; } cout<<endl; } How would you solve this problem? The text book I am using is Data Structures Using C++ Author: D.S. Malik Publisher: Course Technology. Please help I'm really confused on the assignment, I'm using this linked list below #include <iostream> #include <cstdlib> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType *first, *last; int num; createList(first, last); int choice; while(true) { cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Exit. "; cout<<"Enter your choice: "; cin>>choice; switch(choice) { case 1: insertFront(first); break; case 2: insertBack(last); break; case 3: deleteFirst(first); break; case 4: deleteLast(last, first); break; case 5: printList(first); break; case 6: return 0; default: cout<<"Invalid menu option. Try again."<<endl; } } system("PAUSE"); return 0; } void createList(nodeType*& first, nodeType*& last) { int number; nodeType *newNode; first = NULL; last = NULL; cout<<"Enter an integer (-999 to stop): "; cin>>number; while (number != -999) { newNode = new nodeType; // create new node newNode->info = number; newNode->link = NULL; if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } cout<<"Enter an integer (-999 to stop): "; cin>>number; } // end of while-loop } // end of build list function void deleteFirst(nodeType*& first) { nodeType *temp; temp= first; first= temp->link; delete temp; return; } void deleteLast(nodeType*& last, nodeType* current) { nodeType *temp; while(current->link != NULL) { temp=current; current=current->link; } temp=last; current->link=NULL; delete temp; last = current; return; } void insertFront(nodeType*& front) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= front; front= newNode; return; } void insertBack(nodeType*& last) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= NULL; last->link= newNode; last = newNode; return; } void printList(nodeType*& first) { cout<<"Inside printList...printing linked list... "<<endl; nodeType *current; current = new nodeType; current = first; while (current != NULL) { cout << current->info << " "; current = current->link; } cout<<endl; } Lab Exercise 2 COSC 214- Spring 2017 Due: Thursday, February 2,2017 (End of Lab) Ordered Linked List Note-Start with a new C++program/project. Insert See page 302 o This section, in the book, shows class/object implementation o You are not required to implement as class/object o You are not required to use the "count" (inked list counter) In main o Must implement the 4 cases (l,2,3A and3B) o Run the code to show it will insert in all four cases insert first, last, 37 case l insert (first, last, 68); case 3A displaylist(firs); llifyour"insert" function works, it will display sorted order Show your work when finished Although we covered Doubl Linked List today, be sure to re-read "Doubly Linked List" for next Tuesday's class.

Explanation / Answer

#include <iostream>
#include <cstdlib>
using namespace std;
//Node Created
struct nodeType
{
int info;
nodeType *link;
};
//Initial Linked list value inserted
void createList(nodeType*& first, nodeType*& last)
{
int number;
nodeType *newNode;
//Initially first and last pointer is null
first = NULL;
last = NULL;
//Loops till -999 is entered
while (1)
{
newNode = new nodeType;
// Create new node newNode->info = number;
newNode->link = NULL;
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
if(number == -999)
break;
newNode->info = number;
//If it is first node
if (first == NULL)
{
first = newNode;
last = newNode;
}
//If already nodes are available
else
{
last->link = newNode;
last = newNode;
}
} // end of while-loop
} // end of build list function

//To delete first node
void deleteFirst(nodeType*& first)
{
nodeType *temp;
//If first is null it is empty set
if(first == NULL)
{
cout<<" No node to delete"<<endl;
return;
}
temp = first;
cout<<" Deleted node = "<<first -> info<<endl;
first = temp->link;
delete temp;
return;
}
//To delete the last node
void deleteLast(nodeType*& last, nodeType*& first)
{
nodeType *temp, *temp1;
temp1 = first;
//If first is null then it is empty set
if(first == NULL)
{
cout<<" No node to delete"<<endl;
return;
}
//If last is equal to first then it is the only node available
//So after deleting first and last must be null
if(last == first)
{
delete temp1;
first = last = NULL;
}
//If more than one node is available loop till end of list
while(temp1->link != NULL)
{
//Before moving to next node store the previous node
temp = temp1;
temp1 = temp1->link;
}
cout<<" Deleted node = "<<temp1->info<<endl;
temp->link = NULL;
last = temp;
delete temp;
return;
}
//To create a node at the beginning
void insertFront(nodeType*& first, nodeType*& last)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
//If first is null then it is the first node
//So first and last points to the new node
if (first == NULL)
{
first = newNode;
last = newNode;
}
//Otherwise nodes are already there
else
{
newNode->link= first;
first = newNode;
}
return;
}
//To insert at the end
void insertBack(nodeType*& last, nodeType*& first)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= NULL;
last->link= newNode;
//If first is null then no node available so first and last node will point to the new node
if (first == NULL)
{
first = newNode;
last = newNode;
}
//Otherwise nodes are already there
else
{
last->link = newNode;
last = newNode;
}
return;
}
//Display the complete set
void printList(nodeType*& first)
{
cout<<" Printing linked list... "<<endl;
//If first is null no node available
if(first == NULL)
{
cout<<" No Node To Display"<<endl;
return;
}
nodeType *current;
current = new nodeType;
current = first;
//Loops till end of the linked list
while (current != NULL)
{
cout << current->info << " ";
current = current->link;
}
cout<<endl;
}

int main()
{
nodeType *first, *last;
int num;
createList(first, last);
int choice;
//Loops till choice is 6
while(true)
{
cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Exit. ";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
insertFront(first, last);
break;
case 2:
insertBack(last, first);
break;
case 3:
deleteFirst(first);
break;
case 4:
deleteLast(last, first);
break;
case 5:
printList(first);
break;
case 6:
return 0;
default:
cout<<"Invalid menu option. Try again."<<endl;
}
}
system("PAUSE");
return 0;
}

Output:

Enter an integer (-999 to stop): 10
Enter an integer (-999 to stop): -999
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 4

Deleted node = 10
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 4

No node to delete
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 1

Enter the number to insert: 10
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 5

Printing linked list...

10
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 2

Enter the number to insert: 20
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 5

Printing linked list...

10 20
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 3

Deleted node = 10
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 5

Printing linked list...

20
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 3

Deleted node = 20
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 5

Printing linked list...


No Node To Display
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 3

No node to delete
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 1

Enter the number to insert: 33
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 5

Printing linked list...

33
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 2

Enter the number to insert: 66
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 5

Printing linked list...

33 66
1. Insert Front.
2. Insert Last.
3. Delete Front.
4. Delete Last.
5. Print List.
6. Exit.
Enter your choice: 6

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