×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Hands-On Implementation of Linked Lists and File Operations in C Programming: A Sample Assignment

August 28, 2024
Fredrick Smith
Fredrick Smith
🇺🇸 United States
Data Structures and Algorithms
Fredrick Smith is a seasoned software engineer specializing in data structures, algorithms, and C programming. With over 10 years of experience, he excels in designing efficient linked list operations and implementing robust file handling systems. His expertise lies in translating complex concepts into practical solutions, ensuring high performance and reliability in every project.
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​
Key Topics
  • Question:
  • Single Link List – Part I
  • Solution:
  • Part II - Transaction Processing Program using Random Access Files
    • Program Statement:
  • Solution:

Welcome to our detailed sample solution for data structures and analysis of algorithms assignments, designed to showcase our expertise in providing comprehensive programming assignment help. In this example, we delve into the intricacies of singly linked lists and file handling operations. By implementing key functions for managing linked lists and performing file I/O tasks, this assignment highlights essential concepts such as data manipulation, algorithm efficiency, and memory management. Through this demonstration, you'll see how our services can guide you through complex programming challenges, ensuring a solid understanding of data structures and algorithmic analysis. For expert assistance and tailored support, explore our help with data structures and analysis of algorithms assignments.

Question:

In this assignment you are going to implement a set functions for a single link list.

Given the following data type:

typedef struct node { int data; struct node *next; }node_t;

and the given functions:

/*************************************************************/ /* Provided functions * llist_push: Add an element to the head of the list * llist_pop: Removes first element of the list * llist_size: Returns number of elements in the list *************************************************************/

Implement the following function by the end of the lab:

/******************************************************************/ /* Functions you need to implement * 1> EASY functions: no need to modify the list * llist_contains: Returns 1 if an element is contained in the list * 0 otherwise * llist_count: Returns the number of times an element is in the list * llist_find: Returns the index of the first element given it finds, * -1 if not found * llist_is_equal: Returns 1 if lists are the same, 0 if not * Two lists are the same if * they have the same number of elements * and the elements are presented in the same order * * 2> MEDIUM functions: needs to modify the list * llist_addlast: Adds an element to the tail of the list * llist_insert: Adds an element at index * llist_remove: Removes first instance of value * llist_free: Free all elements in the list ********************************************************************/

You MUST implement the llist_free( ) function as it is required in all tests.

By the end of the lab you must demonstrate the passing of at least 5 tests, including at least 2 easy function and 3 medium functions.

You must submit your source code (.c and .h file)

Solution:

.C file /* * llist.c * * Created on: Apr 7, 2021 */ #include <stdio.h> #include <stdlib.h> #include "llist.h" /** * Add an element to the head of the list */ int llist_push(node_t **head, int element) { node_t *newNode = (node_t*) calloc(1, sizeof(node_t)); if (newNode == NULL) return -1; /* Assign data */ newNode->data = element; /* Next note will be current head node*/ newNode->next = *head; /* Make new node is new head node*/ *head = newNode; return 0; } /** * Removes first element of the list */ void llist_pop(node_t **head) { node_t *tmp = *head; /* Can not pop the list empty*/ if (*head == NULL) return; /* Move head to next node*/ *head = tmp->next; /*Free resource */ free(tmp); } /** * Returns number of elements in the list */ int llist_size(node_t *head) { int size = 0; node_t *currentNode = head; while (currentNode != NULL) { currentNode = currentNode->next; size++; } return size; } /** * Returns 1 if an element is contained in the list */ int llist_contains(node_t *head, int element) { node_t *currentNode = head; while (currentNode != NULL) { if (currentNode->data == element) { return 1; } currentNode = currentNode->next; } return 0; } /** * Returns the number of times an element is in the list */ int llist_count(node_t *head, int element) { int count = 0; node_t *currentNode = head; while (currentNode != NULL) { if (currentNode->data == element) { count++; } currentNode = currentNode->next; } return count; } /** * Returns the index of the first element given it finds, * -1 if not found */ int llist_find(node_t *head, int element) { int count = 0; node_t *currentNode = head; while (currentNode != NULL) { if (currentNode->data == element) { return count; } count++; currentNode = currentNode->next; } return -1; } /** * Returns 1 if lists are the same, 0 if not * Two lists are the same if * they have the same number of elements * and the elements are presented in the same order */ int llist_is_equal(node_t *head1, node_t *head2) { node_t *currentNode1 = head1; node_t *currentNode2 = head2; while (currentNode1 != NULL && currentNode2 != NULL) { if (currentNode1->data != currentNode2->data) return 0; currentNode1 = currentNode1->next; currentNode2 = currentNode2->next; } /* If one of them don't go to end */ if (currentNode1 != NULL || currentNode2 != NULL) return 0; return 1; } /** * Adds an element to the tail of the list */ int llist_addlast(node_t **head, int element) { node_t *newNode = (node_t*) calloc(1, sizeof(node_t)); if (newNode == NULL) return 1; newNode->data = element; if (*head == NULL) { /* This is the first node */ newNode->next = NULL; *head = newNode; } else { /* Go to end of list */ node_t *currentNode = *head; while (currentNode->next != NULL) { currentNode = currentNode->next; } /* Now currentNode is the last node. // Add new last node */ newNode->next = NULL; currentNode->next = newNode; } return 0; } /** * Adds an element at index */ int llist_insert(node_t **head, int element, int index) { int count = 0; node_t *currentNode = *head; node_t *prevNode = NULL; node_t *newNode = (node_t*) calloc(1, sizeof(node_t)); if (newNode == NULL) return 1; newNode->data = element; /* if the index is zero. Insert to head */ if (index == 0) { newNode->next = *head; *head = newNode; } else { /* Find to location for add */ while (currentNode != NULL && count < index) { prevNode = currentNode; currentNode = currentNode->next; count++; } if (count != index) { /* Out of index */ free(newNode); return 0; } /* Insert */ newNode->next = currentNode; prevNode->next = newNode; } return 0; } /** * Removes first instance of value */ void llist_remove(node_t **head, int element) { node_t *currentNode = *head; node_t *prevNode = NULL; /* List is empty */ if (*head == NULL) return; /* If remove is head */ if (currentNode->data == element) { *head = currentNode->next; free(currentNode); } else { while (currentNode != NULL) { if (currentNode->data == element) break; prevNode = currentNode; currentNode = currentNode->next; } /* Now current node is node should be delete */ if (currentNode != NULL && currentNode->data == element) { prevNode->next = currentNode->next; free(currentNode); } } } /** * Free all elements in the list */ void llist_free(node_t **head) { node_t *currentNode = *head; while (*head != NULL) { currentNode = currentNode->next; free(*head); *head = currentNode; } }

.h file

/* * llist.h * * Created on: Apr 7, 2021 */ #ifndef LLIST_H_ #define LLIST_H_ typedef struct node { int data; struct node *next; } node_t; /*************************************************************/ /* Provided functions * llist_push: Add an element to the head of the list * llist_pop: Removes first element of the list * llist_size: Returns number of elements in the list *************************************************************/ int llist_push(node_t **head, int element); void llist_pop(node_t **head); int llist_size(node_t *head); /******************************************************************/ /* Functions you need to implement * 1> EASY functions: no need to modify the list * llist_contains: Returns 1 if an element is contained in the list * 0 otherwise * llist_count: Returns the number of times an element is in the list * llist_find: Returns the index of the first element given it finds, * -1 if not found * llist_is_equal: Returns 1 if lists are the same, 0 if not * Two lists are the same if * they have the same number of elements * and the elements are presented in the same order * * 2> MEDIUM functions: needs to modify the list * llist_addlast: Adds an element to the tail of the list * llist_insert: Adds an element at index * llist_remove: Removes first instance of value * llist_free: Free all elements in the list ********************************************************************/ int llist_contains(node_t *head, int element); int llist_count(node_t *head, int element); int llist_find(node_t *head, int element); int llist_is_equal(node_t *head1, node_t *head2); int llist_addlast(node_t **head, int element); int llist_insert(node_t **head, int element, int index); void llist_remove(node_t **head, int element); void llist_free(node_t **head); #endif /* LLIST_H_ */

Part II - Transaction Processing Program using Random Access Files

In this part, you are going to implement file handling functions.

Program Statement:

The program maintains a bank’s account statistics. The program updates existing accounts, adds new accounts, deletes accounts and stores a listing of all the current accounts in a text file for printing. You must assume that the below program (creation of random access file sequentially) has been executed to create the file credit.dat.

/*Consider the following statement for the creation of a random-access file sequentially:--

Creating a credit processing system capable of storing upto 100 fixed-length records. Each record consists of an account number that will be used as the ‘record key’ (first name, last name and a balance). The resulting program must be able update an account, insert a new account record, delete an account and list all the account records in a formatted text file for printing. Make use of a random-access file*/

#include<stdio.h> struct clientInfo { int AccNo; char lastName[20]; char firstName[20]; double accBalance; }; int main(void) { int i; /*counter used to count from 1-100*/ struct clientInfo randomClient = {0, “”, “”, 0.0}; /*create clientInfo with default information*/ FILE *fptr; if((fptr=fopen(“credit.dat”, “wb”))==NULL) { printf(“File could not be opened”); } Else { for(i=1;i<=100;i++) /*output 100 random blank records to file*/ { fwrite(&randomClient, sizeof(struct clientInfo), 1, fptr); } fclose(fptr); } return 0; }

The program has five options:

1) Option 1 calls function textFile to store a formatted list of all the accounts in a text file called accounts.txt that may be printed later. To implement this, the function uses fread and the sequential file access techniques. After choosing option 1, the file accounts.txt contains:

accNo.lastNamefirstNameaccBalance
29BrownPeter-121.01
33DunnGeorge142.23
37BarkerGee0.01
88SmithDavid248.15
89StoneSam35.68

2) Option 2 calls the function updateRecord to update an account. The function will only update a record that already exists, so the function first checks to see if the record specified by the user is empty. The record is read into structure client with fread, then member acctNo is compared to 0. If it’s 0, the record contains no information, and a message is printed stating that “the record is empty”. Then, the menu choices are displayed. If the record contains information, function updateRecord inputs the transaction amount, calculates the new balance and rewrites the record to the file. A typical output for option 2 is:---

Enter account to update (1-100): 37

37 Barker Gee 0.01

Enter charge (+) or payment (-): +81.95

37 Barker Gee 81.96

3) Option 3 calls the function newRecord to add a new account to the file. If the user enters an account number for an existing account, newRecord displays an error message that the record already contains information, and the menu choices are printed again. A typical output for option 3 is:--

Enter new account number (1-100): 21

Enter lastName, firstName, balance

Johnson Sarah 247.845

4) Option 4 calls function deleteRecord to delete a record from the file. Deletion is accomplished by asking the user for the account number and reinitializing the record. If the account contains no information, deleteRecord displays an error message that the account does not exist.

5) Option 5 terminates program execution.

Solution:

.c file

/* PROGRAM: test_llist TOPIC: Single Link List Test PURPOSE: TEST LLIST NOTES: */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <check.h> #include "llist.h" #include "test_llist.h" #define TEST_SUCCESS "SUCCESS: All current tests passed!" #define TEST_FAILURE "FAILURE: At least one test failed or crashed" #define FILLER "*****************************************************************************" #define NUM_RANDOM( min, max ) ( min + (int)( (double)max * rand() / ( RAND_MAX + 1.0 ) ) ) /*******************************************************************/ /* Function Prototypes *******************************************************************/ Suite * test_suite(void); int run_testsuite(void); /*******************************************************************/ /* main( ) *******************************************************************/ int main(int argc, char* argv[]){ srand( time( NULL ) ); return run_testsuite(); } /*******************************************************************/ /* test_suit( ) *******************************************************************/ Suite * test_suite(void) { Suite *s; TCase *tc_llist; s = suite_create("ALL CASES Link List"); tc_llist = tcase_create("Link List"); /*************** TEST A ********************** * 10 Cases **********************************************/ tcase_add_test(tc_llist, test_contains ); tcase_add_test(tc_llist, test_count ); tcase_add_test(tc_llist, test_find ); tcase_add_test(tc_llist, test_equal ); tcase_add_test(tc_llist, test_addlast ); tcase_add_test(tc_llist, test_insert ); tcase_add_test(tc_llist, test_remove ); suite_add_tcase( s, tc_llist ); return s; } /*******************************************************************/ /* run_testsuite( ) *******************************************************************/ int run_testsuite(){ int fail_nr; Suite *s; SRunner *sr; printf("%s\n", FILLER ); s = test_suite(); sr = srunner_create(s); srunner_run_all(sr, CK_VERBOSE); fail_nr = srunner_ntests_failed(sr); srunner_free(sr); printf("%s\n", FILLER ); printf("%s\n", fail_nr ? TEST_FAILURE : TEST_SUCCESS ); printf("%s\n", FILLER ); return ( !fail_nr ) ? EXIT_SUCCESS : EXIT_FAILURE; }

