Use linked list to create queue stack BST in C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _employee {
char name[32];
unsigned char age;
} Employee;
typedef int (* COMPARE) (void *, void *);
typedef void(* DISPLAY) (void *);
typedef struct _node {
void *data;
struct _node *next;
} Node;
typedef struct _linkedList {
Node *head;
Node *current;
Node *tail;
} LinkedList;
//Queue
typedef LinkedList Queue;
//Stack
typedef LinkedList Stack;
//binary search tree
typedef struct _tree {
void *data;
struct _tree *left;
struct _tree *right;
} TreeNode;
void initializeList(LinkedList *);
int compareEmployee(Employee *, Employee *);
void displayEmployee(Employee *);
void addHead(LinkedList *, void *);
void addTail(LinkedList *, void *);
Node *getNode(LinkedList *list, COMPARE compare, void *data);
void delete(LinkedList *list, Node *node);
void displayLinkedList(LinkedList *, DISPLAY displayEmployee);
//Queue
void initializeQueue(Queue *);
void enqueue(Queue*, void *);
void* dequeue(Queue*);
//Stack
void initializeStack(Stack*);
void push(Queue* queue, void *data);
void *pop(Queue* queue);
//BST
void insertNode(TreeNode **root, COMPARE, void *data);
void inOrder(TreeNode*, DISPLAY);
void postOrder(TreeNode*, DISPLAY);
void preOrder(TreeNode*, DISPLAY);
int main(int argc, char **argv) {
LinkedList linkedList;
Employee *samuel = (Employee*) malloc(sizeof(Employee));
strcpy(samuel->name, "Samuel");
samuel->age = 32;
Employee *sally = (Employee*) malloc(sizeof(Employee));
strcpy(sally->name, "Sally");
sally->age = 28;
Employee *susan = (Employee*) malloc(sizeof(Employee));
strcpy(susan->name, "Susan");
susan->age = 45;
initializeList(&linkedList);
addHead(&linkedList, samuel);
addHead(&linkedList, sally);
addHead(&linkedList, susan);
displayLinkedList(&linkedList, (DISPLAY)displayEmployee);
delete(&linkedList, getNode(&linkedList, (COMPARE)compareEmployee, samuel));
printf("%p\t%p\n", linkedList.tail, linkedList.head->next);
displayLinkedList(&linkedList, (DISPLAY)displayEmployee);
printf("\n****** test for Queue *******\n");
Queue queue;
initializeQueue(&queue);
enqueue(&queue, samuel);
enqueue(&queue, sally);
enqueue(&queue, susan);
void *data = dequeue(&queue);
printf("Dequeued %s\n", ((Employee*) data)->name);
data = dequeue(&queue);
printf("Dequeued %s\n", ((Employee*) data)->name);
data = dequeue(&queue);
printf("Dequeued %s\n", ((Employee*) data)->name);
printf("\n****** test for Stack ******\n");
Stack stack;
initializeStack(&stack);
push(&stack, samuel);
push(&stack, sally);
push(&stack, susan);
Employee *employee;
for (int i = 0; i < 4; i++) {
employee = (Employee*) pop(&stack);
printf("Popped %s\n", employee->name);
}
printf("\n****** test for Binary Search Tree*******\n");
TreeNode *tree = NULL;
insertNode(&tree, (COMPARE) compareEmployee, samuel);
insertNode(&tree, (COMPARE) compareEmployee, sally);
insertNode(&tree, (COMPARE) compareEmployee, susan);
preOrder(tree, (DISPLAY) displayEmployee);
inOrder(tree, (DISPLAY) displayEmployee);
postOrder(tree, (DISPLAY) displayEmployee);
return 0;
}
int compareEmployee(Employee *e1, Employee *e2) {
return strcmp(e1->name, e2->name);
}
void displayEmployee(Employee *e) {
printf("%s\t%d\n", e->name, e->age);
}
void initializeList(LinkedList *list) {
list->head = NULL;
list->current = NULL;
list->tail = NULL;
}
void addHead(LinkedList *list, void *data) {
Node *node = (Node*)malloc(sizeof(Node));
node->data = data;
if (list->head == NULL) {
node->next = NULL;
list->tail = node;
} else {
node->next = list->head;
}
list->head = node;
}
void addTail(LinkedList *list, void *data) {
Node *node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
list->tail->next = node;
}
list->tail = node;
}
Node* getNode(LinkedList *list, COMPARE compare, void *data) {
Node *node = list->head;
while (node) {
if (compare(node->data, data) == 0) {
return node;
}
node = node->next;
}
return NULL;
}
void delete(LinkedList *list, Node *node) {
if (node == NULL)
return;
if (node == list->head) {
if (list->head->next == NULL) {
list->head = list->tail = NULL;
} else {
list->head = list->head->next;
}
} else {
Node *temp = list->head;
while (temp->next != node && temp != NULL) {
temp = temp->next;
}
if (temp != NULL) {
temp->next = temp->next->next;
if (temp->next == NULL) {
list->tail = temp;
}
}
}
free(node);
}
void displayLinkedList(LinkedList *list, DISPLAY displayEmployee) {
printf("\n******LinkedList******\n");
Node *current = list->head;
while (current != NULL) {
displayEmployee(current->data);
current = current->next;
}
}
// to implement a Queue
void initializeQueue(Queue *queue) {
initializeList(queue);
}
void enqueue(Queue *queue, void *data) {
addHead(queue, data);
}
void *dequeue(Queue *queue) {
void *data;
Node *temp = queue->head;
if (queue->head == NULL) {
return NULL;
} else if (queue->head == queue->tail) {
queue->head = queue->tail = NULL;
data = temp->data;
free(temp);
} else {
while (temp -> next != queue->tail) {
temp = temp->next;
}
data = temp->next->data;
free(temp->next);
temp->next = NULL;
queue->tail = temp;
}
return data;
}
void initializeStack(Stack *stack) {
initializeList(stack);
}
void push(Stack *stack, void *data) {
addHead(stack, data);
}
void *pop(Stack *stack) {
void *data;
Node *temp = stack->head;
if (stack->head == NULL) {
return NULL;
} else if (stack->head == stack->tail) {
stack->head = stack->tail = NULL;
data = temp->data;
free(temp);
} else {
stack->head = temp->next;
data = temp->data;
free(temp);
}
return data;
}
void insertNode(TreeNode **root, COMPARE compare, void *data) {
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
node->data = data;
node->left = node->right = NULL;
if (*root == NULL) {
*root = node;
return;
}
while (1) {
if (compare((*root)->data, data) > 0) {
if ((*root)->left == NULL) {
(*root)->left = node;
break;
} else {
*root = (*root)->left;
}
} else {
if ((*root)->right == NULL) {
(*root)->right = node;
break;
} else {
*root = (*root)->right;
}
}
}
}
void inOrder(TreeNode *root, DISPLAY display) {
if (root != NULL) {
inOrder(root->left, display);
display(root->data);
inOrder(root->right, display);
}
}
void postOrder(TreeNode *root, DISPLAY display) {
if (root != NULL) {
postOrder(root->left, display);
postOrder(root->right, display);
display(root->data);
}
}
void preOrder(TreeNode *root, DISPLAY display) {
if (root != NULL) {
display(root->data);
preOrder(root->left, display);
preOrder(root->right, display);
}
}