I realize my code is rather long so I'd like opinions mainly on a couple of things if your time is short:
1) Returning INT_MIN to indicate that it was empty.
2) The removeHead and removeTail functions.
Feel free to add more criticism.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct node{
int value;
struct node *next;
}Node;
typedef struct
{
Node* head;
Node* tail;
}LinkedList;
void printListRec(Node* node);
void printHeadAndTail(LinkedList* list);
int printList(LinkedList* list);
int linkedListSize( LinkedList* list );
int searchForElement( LinkedList* list , int value );
int isEmpty( LinkedList* list );
int removeElement( LinkedList* list , int value);
int removeTail( LinkedList* list );
int removeHead( LinkedList* list );
void addTail( LinkedList* list , int value);
void addHead( LinkedList* list , int value);
LinkedList* initialize() ;
//implement a clear list function
int main()
{
LinkedList* linkedlist = initialize();
addHead(linkedlist , 1);
printHeadAndTail(linkedlist);
printList(linkedlist);
addHead(linkedlist , 2);
printHeadAndTail(linkedlist);
printList(linkedlist);
removeTail(linkedlist);
printHeadAndTail(linkedlist);
printList(linkedlist);
removeTail(linkedlist);
printHeadAndTail(linkedlist);
printList(linkedlist);
addTail(linkedlist , 3);
printHeadAndTail(linkedlist);
printList(linkedlist);
return 0;
}
LinkedList* initialize() //creates linked list with no nodes, initializes head & tail to 0
{
LinkedList* list = malloc(sizeof(LinkedList));
list->head = NULL;
list->tail = NULL;
return list;
}
void addHead( LinkedList* list , int value)
{
Node* newNode = malloc( sizeof(Node) );
newNode->value = value;
if( isEmpty(list) )
{
newNode->next = NULL;
list->head = list->tail = newNode;
}
else
{
newNode->next = list->head;
list->head = newNode;
}
puts("Add head");
}
void addTail( LinkedList* list , int value)
{
Node* newNode = malloc( sizeof(Node) );
newNode->value = value;
newNode->next = NULL;
if( isEmpty(list) )
{
newNode->next = NULL;
list->head = list->tail = newNode;
}
else
{
list->tail->next = newNode;
list->tail = newNode;
}
puts("Add tail");
}
int removeHead( LinkedList* list )
{
if(isEmpty(list))
return INT_MIN;
Node* nodeToDelete = list->head;
int value = nodeToDelete->value;
if(list->head == list->tail) //if node is the only node present
list->head = list->tail = NULL;
else
list->head = list->head->next;
free(nodeToDelete);
puts("Remove head");
return value;
}
int removeTail( LinkedList* list )
{
if( isEmpty(list) )
return INT_MIN;
int value ;
if(list->head == list->tail)
{
value = list->tail->value;
free(list->tail);
list->head = list->tail = NULL;
return value;
}
Node* nodeToDelete = list->tail;
value = nodeToDelete->value;
Node* nodeBeforeTail = list->head;
while( nodeBeforeTail->next != nodeToDelete )
nodeBeforeTail = nodeBeforeTail->next;
nodeBeforeTail->next = NULL;
list->tail = nodeBeforeTail;
free(nodeToDelete);
puts("Remove tail");
return value;
}
int removeElement( LinkedList* list , int value)
{
if( list->head->value == value )
{
removeHead(list);
return 0; //element found
}
if(list->tail->value == value)
{
removeTail(list);
return 0;
}
Node* node = list->head;
while( (node->next) != list->tail )
{
if( node->next->value == value)
{
Node* nodeToDelete = node->next;
node->next = node->next->next;
free(nodeToDelete);
return 0;
}
node = node->next;
}
return 1;
}
int isEmpty( LinkedList* list )
{
return !list->head && !list->tail;
}
int searchForElement( LinkedList* list , int value )
{
if(isEmpty(list))
return -1;
Node* node = list->head;
do
{
if(node->value == value) return 0;
}while( (node = node->next) );
return 1;
}
int linkedListSize( LinkedList* list )
{
int counter = 0;
Node* node = list->head;
while( node )
{
counter++;
node = node->next;
}
return counter;
}
void printHeadAndTail(LinkedList* list)
{
if(isEmpty(list))
{
printf("\nHead pointer : %d , tail pointer : %d\n" , list->head , list->tail);
return;
}
printf("Head pointer: %d , Head's pointer to next : %d\n" , list->head, list->head->next );
printf("Tail pointer: %d , tail's pointer to next : %d\n" , list->tail, list->tail->next );
}
void printListRec(Node* node)
{
if( !node )
return;
printf("%d " , node->value);
printListRec(node->next);
}
int printList(LinkedList* list)
{
if( isEmpty(list) )
{
puts("List is empty");
return 1;
}
puts("\nPrint Linked List: ");
Node* node = list->head;
while( node )
{
printf("%d " , node->value);
node = node->next;
}
puts("\n*******************\n");
return 0;
}
printListimplementation in the question. DId you forget it ? \$\endgroup\$printListdoes so using a different naming / naming convention. Can you fix that so the code can be compiled? \$\endgroup\$