#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _employee
{
char name[32];
unsigned char age;
}Employee;
typedef struct _node
{
void *data;
struct _node *next;
}Node;
typedef struct _linkedlist
{
Node *head;
Node *tail;
Node *current;
}LinkedList;
int compareEmployee(Employee *e1,Employee *e2)
{
return strcmp(e1->name,e2->name);
}
void displayEmployee(Employee *emp)
{
printf("%s\t%d\n",emp->name,emp->age);
}
typedef int(*COMPARE)(void*,void*);
typedef void(*DISPLAY)(void*);
void initializeList(LinkedList* list);
void addHead(LinkedList* list,void* data);
void addTail(LinkedList* list,void* data);
void delete(LinkedList* list,Node* node);
Node* getNode(LinkedList* list,COMPARE,void* data);
void displayLinkedList(LinkedList* list,DISPLAY);
void initializeList(LinkedList* list)
{
list->head = NULL;
list->tail = NULL;
list->current = NULL;
}
void addHead(LinkedList* list,void* data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
if(list->head == NULL)
{
list->tail = node;
node->next = NULL;
}
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->tail == NULL)
{
list->head = node;
}
else
{
list->tail->next = node;
}
list->tail = node;
}
void displayLinkedList(LinkedList *list,DISPLAY display)
{
printf("Linked List\n");
Node *current = list->head;
while(current != NULL)
{
display(current->data);
current = current->next;
}
}
Node* getNode(LinkedList* list,COMPARE compare,void* data)
{
Node *node;
node = list->head;
while(node != NULL)
{
if(compare(node->data,data) == 0)
{
return node;
}
node = node->next;
}
return NULL;
}
void delete(LinkedList *list,Node *node)
{
if(node == list->head)
{
if(list->head->next == NULL)
{
list->head = list->tail =NULL;
}
else
{
list->head = list->head->next;
}
}
else
{
Node *tmp = list->head;
while(tmp != NULL && tmp->next != node)
{
tmp = tmp->next;
}
if(tmp != NULL)
{
tmp->next = node->next;
}
}
free(node);
}
void main()
{
DISPLAY displa = (void*)displayEmployee;
LinkedList *linkedList = (LinkedList*)malloc(sizeof(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);
addTail(linkedList,samuel);
addTail(linkedList,sally);
addTail(linkedList,susan);
Node *node = getNode(linkedList,(COMPARE)compareEmployee,sally);
delete(linkedList,node);
displayLinkedList(linkedList,displa);
}