Skip to content

Focus04/intlist.h

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

intlist.h

This C library provides a set of functions to work with singly linked lists of integers. It includes basic operations like insertion, deletion, and traversal, as well as advanced utilities such as insertion sorting the list.

Features

  • Add or remove elements at the head, tail, or specific positions.
  • Check for element existence and retrieve list properties.
  • Reverse the list or reverse subgroups of the list.
  • Memory management with proper freeing of resources.

Installation

To use this library, include intlist.h in your project and link with intlist.c. For example:

#include "intlist.h"

Usage

Data Structure

The library defines the following types:

  • node_t: Represents a single node in the linked list.
  • list_t: A double pointer to the head node, used to represent the list.

Creating and Initializing a List

#include "list.h"

// Advanced usage for insertion sort
int comparator(const int a, const int b)
{
  // Sorts elements in ascending order
  return a - b;
}

int main() 
{
  node_t *head = NULL;
  list_t my_list = &head;

  // Add elements
  list_push(my_list, 10);
  list_append(my_list, 20);
  list_insert_sorted(my_list, 15, comparator);

  // Free resources
  list_free(my_list);
  return 0;
}

Traversing and Checking the List

  • Get Head:

    // Returns the first element
    node_t *head_node = list_head(my_list);
  • Get Tail:

    // Returns the last element
    node_t *tail_node = list_tail(my_list);
  • Get Next Element:

    // Returns the element that follows 10
    node_t *next_node = list_item_next(my_list, 10);
  • Access by Index:

    node_t *node = list_item_index(my_list, 1);
    if (node) 
      printf("Value at index 1: %d\n", node->value);
  • Check for Existence:

    // Returns 1 if 20 exists, otherwise 0
    int exists = list_contains(my_list, 20);
  • Get Length:

    // Returns the number of elements
    size_t len = list_length(my_list);

Adding Elements

  • Push to Head:

    // Adds 10 to the start of the list
    list_push(my_list, 10);
  • Append to Tail:

    // Adds 20 to the end of the list
    list_append(my_list, 20); 
  • Insert After a Value:

    // Inserts 15 after the first occurrence of 10
    list_insert(my_list, 10, 15);
  • Insert at Index:

    // Inserts 15 after the first occurrence of 10
    list_insert_index(my_list, 1, 25);

Removing Elements

  • Pop Head:

    node_t *removed = list_pop_head(my_list);
  • Pop Tail:

    node_t *removed = list_pop_tail(my_list);
  • Remove by Value:

    // Removes the first occurrence of 15
    list_remove(my_list, 15);
  • Remove All Instances of a Value:

    list_remove_all(my_list, 10);
  • Remove by Index:

    node_t *removed = list_remove_index(my_list, 2);

Reversing the List

  • Reverse Entire List:

    list_reverse(my_list);
  • Reverse Groups of Elements:

    // Reverses the list in groups of 3
    list_reverse_group(my_list, 3);

Sorted Insertion

You can insert items in a sorted manner using a custom comparator function, as described in the first example:

list_insert_sorted(my_list, 30, comparator);

Memory Management

Always call list_free to free the memory of the list when it's no longer needed:

list_free(my_list);

About

A library for operating with singly linked lists of integers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages