This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:kkrauss1:portfolio:libdll [2011/12/12 19:05] – [Objectives] kkrauss1 | user:kkrauss1:portfolio:libdll [2011/12/12 19:58] (current) – [Objectives] kkrauss1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | |||
+ | A project for C/C++, Data Structures, and System Programming | ||
+ | |||
+ | This project is one I spent over a week on, and probably the one I learned the most from. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The purpose of this project was to create a library for doubly linked lists. | ||
+ | |||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * understand functions | ||
+ | * understand pointers | ||
+ | * understand structs | ||
+ | * understand malloc() | ||
+ | * ability to create a linked data structure | ||
+ | |||
+ | =====Background===== | ||
+ | *This project was probably the most important project we worked on this semester. | ||
+ | =====Scope===== | ||
+ | To better utilize the functionality of the doubly linked list we've been studying in class, a personal implementation for use as a library will be undertaken for use in other projects and activities this semester. | ||
+ | |||
+ | Working from the class example, a library implementation will be created which has the following functions: | ||
+ | |||
+ | * **create**: generate a new node | ||
+ | * **append**: add a new node to the list after a given node | ||
+ | * **remove**: remove a node from the list | ||
+ | * **search**: find if a node exists (likely search by value) | ||
+ | * **insert**: add a new node to the list before a given node | ||
+ | * **delete**: destroy/ | ||
+ | * **copy**: duplicate an existing list | ||
+ | |||
+ | Creating a header file will be in order, which will contain the function prototypes of all of the above. The code will be implemented across several files (create.c, add.c, delete.c, process.c, or however you wish to structure it-- have at least 4 .c or .cc source files). | ||
+ | |||
+ | There will also be a sample implementation (ie a file with a **main()**) that will include the header file and demonstrate this library being created (it will link against it to compile). | ||
+ | |||
+ | =====Attributes===== | ||
+ | |||
+ | |||
+ | ====Cprog attributes==== | ||
+ | |||
+ | * variables | ||
+ | * pointers | ||
+ | * selection | ||
+ | * i/o | ||
+ | * repetition | ||
+ | * functions | ||
+ | * structures | ||
+ | * libraries | ||
+ | |||
+ | |||
+ | ====Data Structures==== | ||
+ | * Pointers | ||
+ | * Malloc/new | ||
+ | * linked list | ||
+ | * Doubly linked list | ||
+ | * Libraries | ||
+ | |||
+ | ====Systems programming==== | ||
+ | * Terminal I/O | ||
+ | |||
+ | |||
+ | | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | * pointers: pointers are at the heart of dynamic structures. | ||
+ | * malloc/new: dynamic memory allocation. | ||
+ | * linked lists: this code is going to implement linked lists. | ||
+ | * doubly linked lists: not only is the code implementing a linked list, it is implementing doubly linked lists. | ||
+ | * libraries: this code will be part of a library that can be reused in other projects | ||
+ | |||
+ | =====Code===== | ||
+ | This is the header file: | ||
+ | <code c> | ||
+ | # | ||
+ | # | ||
+ | |||
+ | |||
+ | struct node { // | ||
+ | int value; | ||
+ | struct node *next; | ||
+ | struct node *prev; | ||
+ | }; | ||
+ | typedef struct node Node; | ||
+ | |||
+ | struct list {// predefined structure for use of returns in functions allow for easy access to list | ||
+ | Node *start; | ||
+ | Node *end; | ||
+ | }; | ||
+ | typedef struct list List; | ||
+ | |||
+ | List *newNode(List *myList, Node *tmp, int input); | ||
+ | List *insertNode(List *myList, Node *tmp, Node *tmp2, int input); | ||
+ | List *deleteNode(List *myList, Node *tmp, int input); | ||
+ | Node *removeNode(List *myList, Node *tmp, int input); | ||
+ | void printList(List *myList, Node *tmp); | ||
+ | int searchNode(List *myList, int input); | ||
+ | |||
+ | //minimum variables needed in main: int input, Node *tmp, Node *tmp2, List *myList. | ||
+ | |||
+ | #endif | ||
+ | |||
+ | </ | ||
+ | |||
+ | This is the library code: | ||
+ | <code c> | ||
+ | // This is an attempt at writing linked doubly list libraries for a single variable that will include newNode(), insertNode(), | ||
+ | // removeNode, printList() | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | List *newNode(List *myList, Node *tmp, int input)// | ||
+ | { | ||
+ | |||
+ | if(myList-> | ||
+ | { | ||
+ | myList-> | ||
+ | myList-> | ||
+ | myList-> | ||
+ | myList-> | ||
+ | myList-> | ||
+ | |||
+ | |||
+ | } | ||
+ | else // list is not empty so now will will create a new node and append | ||
+ | { | ||
+ | tmp = (Node *)malloc(sizeof(Node)); | ||
+ | myList-> | ||
+ | tmp-> | ||
+ | myList-> | ||
+ | myList-> | ||
+ | myList-> | ||
+ | } | ||
+ | |||
+ | return myList; | ||
+ | } | ||
+ | |||
+ | List *insertNode(List *myList, Node *tmp, Node *tmp2, int input) | ||
+ | { | ||
+ | if(myList-> | ||
+ | { | ||
+ | tmp2 = (Node *)malloc(sizeof(Node)); | ||
+ | tmp2-> | ||
+ | myList-> | ||
+ | myList-> | ||
+ | myList-> | ||
+ | } | ||
+ | else //inserting before a node in the list but not prepending or appending | ||
+ | { | ||
+ | tmp2 = (Node *)malloc(sizeof(Node)); | ||
+ | tmp2-> | ||
+ | tmp2-> | ||
+ | tmp-> | ||
+ | tmp-> | ||
+ | tmp2-> | ||
+ | } | ||
+ | |||
+ | return myList; | ||
+ | } | ||
+ | |||
+ | List *deleteNode(List *myList, Node *tmp, int input) | ||
+ | { | ||
+ | if((tmp != myList-> | ||
+ | { | ||
+ | tmp-> | ||
+ | tmp-> | ||
+ | tmp-> | ||
+ | tmp-> | ||
+ | free(tmp); | ||
+ | } | ||
+ | else if(myList-> | ||
+ | { | ||
+ | free(tmp); | ||
+ | } | ||
+ | else if(tmp == myList-> | ||
+ | { | ||
+ | tmp-> | ||
+ | myList-> | ||
+ | tmp-> | ||
+ | free(tmp);// | ||
+ | } | ||
+ | else //only thing left to check is if the node to be deleted is the last node aka tmp == end | ||
+ | { | ||
+ | tmp-> | ||
+ | myList-> | ||
+ | tmp-> | ||
+ | free(tmp);// | ||
+ | } | ||
+ | |||
+ | return myList; | ||
+ | } | ||
+ | |||
+ | Node *removeNode(List *myList, Node *tmp, int input) | ||
+ | { | ||
+ | if((tmp != myList-> | ||
+ | { | ||
+ | tmp-> | ||
+ | tmp-> | ||
+ | tmp-> | ||
+ | tmp-> | ||
+ | |||
+ | } | ||
+ | |||
+ | else if(tmp == myList-> | ||
+ | { | ||
+ | tmp-> | ||
+ | myList-> | ||
+ | tmp-> | ||
+ | |||
+ | } | ||
+ | else //only thing left to check is if the node to be deleted is the last node aka tmp == end | ||
+ | { | ||
+ | tmp-> | ||
+ | myList-> | ||
+ | tmp-> | ||
+ | } | ||
+ | |||
+ | return tmp; | ||
+ | } | ||
+ | |||
+ | int searchNode(List *myList, int input)// returns the first node containing value, 0 means no such node with value exists | ||
+ | { | ||
+ | Node *tmp = myList-> | ||
+ | int nodeNum = 0; | ||
+ | if (tmp != NULL) | ||
+ | { | ||
+ | nodeNum = 1; | ||
+ | while (tmp-> | ||
+ | { | ||
+ | if (tmp-> | ||
+ | { | ||
+ | tmp = tmp-> | ||
+ | nodeNum++; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | if (tmp-> | ||
+ | { | ||
+ | if (tmp-> | ||
+ | { | ||
+ | nodeNum++; | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | nodeNum = 0; | ||
+ | } | ||
+ | return nodeNum; | ||
+ | } | ||
+ | void printList(List *myList, Node *tmp) | ||
+ | { | ||
+ | int i = 1; | ||
+ | tmp = myList-> | ||
+ | |||
+ | while (tmp != NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | i++; | ||
+ | tmp = tmp-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | =====Reflection===== | ||
+ | *As I stated earlier I felt this could possibly be the most important project we did this semester. | ||