This C program implements linked lists for two different data...

August 29, 2025 at 09:01 AM

#include <stdio.h> #include <stdlib.h> // Node for float list typedef struct FloatNode { float data; struct FloatNode* next; } FloatNode; // Node for int list typedef struct IntNode { int data; struct IntNode* next; } IntNode; // Function prototypes FloatNode* createFloatList(); void displayFloatList(FloatNode* head); IntNode* createIntList(); void displayIntList(IntNode* head); int countIntList(IntNode* head); void displayReverse(IntNode* head); void displayAlternate(IntNode* head); void searchElement(IntNode* head, int key); // ------------------ Main Program ------------------ int main() { int choice; FloatNode* floatList = NULL; IntNode* intList = NULL; int key; while (1) { printf("\n========= Linked List Menu =========\n"); printf("1. Create & Display Float Linked List\n"); printf("2. Create & Count Elements in Int Linked List\n"); printf("3. Create & Display Int List in Reverse\n"); printf("4. Create & Display Alternate Nodes of Int List\n"); printf("5. Create & Search Element in Int Linked List\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: floatList = createFloatList(); printf("Float Linked List: "); displayFloatList(floatList); break; case 2: intList = createIntList(); printf("Int Linked List: "); displayIntList(intList); printf("Count = %d\n", countIntList(intList)); break; case 3: intList = createIntList(); printf("Reversed List: "); displayReverse(intList); printf("\n"); break; case 4: intList = createIntList(); printf("Alternate Nodes: "); displayAlternate(intList); printf("\n"); break; case 5: intList = createIntList(); printf("Enter element to search: "); scanf("%d", &key); searchElement(intList, key); break; case 0: printf("Exiting program. Goodbye!\n"); exit(0); default: printf("Invalid choice. Please try again.\n"); } } return 0; } // --------------- Float Linked List Functions ----------------- FloatNode* createFloatList() { int n, i; float value; FloatNode *head = NULL, *temp, *newNode; printf("Enter number of float elements: "); scanf("%d", &n); printf("Enter %d float values (space-separated): ", n); for (i = 0; i < n; i++) { scanf("%f", &value); newNode = (FloatNode*)malloc(sizeof(FloatNode)); newNode->data = value; newNode->next = NULL; if (!head) head = newNode; else { temp = head; while (temp->next) temp = temp->next; temp->next = newNode; } } return head; } void displayFloatList(FloatNode* head) { FloatNode* temp = head; while (temp) { printf("%.1f", temp->data); if (temp->next) printf("->"); temp = temp->next; } printf("\n"); } // --------------- Integer Linked List Functions ----------------- IntNode* createIntList() { int n, i, value; IntNode *head = NULL, *temp, *newNode; printf("Enter number of int elements: "); scanf("%d", &n); printf("Enter %d integer values (space-separated): ", n); for (i = 0; i < n; i++) { scanf("%d", &value); newNode = (IntNode*)malloc(sizeof(IntNode)); newNode->data = value; newNode->next = NULL; if (!head) head = newNode; else { temp = head; while (temp->next) temp = temp->next; temp->next = newNode; } } return head; } void displayIntList(IntNode* head) { IntNode* temp = head; while (temp) { printf("%d", temp->data); if (temp->next) printf("->"); temp = temp->next; } printf("\n"); } int countIntList(IntNode* head) { int count = 0; while (head) { count++; head = head->next; } return count; } void displayReverse(IntNode* head) { if (!head) return; displayReverse(head->next); printf("%d", head->data); if (head->next) printf("->"); } void displayAlternate(IntNode* head) { int toggle = 1; while (head) { if (toggle) { printf("%d", head->data); if (head->next && head->next->next) printf("->"); } toggle = !toggle; head = head->next; } } void searchElement(IntNode* head, int key) { int pos = 1; while (head) { if (head->data == key) { printf("Element %d found at position %d\n", key, pos); return; } head = head->next; pos++; } printf("Element %d not found in the list.\n", key); }

This C program implements linked lists for two different data types: float and int. It provides several functionalities for creating, displaying, and manipulating these linked lists. Below is a detailed explanation of the code:


Key Functionalities:

  1. Float Linked List Operations:

    • createFloatList: Creates a linked list of float elements by dynamically allocating memory for nodes.
    • displayFloatList: Traverses and displays the elements of the float linked list in the order they appear.
  2. Integer Linked List Operations:

    • createIntList: Creates a linked list of int elements by dynamically allocating memory for nodes.
    • displayIntList: Traverses and displays the elements of the int linked list in the order they appear.
    • countIntList: Counts the number of nodes in an int linked list.
    • displayReverse: Recursively traverses and displays the int linked list in reverse order.
    • displayAlternate: Displays every alternate node of the int linked list starting from the first node.
    • searchElement: Searches for a specific integer value (key) in the int linked list and reports its position if found. Otherwise, it indicates that the key is not present.
  3. Menu-Based Interaction:

    • The main function provides a menu-based interface for the user to interact with the program. The user can choose from the following options:
      • Option 1: Create and display a float linked list.
      • Option 2: Create an int linked list, display it, and count its elements.
      • Option 3: Create an int linked list and display its elements in reverse order.
      • Option 4: Create an int linked list and display its alternate nodes.
      • Option 5: Create an int linked list and search for a user-provided key in it.
      • Option 0: Exit the program.
  4. Dynamic Memory Management:

    • The program uses malloc to allocate memory for nodes in the linked lists, ensuring efficient dynamic memory management.

Example Usage:

Suppose a user runs the program and provides the following inputs:

  1. Option 1 (Float List Creation and Display):

    • Input: Enter number of float elements: 3
      Enter 3 float values: 1.1 2.2 3.3
    • Output: "Float Linked List: 1.1->2.2->3.3"
  2. Option 2 (Integer List Creation, Display, and Count):

    • Input: Enter number of int elements: 4
      Enter 4 integer values: 5 6 7 8
    • Output: "Int Linked List: 5->6->7->8"
      "Count = 4"
  3. Option 3 (Reverse Display):

    • Input: Same integer list as above.
    • Output: "Reversed List: 8->7->6->5"
  4. Option 4 (Alternate Nodes Display):

    • Input: Same integer list as above.
    • Output: "Alternate Nodes: 5->7"
  5. Option 5 (Search for an Element):

    • Input: Enter element to search: 7
    • Output: "Element 7 found at position 3"
  6. Option 0 (Exit):

    • Output: "Exiting program. Goodbye!"

Summary of Code Behavior:

The program allows users to interactively create and manipulate float and integer linked lists. Users can:

  • Create lists of float or int values.
  • Display these lists in standard, reverse, or alternate order.
  • Count the number of integer nodes.
  • Search for specific elements in integer lists.
  • Exit the program whenever desired.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node