C Programming BASE CODE: linkedlist.h #ifndef CT331_ASSIGNMENT_LINKED_LIST #defi
ID: 3756193 • Letter: C
Question
C Programming
BASE CODE:
linkedlist.h
#ifndef CT331_ASSIGNMENT_LINKED_LIST
#define CT331_ASSIGNMENT_LINKED_LIST
typedef struct listElementStruct 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);
//Prints out each element in the list
void traverse(listElement* start);
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* after, char* data, size_t size);
//Delete the element after the given el
void deleteAfter(listElement* after);
// Returns the number of elements in a linked list.
int length(listElement* list);
// Push a new element onto the head of a list.
void push(listElement** list, char* data, size_t size);
// Pop an element from the head of a list.
listElement* pop(listElement** list);
// Enqueue a new element onto the head of the list.
void enqueue(listElement** list, char* data, size_t size);
// Dequeue an element from the tail of the list.
listElement* dequeue(listElement* list);
#endif
linkedlist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}
Implement the following functions into the linkedlist.c file
int length(listElement list) o Returns the number of elements in a linked list. void push(listElement list, char* data, size_t size) o Push a new element onto (See: swap() from lecture 3) o listElement* pop(listElement" list) o Pop an element from the head of a list. o Update the list reference using side effects. NOTE: We have a linked list stack! void enqueue(listElement" list, char data, size_t size) element onto the head of the list. Enqueue a new o Update the list reference using side effects. listElement" dequeue(listElement list); o Dequeue an element from the tail of the list. NOTE: We have a linked list queue!Explanation / Answer
int length(listElement* list) { if(list == NULL) { return 0; } return 1 + length(list->next); } void push(listElement** list, char *data, size_t size) { listElement* node = createEl(data, size); node->next = *list; *list = node; } listElement* pop(listElement** list) { if(*list == NULL) { return NULL; } listElement* node = *list; *list = *list->next; return node; } void enqueue(listElement** list, char *data, size_t size) { push(list, data, size); } listElement* dequeue(listElement** list) { return pop(list); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.