This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:kkrauss1:portfolio:queue_library [2011/12/12 20:45] – kkrauss1 | user:kkrauss1:portfolio:queue_library [2011/12/12 20:55] (current) – [Code] kkrauss1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | |||
+ | A project for C/C++, Data Structures, and System Programming | ||
+ | |||
+ | I spent a few hours on this. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The purpose of this project was to create a library for queues. | ||
+ | |||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * understand functions | ||
+ | * understand pointers | ||
+ | * understand structs | ||
+ | * understand malloc() | ||
+ | |||
+ | =====Background===== | ||
+ | |||
+ | This was the next evolution of the doubly linked list and stack library. | ||
+ | |||
+ | =====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 | ||
+ | |||
+ | =====Code===== | ||
+ | This is the header file: | ||
+ | <code c> | ||
+ | # | ||
+ | # | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | struct node { // | ||
+ | |||
+ | int value; | ||
+ | struct node *next; | ||
+ | struct node *prev; | ||
+ | }; | ||
+ | typedef struct node Node; | ||
+ | |||
+ | |||
+ | |||
+ | struct queue {// predefined structure for use of returns in functions allow for easy access to start. | ||
+ | Node *start; | ||
+ | Node *end; | ||
+ | }; | ||
+ | typedef struct queue | ||
+ | Queue; | ||
+ | |||
+ | |||
+ | |||
+ | Queue *enqueue(Queue *myQueue, int input); | ||
+ | Node *dequeue(Queue *myQueue); | ||
+ | Node *peek(Queue *myQueue); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | //minimum variables needed in main: int input, Node *tmp, Node *tmp2, Queue *myList. | ||
+ | |||
+ | |||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | This is the code: | ||
+ | <code c> | ||
+ | // This is an attempt at writing a queue library for a single variable that will include enqueue(), | ||
+ | // printList() | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | Queue *enqueue(Queue *myQueue, int input) | ||
+ | { | ||
+ | if(myQueue-> | ||
+ | { | ||
+ | myQueue-> | ||
+ | myQueue-> | ||
+ | myQueue-> | ||
+ | myQueue-> | ||
+ | myQueue-> | ||
+ | } | ||
+ | else // list is not empty so now will will create a new node and append | ||
+ | { | ||
+ | Node *tmp = (Node *)malloc(sizeof(Node)); | ||
+ | myQueue-> | ||
+ | tmp-> | ||
+ | myQueue-> | ||
+ | myQueue-> | ||
+ | myQueue-> | ||
+ | } | ||
+ | |||
+ | return myQueue; | ||
+ | } | ||
+ | |||
+ | Node *dequeue(Queue *myQueue) | ||
+ | { | ||
+ | Node *dequeuedVal = NULL; | ||
+ | if (myQueue-> | ||
+ | { | ||
+ | dequeuedVal = myQueue-> | ||
+ | myQueue-> | ||
+ | if(myQueue-> | ||
+ | { | ||
+ | myQueue-> | ||
+ | } | ||
+ | / | ||
+ | /{ | ||
+ | myStack-> | ||
+ | }*/ | ||
+ | } | ||
+ | |||
+ | |||
+ | return dequeuedVal; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | *As with doubly linked lists, | ||