Answer to Question #254874 in Java | JSP | JSF for RMJ

Question #254874
  1. Give an implementation of the size() method for the DoublyLinkedList class, assuming that we did not maintain size as an instance variable.
1
Expert's answer
2021-10-22T06:36:53-0400


#include <iostream>
using namespace std;
 


struct List
{
    int data;
    struct List *next;
    struct List *prev;
};
 
void push(struct List** head, int data)
{
    struct List* node = new List;
    node->data  = data;
    node->next = (*head);
    node->prev = NULL;
    if ((*head) !=  NULL)
      (*head)->prev = node ;
    (*head)    = node;
}
 
//Answer to the question
int Size(struct List *node)
{
   int res = 0;
   while (node != NULL)
   {
       res++;
       node = node->next;
   }
   return res;
}
 


int main()
{
    struct List* node = NULL;
    push(&node, 4);
    push(&node, 3);
    push(&node, 2);
    push(&node, 1);
    cout << Size(node);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS