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

Insert See book section 16-4b - Insert a Node - o This section, in the book, sho

ID: 3743637 • Letter: I

Question

Insert See book section 16-4b - Insert a Node - o This section, in the book, shows class/object implementation o You are not required to implement as a class/object o You are not required to use the "count" (linked list counter) In main o Must implement the 4 cases (1, 2, 3A and 3B) o Run the code to show it will insert in all four cases insert(first, last, 37); case 1 insert(first, last, 14); // case 2 insert(first, last, 68);// case 3A insert(first, last, 47); // case 3B displayList(first); // if your "insert" function works, it will display sorted/order

Explanation / Answer

ANS:

CODE:

#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

PLEASE RATE THUMBSUP PLEASE