https://github.com/rajnandinithopte/advanced-systems-programming_doubly-linked-circular-list-transaction-sorting
This project implements a doubly-linked circular list for efficient transaction management and sorting, ensuring robust memory handling, error detection, and structured financial reporting with optimized insertion, deletion, and in-place sorting.
https://github.com/rajnandinithopte/advanced-systems-programming_doubly-linked-circular-list-transaction-sorting
algorithms-and-data-structures c memory-management operating-systems pointers
Last synced: about 2 months ago
JSON representation
This project implements a doubly-linked circular list for efficient transaction management and sorting, ensuring robust memory handling, error detection, and structured financial reporting with optimized insertion, deletion, and in-place sorting.
- Host: GitHub
- URL: https://github.com/rajnandinithopte/advanced-systems-programming_doubly-linked-circular-list-transaction-sorting
- Owner: rajnandinithopte
- Created: 2025-02-03T22:16:03.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-02-03T22:44:41.000Z (3 months ago)
- Last Synced: 2025-02-03T23:28:46.008Z (3 months ago)
- Topics: algorithms-and-data-structures, c, memory-management, operating-systems, pointers
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# š· Advanced Systems Programming: Doubly-Linked Circular List & Transaction Sorting
## š¶ Overview
This project implements a **doubly-linked circular list** (`MyList`) and a **transaction sorting utility** (`transaction.c`). The doubly-linked circular list serves as a fundamental data structure for handling transactions, which are read from a file or standard input. The project emphasizes **low-level memory management**, **pointer manipulation**, and **efficient sorting algorithms** to ensure correct functionality.---
## š· Libraries Used:
- **stdio.h** ā Standard I/O operations.
- **stdlib.h** ā Memory allocation and process control.(malloc, free)
- **string.h** ā String manipulation.(strcmp, strcpy)
- **time.h** ā Handling UNIX timestamps (ctime, time)
- **ctype.h** ā Character handling.
- **unistd.h** ā File access control.
- **errno.h** - Error handling---
## š· Data Structure: Doubly-Linked Circular List (`MyList`)
### š¶ Structure & Characteristics:
The `MyList` is a **circular doubly-linked list** with an **anchor node** that simplifies list operations. Unlike a traditional doubly-linked list, the **anchor node is always present** and is used as a placeholder to manage list boundaries effectively.### š¶ Components:
- **Anchor Node (`anchor`)**:
- A sentinel node that **does not store user data**.
- Its `next` pointer points to the **first real element**, and its `prev` pointer points to the **last real element**.
- This eliminates edge cases when inserting/removing elements.
- **List Elements (`MyListElem`)**:
- Each element stores an **object (`obj`)** representing a transaction.
- Contains two pointers:
- `next` ā Points to the next element in the list.
- `prev` ā Points to the previous element in the list.
- **List Operations**:
- `MyListInit()` ā Initializes an empty list with only the anchor node.
- `MyListAppend()` ā Inserts an element at the end of the list.
- `MyListPrepend()` ā Inserts an element at the beginning of the list.
- `MyListInsertAfter()` ā Inserts an element after a given node.
- `MyListInsertBefore()` ā Inserts an element before a given node.
- `MyListUnlink()` ā Removes a specific element from the list.
- `MyListUnlinkAll()` ā Clears the entire list.
- `MyListFirst()` ā Returns the first element in the list.
- `MyListLast()` ā Returns the last element in the list.
- `MyListNext()` ā Returns the next element in the list.
- `MyListPrev()` ā Returns the previous element in the list.### š¶ Benefits of Circular Design:
- **Avoids null pointer checks** at list boundaries.
- **Simplifies iteration** through the list.
- **Efficient insertion/deletion** at both ends without special conditions.---
## š· Transaction Sorting (`transaction.c`)
This program reads a file containing **financial transactions**, stores them in a `MyList`, and sorts them by date.### š¶ Transaction Data Format:
Each transaction is stored as a **line** in the input file and consists of:
[TYPE] [TIMESTAMP] [AMOUNT] [DESCRIPTION]
- `[TYPE]`: `+` for **credit**, `-` for **debit**.
- `[TIMESTAMP]`: Unix timestamp indicating the transaction time.
- `[AMOUNT]`: Decimal value representing the transaction amount.
- `[DESCRIPTION]`: A string describing the transaction.### š¶ Parsing and Validation:
- Ensures the **correct number of fields** are present.
- Verifies that the **timestamp is a valid numeric value**.
- Confirms the **amount is non-negative and formatted correctly**.
- **Trims leading/trailing spaces** from the description.### š¶ Sorting Algorithm:
- Transactions are stored in a `MyList` and **sorted by timestamp**.
- Uses **Insertion Sort** due to the **small dataset size** (efficient for up to a few thousand elements).
- Maintains **stable sorting** so that transactions with identical timestamps **retain their input order**.---
## š· Steps Taken to Accomplish the Project### š¶ 1. Implementing the Doubly-Linked Circular List
- Developed a custom linked list to manage transactions efficiently.
- Implemented O(1) insertion and deletion operations.
- Ensured the list remains circular by updating `next` and `prev` pointers.### š¶ 2. Parsing and Validating Transaction Data
- Read transaction records from an input file or `stdin`.
- Tokenized each line using **tab-separated fields**.
- Checked the validity of timestamps, amounts, and descriptions:
- **Timestamps**: Must be **greater than zero** and **less than the current time**.
- **Amounts**: Must be **formatted correctly** with **two decimal places**.
- **Descriptions**: Cannot be **empty** after trimming leading spaces.### š¶ 3. Sorting Transactions by Timestamp
- Implemented **bubble sort** for sorting linked list elements **in-place**.
- Ensured **O(n²) complexity** is reasonable given the constraints.
- Handled **duplicate timestamps** by printing an error and terminating execution.### š¶ 4. Formatting and Displaying Transactions
- Used **structured formatting** for output:
- **Date Field**: Converted UNIX timestamps using `ctime()`.
- **Description Field**: Truncated to **24 characters max**.
- **Amount Field**: Right-aligned and formatted correctly.
- **Balance Field**: Updated dynamically while processing transactions.
- Printed the **transaction history** as an **80-character-wide table**.### š¶ 5. Handling Edge Cases
- **Empty Files**: Checked for empty input and **printed an error**.
- **Malformed Transactions**: Handled incorrect formats gracefully.
- **Extreme Balances**: Ensured balances over **10 million** were marked as `?,???,???.??`.### š¶ 6. Testing and Debugging
- Used `gdb` to **debug segmentation faults** and **memory allocation errors**.
- Verified correctness using **sample test cases**.
- Ensured **no memory leaks** by freeing allocated structures before exit.---
## š· Rigorous Testing ProcedureTo ensure the correctness and robustness of the implementation, the following testing strategies were employed:
### š¶ 1. Unit Testing of Individual Components
- **Doubly-Linked Circular List**: Verified insertion, deletion, and traversal.
- **Sorting Algorithm**: Ensured the transactions are sorted correctly by timestamps.
- **Transaction Parsing**: Checked handling of various formats and erroneous data.### š¶ 2. Edge Case Handling
- **Empty Input File**: Confirmed that the program properly detects and handles empty files.
- **Malformed Transactions**: Tested various incorrect formats (e.g., missing fields, invalid timestamps, incorrect amounts).
- **Duplicate Timestamps**: Ensured that duplicate timestamps trigger an error message and halt execution.### š¶ 3. Memory Management Validation
- **Valgrind Analysis**: Used valgrind to detect memory leaks and invalid memory accesses.
- **Pointer Debugging**: Ensured proper freeing of allocated memory to avoid segmentation faults.### š¶ 4. Stress Testing
- **Large Dataset Processing**: Evaluated performance with a high number of transactions.
- **Extreme Values**: Tested balances exceeding 10 million to check correct formatting (?,???,???.??).
- **High-Frequency Transactions**: Ensured sorting and display work efficiently with closely spaced timestamps.### š¶ 5. Automated Grading Scripts
- Used a predefined grading script that runs multiple test cases and validates output using `diff`.
- Ensured no unexpected formatting issues arise in the final transaction table.---
š **Note**:
The code for this project cannot be made publicly available due to academic restrictions. However, it can be shared upon request for review or discussion purposes.