c - How to Pass a Char Array (String) into a Linked List (Queue)

C - How to Pass a Char Array (String) into a Linked List (Queue)

To pass a char array (string) into a linked list (queue) in C, you can create a linked list structure and implement functions to enqueue and dequeue elements. Here's a simple example:

#include <stdio.h> #include <stdlib.h> #include <string.h> // Node structure for the linked list typedef struct Node { char data; struct Node* next; } Node; // Linked list structure typedef struct { Node* front; Node* rear; } LinkedList; // Function to initialize an empty linked list void initializeList(LinkedList* list) { list->front = NULL; list->rear = NULL; } // Function to enqueue a character into the linked list void enqueue(LinkedList* list, char data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { fprintf(stderr, "Memory allocation failed\n"); exit(EXIT_FAILURE); } newNode->data = data; newNode->next = NULL; if (list->rear == NULL) { list->front = newNode; list->rear = newNode; } else { list->rear->next = newNode; list->rear = newNode; } } // Function to dequeue a character from the linked list char dequeue(LinkedList* list) { if (list->front == NULL) { fprintf(stderr, "Queue is empty\n"); exit(EXIT_FAILURE); } Node* frontNode = list->front; char data = frontNode->data; list->front = frontNode->next; free(frontNode); if (list->front == NULL) { list->rear = NULL; } return data; } // Function to print the linked list void printList(LinkedList* list) { Node* current = list->front; while (current != NULL) { printf("%c ", current->data); current = current->next; } printf("\n"); } int main() { char inputString[] = "Hello"; // Create and initialize a linked list LinkedList myQueue; initializeList(&myQueue); // Enqueue each character from the input string for (int i = 0; i < strlen(inputString); ++i) { enqueue(&myQueue, inputString[i]); } // Print the linked list printf("Linked List: "); printList(&myQueue); // Dequeue characters and print printf("Dequeued characters: "); while (myQueue.front != NULL) { printf("%c ", dequeue(&myQueue)); } printf("\n"); return 0; } 

In this example, the LinkedList structure represents the linked list, and the Node structure represents each node in the list. The enqueue function adds a character to the end of the linked list, and the dequeue function removes and returns a character from the front of the linked list. The printList function is used to print the contents of the linked list.

This is a basic example, and you might want to extend or modify it based on your specific requirements.

