Concept explainers
Linked list:
- Linked list denotes a linear data structure.
- The elements are not stored at contiguous locations; the elements are linked using pointers.
- It stores linear data of similar types not like arrays.
- The size of linked list can be changed based on requirement.
- It is represented by a pointer to first linked list node.
- The first node denotes a head.
- If linked list is empty, value of head is NULL.
- The node in a list has two parts, data and pointer to next node.
Given code:
//Define a structure
struct Box
{
//Declare variable
string name;
//Declare variable
int number;
//Define pointer to next
Box *next;
};
//Create an instance
BoxPtr head;
//Assign value
head = new Box;
//Assign value
head->name = "Sally";
//Assign value
head->number = 18;
//Assign value
head->next = NULL;
//Display value
cout << (*head).name << endl;
//Display value
cout << head->name << endl;
//Display value
cout << (*head).number << endl;
//Display value
cout << head->number << endl;
Explanation:
- The “Box” defines a structure with “name” and “number” as members.
- It contains a pointer to next element of list.
- The “BoxPtr head” creates an instance of structure.
- A new “Box” element is created.
- The values of “name” and “number” are been set.
- The values of “name” and “number” are been displayed.
- The member variable next to the node is been set to NULL.
Complete program:
//Include libraries
#include<iostream>
#include<string>
//Use namespace
using namespace std;
//Define a structure
struct Box
{
//Declare variable
string name;
//Declare variable
int number;
//Define pointer to next
Box *next;
};
//Define instance
typedef Box* BoxPtr;
//Define main method
void main()
{
//Create an instance
BoxPtr head;
//Assign value
head = new Box;
//Assign value
head->name = "Sally";
//Assign value
head->number = 18;
//Display value
cout << (*head).name << endl;
//Display value
cout << head->name << endl;
//Display value
cout << (*head).number << endl;
//Display value
cout << head->number << endl;
//Delete
delete head;
//Display message
cout<<"List head is been destroyed"<<endl;
//Pause console window
system("pause");
}
Explanation:
- The “Box” defines a structure with “name” and “number” as members.
- It contains a pointer to next element of list.
- Define a main method
- The “BoxPtr head” creates an instance of structure.
- A new “Box” element is created.
- The values of “name” and “number” are been set.
- The values of “name” and “number” are been displayed.
- The member variable next to the node is been set to NULL.
- The linked list is been destroyed using “delete” statement.
Want to see the full answer?
Check out a sample textbook solutionChapter 13 Solutions
Problem Solving with C++ (10th Edition)
- Please add a destructor to the code. Solve this in C++arrow_forwardIs there a special reason why the symbol or name used in the C++ inclusion guard on a library interface file has to be special? A helpful tactic in this situation is to use proof by contradiction: assume that the symbol doesn't need to be unique, and then show what happens when two libraries that are both #included in the same application use the same symbol.arrow_forwardAssume that you need to organize the data of books. Existing books should be rearranged so that only one side of the shelf is empty, or new volumes should be placed there. In this case, what would be the best data structure? Make it possible to dynamically implement the process of adding books to the bookcase. Furthermore, what is the circumstance where fresh data need to be entered into a data structure, but there is no room?arrow_forward
- in C++... kth Element Extend the class linkedListType by adding the following operations:a. Write a function that returns the info of the kth element of the linked list. If no such element exists, terminate the program.b. Write a function that deletes the kth element of the linked list. If no such element exists, terminate the program. Provide the definitions of these functions in the class linkedListType. PLEASE DON'T reject this question, this is the whole question that I have... so please do it however u can, Thank you!arrow_forwardSuppose that an array is passed as a parameter. How does this differ from the usual use of a value parameter? When an array is passed as a parameter, it is like passed by reference. A new array will be created in the called function and any changes to the new array will pass back to the original array. When an array is passed as a parameter, it is passed by value. So any changes to the parameter do not affect the actual argument. When an array is passed as a parameter, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a value parameter (where changes to the parameter do not affect the actual argument). When an array is passed as a value, changes to the array affect the actual argument. This is because the parameter is treated as a pointer that points to the first component of the array. This is different from a parameter (where changes to the parameter do not…arrow_forwardThe distinction between void and NULL pointers is as follows: Make use of appropriate examples to support your response.arrow_forward
- In C++, how simple is it to duplicate an array of shared pointers into a new array? Create a list of potential solutions to the issue at hand. Do the objects controlled by a shared pointer also get copied when it's copied? Explainarrow_forwardGive solution in C ++ Language with secreenshoot of source code. Part 01In this task, you need to do the following:• Write a function named displayMessage() that takes user name as input in character array and then shows greetings• Now take the name input in main() and pass the name as an argument to displayMessage() function• Change the displayMessage() method such that it returns the number of characters after displaying the greetings part 02Write a function power that takes two parameters a and b. And it returns the power as ab.arrow_forwardIn C Programming: Write a function printCourseRow() which receives a course pointer and prints all its fields as a single row. Use proper formatting so that when we print 2 or more courses as rows, the same members align below each other. Test the function, but don’t include the testing code in your homework.Upload a screenshot of a sample output.arrow_forward
- What is the difference between void and null pointers.Give suitable example in support of your answerarrow_forwardby using c++. How can I solve this? Kindly, no class, linked list, and pointers shall be used in this program. Kindly use only SELECTION, ITERATION, INPUT, AND OUTPUT, FUNCTION, ARRAY, AND STRING ONLY.arrow_forwardThis is my lab homework can you help me to solve this?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education