Monday, December 05, 2005

Singly Linked List Class - libyirae genesis

/*-------------------ABOUT--------------------*/
A collection of data structures, implemented for the sake of learning. Yirae, is a contamination of the Efik work Y're, which means to link or join. I chose the name b/cos the first structures implemented for yirae was a singly linked list. The library is called libyirae :)

*---------------------------------------------------*/

/*-----------------Makefile--------------------*/
all: libyirae.a test
@echo "All Done"

test: ysnode_test
@echo "All Test Done"

ysnode_test: ysnode_test.c libyirae.a
gcc -o ysnode_test ysnode_test.c -I. -L. -lyirae


libyirae.a: ysnode.c ysnode.h
gcc -c ysnode.c
ar -rcs libyirae.a ysnode.o


clean:
rm *.o
rm *.a

/*---------------------------------------------------*/

/*-------------------ynode.h-------------------*/
#ifndef YNODE_H
#define YNODE_H

typedef struct _ysNode *ysNode;

ysNode ysCreate(void);
void ysFree(ysNode);

ysNode ysPush(ysNode, int);
ysNode ysRemove(ysNode, int);

void ysSwapNodes(ysNode*, ysNode*);
ysNode ysSwap12(ysNode);

int ysLength(ysNode);
void ysPrint(const ysNode);


#endif
/*---------------------------------------------------*/


/*----------------ysnode.c---------------------*/
#include

struct _ysNode {
int data;
struct _ysNode *next;
};

typedef struct _ysNode *ysNode;

ysNode ysCreate()
{
ysNode head = malloc(sizeof(struct _ysNode));

if(head) {
head->data = 0;
head->next = NULL;
}

return head;
}

void ysFree(ysNode head)
{
ysNode current = head;
ysNode next;

while(current) {
next = current->next;
free(current);
current = next;
}
}

ysNode ysFind_Last(ysNode head)
{
ysNode current = head;

while(current) {
if(current->next == NULL) {
return current;
}

current = current->next;
}
}

ysNode ysPush(ysNode head, int data)
{
ysNode n = ysCreate();

if(n) {
n->data = data;
n->next = head;
head = n;
}

return head;
}

ysNode ysRemove(ysNode head, int data)
{
ysNode current = head;
ysNode prev = NULL;

while(current) {
if(current->data == data) {
if(prev) {
prev->next = current->next;
free(current);
break;
} else {
head = current->next;
free(current);
break;
}
}

prev = current;
current = current->next;
}

return head;
}

ysNode ysCopy(ysNode node)
{
ysNode n = ysCreate();
if(n) {
n->data = node->data;
n->next = node->next;
}
return n;
}


int ysLength(ysNode head) {
int count = 0;
ysNode current = head;

while(current) {
count++;
current = current->next;
}

return count;
}

void ysPrint(ysNode head)
{
if(!head) {
printf("(null)\n");
} else {
ysNode current = head;
while(current) {
printf("%d->", current->data);
current = current->next;
}
printf(".\n");
}
}


/*-------------------------------------------------------*/


/*-------------------ynode_test.c----------------*/

#include
#include

int main()
{
ysNode l = NULL;

/* using ysPush() eventually, faster than Append, that has to Find_Last() first, before appending*/
l = ysPush(l, 5);
l = ysPush(l, 10);
l = ysPush(l, 7);
l = ysPush(l, 23);

printf("The list is now %d nodes long as shown below: \n", ysLength(l));
ysPrint(l);

printf("Starting removals\n");

l = ysRemove(l, 7);
ysPrint(l);

l = ysRemove(l, 23);
ysPrint(l);

l = ysRemove(l, 5);
ysPrint(l);

l = ysRemove(l, 10);
ysPrint(l);

printf("Removals finished\n");

ysFree(l);
return 0;
}

/*------------------------------------------------*/

0 Comments:

Post a Comment

<< Home