Question: Using your linked list program from last week, add two new classes, orderedLinkedList and doublyLinkedList. Each will contain all the same functionality as the original
Using your linked list program from last week, add two new classes, orderedLinkedList and doublyLinkedList. Each will contain all the same functionality as the original program but will be implemented using the new classes the ordered and doubly linke lists the integers will be output in ascending order and you will add a reversePrint function in the doubly linked list to process the list in revers order.
#include
struct node
{
int data;
struct node* next;
};
void display_list(struct node* start)
{
struct node* temp;
cout << " The new linked list is: ";
temp = start;
while (1)
{
cout << "\t" << temp->data;
if (temp->next == NULL)
break;
else
temp = temp->next;
}
cout << " ********************************************************";
}
int main()
{
int value, n, i, choice, sub_choice, check_data;
struct node* start, * temp, * ptr, * prev;
void display_list(struct node*);
cout << " How many nodes to create: ";
cin >> n;
cout << " Enter the data of node # 1 : ";
cin >> value;
temp = new struct node;
temp->data = value;
temp->next = NULL;
ptr = temp;
start = temp;
for (i = 2; i <= n; i++)
{
cout << " Enter the data of node # " << i << " : ";
cin >> value;
temp = new struct node;
temp->data = value;
temp->next = NULL;
ptr->next = temp;
ptr = temp;
}
temp = start;
cout << " Current Node Address\t\tValue\t\tNext NodeAddress ";
while (1)
{
cout << temp << "\t\t\t" << temp->data << "\t\t" << temp->next << " ";
if (temp->next == NULL)
break;
else
temp = temp->next;
}
display_list(start);
level:
cout << " What would you like to do?";
cout << " 1. Insert 2. Delete 3. Exit";
cout << " Enter your choice: ";
cin >> choice;
if (choice == 1)
{
cout << " Enter the data of the new node: ";
cin >> value;
temp = new struct node;
temp->data = value;
temp->next = NULL;
cout << " where would you like to enter the new node?";
cout << " 1. at begining 2. in between 3. at end: ";
cout << " Enter your choice: ";
cin >> sub_choice;
if (sub_choice == 1)
{
temp->next = start;
start = temp;
}
if (sub_choice == 2)
{
cout << " Enter the data after which you would like to insert the newnode: ";
cin >> check_data;
ptr = start;
while (1)
{
if (ptr->data == check_data)
{
temp->next = ptr->next;
ptr->next = temp;
break;
}
ptr = ptr->next;
}
}
if (sub_choice == 3)
{
temp->next = NULL;
ptr = start;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
display_list(start);
goto level;
}
if (choice == 2)
{
cout << " which node would you like to delete? : ";
cout << " 1. Initial node2. Internal node3. Endnode";
cout << " Enter your choice: ";
cin >> sub_choice;
if (sub_choice == 1)
start = start->next;
if (sub_choice == 3)
{
ptr = start->next;
prev = start;
while (ptr->next != NULL)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = NULL;
delete ptr;
}
//Internal node to delete
if (sub_choice == 2)
{
cout << " Enter the data to be deleted: ";
cin >> check_data;
prev = start;
ptr = start->next;
while (ptr->data != check_data)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
delete ptr;
}
//Display list of function
display_list(start);
goto level;
}
if (choice == 3)
{
cout << " Thank you for using linked list program ";
exit(0);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
