updated list lib

This commit is contained in:
2025-07-08 20:05:01 -06:00
parent d1f38e397e
commit c002c78b9a
7 changed files with 579 additions and 97 deletions
+74 -86
View File
@@ -6,7 +6,6 @@
#ifndef __LIST_H__
#define __LIST_H__
typedef struct node {
void *data;
struct node *next;
@@ -22,57 +21,51 @@ typedef struct list {
} LIST;
/*
Create empty list
Returns: Pointer to new empty list if successful, NULL pointer otherwise
/*
* Create an empty list
* Return: Pointer to new empty list
*/
LIST *ListCreate();
LIST *ListCreate(void);
/* Returns the number of items contained by the list */
/*
* Get the number of items in the list
* Return: Integer containing number of items
*/
int ListCount(LIST *list);
/*
Returns pointer to first item in the list and makes it the current item
list: Pointer to a list
Returns: Pointer to first item in the list
*/
* Get the first item in the list and make it the current item
* list: Pointer to a list
* Return: Pointer to head's data
*/
void *ListFirst(LIST *list);
/*
Return pointer to last item in list
list: Pointer to a list
Returns: Pointer to last item in list
*/
* Get the last item in the list and make it the current item
* list: Pointer to a list
* Return: Pointer to tail's data
*/
void *ListLast(LIST *list);
/*
Return next item in the list
list: Pointer to a list
Returns: Pointer to next item in list
*/
/* Get the item in the list after the cursor and make the cursor point to it
* list: Pointer to a list
* Return: Pointer to next item's data
*/
void *ListNext(LIST *list);
/*
Return previous item in list
list: Pointer to a list
Returns: Pointer to last item
*/
* Get the item before the cursor and set the cursor to it.
* list: Pointer to a list
* Return: Pointer to previous item's data
*/
void *ListPrev(LIST *list);
/* Get current item in list
list: Pointer to list
Returns: Pointer to last item
* list: Pointer to list
* Return: Pointer to current item's data
*/
void *ListCurr(LIST *list);
/*
Add item to list after current item,
switch current item to the item we just added.
@@ -83,80 +76,75 @@ void *ListCurr(LIST *list);
*/
int ListAdd(LIST *list, void *item);
/*
Insert an item right before the current item.
If current is the start of the list, add to the start.
list: Pointer to list
item: Pointer to item to add
Returns: 0 if successful, -1 otherwise
* Insert an item right before the current item, switch cursor to new item.
* If current is the start of the list, add to the start.
* list: Pointer to list
* item: Pointer to item to add
* Return: 0 if successful, -1 otherwise
*/
int ListInsert(LIST *list, void *item);
/*
Add an item to the end of the list
list: Pointer to list
item: Pointer to item to add
Returns: 0 if successful, -1 otherwise
*/
* Add an item to the end of the list. Keep cursor where it is.
* list: Pointer to list
* item: Pointer to item to add
* Return: 0 if successful, -1 otherwise
*/
int ListAppend(LIST *list, void *item);
/*
Add item to the front of the list
list: Pointer to list
item: Pointer to item to add
Returns: 0 if successful, -1 otherwise
*/
* Add item to the front of the list
* list: Pointer to list
* item: Pointer to item to add
* Return: 0 if successful, -1 otherwise
*/
int ListPrepend(LIST *list, void *item);
/*
Delete current item from list and make next item current
list: Pointer to list
Returns: Deleted item
*/
void *ListRemove(LIST *list);
* Delete current item from list, free current item, and make next item current
* list: Pointer to list
* Return: 0 on success, -1 on failure
*/
int ListRemove(LIST *list);
/*
Adds list2 to the end of list1
Current pointer set to current pointer of list1
List2 ceases to exist
list1: Pointer to list1
list2: Pointer to list2
*/
* Adds list2 to the end of list1
* Current pointer set to current pointer of list 1. List2 ceases to exist
* list1: Pointer to list1
* list2: Pointer to list2
*/
void ListConcat(LIST *list1, LIST *list2);
/*
Deletes list
list: Pointer to list
itemFree: routine that frees an item
*/
void ListFree(LIST *list, void (*itemFree)(void *));
* Deletes list
* Ensure you set your pointer to NULL after calling so you don't get
* dangling pointers!
* list: Pointer to list
*/
void ListFree(LIST *list);
/*
Returns last item and takes it out of the list
list: Pointer to list
Returns: Pointer to last item that was just deleted from list
*/
* Returns last item and takes it out of the list
* list: Pointer to list
* Returns Pointer to last item that was just deleted from list
*/
void *ListTrim(LIST *list);
/*
Search list starting at current item.
Finishes at end of list or when a match is found
list: Pointer to list
comparator: Routine that returns 0
if the item and comparisonArg don't match,
or 1 if they do. If a match is found the current pointer
is left at the matched item
If no match is found,
current pointer is left at the end
Returns: Pointer to matching item if found, otherwise NULL pointer
*/
void *ListSearch(LIST *list, int (*comparator)(void *, void *), void *comparisonArg);
* Search list starting at current item.
* Finishes at end of list or when a match is found
* list: Pointer to list
* comparator: Routine that returns 0
* if the item and comparisonArg don't match,
* or 1 if they do. If a match is found the current pointer
* is left at the matched item
* If no match is found,
* current pointer is left at the end
* Return: Pointer to matching item if found, otherwise NULL pointer
*/
void *ListSearch(LIST *list, int (*comparator)(void *, void *),
void *comparisonArg);
#endif
+124 -6
View File
@@ -1,13 +1,131 @@
#include <list.h>
#include <stdlib.h>
#include <stdio.h>
LIST *ListCreate();
LIST *ListCreate(void) {
LIST *new_list;
new_list = malloc(sizeof(LIST));
if (!new_list) {
return NULL;
}
new_list->size = 0;
new_list->current = 0;
new_list->head = 0;
new_list->tail = 0;
int ListAdd();
return new_list;
}
int ListInsert();
int ListAdd(LIST *list, void *item) {
/* 3 cases
* 1. Empty list
* 2. Insert when cursor is at the tail (& list not empty)
* 3. Insert when cursor is anywhere but the tail (& list not empty)
*/
NODE *new = malloc(sizeof(NODE));
if (new == NULL || (list->current == NULL && list->size != 0)) {
return -1;
}
new->data = item;
if (list->size == 0) {
list->current = new;
list->head = new;
list->tail = new;
}
else if (list->current == list->tail) {
new->previous = list->tail;
list->tail->next = new;
list->tail = new;
list->current = new;
}
else if (list->current != list->tail && list->size > 0) {
new->next = list->current->next;
new->previous = list->current;
list->current->next->previous = new;
list->current->next = new;
list->current = new;
}
else {
return -1;
}
int ListPrepend();
list->size++;
return 0;
}
int ListAppend();
int ListInsert(LIST *list, void *item) {
/* 3 cases
* 1. Empty list
* 2. Insert when cursor is at the head (& list not empty)
* 3. Insert when cursor is anywhere but the head (& list not empty)
*/
NODE *new = malloc(sizeof(NODE));
if (new == NULL || (list->current == NULL && list->size != 0)) {
return -1;
}
new->data = item;
if (list->size == 0) {
list->current = new;
list->head = new;
list->tail = new;
}
else if (list->current == list->head) {
new->next = list->head;
list->head->previous = new;
list->head = new;
list->current = new;
}
else if (list->current != list->head && list->size > 0) {
new->next = list->current;
new->previous = list->current->previous;
list->current->previous->next = new;
list->current->previous = new;
list->current = new;
}
else {
return -1;
}
list->size++;
return 0;
}
int ListPrepend(LIST *list, void *item) {
NODE *new = malloc(sizeof(NODE));
if (new == NULL) {
return -1;
}
new->data = item;
new->next = list->head;
list->head->previous = new;
list->head = new;
list->size++;
return 0;
}
int ListAppend(LIST *list, void *item) {
NODE *new = malloc(sizeof(NODE));
if (new == NULL) {
return -1;
}
new->data = item;
new->previous = list->tail;
list->tail->next = new;
list->tail = new;
list->size++;
return 0;
}
void ListConcat(LIST *listone, LIST *listtwo) {
listone->tail->next = listtwo->head;
listone->tail = listtwo->tail;
listone->size += listtwo->size;
free(listtwo);
listtwo = NULL;
}
int ListConcat();
+40
View File
@@ -0,0 +1,40 @@
#include <list.h>
#include <stddef.h>
int ListCount(LIST *list) {
return (int) list->size;
}
void *ListFirst(LIST *list) {
list->current = list->head;
return list->current->data;
}
void *ListLast(LIST *list) {
list->current = list->tail;
return list->current->data;
}
void *ListNext(LIST *list) {
if (list->current->next != NULL) {
list->current = list->current->next;
return list->current->data;
}
return NULL;
}
void *ListPrev(LIST *list) {
if (list->current->next != NULL) {
list->current = list->current->previous;
return list->current->data;
}
return NULL;
}
void *ListCurr(LIST *list) {
if (list->current != NULL) {
return list->current->data;
}
return NULL;
}
+81
View File
@@ -0,0 +1,81 @@
#include <list.h>
#include <stdlib.h>
int ListRemove(LIST *list) {
NODE *unused;
if (list->current == NULL) {
return -1;
}
if (list->size == 1) {
unused = list->current;
list->current = NULL;
list->head = NULL;
list->tail = NULL;
}
else if (list->current == list->head) {
unused = list->head;
list->head = list->head->next;
list->current = list->head;
list->current->previous = NULL;
}
else if (list->current == list->tail) {
unused = list->tail;
list->tail= list->tail->previous;
list->current = list->tail;
list->current->next = NULL;
}
else {
unused = list->current;
list->current->previous->next = list->current->next;
list->current->next->previous = list->current->previous;
list->current = list->current->next;
}
unused->data = NULL;
free(unused->data);
unused->next = NULL;
unused->previous = NULL;
unused = NULL;
free(unused);
list->size--;
return 0;
}
void ListFree(LIST *list) {
ListFirst(list);
while (list->head != NULL) {
ListRemove(list);
}
free(list);
}
void *ListTrim(LIST *list) {
NODE *tempnode = list->tail->previous;
list->tail->previous = NULL;
free(list->tail);
list->tail = tempnode;
list->size--;
return 0;
}
void *ListSearch(LIST *list, int (*comparator)(void *, void*),
void *comparisonArg) {
ListFirst(list);
while (list->current != NULL) {
if ((*comparator)(list->current->data, comparisonArg)) {
break;
}
else {
ListNext(list);
}
}
if (list->current == NULL) {
list->current = list->tail;
return NULL;
}
return list->current;
}
View File
+255
View File
@@ -0,0 +1,255 @@
#include <list.h>
#include <stdio.h>
#include <stdlib.h>
int ListPrint(LIST *list) {
if (ListFirst(list) != NULL) {
printf("%d, ", *(int*)list->current->data);
}
while (ListNext(list) != NULL) {
printf("%d, ", *(int*)list->current->data);
}
printf("\n");
return 0;
}
int main(void) {
LIST *listone = ListCreate();
LIST *listtwo = ListCreate();
NODE *cursor, *temp;
int errors = 0;
int one = 1;
int two = 2;
int three = 3;
int four = 4;
int five = 5;
int six = 6;
int seven = 7;
/* Adders tests */
if (listone == NULL) {
errors++;
printf("ListCreate failed on list 1\n");
}
if (listtwo == NULL) {
errors++;
printf("ListCreate failed on list 2\n");
}
/* Test each of the three cases for ListAdd */
ListAdd(listone, &one);
if (ListCount(listone) != 1) {
printf("Error in ListAdd: ListCount expected 1 got %d\n",
ListCount(listone));
errors++;
}
if (listone->head->data != &one) {
printf("Error in ListAdd: head data unexpected\n");
errors++;
}
if (listone->tail->data != &one) {
printf("Error in ListAdd: tail data unexpected\n");
errors++;
}
if (listone->current->data != &one) {
printf("Error in ListAdd: current data unexpected\n");
errors++;
}
/* ListAdd 2 */
ListAdd(listone, &two);
if (ListCount(listone) != 2) {
printf("Error in ListAdd 2: ListCount expected 2 got %d\n",
ListCount(listone));
errors++;
}
if (listone->tail->data != &two) {
printf("Error in ListAdd 2: tail data unexpected\n");
errors++;
}
if (listone->current->data != &two) {
printf("Error in ListAdd 2: current data unexpected\n");
errors++;
}
/* ListAdd 3 */
ListFirst(listone);
if (listone->current != listone->head) {
printf("Error in ListAdd 3: ListFirst didn't move cursor\n");
errors++;
}
ListAdd(listone, &three);
if (ListCount(listone) != 3) {
printf("Error in ListAdd 3: ListCount expected 3 got %d\n",
ListCount(listone));
errors++;
}
if (listone->head->data != &one) {
printf("Error in ListAdd 3: head data unexpected\n");
errors++;
}
if (listone->tail->data != &two) {
printf("Error in ListAdd 3: tail data unexpected\n");
errors++;
}
if (listone->current->data != &three) {
printf("Error in ListAdd 3: current data unexpected\n");
errors++;
}
ListLast(listone);
if (listone->current != listone->tail) {
printf("Error: ListLast didn't move cursor\n");
errors++;
}
/* List Append */
ListFirst(listone);
cursor = listone->current;
ListAppend(listone, &four);
if (ListCount(listone) != 4) {
printf("Error in ListAppend: ListCount expected 4 got %d\n",
ListCount(listone));
errors++;
}
if (listone->tail->data != &four) {
printf("Error in ListAppend: tail data unexpected\n");
errors++;
}
if (listone->current != cursor) {
printf("Error in ListAppend: Cursor moved");
errors++;
}
ListNext(listone);
if (listone->current->data != &three) {
printf("Error: ListNext didn't move the cursor\n");
errors++;
}
/* List Prepend */
cursor = listone->current;
ListPrepend(listone, &five);
if (ListCount(listone) != 5) {
printf("Error in ListPrepend: ListCount expected 5 got %d\n",
ListCount(listone));
errors++;
}
if (listone->head->data != &five) {
printf("Error in ListPrepend: head data unexpected\n");
errors++;
}
if (listone->current != cursor) {
printf("Error in ListPrepend: Cursor moved");
errors++;
}
temp = listone->current->previous;
ListPrev(listone);
if (temp != listone->current) {
printf("Error: ListPrev did not function properly\n");
errors++;
}
/* List Insert */
/* Test w/ cursor at head of list */
ListFirst(listone);
temp = listone->current;
ListInsert(listone, &six);
if (listone->head->data != &six) {
printf("Error in ListInsert: Did not change head of list\n");
errors++;
}
if (ListCount(listone) != 6) {
printf("Error: ListInsert did not update count\n");
errors++;
}
/* Move cursor and test */
ListNext(listone);
ListNext(listone);
ListInsert(listone, &seven);
if (ListCount(listone) != 7) {
printf("Error: ListInsert did not update count\n");
errors++;
}
if (listone->current->data != &seven) {
printf("Error: ListInsert did not update current\n");
errors++;
}
if (ListCurr(listone) != listone->current->data) {
printf("Error: ListCurr did not return current data\n");
errors++;
}
ListAdd(listtwo, &one);
ListAdd(listtwo, &two);
ListAdd(listtwo, &three);
ListAdd(listtwo, &four);
ListAdd(listtwo, &five);
ListFirst(listtwo);
ListNext(listtwo);
ListNext(listtwo);
if (ListCurr(listtwo) != &three) {
printf("Error: List movers\n");
errors++;
}
/* Test remove */
ListRemove(listtwo);
if (ListCurr(listtwo) != &four) {
printf("Error: ListRemove did not move the cursor properly\n");
errors++;
}
if (ListCount(listtwo) != 4) {
printf("Error: ListRemove did not update count\n");
errors++;
}
ListFirst(listtwo);
ListRemove(listtwo);
if (listtwo->head->data != &two || listtwo->current->data != &two) {
printf("Error: ListRemove did not change the head or cursor\n");
errors++;
}
ListLast(listtwo);
ListRemove(listtwo);
if (listtwo->tail->data != &four || listtwo->current->data != &four) {
printf("Error: ListRemove did not change the tail or cursor\n");
errors++;
}
/* Test concat */
ListConcat(listone, listtwo);
listtwo = NULL;
if (ListCount(listone) != 9) {
printf("Error: ListConcat did not change list size\n");
errors++;
}
if (listone->tail->previous->data != &two || listone->tail->data != &four) {
printf("Error: ListConcat did not concatenate properly\n");
errors++;
}
/* Tested free in GDB */
ListTrim(listone);
if (listone->tail->data != &two) {
printf("Error: ListTrim did not set tail properly\n");
errors++;
}
if (ListCount(listone) != 8) {
printf("Error: ListTrim did not change count properly\n");
errors++;
}
printf("Finished with %d errors\n", errors);
return 0;
}
+5 -5
View File
@@ -16,12 +16,12 @@
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
/*#include "list.h"*/
#include <list.h>
#define MAXBUFSIZE 30
#define BACKLOG 10
/*LIST *receivelist, *sendlist;*/
LIST *receivelist, *sendlist;
void *input(void *arg) {
char msg[512];
@@ -74,10 +74,10 @@ int main(int argc, char* argv[]) {
remote_machine = argv[2];
remote_port = argv[3];
/*receivelist = ListCreate();
receivelist = ListCreate();
sendlist = ListCreate();
*/
if (atoi(local_port) < atoi(remote_port)) {
if (atoi(local_port) < atoi(remote_port)) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;