Overview
This implementation extends singly linked lists with advanced operations like insertion at index, deletion, and stack-like operations. Unlike the basic implementation, this version stores integers instead of strings.Data Structure Definition
The integer-based singly linked list is defined inlists.h:14-18:
lists.h
This simpler structure stores only:
n: The integer valuenext: Pointer to the next node
Basic Operations
Adding Nodes
add_nodeint.c
Space Complexity: O(1) - single node allocation
Advanced Operations
Stack Operations: Pop
pop_listint.c
This implements stack-like LIFO (Last In, First Out) behavior:
- Save the value from the head node
- Update head to point to next node
- Free the old head
- Return the saved value
Indexed Access
get_nodeint_at_index.c
Space Complexity: O(1) - only index counter
Insertion at Index
insert_nodeint_at_index.c
Deletion at Index
delete_nodeint_at_index.c
Return Values:
1: Successfully deleted node-1: Failed (invalid index or empty list)
- Empty list (
*head == NULL) - Delete first node (
index == 0) - Index out of bounds (traversal reaches NULL)
Complete Function Set
Fromlists.h:20-30, the full API includes:
lists.h
Usage Example
example.c
Complexity Summary
| Operation | Time | Space | Notes |
|---|---|---|---|
add_nodeint | O(1) | O(1) | Insert at head |
add_nodeint_end | O(n) | O(1) | Must traverse to end |
pop_listint | O(1) | O(1) | Remove from head |
get_nodeint_at_index | O(n) | O(1) | Linear search |
insert_nodeint_at_index | O(n) | O(1) | Traverse to position |
delete_nodeint_at_index | O(n) | O(1) | Traverse to position |
sum_listint | O(n) | O(1) | Must visit all nodes |
Key Improvements Over Basic Lists
- Index-based operations: Direct access to nodes by position
- Stack operations: Efficient push/pop for LIFO behavior
- In-place modification: Delete and insert without rebuilding list
- Simpler memory model: Integers don’t require separate allocation like strings