Commit 38595ced authored by Zhaozhong Liu's avatar Zhaozhong Liu
Browse files

repo setup (imc004)

parent 5b48ebe9
CC = gcc
CFLAGS = -std=gnu99 -Wall -g #-DDEBUG
EXECS = dlisttest
all: $(EXECS)
dnode.o:
dlist.o:
dlisttest:
.PHONY: clean
clean:
/bin/rm -rf $(EXECS) dnode.o dlist.o
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdlib.h>
#include <stdio.h>
#include "dnode.h"
#include "dlist.h"
struct dlist *
dlist_create() {
return calloc(1, sizeof(struct dlist));
}
void
dlist_destroy(struct dlist *l) {
struct dnode *p = l->front;
do {
l->front = l->front->next;
free(p->data);
free(p);
p = l->front;
} while (l->front != NULL);
l->front = l->back = NULL;
l->counter = 0;
}
void
dlist_obliterate(struct dlist *l) {
dlist_destroy(l);
free(l);
}
void
dlist_add_front(struct dlist *l, void *ptr) {
// YOUR CODE HERE
#ifdef DEBUG
printf("counter= %d, %s\n", l->counter, (char *) ptr);
printf("front= %s\n", (char *) l->front->data);
printf("back= %s\n\n", (char *) l->back->data);
#endif /* DEBUG */
}
void
dlist_add_back(struct dlist *l, void *ptr) {
// YOUR CODE HERE
#ifdef DEBUG
printf("counter= %d, %s\n", l->counter, (char *) ptr);
printf("front= %s\n", (char *) l->front->data);
printf("back= %s\n\n", (char *) l->back->data);
#endif /* DEBUG */
}
void *
dlist_remove_front(struct dlist *l) {
void *ptr;
// YOUR CODE HERE
return ptr;
}
void *
dlist_remove_back(struct dlist *l) {
struct dnode *n = l->back;
void *ptr = n->data;
if (1 == l->counter) {
l->front = l->back = NULL;
} else {
l->back = l->back->prev;
l->back->next = NULL;
}
(l->counter)--;
free(n);
return ptr;
}
void *
dlist_find_remove(struct dlist *l, void *ptr) {
struct dnode *n = l->front;
void *ret_ptr = NULL;
while ((n != NULL) && (n->data != ptr)) {
n = n->next;
}
if (n != NULL) {
if (l->front == n) {
return dlist_remove_front(l);
} else if (l->back == n) {
return dlist_remove_back(l);
} else {
if (1 == l->counter) {
l->front = l->back = NULL;
} else {
n->prev->next = n->next;
n->next->prev = n->prev;
}
(l->counter)--;
}
ret_ptr = n->data;
free(n);
}
return ret_ptr;
}
uint32_t
dlist_num_elems(struct dlist *l) {
return l->counter;
}
void *
dlist_iter_begin(struct dlist *l) {
void *ret_val = NULL;
l->iter = l->front;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
return ret_val;
}
void *
dlist_iter_next(struct dlist *l) {
void *ret_val = NULL;
if (l->iter != NULL) {
l->iter = l->iter->next;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
}
return ret_val;
}
bool
dlist_iter_has_next(struct dlist *l) {
bool ret_val = false;
if (l->iter != NULL) {
ret_val = (l->iter->next != NULL);
}
#ifdef DEBUG
if (ret_val) {
printf("dlist_has_next: current is %s\n", (char *) l->iter->data);
printf("dlist_has_next: returning %d\n\n", ret_val);
}
#endif /* DEBUG */
return ret_val;
}
void *
dlist_iter_end(struct dlist *l) {
void *ret_val = NULL;
l->iter = l->back;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
return ret_val;
}
void *
dlist_iter_prev(struct dlist *l) {
void *ret_val = NULL;
if (l->iter != NULL) {
l->iter = l->iter->prev;
if (l->iter != NULL) {
ret_val = l->iter->data;
}
}
return ret_val;
}
bool
dlist_iter_has_prev(struct dlist *l) {
bool ret_val = false;
if (l->iter != NULL) {
ret_val = (l->iter->prev != NULL);
}
return ret_val;
}
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#ifndef _DLIST_H_
#define _DLIST_H_
#include <stdint.h>
#include <stdbool.h>
#include "dnode.h"
/**
* Doubly-linked list.
*/
struct dlist {
struct dnode *front;
struct dnode *back;
struct dnode *iter;
uint32_t counter;
};
/**
* Allocates new dlist dynamically.
*
* @return pointer to the list.
*/
struct dlist *dlist_create();
/**
* Deallocates all nodes in a dlist and the data they point to, but
* preserve the list.
*
* @param l pointer to the list (non-NULL)
*/
void dlist_destroy(struct dlist *l);
/**
* Deallocates all nodes in a dlist and the data they point to; deallocate
* the list itself as well. This is the inverse of dlist_create.
* @param l pointer to the list (non-NULL)
*/
void dlist_obliterate(struct dlist *l);
/**
* Inserts new node in dlist before the first node.
*
* @param l pointer to the list (non_NULL)
* @param ptr pointer to generic data to store in new list node
*/
void dlist_add_front(struct dlist *l, void *ptr);
/**
* Inserts new node in dlist after the last node.
*
* @param l pointer to the list (non-NULL)
* @param ptr pointer to generic data to store in new list node
*/
void dlist_add_back(struct dlist *l, void *ptr);
/**
* Remove the first node and return pointer to the data. It is the
* responsibility of the caller to free the memory pointed by the return
* value.
*
* @param l pointer to the list (non-NULL)
* @return pointer to data stored in front node
*/
void *dlist_remove_front(struct dlist *l);
/**
* Remove the last node and return pointer to the data. It is the
* responsibility of the caller to free the memory pointed by the return
* value.
*
* @param l pointer to the list (non-NULL)
* @return pointer to data stored in back node
*/
void *dlist_remove_back(struct dlist *l);
/**
* Seek out the node that contains a pointer and remove from list returning
* the pointer to the data. It is the
* responsibility of the caller to free the memory pointed by the return
* value.
*
* @param l pointer to the list (non-NULL)
* @param ptr pointer to the data item been sought
* @return pointer to data stored in the node that was found node
*/
void *dlist_find_remove(struct dlist *l, void *ptr);
/**
* Returns the number of elements in the list (nodes).
*
* @param l pointer to the list (non-NULL)
*/
uint32_t dlist_num_elems(struct dlist *l);
/**
* Initialize iterator for traversing a dlist forwards.
*/
void *dlist_iter_begin(struct dlist *l);
/**
* Returns current item and advances iterator to next item in list.
*/
void *dlist_iter_next(struct dlist *l);
/**
* Check if iterator has reached the end of the list.
*/
bool dlist_iter_has_next(struct dlist *l);
/**
* Initialize iterator for traversing a dlist backwards.
*/
void *dlist_iter_end(struct dlist *l);
/**
* Returns current item and advances iterator to previous item in list.
*/
void *dlist_iter_prev(struct dlist *l);
/**
* Check if iterator has reached the start of the list.
*/
bool dlist_iter_has_prev(struct dlist *l);
#endif /* _DLIST_H_ */
/*
* Copyright (c) 2014 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dlist.h"
void traverse_forward(struct dlist *l) {
char *str;
printf("forward traversal\n");
for (str = dlist_iter_begin(l); str != NULL;
str = (char *) dlist_iter_next(l)) {
printf("string = %s\n", str);
}
}
void traverse_backward(struct dlist *l) {
char *str;
printf("backward traversal\n");
for (str = dlist_iter_end(l); str != NULL;
str = (char *) dlist_iter_prev(l)) {
printf("string = %s\n", str);
}
}
int main(int argc, char *argv[]) {
char *mystr;
struct dlist *mylist = dlist_create();
printf("dlisttest running...\n");
char *str1 = malloc(20);
strncpy(str1, "animal", 20);
char *str2 = malloc(20);
strncpy(str2, "barnacle", 20);
char *str3 = malloc(20);
strncpy(str3, "carnage", 20);
char *str4 = malloc(20);
strncpy(str4, "demented", 20);
char *str5 = malloc(20);
strncpy(str5, "error", 20);
printf("\ntesting dlist_add_back\n");
dlist_add_back(mylist, str1);
dlist_add_back(mylist, str2);
dlist_add_back(mylist, str3);
dlist_add_back(mylist, str4);
dlist_add_back(mylist, str5);
traverse_forward(mylist);
traverse_backward(mylist);
dlist_destroy(mylist);
printf("\nlist destroyed\n");
traverse_forward(mylist);
traverse_backward(mylist);
printf("\ntraversal of empty list completed\n");
str1 = malloc(20);
strncpy(str1, "folly", 20);
str2 = malloc(20);
strncpy(str2, "gunk", 20);
str3 = malloc(20);
strncpy(str3, "hospital", 20);
str4 = malloc(20);
strncpy(str4, "ignorant", 20);
str5 = malloc(20);
strncpy(str5, "jelly", 20);
printf("\ntesting dlist_add_front\n");
dlist_add_front(mylist, str1);
dlist_add_front(mylist, str2);
dlist_add_front(mylist, str3);
dlist_add_front(mylist, str4);
dlist_add_front(mylist, str5);
traverse_forward(mylist);
traverse_backward(mylist);
printf("\ntesting remove front\n");
mystr = dlist_remove_front(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
printf("list length= %d\n\n", dlist_num_elems(mylist));
traverse_forward(mylist);
traverse_backward(mylist);
printf("\ntesting remove back\n");
mystr = dlist_remove_back(mylist);
printf("removed string = %s\n", mystr);
free(mystr);
printf("list length= %d\n\n", dlist_num_elems(mylist));
traverse_forward(mylist);
traverse_backward(mylist);
printf("\ntesting find_remove\n");
mystr = dlist_find_remove(mylist, str3);
printf("removed string = %s\n", mystr);
free(mystr);
printf("contents of the list\n");
traverse_forward(mylist);
traverse_backward(mylist);
}
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdlib.h>
#include "dnode.h"
struct dnode *dnode_create() {
return calloc(1, sizeof(struct dnode));
}
void dnode_setdata(struct dnode *n, void *ptr) {
n->data = ptr;
}
void *dnode_destroy(struct dnode *n) {
void *ptr = n->data;
free(n);
return ptr;
}
void dnode_obliterate(struct dnode *n) {
free(n->data);
free(n);
}
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#ifndef _DNODE_H_
#define _DNODE_H_
/**
* Node in a doubly-linked list.
*/
struct dnode {
struct dnode *prev;
struct dnode *next;
void *data;
};
/**
* Allocates a new dnode leaving the pointer to data as null.
*
* @return pointer to a new dnode
*/
struct dnode *dnode_create();
/**
* Sets the pointer to data with the value passed in.
*
* @param n pointer to the dnone
* @param ptr pointer to a generic data type to store in dnode
*/
void dnode_setdata(struct dnode *n, void *ptr);
/**
* Deallocates a dnode but not the data it points to. The caller must take
* the responsibility of deallocating the data later on.
*
* @param n pointer to the dnode
* @return pointer to the generic data type associated with node
*/
void *dnode_destroy(struct dnode *n);
/**
* Deallocates a dnode and also the data it points to.
*
* @param ptr pointer to the dnode
*/
void dnode_obliterate(struct dnode *n);
#endif /* _DNODE_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment