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)
- The distinction between void and NULL pointers is as follows: Make use of appropriate examples to support your response.arrow_forwardIn 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_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_forwardThe block is practically a square-size placeholder for the Tile objects. But not all the spots are necessarily occupied. When the block is generated, we randomly pick the spots that will be filled in with tiles. All the tiles of a block must have the same color. write the code for these functions below private DynamicArray<DynamicArray<Tile>> block; // the internal storage of the block data public Block(int y, int x, int size) // this contructor creates a 2D placeholder of null value; //these values will be populated later with calls to setTile() public Block(int y, int x, int size, byte color) // overloaded constuctor that creates a 2D matrix with actual tile objects; //no need to call setTile afterwardsarrow_forward
- This is my lab homework can you help me to solve this?arrow_forwardWrite a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. findLast: takes an array of integers, its size and a target integer value. Returns an index of the last occurrence of the target element in the array. If the target value doesn't exist, returns -1. For this function only: Do not use square brackets anywhere in the function, not even the parameter list (use pointer notation instead). nextFibonacci. Modify the following function to use pointers as parameters (instead of reference parameters). The Fibonacci sequence is a series of numbers where each number is the addition of the previous two numbers, starting with 0, and 1 (specifically: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…). This function updates the given i-th and (i+1)-st Fibonacci numbers to (i+1)-st and…arrow_forwardIn C++, how can one easily duplicate a set of shared pointers into another array? Make a list of various solutions to the situation you've been given. Is it true that copying shared pointers also copies the objects they manage? Explainarrow_forward
- Do void and NULL pointers differ? Support your answer with examples. Tabulate the differences.arrow_forwardASAP Please write this C++ program using vector of objects as per the following instructions ( read carefully ). Explain all your steps as if any beginner could understand like how many cpp files or text files should be there. Don't miss or copy from any other source this task needs to be done with vector of TAs object. Please explain how does it work as I don't know anything about it. Any explanation and comment is very needed . please don't copy from any other sources .please write on your own or skip. PLEASE ATTACH PICTURES w te written code and show the output too Thank you so much for your time and helparrow_forwardNeed help with these assignments, I use C++. Thank you. Lab 4: Declare and implement a BSTNode ADT with a data attribute and two pointer attributes, one for the left child and the other for the right child. Implement the usual getters/setters for these attributes. Declare and implement a BST as a link-based ADT whose data will be Dollar objects - the data will be inserted based on the actual money value of your Dollar objects as a combination of the whole value and fractional value attributes. For the BST, implement the four traversal methods as well as methods for the usual search, insert, delete, print, count, isEmpty, empty operations and any other needed. Your pgm will use the following 20 Dollar objects to be created in the exact order in your main to seed the tree: $57.12 $23.44 $87.43 $68.99 $111.22 $44.55 $77.77 $18.36 $543.21 $20.21 $345.67 $36.18 $48.48 $101.00 $11.00 $21.00 $51.00 $1.00 $251.00 $151.00 Also, create an output file to write program output as specified…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