User Tools

Site Tools


user:kkrauss1:portfolio:queue_library

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
user:kkrauss1:portfolio:queue_library [2011/12/12 20:54] – [Systems programming] kkrauss1user:kkrauss1:portfolio:queue_library [2011/12/12 20:55] (current) – [Code] kkrauss1
Line 1: Line 1:
 +======Project: Queue Library Implementation======
 +
 +A project for C/C++, Data Structures, and System Programming  by Karl Krauss during the SEMESTER YEAR.
 +
 +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/perform this project, the listed resources/experiences need to be consulted/achieved:
 +
 +  * understand functions
 +  * understand pointers
 +  * understand structs
 +  * understand malloc()
 +
 +=====Background=====
 +
 +This was the next evolution of the doubly linked list and stack library.  This is the queue library, taking what I learned from implementing the dl library this was much simpler.
 +
 +=====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>
 +#ifndef QUEUE_H
 +#define      QUEUE_H
 +
 +
 +
 +
 +
 +struct node { //predefined structure with one variable (
 +
 + 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.  *end is end of queue start is 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
 +</code>
 +This is the code:
 +<code c>
 +// This is an attempt at writing a queue library for a single variable that will include enqueue(),dequeue(), peek(),
 +// printList()
 +
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include "queue.h"
 +
 +Queue *enqueue(Queue *myQueue, int input)
 +{
 + if(myQueue->start == NULL) // this is to test if the list is gempty
 + {
 + myQueue->start = (Node *)malloc(sizeof(Node));
 + myQueue->end = myQueue->start; // with only one node end and start are the same
 + myQueue->start->next = NULL; //no nodes after start(no next)
 + myQueue->start->prev = NULL; //no nodes before start(no before)
 + myQueue->start->value = input; //value is now stored in first node
 + }
 + else // list is not empty so now will will create a new node and append
 + {
 + Node *tmp = (Node *)malloc(sizeof(Node));
 + myQueue->end->next = tmp; // end's next now points to tmp instead of null, so adding a new node to end of list
 + tmp->prev = myQueue->end; //new nodes prev now points to current end
 + myQueue->end = myQueue->end->next; // moves end to the true end of the list.
 + myQueue->end->value = input; // store input into new node(end)
 + myQueue->end->next = NULL;// nothing beyond the end of the list
 + }
 +
 + return myQueue;
 +}
 +
 +Node *dequeue(Queue *myQueue)
 +{
 + Node *dequeuedVal = NULL;
 + if (myQueue->start != NULL)//test if queue is empty
 + {
 + dequeuedVal = myQueue->start;// setting poppedVal equal to the current "top" of the stack
 + myQueue->start = myQueue->start->next;// changing the top of the list to the previous node
 + if(myQueue->start != NULL)
 + {
 + myQueue->start->prev = NULL;
 + }
 + /*else 
 + /{
 + myStack->end->next = NULL; //disconnecting the old top from the list
 + }*/
 + }
 +
 +
 + return dequeuedVal;
 +}
 +</code>
 +
 +=====Reflection=====
 +  *As with doubly linked lists,  I really learned a lot.  This library was written quite some time ago and the best part is when I came back to it, I had learned enough to realize I could vastly improve upon it. Something I will possibly do in the future.