Is there a way to improve this code? Any suggestions are highly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DEBUG
#undef DEBUG
typedef struct Node
{
int data;
struct Node *next;
} Node;
typedef struct LinkedList {
Node *head, *tail;
int size;
} LinkedList;
LinkedList *list;
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void init()
{
list->head = NULL;
list->tail = list->head;
list->size = 0;
}
void add(Node* node)
{
if(list->head!=NULL)
{
list->tail->next = (struct Node*)node;
list->tail = node;
}
else
{
list->head = node;
list->tail = list->head;
}
++list->size;
}
Node* createNode(int data)
{
Node *tmp = (Node*)malloc(sizeof(Node));
tmp->data = data;
tmp->next = NULL;
return tmp;
}
void show()
{
Node *tmp = list->head;
while(tmp != NULL)
{
printf("%d\t",tmp->data);
tmp = (Node*)tmp->next;
}
printf("\n");
}
void pushFront(Node* node)
{
node->next = (struct Node*)list->head;
if(list->head!=NULL)
{
list->head = node;
}
else
{
list->head = node;
list->tail = list->head;
}
++list->size;
}
int getDataAt(int index)
{
Node *tmp = list->head;
int count = 0;
if(index >= 0 && index < list->size)
{
while(tmp!=NULL)
{
if(count == index) return tmp->data;
++count;
tmp = (Node*)tmp->next;
}
}
return 0;
}
Node *getNode(int index)
{
Node *tmp = list->head;
int count = 0;
if(index >= 0 && index < list->size)
{
while(tmp!=NULL)
{
if(count == index) return tmp;
++count;
tmp = (Node*)tmp->next;
}
}
return NULL;
}
void deleteLast()
{
Node *tmp = list->head;
int i;
Node *beforeLast;
if(list->size>0)
{
for (i = 0; i < list->size; ++i)
{
if(i == list->size-2)
{
beforeLast = tmp;
}
if(tmp!=NULL)
{
tmp =(Node*) tmp->next;
}
}
free(beforeLast->next);
beforeLast->next = NULL;
list->tail = beforeLast;
}
else
{
free(list->head);
init();
}
if(list->size>0)
{
--list->size;
}
#ifdef DEBUG
printf("Actual size -> [%d]\n",size);
#endif // DEBUG
}
bool found(int toFind)
{
Node *tmp = list->head;
while(tmp!=NULL)
{
if(toFind==tmp->data) return true;
tmp = (Node*)tmp->next;
}
return false;
}
void reverse()
{
int len, i;
for (i = 0, len = list->size-1; i < len; ++i, --len)
{
swap((int*)getNode(i), (int*)getNode(len));
}
}
void clear()
{
while(list->head!=NULL)
{
deleteLast();
}
}
void insertAt(int index, int data)
{
Node *tmp = list->head;
Node *newNode = createNode(data);
Node *left, *right;
int count = 0;
if(index == 0) pushFront(createNode(data));
else if (index == list->size-1) add(createNode(data));
else if(index > 0 && index < list->size)
{
{
while(tmp!=NULL)
{
left = tmp;
right = tmp->next;
if(count == index - 1)
{
left->next = newNode;
newNode->next = right;
++list->size;
break;
}
++count;
tmp = (Node*)tmp->next;
}
}
}
}
void deleteAt(int index)
{
Node *left, *right;
Node *tmp = list->head;
int count = 0;
if(index > 0 && index < list->size-1)
{
while(tmp!=NULL)
{
left = tmp;
right = tmp->next;
if(count == index-1)
{
#ifdef DEBUG
printf("left -> %d right -> %d\n", left->data, right->data);
#endif
left->next = right->next;
break;
}
++count;
tmp = tmp->next;
}
--list->size;
}
else if (index == list->size-1)
{
deleteLast();
}
else
{
list->head = list->head->next;
free(tmp);
tmp = NULL;
}
}
int main(int argc, char const *argv[])
{
list = (LinkedList*)malloc(sizeof(LinkedList));
init();
add(createNode(1));
add(createNode(2));
add(createNode(3));
add(createNode(4));
pushFront(createNode(5));
deleteAt(1);
reverse();
show();
clear();
free(list);
list = NULL;
return 0;
}