SoFunction
Updated on 2025-04-12

Implementation of C language method to limit the maximum length of linked list

In C, limiting the length of a linked list usually means checking the current length of the linked list when adding a new element to the linked list, and no new elements are added if the length has reached the preset maximum. Here is a simple example of how to implement this feature.

First, define the structure of the linked list node:

#include <>  
#include <>  
  
typedef struct Node {  
    int data;  
    struct Node* next;  
} Node;  
  
// Linked list structure, including the head node and maximum length of linked listtypedef struct LinkedList {  
    Node* head;  
    int maxLength;  
    int currentLength;  
} LinkedList;

Then, implement the initialization function of the linked list, including setting the maximum length and current length of the linked list:

LinkedList* createLinkedList(int maxLength) {  
    LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));  
    if (list == NULL) {  
        printf("Memory allocation failed\n");  
        return NULL;  
    }  
    list->head = NULL;  
    list->maxLength = maxLength;  
    list->currentLength = 0;  
    return list;  
}

Next, implement the function to add nodes and check the length of the linked list before adding:

void appendNode(LinkedList* list, int data) {  
    if (list->currentLength >= list->maxLength) {  
        printf("Cannot add more elements, list is full.\n");  
        return;  
    }  
  
    Node* newNode = (Node*)malloc(sizeof(Node));  
    if (newNode == NULL) {  
        printf("Memory allocation failed\n");  
        return;  
    }  
  
    newNode->data = data;  
    newNode->next = NULL;  
  
    // If the linked list is empty, the new node is the header node    if (list->head == NULL) {  
        list->head = newNode;  
    } else {  
        // Otherwise, iterate over to the end of the linked list and add a new node        Node* temp = list->head;  
        while (temp->next != NULL) {  
            temp = temp->next;  
        }  
        temp->next = newNode;  
    }  
  
    list->currentLength++;  
}

Next, implement the function to add nodes and check the length of the linked list before adding:

void appendNode(LinkedList* list, int data) {  
    if (list->currentLength >= list->maxLength) {  
        printf("Cannot add more elements, list is full.\n");  
        return;  
    }  
  
    Node* newNode = (Node*)malloc(sizeof(Node));  
    if (newNode == NULL) {  
        printf("Memory allocation failed\n");  
        return;  
    }  
  
    newNode->data = data;  
    newNode->next = NULL;  
  
    // If the linked list is empty, the new node is the header node    if (list->head == NULL) {  
        list->head = newNode;  
    } else {  
        // Otherwise, iterate over to the end of the linked list and add a new node        Node* temp = list->head;  
        while (temp->next != NULL) {  
            temp = temp->next;  
        }  
        temp->next = newNode;  
    }  
  
    list->currentLength++;  
}

Finally, you can use these functions like this:

int main() {  
    LinkedList* list = createLinkedList(5); // Create a link table with a maximum length of 5  
    appendNode(list, 1);  
    appendNode(list, 2);  
    appendNode(list, 3);  
    appendNode(list, 4);  
    appendNode(list, 5);  
  
    // Try to add again, it should be blocked    appendNode(list, 6);  
  
    // Here you can add code to iterate through the linked list and print its contents  
    return 0;  
}

In this example, we define aLinkedListStructure to store the head node, maximum length and current length of the linked list. This way, when adding new nodes, we can easily check whether the linked list is full, thus preventing the preset length limit from exceeding.

This is the article about the method of limiting the maximum length of the C-language list. For more related content on limiting the maximum length of the C-language list, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!