Examples

  1. "C pass string to linked list node"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char data[100]; struct Node* next; }; int main() { struct Node* head = NULL; char inputString[] = "Hello, World!"; // Function to add string to linked list void enqueue(char str[]) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); strcpy(newNode->data, str); newNode->next = head; head = newNode; } enqueue(inputString); // Access the linked list struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code defines a simple linked list node structure (Node) and a function (enqueue) to add strings to the linked list.

  2. "C pass string to linked list with dynamic memory allocation"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char* data; struct Node* next; }; int main() { struct Node* head = NULL; char inputString[] = "Dynamic Memory"; // Function to add string to linked list void enqueue(char str[]) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = strdup(str); // Uses dynamic memory allocation for strings newNode->next = head; head = newNode; } enqueue(inputString); // Access the linked list struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code uses dynamic memory allocation (malloc and strdup) to store strings in the linked list.

  3. "C linked list with string copy function"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char data[100]; struct Node* next; }; // Function to copy string to a new node struct Node* copyString(char str[]) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); strcpy(newNode->data, str); newNode->next = NULL; return newNode; } int main() { struct Node* head = NULL; char inputString[] = "String Copy Function"; // Function to add string to linked list void enqueue(char str[]) { struct Node* newNode = copyString(str); newNode->next = head; head = newNode; } enqueue(inputString); // Access the linked list struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code introduces a separate function (copyString) for copying strings into new nodes.

  4. "C linked list with string length check"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char data[100]; struct Node* next; }; // Function to check if the string length is within the limit int isStringLengthValid(char str[]) { return strlen(str) < sizeof(struct Node) - sizeof(struct Node*); } // Function to add string to linked list with length check void enqueue(char str[], struct Node** head) { if (isStringLengthValid(str)) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); strcpy(newNode->data, str); newNode->next = *head; *head = newNode; } else { printf("String length exceeds the limit.\n"); } } int main() { struct Node* head = NULL; char inputString[] = "Length Check"; enqueue(inputString, &head); // Access the linked list struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code adds a length check function (isStringLengthValid) to ensure that the string length does not exceed the node's data size.

  5. "C linked list with string validation and error handling"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char data[100]; struct Node* next; }; // Function to validate string and add to linked list with error handling void enqueue(char str[], struct Node** head) { if (strlen(str) < sizeof(struct Node) - sizeof(struct Node*)) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); strcpy(newNode->data, str); newNode->next = *head; *head = newNode; } else { printf("Error: String length exceeds the limit.\n"); } } int main() { struct Node* head = NULL; char inputString[] = "String Validation"; enqueue(inputString, &head); // Access the linked list struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code incorporates string validation and error handling directly within the enqueue function.

  6. "C linked list with string duplication"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char* data; struct Node* next; }; // Function to duplicate string and add to linked list void enqueue(char str[], struct Node** head) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = strdup(str); newNode->next = *head; *head = newNode; } int main() { struct Node* head = NULL; char inputString[] = "String Duplication"; enqueue(inputString, &head); // Access the linked list struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code uses the strdup function to duplicate strings for each node in the linked list.

  7. "C linked list with string deletion"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char* data; struct Node* next; }; // Function to delete string and free memory void dequeue(struct Node** head) { if (*head != NULL) { struct Node* temp = *head; *head = (*head)->next; free(temp->data); free(temp); } } int main() { struct Node* head = NULL; char inputString1[] = "First String"; char inputString2[] = "Second String"; enqueue(inputString1, &head); enqueue(inputString2, &head); // Access the linked list before deletion struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } // Delete the first node dequeue(&head); // Access the linked list after deletion current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code introduces a dequeue function to delete the first node in the linked list and free memory.

  8. "C linked list with string search"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char data[100]; struct Node* next; }; // Function to search for a string in linked list struct Node* search(char str[], struct Node* head) { struct Node* current = head; while (current != NULL) { if (strcmp(current->data, str) == 0) { return current; } current = current->next; } return NULL; } int main() { struct Node* head = NULL; char inputString1[] = "Search String"; char inputString2[] = "Target String"; enqueue(inputString1, &head); enqueue(inputString2, &head); // Search for a string in the linked list char searchString[] = "Target String"; struct Node* foundNode = search(searchString, head); if (foundNode != NULL) { printf("String '%s' found in the linked list.\n", searchString); } else { printf("String '%s' not found in the linked list.\n", searchString); } return 0; } 

    Description: This code includes a search function to find a specific string in the linked list.

  9. "C linked list with string update"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char data[100]; struct Node* next; }; // Function to update a string in linked list void update(char oldStr[], char newStr[], struct Node* head) { struct Node* current = head; while (current != NULL) { if (strcmp(current->data, oldStr) == 0) { strcpy(current->data, newStr); break; } current = current->next; } } int main() { struct Node* head = NULL; char inputString1[] = "Old String"; char inputString2[] = "New String"; enqueue(inputString1, &head); // Update a string in the linked list char oldString[] = "Old String"; char newString[] = "Updated String"; update(oldString, newString, head); // Access the linked list after the update struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code introduces an update function to modify a specific string in the linked list.

  10. "C linked list with string reverse"

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char data[100]; struct Node* next; }; // Function to reverse the linked list struct Node* reverse(struct Node* head) { struct Node* prev = NULL; struct Node* current = head; struct Node* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } return prev; } int main() { struct Node* head = NULL; char inputString1[] = "First String"; char inputString2[] = "Second String"; enqueue(inputString1, &head); enqueue(inputString2, &head); // Access the linked list before reversal struct Node* current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } // Reverse the linked list head = reverse(head); // Access the linked list after reversal current = head; while (current != NULL) { printf("%s\n", current->data); current = current->next; } return 0; } 

    Description: This code includes a reverse function to reverse the order of strings in the linked list.


More Tags

plot-annotations permutation mapper tomcat-jdbc dot google-api-nodejs-client formset stackdriver react-router prepared-statement

More Programming Questions

More Other animals Calculators

More Weather Calculators

More Dog Calculators

More Everyday Utility Calculators