Overview
Push Swap uses a doubly-linked list structure to represent both Stack A and Stack B. Each node contains not only the data value but also metadata used for optimization during sorting.The t_list Structure
The core data structure is defined inpush_swap.h:21-31:
Field Descriptions
nbr - Node Value
nbr - Node Value
Stores the integer value that was passed as input. This is the actual number that needs to be sorted.Type:
intUsage: Compared during sorting operations to determine the correct position of nodes.index - Position Index
index - Position Index
Represents the current position of the node in its stack, starting from 0 at the top.Type:
intUpdated by: current_index() function (init_a_to_b.c:15-34)Purpose: Used to calculate rotation costs and determine if a node is above or below the median.push_cost - Movement Cost
push_cost - Movement Cost
The total number of operations required to move this node to its target position in the other stack.Type:
intCalculated by: cost_analysis_a() function (init_a_to_b.c:64-82)Formula:- If above median:
cost = index + target_index - If below median:
cost = (stack_len - index) + target_cost
above_median - Position Flag
above_median - Position Flag
Boolean flag indicating whether the node is in the upper half of the stack.Type:
int (used as boolean)Purpose: Determines whether to use rotate (ra/rb) or reverse rotate (rra/rrb) operations.Set in: current_index() - nodes at position ≤ median/2 are marked as above_median.cheapest - Optimal Move Flag
cheapest - Optimal Move Flag
Boolean flag marking the node with the lowest push_cost in the stack.Type:
int (used as boolean)Set by: set_cheapest() function (init_a_to_b.c:84-102)Usage: Only one node per stack is marked as cheapest at any time.target_node - Destination Pointer
target_node - Destination Pointer
Points to the node in the opposite stack where this node should be positioned.Type:
struct s_list *Set by:set_target_a()- When moving from A to B (init_a_to_b.c:36-62)set_target_b()- When moving from B to A (init_b_to_a.c:15-41)
- From A to B: Find the largest value in B that is smaller than the current node
- From B to A: Find the smallest value in A that is larger than the current node
next - Forward Pointer
next - Forward Pointer
Points to the next node in the stack (towards the bottom).Type:
struct s_list *Value: NULL for the last node in the stack.prev - Backward Pointer
prev - Backward Pointer
Points to the previous node in the stack (towards the top).Type:
struct s_list *Value: NULL for the first node (top) of the stack.Purpose: Enables bidirectional traversal of the list.Doubly-Linked List Implementation
The Push Swap implementation uses a circular doubly-linked list structure that provides several advantages:Benefits
- Efficient Rotation: Both forward (ra/rb) and reverse rotation (rra/rrb) operations are O(1) with proper pointer manipulation
- Bidirectional Traversal: Can traverse the stack from top to bottom or bottom to top
- Easy Node Insertion/Deletion: Push and pop operations are efficient
Structure Properties
- The top of the stack is the first node (stack head pointer)
- The bottom of the stack is the last node
- Top node’s
prevpointer isNULL - Bottom node’s
nextpointer isNULL
Stack Utility Functions
The implementation provides several utility functions for working with stacks:stack_len()
find_min()
- Traverses the entire stack
- Compares each node’s
nbrvalue - Returns pointer to the node with the smallest value
- Returns
NULLif stack is empty
set_target_a(), set_target_b(), and min_on_top() functions.
find_max()
- Traverses the entire stack
- Compares each node’s
nbrvalue - Returns pointer to the node with the largest value
- Returns
NULLif stack is empty
set_target_a() when no suitable smaller value exists in Stack B.
Memory Management
All nodes are dynamically allocated and must be properly freed. The implementation includes a
free_stack() function to deallocate all nodes in a stack when sorting is complete or when an error occurs.Performance Characteristics
| Operation | Time Complexity |
|---|---|
| stack_len() | O(n) |
| find_min() | O(n) |
| find_max() | O(n) |
| Access by index | O(n) |
| Push/Pop (top) | O(1) |
| Rotation | O(1) |