This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:tgalpin2:portfolio:libdll [2011/12/16 03:58] – [Code] tgalpin2 | user:tgalpin2:portfolio:libdll [2011/12/16 18:29] (current) – [Execution] tgalpin2 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | |||
+ | A project for Matt Haas' Fall 2011 Data Structures class. | ||
+ | |||
+ | This project was done over the month of October, but was worked on for about a week in terms of days in which programming this implementation. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The purpose of this lab is to create linked list manipulating functions, make a library from these functions, then write a program that implements this library. | ||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * understand functions | ||
+ | * understand pointers | ||
+ | * understand structs | ||
+ | * understand malloc() | ||
+ | * ability to create a linked data structure | ||
+ | |||
+ | =====Background===== | ||
+ | As previously stated, I am trying to make a linked list library implementation. In doing so, I will have gained an understanding of a linked list, which is our basis for learning about data structures. This lets one create a list of data that can be accessed by moving back and forth. | ||
+ | |||
+ | =====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 | ||
+ | * **copy**: duplicate an existing node | ||
+ | * **copy list**: duplicate the current list of nodes | ||
+ | * **display**: | ||
+ | |||
+ | 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 . | ||
+ | |||
+ | 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===== | ||
+ | |||
+ | * 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 | ||
+ | * file I/O: The manager in the main function uses the library to take input and give output. | ||
+ | |||
+ | =====Code===== | ||
+ | ===main.c=== | ||
+ | <code c> | ||
+ | /* | ||
+ | * Doubly Linked List Library - a doubly linked list implementation | ||
+ | * | ||
+ | * Fall2011 Data Structures | ||
+ | */ | ||
+ | |||
+ | // Include files | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | unsigned int makeChoice(); | ||
+ | |||
+ | // main function | ||
+ | int main() | ||
+ | { | ||
+ | // initialize variables | ||
+ | start = end = tmp = tmp2 = NULL; | ||
+ | unsigned int choice, created=0; | ||
+ | system(" | ||
+ | printf(" | ||
+ | while(created == 0) | ||
+ | { | ||
+ | create(); | ||
+ | if(start != NULL) | ||
+ | { | ||
+ | created=1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | system(" | ||
+ | choice=makeChoice(); | ||
+ | system(" | ||
+ | while(choice != 0) | ||
+ | { | ||
+ | system(" | ||
+ | if(choice==1) | ||
+ | { | ||
+ | displayList(); | ||
+ | } | ||
+ | |||
+ | else if(choice==2) | ||
+ | { | ||
+ | append(); | ||
+ | } | ||
+ | else if(choice==3) | ||
+ | { | ||
+ | insert(); | ||
+ | } | ||
+ | |||
+ | else if(choice==4) | ||
+ | { | ||
+ | removeNode(); | ||
+ | } | ||
+ | |||
+ | else if(choice==5) | ||
+ | { | ||
+ | copy(); | ||
+ | } | ||
+ | else if(choice==6) | ||
+ | { | ||
+ | copyList(); | ||
+ | } | ||
+ | if (start == NULL) | ||
+ | { | ||
+ | choice=0; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | choice=makeChoice(); | ||
+ | } | ||
+ | system(" | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | unsigned int makeChoice() | ||
+ | { | ||
+ | unsigned int choice=-1; | ||
+ | while ((choice < 0) || (choice > 6)) | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | if ((choice < 0) || (choice > 6)) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | return(choice); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===dlllib.h=== | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * Doubly Linked List Library - a doubly linked list implementation | ||
+ | * | ||
+ | * Fall2011 Data Structures | ||
+ | */ | ||
+ | |||
+ | #ifndef _DLLLIB_H | ||
+ | #define _DLLLIB_H | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Create our node structure | ||
+ | struct node { | ||
+ | int value; | ||
+ | struct node *next; | ||
+ | struct node *prev; | ||
+ | }; | ||
+ | typedef struct node Node; | ||
+ | |||
+ | int create(); | ||
+ | int append(); | ||
+ | int removeNode(); | ||
+ | int search(); | ||
+ | int insert(); | ||
+ | int deleteList(); | ||
+ | int copyList(); | ||
+ | int copy(); | ||
+ | int displayList(); | ||
+ | |||
+ | int input, input2; | ||
+ | Node *start, *end, *tmp, *tmp2; | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | ===create.c=== | ||
+ | <code c> | ||
+ | # | ||
+ | # | ||
+ | #include " | ||
+ | |||
+ | int create() | ||
+ | { | ||
+ | // initialize variables | ||
+ | start = end = tmp = tmp2 = NULL; | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | // build our list | ||
+ | while (input != -1) | ||
+ | { | ||
+ | if (start == NULL) // we have an empty list | ||
+ | { | ||
+ | start = (Node *) malloc (sizeof(Node)); | ||
+ | end = start; // only one node, start and end are same | ||
+ | start -> next = NULL; // nothing after | ||
+ | start -> prev = NULL; // nothing before | ||
+ | tmp = start; // tmp points to beginning of list | ||
+ | start -> value = input; // enter value into node | ||
+ | } | ||
+ | else // there is something in our list | ||
+ | { | ||
+ | tmp = (Node *) malloc (sizeof(Node)); | ||
+ | end -> next = tmp; // tack new node onto end of list | ||
+ | tmp -> prev = end; // new node points to current end | ||
+ | end = end -> next; // advance end to new end | ||
+ | end -> value = input; // put input in node | ||
+ | end -> next = NULL; // nothing beyond end | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===append.c=== | ||
+ | <code c> | ||
+ | # | ||
+ | # | ||
+ | #include " | ||
+ | |||
+ | int append() | ||
+ | { | ||
+ | // | ||
+ | |||
+ | tmp=start; | ||
+ | int i=0; | ||
+ | while(tmp != NULL) | ||
+ | { | ||
+ | i++; | ||
+ | tmp=tmp-> | ||
+ | } | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | tmp = (Node *) malloc (sizeof(Node)); | ||
+ | end -> next = tmp; // tack new node onto end of list | ||
+ | tmp -> prev = end; // new node points to current end | ||
+ | end = end -> next; // advance end to new end | ||
+ | end -> value = input; // put input in node | ||
+ | end -> next = NULL; // nothing beyond end | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===insert.c=== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int insert() | ||
+ | { | ||
+ | // Insert a new node | ||
+ | int i=0; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | tmp = start; | ||
+ | for(i=0; i<input; i++) //position tmp at node to insert before | ||
+ | { | ||
+ | tmp = tmp -> next; | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | if (start == NULL) // if our list is empty | ||
+ | { | ||
+ | start = (Node *) malloc (sizeof(Node)); | ||
+ | end = tmp = start; | ||
+ | start -> prev = NULL; | ||
+ | end -> prev = NULL; | ||
+ | start -> value = input2; | ||
+ | } | ||
+ | else if (start == tmp) // we're inserting before start of list | ||
+ | { | ||
+ | tmp2 = (Node *) malloc (sizeof(Node)); | ||
+ | tmp2 -> next = start; // point new node's next at start | ||
+ | start -> prev = tmp2; // point start' | ||
+ | start = start -> prev; // move start to new node | ||
+ | start -> value = input2; // put value in node | ||
+ | } | ||
+ | else // we're inserting before a node in the list | ||
+ | { | ||
+ | tmp2 = (Node *) malloc (sizeof(Node)); | ||
+ | tmp2 -> next = tmp; // point new node's next at tmp | ||
+ | tmp2 -> prev = tmp -> prev; // point new node's prev at tmp's prev | ||
+ | tmp -> prev -> next = tmp2; // tmp's previous next is new node | ||
+ | tmp -> prev = tmp2; // tmp's prev is new node | ||
+ | tmp2 -> value = input2; // put value in node | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===copy.c=== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int copy() | ||
+ | { | ||
+ | //Copy | ||
+ | int i; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | // Move tmp to exact node we wish to copy | ||
+ | tmp=start; | ||
+ | for(i=0; i<input; i++) | ||
+ | { | ||
+ | tmp = tmp -> next; | ||
+ | } | ||
+ | tmp2 = (Node *) malloc (sizeof(Node)); | ||
+ | end -> next = tmp2; // tack new node onto end of list | ||
+ | tmp2 -> prev = end; // new node points to current end | ||
+ | end = end -> next; // advance end to new end | ||
+ | end -> value = tmp-> value; // put value of copied node in node | ||
+ | end -> next = NULL; // nothing beyond end | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ===search.c=== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int search() | ||
+ | { | ||
+ | //Find if node exists (search by value) | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | tmp=start; | ||
+ | int i = 0; | ||
+ | int found=0; //if found = 0, then no node matches the search. | ||
+ | |||
+ | while (tmp != NULL) | ||
+ | { | ||
+ | if(tmp-> | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | found=1; | ||
+ | } | ||
+ | i++; | ||
+ | tmp = tmp -> next; | ||
+ | } | ||
+ | if (found == 0) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ===copyList.c=== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int copyList() | ||
+ | { | ||
+ | //Copy entire list. | ||
+ | tmp=start; | ||
+ | int i=0; | ||
+ | int copyFor; | ||
+ | while (tmp != NULL) | ||
+ | { | ||
+ | i++; | ||
+ | tmp = tmp-> | ||
+ | } | ||
+ | tmp=start; | ||
+ | for(copyFor=0; | ||
+ | { | ||
+ | tmp2 = (Node *) malloc (sizeof(Node)); | ||
+ | end -> next = tmp2; // tack new node onto end of list | ||
+ | tmp2 -> prev = end; // new node points to current end | ||
+ | end = end -> next; // advance end to new end | ||
+ | end -> value = tmp-> value; // put value of copied node in node | ||
+ | end-> | ||
+ | tmp=tmp-> | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===displayList.c=== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int displayList() | ||
+ | { | ||
+ | // Display our linked list | ||
+ | int i = 0; | ||
+ | tmp = start; | ||
+ | |||
+ | while (tmp != NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | i++; | ||
+ | tmp = tmp -> next; | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ===removeNode.c=== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int removeNode() | ||
+ | { | ||
+ | // Remove a node from the list | ||
+ | tmp = start; | ||
+ | int i=0; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | // Move tmp to exact node we wish to delete | ||
+ | for(i=0; i<input; i++) | ||
+ | { | ||
+ | tmp = tmp -> next; | ||
+ | } | ||
+ | |||
+ | if ((tmp != start) && (tmp != end)) | ||
+ | { | ||
+ | tmp -> next -> prev = tmp -> prev; | ||
+ | // The node after points to the node before tmp | ||
+ | tmp -> prev -> next = tmp -> next; | ||
+ | // Now, the now before points to the node after tmp | ||
+ | tmp -> prev = NULL; | ||
+ | tmp -> next = NULL; | ||
+ | // tmp is now disconnected from the list | ||
+ | free (tmp); // deallocate the node tmp is pointing to | ||
+ | } | ||
+ | else if (start == end) // list is only a single node | ||
+ | { | ||
+ | free(start); | ||
+ | start=end=tmp=NULL; | ||
+ | } | ||
+ | else if (tmp == start) | ||
+ | { | ||
+ | tmp -> next -> prev = NULL; | ||
+ | // The node following tmp is no longer pointing to us | ||
+ | start = start -> next; | ||
+ | // Adjust start to point to the new start of the list | ||
+ | tmp -> next = NULL; // Disconnect tmp from the list | ||
+ | free(tmp); | ||
+ | } | ||
+ | else // tmp is equal to end | ||
+ | { | ||
+ | tmp -> prev -> next = NULL; | ||
+ | // The node preceding tmp is no longer pointing to us | ||
+ | end = end -> prev; | ||
+ | // Adjust end to point to the new end of the list | ||
+ | tmp -> prev = NULL; // Disconnect tmp from the list | ||
+ | free(tmp); | ||
+ | } | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Execution===== | ||
+ | This is how to compile and execute: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | I had some problems with my node removing function. Rather than having a function that removes a list, I settled for a single node deleting function that can be run until the list is gone. Overall, this is a very important project, because it lays the foundation for the rest of the class, really. It is a big hurdle to get over, but once you do, there is a lot to be learned from going back and using this project. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | * Matt Haas | ||
+ | * Fellow classmates (simple discussion about the aspects of our personal implementations) | ||
+ | * C Programming Language O' | ||