Based on the example linked list code, extend that code to implement the following functions. You will
Question:
Based on the example linked list code, extend that code to implement the following functions.
You will have to update the linkedList.h file and the linkedList.c file.
[2 marks each]
● int length(listElement* list) ○ Returns the number of elements in a linked list.
● void push(listElement list, char* data, size_t size) ○ Push a new element onto the head of a list. ○ Update the list reference using side effects.
● listElement* pop(listElement list) ○ Pop an element from the head of a list. ○ Update the list reference using side effects. NOTE: We have a linked list stack!
● void enqueue(listElement** list, char* data, size_t size); ○ Enqueue a new element onto the head of the list. ○ Update the list reference using side effects.
● listElement* dequeue(listElement* list); ○ Dequeue an element from the tail of the list. NOTE: We have a linked list queue!
In the main file test to see if your methods work by inserting values into the methods and printing their results
Tests.c :
#include
#include "tests.h"
#include "linkedList.h"
void runTests(){
printf("Tests running. hello..");
listElement* l = createEl("Test String (1).", 30);
//printf("%s%p", l->data, l->next);
//Test create and traverse
traverse(l);
printf("");
//Test insert after
listElement* l2 = insertAfter(l, "another string (2)", 30);
insertAfter(l2, "a final string (3)", 30);
traverse(l);
printf("");
// Test delete after
printf("DELETE");
deleteAfter(l);
traverse(l);
//testing the length
//testing push and pop methods.
//testing enque and deque methods
printf("Tests complete.");
}
linkedlist.c :
#include
#include
#include
#include "linkedList.h"
typedef struct listElementStruct{
char* data;
size_t size;
struct listElementStruct* next;
} listElement;
//Creates a new linked list element with given content of size
//Returns a pointer to the element
listElement* createEl(char* data, size_t size){
listElement* e = malloc(sizeof(listElement));
if(e == NULL){
//malloc has had an error
return NULL; //return NULL to indicate an error.
}
char* dataPointer = malloc(sizeof(char)*size);
if(dataPointer == NULL){
//malloc has had an error
free(e); //release the previously allocated memory
return NULL; //return NULL to indicate an error.
}
strcpy(dataPointer, data);
e->data = dataPointer;
e->size = size;
e->next = NULL;
return e;
}
//Prints out each element in the list
void traverse(listElement* start){
listElement* current = start;
while(current != NULL){
printf("%s", current->data);
current = current->next;
}
}
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* el, char* data, size_t size){
listElement* newEl = createEl(data, size);
listElement* next = el->next;
newEl->next = next;
el->next = newEl;
return newEl;
}
//Delete the element after the given el
void deleteAfter(listElement* after){
listElement* delete = after->next;
listElement* newNext = delete->next;
after->next = newNext;
//need to free the memory because we used malloc
free(delete->data);
free(delete);