Skip to content

Commit

Permalink
Implementation of functions
Browse files Browse the repository at this point in the history
clist_init and clist_free have been defined.
  • Loading branch information
Zhengli Wang committed Dec 15, 2022
1 parent 9e2f3a6 commit f5066b9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
23 changes: 21 additions & 2 deletions include/clist.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 158,8 @@ void *clist_append(struct clist *clist, void *item);
@brief Pops the last item from a clist.
The last item is popped from a clist. clist::list_length is updated
accordingly.
accordingly. If clist::current is the last index node, i.e. clist::current
will be popped after this function, move one node back.
@param clist
The list to be operated on.
Expand Down Expand Up @@ -226,7 227,8 @@ void *clist_write(const struct clist *clist, void *item);
/*!
@brief Insert an item.
On the list, insert an item after the index spefified by index.
On the list, insert an item after the index specified by index. The current
index (clist::current) is preserved.
@param clist
The list to be operated on.
Expand All @@ -241,4 243,21 @@ void *clist_write(const struct clist *clist, void *item);
*/
void *clist_insert(struct clist *clist, void *item, unsigned long index);

/*!
@brief Remove an item.
Removes the item specified by index. If the current index (clist::current)
is to be removed, move one index back. If this is not possible, move one index
forward. If neither of both is possible, clist::current is set to NULL.
@param clist
The list to be operated on.
@param index
The index which has to be removed.
@return The removed item if succeeded, NULL if failed.
*/
void *clist_remove(struct clist *clist, unsigned long index);

#endif
62 changes: 62 additions & 0 deletions src/clist.c
Original file line number Diff line number Diff line change
@@ -0,0 1,62 @@
/*
* The MIT License (MIT)
* Copyright (c) 2022 Zhengli Wang
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*!
@file
@brief Function definitions for clist.h
This file provides function definitions for clist.h.
*/

#include "../include/clist.h"
#include <stdlib.h>

struct clist *clist_init(size_t item_size)
{
// I am lazy.
struct clist *clist = (struct clist *)malloc(sizeof(struct clist));
clist->begin = NULL;
clist->current = NULL;
clist->end = NULL;
clist->current_index = 0;
clist->item_size = item_size;
clist->list_length = 0;
return clist;
}

size_t clist_free(struct clist *clist)
{
struct node *temp_node;
for (temp_node = clist->begin; temp_node != NULL; temp_node = temp_node->next)
{
if (clist->current->item)
{
free(clist->current->item);
}
if (clist->current)
{
free(clist->current);
}
}
free(clist);
}

0 comments on commit f5066b9

Please sign in to comment.