Could someone please explain this code to me.. #include <iostream> using namespa
ID: 658602 • Letter: C
Question
Could someone please explain this code to me..
#include <iostream>
using namespace std;
struct link
{
int data;
link* next;
};
class linklist
{
private:
link* first;
public:
linklist() : first(NULL)
{}
void additem(int);
void display();
};
void linklist::additem(int d)
{
link* newlink = new link;
newlink->data = d;
newlink->next = first;
first = newlink;
}
void linklist::display()
{
link* current = first;
while( current != NULL )
{
cout<<current->data<<" ";
current = current->next;
}
}
int main()
{
linklist li;
li.additem(25);
li.additem(36);
li.additem(47);
li.additem(58);
li.display();
return 0;
}
i get most of the code but i don't understand the following:
What is happening when additem(int) is called?
How does one object create 4 ints and loop through them?
How is the while loop working in this code sample?
Explanation / Answer
Question 1: What is happening when additem(int) is called?
When additem(int) is called:
For example: additem(20)
void linklist::additem(int d)
{
link* newlink = new link;
newlink->data = d;
newlink->next = first;
first = newlink;
}
As we called additem(20), variable d will contain the number 20.
As for the function additem(int d):
First we instantiate a new link structure and make pointer newlink point to it:
link* newlink = new link;<br/>
Then we store our data (20) inside the data variable of our link structure.
newlink->data = d;
Then we state that the next variable in our link structure will contain a reference to whatever first (another pointer) is pointing at.
newlink->next = first;
Then we make pointer first point to our new link structure.
first = newlink;
Question 2: How does one object create 4 items and loop through them?
This is a two part question: First, how does an object create 4 items?
Very simple, by calling additem 4 times. Note that the additem function that's in the linklist object has an object creation statement in it.
link* newlink = new link;
Now how does it loop through them? Look at the additem function again. What it is doing is creating a new object called LINK, then storing a reference to a different LINK object in next. (which is referenced to by the 'first' variable) It then stores a reference to the new link in the 'first' variable.
So if you call additem(1) it creates a link with data 1 and next NULL (because this is the first call)
newlink->data = 1
newlink->next = first(NULL because we did not make a first yet, this IS first)
first = (The link we made when we called additem(1)
If you after additem(1) call additem(2) it creates a link with :
newlink->data = 2
newlink->next = first(The link we made when we called additem(1)
first = (The link we made when we called additem(2)
If you after additem(2) call additem(3) it creates a link with:
newlink->data = 3
newlink->next = first(The link we made when we called additem(2)
first = (The link we made when we called additem(3)
Now to loop through them when you call display():
void linklist::display()
{
link* current = first;
while( current != NULL )
{
cout<<current->data<<" ";
current = current->next;
}
}
link is a link object.
link->next is a link object.
To loop through them, the display function creates a pointer called current that points to 'first'.
Then whenever it wants to loop to the next it can do current = current->next. As next is a variable that is part of the link structure, that points to a different link.
In our example, our additem(3) link object pointed to additem(2) which pointed to additem(1).
Question 3: How is the while loop working in our example?
Very simple: First it creates a pointer called current that points to whatever FIRST is pointing to.
Then it checks if current is actually pointing to anything (current != NULL).
Then it prints the data variable of the link that current is pointing at. (link->data) Then it makes current point to whatever the NEXT variable of the link is pointing at.
In our example we called additem(1);additem(2);additem(3);, and you could see that each link created in the next call would have a reference to the previous link.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.