Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let\'s look to build the basic methods of our linked list. Look to implement lin

ID: 3824470 • Letter: L

Question

Let's look to build the basic methods of our linked list. Look to implement linked list that will create methods for pushing value onto head of the list and return the length of the linked list. This exercise will span multiple lectures. void BasicLinkedList() {struct node* head; int len; head = BuildOneTwo Three();//Start with {1, 2, 3} Push(&head;, 13);//Push 13 on the front, yielding {13, 1, 2, 3)//(The '&' is because head is passed//as a reference pointer.) len = Length (head);//Computes that the length is 5.}

Explanation / Answer

Hi, Please find my implementation of both methods.

void push(struct node** head_ref, int new_data)
{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));

/* 2. put in the data */
new_node->data = new_data;

/* 3. Make next of new node as head */
new_node->next = (*head_ref);

/* 4. move the head to point to the new node */
(*head_ref) = new_node;
}

int Length(struct node* head_ref){
  
   int count = 0;

   while(head_ref != NULL){
       count = count + 1;
       head_ref = head_ref->next;
   }
   return count;
}