What is a node class?
A node class is a class that,
* relies on the base class for services and implementation,
* provides a wider interface to the users than its base class,
* relies primarily on virtual functions in its public interface
* depends on all its direct and indirect base class
* can be understood only in the context of the base class
* can be used as base for further derivation
* can be used to create objects.
A node class is a class that has added new services or functionality beyond the services inherited from its base class.
How do you write a function that can reverse a linked-list?
Answer1:
void reverselist(void)
{
if(head==0)
return;
if(head-
head-
else
{
node* pre = head;
node* cur = head-
cur-
curnext-
}
Answer2:
node* reverselist(node* head)
{
if(0==head || 0==head->next)
//if head->next ==0 should return head instead of 0;
return 0;
{
node* prev = head;
node* curr = head->next;
node* next = curr->next;
for(; next!=0; )
{
curr->next = prev;
prev = curr;
curr = next;
next = next->next;
}
curr->next = prev;
head->next = 0;
head = curr;
}
return head;
}
What is polymorphism?
Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.
How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle.
No comments:
Post a Comment
Thanks to given comments.......