Jim's
Tutorials

Spring 2018
course
site
// function to print a linked list l starting at the first node
void printList(list l){
  node n = l->first;    // n = first node
  while(n != NULL){
    printf(" %d ", n->data);
    n = n->next;
  };
};

// function to return a list struct with first and last node initiated as NULL and length 0
list createList(){
  list new_list = (list)malloc(sizeof(struct node_list));
  new_list->length = 0;
  new_list->first = NULL;
  new_list->last = NULL;
  return new_list;
};

// function to push a given node n into the end of a given list l
void push(list l, node n){
  if(l->length == 0){
    l->first = n;
    l->last = n;
    l->length = 1;
  } else {
    l->last->next = n;
    l->last = n;
    l->length++;
  }
}

// insert a new node n, with data - int data- after node prev
void insertAfter(node prev, int data){
  node n = createNode(data);
  n->next = prev->next;
  prev->next = n;
}

attachments [paper clip]

  last modified size
TXT binary-tree.c Fri May 03 2024 08:11 am 1.2K
TXT linked-list.c Fri May 03 2024 08:11 am 2.3K