#I just wrote this code and I don't know how it works

11 messages · Page 1 of 1 (latest)

worn dune
#
typedef struct ListNode
{
  int element;
  struct ListNode *next;
} ListNode;

typedef struct
{
  ListNode *head;
} LinkedList;

void remove_first(LinkedList* S_Node){
    S_Node->head = S_Node->head->next;
}

The bulk of where my question lies (remove_first)

#include <stdio.h>
#include <stdlib.h>



typedef struct ListNode
{
  int element;
  struct ListNode *next;
} ListNode;

typedef struct
{
  ListNode *head;
} LinkedList;

void remove_first(LinkedList* S_Node){
    S_Node->head = S_Node->head->next;
}


/* remove_first(L)
12 Given a pointer to a LinkedList structure, remove the first element
13 from the list. Remember to deallocate the memory for the node. */
void remove_first (LinkedList * L);

/* get_last(L)
17 Given a pointer to a LinkedList structure, return the value
18 of the last element in the list. */
//int get_last (LinkedList * L);

void
print_list (LinkedList * L)
{
  for (ListNode * node = L->head; node != NULL; node = node->next)
    {
      printf ("%d ", node->element);
    }
  printf ("\n");
}

void
add_front (LinkedList * L, int new_element)
{
  ListNode *new_node = malloc (sizeof (ListNode));
  new_node->element = new_element;
  new_node->next = L->head;
  L->head = new_node;
}

int main ()
{
  LinkedList L1, L2;
  L1.head = NULL;
  L2.head = NULL;
  add_front (&L1, 187);
  add_front (&L1, 17);
  add_front (&L1, 10);
  add_front (&L1, 6);
  add_front (&L2, 225);
  add_front (&L2, 116);
  add_front (&L2, 111);
  print_list (&L1);
 // printf ("Last element: %d\n", get_last (&L1));
  print_list (&L2);
//  printf ("Last element: %d\n", get_last (&L2));
  printf ("After removing first element:\n");
  remove_first (&L1);
  print_list (&L1);
  remove_first (&L2);
  print_list (&L2);
  return 0;
}```
steep falconBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

worn dune
#

In regards to my question

#

Btw this is practice

#

not homework or anything

#

I am confused on where the head pointer is actually stored

#

Like to me that reads

#

I am not actually entirely sure how thats read or exactly how this works

#
void remove_first(LinkedList* S_Node){
    S_Node->head = S_Node->head->next;
}

This is my smol block of code here

urban glade
#

I just wrote this code and I don't know how it works

not homework

Code: highly organised
Everyone: doubt

steep falconBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.