bug fixes

This commit is contained in:
2025-07-09 18:02:45 -06:00
parent 503ab912f8
commit af2e194ad4
3 changed files with 88 additions and 59 deletions
+22 -4
View File
@@ -2,11 +2,29 @@ CC = gcc
CFLAGS = -g CFLAGS = -g
CPPFLAGS = -Wall -Wextra -pedantic -std=gnu90 CPPFLAGS = -Wall -Wextra -pedantic -std=gnu90
chatty: main.o chatty: main.o liblist.a
$(CC) $(CFLAGS) -o chatty main.o -lpthread $(CC) $(CFLAGS) -L. -o chatty main.o -llist -lpthread
main.o: main.c main.o: main.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o main.o main.c $(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o main.o main.c
liblist.a: list_adders.o list_removers.o list_movers.o
ar -rcs liblist.a list_adders.o list_removers.o list_movers.o
list_adders.o: list_adders.c
$(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o list_adders.o list_adders.c
list_movers.o: list_movers.c
$(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o list_movers.o list_movers.c
list_removers.o: list_removers.c
$(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o list_removers.o list_removers.c
listtest: listtest.o liblist.a
$(CC) $(CFLAGS) -L. -o listtest listtest.o -llist
listtest.o: listtest.c
$(CC) $(CFLAGS) $(CPPFLAGS) -I. -c -o listtest.o listtest.c
clean: clean:
rm -f *.o chatty rm -f *.o chatty liblist.a
+62 -50
View File
@@ -3,19 +3,19 @@
#include <stdio.h> #include <stdio.h>
LIST *ListCreate(void) { LIST *ListCreate(void) {
LIST *new_list; LIST *new_node_list;
new_list = malloc(sizeof(LIST)); new_node_list = malloc(sizeof(LIST));
if (!new_list) { if (!new_node_list) {
return NULL; return NULL;
} }
new_list->size = 0; new_node_list->size = 0;
new_list->current = 0; new_node_list->current = 0;
new_list->head = 0; new_node_list->head = 0;
new_list->tail = 0; new_node_list->tail = 0;
return new_list; return new_node_list;
} }
int ListAdd(LIST *list, void *item) { int ListAdd(LIST *list, void *item) {
@@ -24,28 +24,28 @@ int ListAdd(LIST *list, void *item) {
* 2. Insert when cursor is at the tail (& list not empty) * 2. Insert when cursor is at the tail (& list not empty)
* 3. Insert when cursor is anywhere but the tail (& list not empty) * 3. Insert when cursor is anywhere but the tail (& list not empty)
*/ */
NODE *new = malloc(sizeof(NODE)); NODE *new_node = malloc(sizeof(NODE));
if (new == NULL || (list->current == NULL && list->size != 0)) { if (new_node == NULL || (list->current == NULL && list->size != 0)) {
return -1; return -1;
} }
new->data = item; new_node->data = item;
if (list->size == 0) { if (list->size == 0) {
list->current = new; list->current = new_node;
list->head = new; list->head = new_node;
list->tail = new; list->tail = new_node;
} }
else if (list->current == list->tail) { else if (list->current == list->tail) {
new->previous = list->tail; new_node->previous = list->tail;
list->tail->next = new; list->tail->next = new_node;
list->tail = new; list->tail = new_node;
list->current = new; list->current = new_node;
} }
else if (list->current != list->tail && list->size > 0) { else if (list->current != list->tail && list->size > 0) {
new->next = list->current->next; new_node->next = list->current->next;
new->previous = list->current; new_node->previous = list->current;
list->current->next->previous = new; list->current->next->previous = new_node;
list->current->next = new; list->current->next = new_node;
list->current = new; list->current = new_node;
} }
else { else {
return -1; return -1;
@@ -61,29 +61,29 @@ int ListInsert(LIST *list, void *item) {
* 2. Insert when cursor is at the head (& list not empty) * 2. Insert when cursor is at the head (& list not empty)
* 3. Insert when cursor is anywhere but the head (& list not empty) * 3. Insert when cursor is anywhere but the head (& list not empty)
*/ */
NODE *new = malloc(sizeof(NODE)); NODE *new_node = malloc(sizeof(NODE));
if (new == NULL || (list->current == NULL && list->size != 0)) { if (new_node == NULL || (list->current == NULL && list->size != 0)) {
return -1; return -1;
} }
new->data = item; new_node->data = item;
if (list->size == 0) { if (list->size == 0) {
list->current = new; list->current = new_node;
list->head = new; list->head = new_node;
list->tail = new; list->tail = new_node;
} }
else if (list->current == list->head) { else if (list->current == list->head) {
new->next = list->head; new_node->next = list->head;
list->head->previous = new; list->head->previous = new_node;
list->head = new; list->head = new_node;
list->current = new; list->current = new_node;
} }
else if (list->current != list->head && list->size > 0) { else if (list->current != list->head && list->size > 0) {
new->next = list->current; new_node->next = list->current;
new->previous = list->current->previous; new_node->previous = list->current->previous;
list->current->previous->next = new; list->current->previous->next = new_node;
list->current->previous = new; list->current->previous = new_node;
list->current = new; list->current = new_node;
} }
else { else {
return -1; return -1;
@@ -94,28 +94,40 @@ int ListInsert(LIST *list, void *item) {
} }
int ListPrepend(LIST *list, void *item) { int ListPrepend(LIST *list, void *item) {
NODE *new = malloc(sizeof(NODE)); NODE *new_node = malloc(sizeof(NODE));
if (new == NULL) { if (new_node == NULL) {
return -1; return -1;
} }
new->data = item; new_node->data = item;
new->next = list->head; if (list->size == 0) {
list->head->previous = new; list->current = new_node;
list->head = new; list->head = new_node;
list->tail = new_node;
}
new_node->next = list->head;
list->head->previous = new_node;
list->head = new_node;
list->size++; list->size++;
return 0; return 0;
} }
int ListAppend(LIST *list, void *item) { int ListAppend(LIST *list, void *item) {
NODE *new = malloc(sizeof(NODE)); NODE *add = malloc(sizeof(NODE));
if (new == NULL) { if (add == NULL) {
return -1; return -1;
} }
new->data = item; add->data = item;
new->previous = list->tail; if (list->size == 0) {
list->tail->next = new; list->current = add;
list->tail = new; list->head = add;
list->tail = add;
}
else {
add->previous = list->tail;
list->tail->next = add;
list->tail = add;
}
list->size++; list->size++;
return 0; return 0;
+3 -4
View File
@@ -25,13 +25,12 @@ LIST *receivelist, *sendlist;
pthread_mutex_t sendmut, receivemut; pthread_mutex_t sendmut, receivemut;
void *input(void *arg) { void *input(void *arg) {
char msg[512]; char msg[30];
while (1) { while (1) {
printf("Type a message: "); printf("Type a message: ");
scanf("%s", msg); fgets(msg, sizeof(msg), stdin);
pthread_mutex_lock(&sendmut); pthread_mutex_lock(&sendmut);
ListAppend(sendlist, msg); ListAppend(sendlist, &msg);
pthread_mutex_unlock(&sendmut); pthread_mutex_unlock(&sendmut);
} }
return 0; return 0;