
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node {
    char* value;
    struct Node* right_ptr;
    struct Node* left_ptr;
};

// Create a new node
struct Node* createNode(const char* str) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (!newNode) {
        printf("Memory allocation failed\n");
        exit(1);
    }

    newNode->value = (char*)malloc(strlen(str) + 1);
    if (!newNode->value) {
        printf("Memory allocation failed\n");
        exit(1);
    }

    strcpy(newNode->value, str);

    newNode->left_ptr = NULL;
    newNode->right_ptr = NULL;

    return newNode;
}

// Append node to the end of the list
void append(struct Node** head, const char* str) {
    struct Node* newNode = createNode(str);

    if (*head == NULL) {
        *head = newNode;
        return;
    }

    struct Node* temp = *head;
    while (temp->right_ptr != NULL) {
        temp = temp->right_ptr;
    }

    temp->right_ptr = newNode;
    newNode->left_ptr = temp;
}

// Print list forward
void printForward(struct Node* head) {
    struct Node* temp = head;
    printf("Forward: ");
    while (temp != NULL) {
        printf("%s ", temp->value);
        temp = temp->right_ptr;
    }
    printf("\n");
}

// Print list backward
void printBackward(struct Node* head) {
    if (!head) return;

    struct Node* temp = head;
    while (temp->right_ptr != NULL) {
        temp = temp->right_ptr;
    }

    printf("Backward: ");
    while (temp != NULL) {
        printf("%s ", temp->value);
        temp = temp->left_ptr;
    }
    printf("\n");
}

// Free memory
void freeList(struct Node* head) {
    struct Node* temp;
    while (head != NULL) {
        temp = head;
        head = head->right_ptr;
        free(temp->value);
        free(temp);
    }
}

int main() {
    struct Node* head = NULL;

    // 7 char* values
    const char* data[7] = {
        "Node1", "Node2", "Node3", "Node4",
        "Node5", "Node6", "Node7"
    };

    for (int i = 0; i < 7; i++) {
        append(&head, data[i]);
    }

    printForward(head);
    printBackward(head);

    freeList(head);

    return 0;
}