.h file

/*********************************************************************** * Filename: test_llist.h * Version: 2.0 * * Lab Name: Single Link List Test Case * * Purpose: Header of test_llist.c. Define test cases to test following * functions of single link list: llist_contains, llist_count, llist_find, * llist_is_equal, llist_addlast, llist_insert, llist_remove. * * *********************************************************************/ START_TEST(test_contains) { node_t *head = NULL; llist_push(&head, 10); ck_assert(llist_contains(head, 10) == 1); llist_free(&head); } END_TEST START_TEST(test_count) { node_t *head = NULL; ck_assert(llist_count(head, 10) == 0); llist_push(&head, 10); llist_push(&head, 10); llist_push(&head, 10); llist_push(&head, 10); ck_assert(llist_count(head, 10) == 4); llist_free(&head); } END_TEST START_TEST(test_find) { node_t *head = NULL; ck_assert(llist_find(head, 5) == -1); llist_push(&head, 10); llist_push(&head, 5); ck_assert(llist_find(head, 5) == 0); ck_assert(llist_find(head, 10) == 1); ck_assert(llist_find(head, 7) == -1); llist_free(&head); } END_TEST START_TEST(test_equal) { node_t *h1 = NULL; node_t *h2 = NULL; ck_assert(llist_is_equal(h1, h1)); llist_push(&h1, 10); llist_push(&h1, 5); llist_push(&h2, 10); llist_push(&h2, 5); ck_assert(llist_is_equal(h1, h2)); llist_push(&h2, 15); ck_assert(!llist_is_equal(h1, h2)); llist_free(&h1); llist_free(&h2); } END_TEST START_TEST(test_addlast) { node_t *h1 = NULL; llist_addlast(&h1, 76); ck_assert(llist_find(h1, 76) == 0); llist_push(&h1, 10); llist_push(&h1, 5); llist_push(&h1, 8); llist_addlast(&h1, 20); ck_assert(llist_size(h1) == 5); ck_assert(llist_find(h1, 20) == 4); llist_free(&h1); } END_TEST START_TEST(test_insert) { node_t *h1 = NULL; llist_insert(&h1, 76, 0); ck_assert(llist_find(h1, 76) == 0); ck_assert(llist_insert(&h1, 6, 10) == 0); ck_assert(!llist_contains(h1, 6)); llist_push(&h1, 10); llist_push(&h1, 5); llist_push(&h1, 8); llist_insert(&h1, 20, 2); ck_assert(llist_size(h1) == 5); ck_assert(llist_find(h1, 20) == 2); llist_free(&h1); } END_TEST START_TEST(test_remove) { node_t *h1 = NULL; llist_remove(&h1, 10); llist_push(&h1, 10); llist_remove(&h1, 10); ck_assert(llist_count(h1, 10) == 0); llist_push(&h1, 20); llist_push(&h1, 10); llist_push(&h1, 5); llist_push(&h1, 8); llist_push(&h1, 10); llist_remove(&h1, 10); ck_assert(llist_count(h1, 10) == 1); llist_remove(&h1, 8); ck_assert(!llist_contains(h1, 8)); llist_remove(&h1, 10); ck_assert(llist_count(h1, 10) == 0); llist_free(&h1); } END_TEST

Similar Samples

Explore our extensive collection of expertly solved programming samples at Programming Homework Help. Each example is carefully crafted to demonstrate the highest quality solutions. Check out our work and consider using our services to tackle your assignments with confidence.