Add CT331 Programming Paradigms
This commit is contained in:
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int arg, char* argc[]){
|
||||
printf("Hello assignment1.\n");
|
||||
|
||||
int my_integer;
|
||||
int* my_integer_pointer;
|
||||
long my_long;
|
||||
double *my_double_pointer;
|
||||
char **my_char_pointer_pointer;
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include "linkedList.h"
|
||||
#include "tests.h"
|
||||
|
||||
int main(int arg, char* argc[]){
|
||||
runTests();
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
#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\n", 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);
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
#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);
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include "tests.h"
|
||||
#include "linkedList.h"
|
||||
|
||||
void runTests(){
|
||||
printf("Tests running...\n");
|
||||
listElement* l = createEl("Test String (1).", 30);
|
||||
//printf("%s\n%p\n", l->data, l->next);
|
||||
//Test create and traverse
|
||||
traverse(l);
|
||||
printf("\n");
|
||||
|
||||
//Test insert after
|
||||
listElement* l2 = insertAfter(l, "another string (2)", 30);
|
||||
insertAfter(l2, "a final string (3)", 30);
|
||||
traverse(l);
|
||||
printf("\n");
|
||||
|
||||
// Test delete after
|
||||
deleteAfter(l);
|
||||
traverse(l);
|
||||
printf("\n");
|
||||
|
||||
printf("\nTests complete.\n");
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
#ifndef CT331_ASSIGNMENT_TESTS
|
||||
#define CT331_ASSIGNMENT_TESTS
|
||||
|
||||
void runTests();
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user