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

Using C++, Write a program that creates a linked list object of 10 characters an

ID: 3805366 • Letter: U

Question

Using C++, Write a program that creates a linked list object of 10 characters and creates a second list object containing a copy of the first list, but in reverse order.
Sample Run
The list is: a b c d e f g h i j
All nodes destroyed
The list after reversing: The list is: j i h g f e d c b a
Destroying nodes ... j i h g f e d c b a All nodes destroyed
Destroying nodes ... a b c d e f g h i j All nodes destroyed Using C++, Write a program that creates a linked list object of 10 characters and creates a second list object containing a copy of the first list, but in reverse order.
Sample Run
The list is: a b c d e f g h i j
All nodes destroyed
The list after reversing: The list is: j i h g f e d c b a
Destroying nodes ... j i h g f e d c b a All nodes destroyed
Destroying nodes ... a b c d e f g h i j All nodes destroyed Write a program that creates a linked list object of 10 characters and creates a second list object containing a copy of the first list, but in reverse order.
Sample Run
The list is: a b c d e f g h i j
All nodes destroyed
The list after reversing: The list is: j i h g f e d c b a
Destroying nodes ... j i h g f e d c b a All nodes destroyed
Destroying nodes ... a b c d e f g h i j All nodes destroyed Write a program that creates a linked list object of 10 characters and creates a second list object containing a copy of the first list, but in reverse order.
Sample Run
The list is: a b c d e f g h i j
All nodes destroyed
The list after reversing: The list is: j i h g f e d c b a
Destroying nodes ... j i h g f e d c b a All nodes destroyed
Destroying nodes ... a b c d e f g h i j All nodes destroyed

Explanation / Answer

Please refer below code

#include<iostream>

using namespace std;

typedef struct node
{
char ch;
struct node *next;
}node;

//creating list
void create_list(node **head, char ch)
{
node *temp;
temp = new node; //creating new node

temp->ch = ch;
temp->next = NULL;

if(*head == NULL) //if node is first node to be inserted
{
*head = temp;
}
else
{
node *travel = *head;
//traveling to the end of list and attching the node

while(travel->next != NULL)
travel = travel->next;

travel->next = temp;
}
}
//printing list
void print_list(node *head)
{
node *travel = head;
//traversing wjole list and printing the node info;
while(travel != NULL)
{
cout<<travel->ch<<endl;
travel = travel->next;
}
}
//reversing the list
void reverse_list(node *head, node **head_rev)
{
node *travel = head;
for(int i = 0; i < 10; i++)
{
node *temp;
temp = new node;
temp->ch = travel->ch;
temp->next = NULL;
//attaching the node at the start of list
temp->next = *head_rev;
*head_rev = temp;

travel = travel->next;
}
}
//deleting the list
void delete_list(node **head)
{
node *travel = *head;
node *next;

while(travel != NULL)
{
cout<<travel->ch<<endl;
next = travel->next;
delete travel;
travel = next;
}
*head = NULL;
}
int main()
{
node *head = NULL;
node *head_rev = NULL;

char ch = 'a';
for(int i = 0; i < 10; i++)
{
create_list(&head, ch);
ch++;
}
cout<<"The list is : "<<endl<<endl;
print_list(head);

reverse_list(head,&head_rev);
cout<<"The list after reversing : "<<endl;
print_list(head_rev);

cout<<"Destroying Reversed List nodes ...."<<endl;
delete_list(&head_rev);
cout<<"All nodes destroyed"<<endl<<endl;

cout<<"Destroying Original List nodes ...."<<endl;
delete_list(&head);
cout<<"All nodes destroyed"<<endl<<endl;

return 0;
}

Please refer below output for reference

The list is :

a
b
c
d
e
f
g
h
i
j
The list after reversing :
j
i
h
g
f
e
d
c
b
a
Destroying Reversed List nodes ....
j
i
h
g
f
e
d
c
b
a
All nodes destroyed

Destroying Original List nodes ....
a
b
c
d
e
f
g
h
i
j
All nodes destroyed


Process returned 0 (0x0) execution time : 0.040 s
Press any key to continue.

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