BarryServer : Git

All the code for all my projects
// BarryServer : Git / IRCWebHooks / blob / 3a8789aa42abfd82ef3c2612345df637301092f2 / src / list.c

// Related

IRCWebHooks

Barry Adding files 3f04e22 (3 years, 3 months ago)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#include "list.h"
#include "irc.h"

/* Insert first item */
void
insert_first(char *data)
{
	struct Node *link = (struct Node*) malloc(sizeof(struct Node));
	strcpy(link->data, data);
	link->next = head;
	head = link;
}

/* Delete the first item */
struct Node *
delete_first(void)
{
	struct Node *tempLink = head;
	head = head->next;
	free(tempLink);
	return tempLink;
}

/* Check if list is empty */
bool
is_empty(void)
{
	return head == NULL;
}

int
length(void)
{
	int length = 0;
	struct Node *current;
	for(current = head; current != NULL; current = current->next) {
		length++;
	}
	return length;
}

/* Find Node with data */
struct Node *
find(char *data)
{
	struct Node* current = head;
	if (head == NULL) {
		return NULL;
	}
	while (strncmp(current->data, data, strlen(data))) {
		if (current->next == NULL) {
			return NULL;
		} else {
			current = current->next;
		}
	}
	return current;
}

/* Delete a Node */
struct Node *
delete(char *data)
{
	struct Node* current = head;
	struct Node* previous = NULL;
	if (head == NULL) {
		return NULL;
	}
	while (strncmp(current->data, data, strlen(data))) {
		if(current->next == NULL) {
			return NULL;
		} else {
			previous = current;
			current = current->next;
		}
	}
	if (current == head) {
		head = head->next;
	} else {
		previous->next = current->next;
	}
	free(current);
	return current;
}