One Way Linked List
/*
* File: OneWayList.c
* Author: xinghuazhang
*
* Created on March 22, 2012, 9:41 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/* one way linked list*/
struct Node
{
char data[51];
struct Node* next;
};
typedef struct Node Node_t;
void LinkedListInit( Node_t **head);
void LinkedListAddHead( Node_t **head, char* data );
void LinkedListAddTail( Node_t **head, char* data );
void LinkedListAddInOrder( Node_t **head, char* data );
bool LinkedListRemove( Node_t **head, char* data );
bool LinkedListRemoveHead( Node_t** head );
bool LinkedListRemoveTail( Node_t** head );
int LinkedListSize( const Node_t * head );
Node_t* LinkedListFind( Node_t *head, const char* data );
bool LinkedListEmpty( Node_t *head );
void LinkedListPrint( const Node_t *head );
void LinkedListDestroy( Node_t **head );
/* main starts here */
int main(int argc, char** argv)
{
Node_t *list;
LinkedListInit( &list );
LinkedListAddHead( &list, "HELLO" );
LinkedListPrint( list );
LinkedListDestroy( &list );
return(EXIT_SUCCESS);
}
/**
* void LinkedListInit( Node_t **head)
* @param head: the head of the list
*/
void LinkedListInit( Node_t **head)
{
*head = NULL;
}
/**
* void LinkedListAddHead(Node_t **head, char* data )
* @param head: the head of the list
* @param data: the data to be added
*/
void LinkedListAddHead( Node_t **head, char* data )
{
/* make a new Node */
Node_t *newNode = malloc( sizeof( Node_t) );
if( newNode == NULL )
{
fprintf( stderr, "fail to malloc memory for new Node\n" );
exit( EXIT_FAILURE );
}
strncpy( newNode->data, data, strlen(data) + 1 );
newNode->next = *head;
*head = newNode;
}
/**
* void LinkedListAddTail( Node_t **head, char* data )
* @param head: the head of the list
* @param data: the data to be added
*/
void LinkedListAddTail( Node_t **head, char* data )
{
/* make a new Node */
Node_t *newNode = malloc( sizeof( Node_t) );
if( newNode == NULL )
{
fprintf( stderr, "fail to malloc memory for new Node\n" );
exit( EXIT_FAILURE );
}
strncpy( newNode->data, data, strlen(data) + 1 );
newNode->next = NULL;
if( *head == NULL )/* empty */
{
*head = newNode;
}
else /* not empty, add to tail */
{
Node_t *walk = *head;
while(walk->next)
{
walk = walk->next;
}
walk->next = newNode;
}
}
/**
* void LinkedListAddInOrder( Node_t **head, char* data )
* @param head: the head of the list
* @param data: the data to be added
*/
void LinkedListAddInOrder( Node_t **head, char* data )
{
/* make a new Node */
Node_t *newNode = malloc( sizeof( Node_t));
if( newNode == NULL )
{
fprintf( stderr, "fail to malloc memory for new Node\n" );
exit( EXIT_FAILURE );
}
strncpy( newNode->data, data, strlen(data)+1 );
newNode->next = NULL;
/* add it in order */
Node_t *next = *head;
Node_t *pre = NULL;
while(next != NULL && strcmp( next->data, data) >= 0 )
{
pre = next;
next = next->next;
}
if(next != NULL ) /* next exist */
{
newNode->next = next;
}
if(pre != NULL )/* pre exist */
{
pre->next = newNode;
}
else /* pre not exist, newNode is the new head */
{
*head = newNode;
}
}
/**
* bool LinkedListRemove( Node_t **head, char* data )
* @param head: the head of the list
* @param data: the data to be removed
* @return true if the Node is removed, false if not find
*/
bool LinkedListRemove( Node_t **head, char* data )
{
/* find position */
Node_t *walk = *head;
Node_t *pre = NULL;
while(walk != NULL && strcmp( walk->data, data) != 0 )
{
pre = walk;
walk = walk->next;
}
if( walk == NULL )/* can't find it */
{
return false;
}
else /* founded! */
{
if( pre != NULL)
pre->next = walk->next;
else
*head = walk->next;
free( walk );
return true;
}
}
/**
* bool LinkedListRemoveHead( Node_t** head )
* @param head: the head of the list
* @return true if successfully removed
*/
bool LinkedListRemoveHead( Node_t** head )
{
/* empty */
if( *head == NULL )
{
return false;
}
/*not empty */
Node_t *oldHead = *head;
*head = (*head)->next;
free( oldHead );
return true;
}
/**
* bool LinkedListRemoveTail( Node_t** head )
* @param head: the head of the list
* @return true if it is successfully removed
*/
bool LinkedListRemoveTail( Node_t** head )
{
/* empty */
if( *head == NULL )
{
return false;
}
/* not empty, go to tail */
Node_t *pre = NULL;
Node_t *walk = *head;
while( walk->next != NULL )
{
pre = walk;
walk = walk->next;
}
if( pre != NULL ) /* remove tail */
{
pre->next = NULL;
}
else /* only Node, set head to NULL */
{
*head = NULL;
}
free( walk );
return true;
}
/**
* int LinkedListSize( const Node_t * head )
* @param head: the head of the list
* @return the size of the linked list
*/
int LinkedListSize( const Node_t * head )
{
int size = 0;
const Node_t * walk = head;
while( walk )
{
walk = walk->next;
size++;
}
return size;
}
/**
* bool LinkedListFind( const Node_t *head, const char* data )
* @param head: the head of the lsit
* @param data: the data to look for
* @return: true if the data is in the list
*/
Node_t* LinkedListFind( Node_t *head, const char* data )
{
Node_t *walk = head;
while( walk && strcmp(walk->data, data) != 0 )
walk = walk->next;
return walk;
}
/**
* bool LinkedListEmpty( Node_t *head )
* @param head: the head of the list
* @return true if list is empty
*/
bool LinkedListEmpty( Node_t *head )
{
return head==NULL;
}
/**
* void LinkedListPrint( const Node_t *head )
* @param head: the head of the list
*/
void LinkedListPrint( const Node_t *head )
{
const Node_t *walk = head;
while( walk )
{
printf( "%s\n", walk->data );
walk = walk->next;
}
}
/**
* void LinkedListDestroy( Node_t **head )
* @param head: the head of the list
*/
void LinkedListDestroy( Node_t **head )
{
Node_t *next;
while( *head )
{
next = (*head)->next;
free( *head );
*head = next;
}
}
Two Way Linked List
/*
* File: Twoway.c
* Author: xing hua zhang
*
* Created on March 22, 2012, 9:41 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/* two way linked list*/
struct Node
{
char data[51];
struct Node *prev;
struct Node *next;
};
typedef struct Node Node_t;
/* two way list */
struct List
{
Node_t *head;
Node_t *tail;
};
typedef struct List List_t;
void LinkedListInit(List_t *list);
void LinkedListAddHead(List_t *list, char* data);
void LinkedListAddTail(List_t *list, char* data);
void LinkedListAddInOrder(List_t *list, char* data);
bool LinkedListRemove( List_t *list, char* data);
bool LinkedListRemoveHead( List_t *list);
bool LinkedListRemoveTail( List_t *list);
void LinkedListPrint(const List_t *list);
void LinkedListDestroy(const List_t *list);
void println(char* str);
void LinkedListInit(List_t *list)
{
list->head = NULL;
list->tail = NULL;
}
void LinkedListAddHead(List_t *list, char *data)
{
/* make a new Node */
Node_t *newNode = malloc(sizeof( Node_t));
if(newNode == NULL)
{
fprintf(stderr, "fail to malloc memory for new Node\n");
exit(EXIT_FAILURE);
}
strncpy(newNode->data, data, strlen(data) + 1);
newNode->prev = NULL;
/* connect it to the head*/
if(list->head != NULL)
list->head->prev = newNode;
if(list->tail == NULL)
list->tail = newNode;
newNode->next = list->head;
list->head = newNode;
}
void LinkedListAddTail(List_t *list, char *data)
{
/* make a new Node */
Node_t *newNode = malloc(sizeof( Node_t));
if(newNode == NULL)
{
fprintf(stderr, "fail to malloc memory for new Node\n");
exit(EXIT_FAILURE);
}
strncpy(newNode->data, data, strlen(data) + 1);
newNode->next = NULL;
/* connect it to the tail */
if(list->tail != NULL)
list->tail->next = newNode;
if(list->head == NULL)
list->head = newNode;
newNode->prev = list->tail;
list->tail = newNode;
}
void LinkedListAddInOrder(List_t *list, char* data)
{
/* make a new Node */
Node_t *newNode = malloc(sizeof( Node_t));
if(newNode == NULL)
{
fprintf(stderr, "fail to malloc memory for new Node\n");
exit(EXIT_FAILURE);
}
strncpy(newNode->data, data, strlen(data) + 1);
/* add it in order */
Node_t *walk = list->head;
while(walk != NULL && strcmp(walk->data, data) <= 0)
{
walk = walk->next;
}
if(walk == NULL) /* add to tail */
{
newNode->next = NULL;
if(list->tail != NULL)
list->tail->next = newNode;
if(list->head == NULL)
list->head = newNode;
newNode->prev = list->tail;
list->tail = newNode;
}
else /* add before walk */
{
if(walk->prev)
{
walk->prev->next = newNode;
newNode->prev = walk->prev;
}
else
{
list->head = newNode;
}
newNode->next = walk;
walk->prev = newNode;
}
}
bool LinkedListRemove( List_t *list, char* data)
{
Node_t *walk = list->head;
while( walk != NULL && strcmp( walk->data, data )!= 0)
{
walk = walk->next;
}
if( walk == NULL )/* not found */
{
return false;
}
else if (walk->prev == NULL && walk->next == NULL ) /*remove the only one */
{
list->head = list->tail = NULL;
free(walk);
return true;
}
else /*found*/
{
if( walk->prev != NULL)
{
walk->prev->next = walk->next;
}
else
{
list->head = walk->next;
if( list->head != NULL ) list->head->prev = NULL;
}
if( walk->next != NULL)
{
walk->next->prev = walk->prev;
}
else
{
list->tail = walk->prev;
if( list->tail != NULL ) list->tail->next = NULL;
}
free( walk);
return true;
}
}
bool LinkedListRemoveHead( List_t *list)
{
/* empty */
if( list->head == NULL )
{
return false;
}
/*not empty */
Node_t *oldHead = list->head;
list->head = list->head->next;
if( list->head != NULL )
list->head->prev = NULL;
else
list->tail = NULL;
free( oldHead );
return true;
}
bool LinkedListRemoveTail( List_t *list)
{
/* empty */
if( list->tail == NULL )
{
return false;
}
/*not empty */
Node_t *oldTail = list->tail;
list->tail = list->tail->prev;
if( list->tail != NULL )
list->tail->next = NULL;
else
list->head = NULL;
free( oldTail );
return true;
}
Node_t* LinkedListFind( const List_t *list, char* data )
{
Node_t *walk = list->head;
while( walk && strcmp(walk->data, data) != 0 )
walk = walk->next;
return walk;
}
bool LinkedListEmpty( const List_t *list )
{
return list->head==NULL;
}
int LinkedListSize( const List_t * list )
{
int size = 0;
const Node_t * walk = list->head;
while( walk )
{
walk = walk->next;
size++;
}
return size;
}
void LinkedListPrint(const List_t *list)
{
println("The list is now");
Node_t *walk = list->head;
while(walk)
{
printf("%s\n", walk->data);
walk = walk->next;
}
println("");
}
void LinkedListDestroy(const List_t *list)
{
Node_t *walk = list->head;
Node_t *next = NULL;
while(walk)
{
next = walk->next;
free(walk);
walk = next;
}
}
/* main starts here */
int main(int argc, char** argv)
{
List_t list;
LinkedListInit(&list);
LinkedListAddInOrder(&list, "gg.");
LinkedListAddInOrder(&list, "gwgr");
LinkedListPrint(&list);
Node_t *goal = NULL;
if( (goal = LinkedListFind( &list, "gg.")) != NULL)
printf( "The goal is found at %p with data %s\n", goal, goal->data );
LinkedListRemoveTail(&list);
LinkedListRemoveTail(&list);
LinkedListPrint(&list);
LinkedListDestroy(&list);
return(EXIT_SUCCESS);
}
void println(char* str)
{
printf("%s\n", str);
}