This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:bkenne11:portfolio:libque [2011/10/18 22:41] – [Attributes] bkenne11 | user:bkenne11:portfolio:libque [2011/11/18 19:56] (current) – [Attributes] bkenne11 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for Data Structures by Brandon Kennedy during the Fall 2011 Semester. | ||
+ | |||
+ | This project was begun on 10/14/2011 and was completed on 10/ | ||
+ | =====Objectives===== | ||
+ | The purpose of this project is to pursue a better understanding of Queue' | ||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * Basic understanding of queue' | ||
+ | * Basic understanding of library creation and execution | ||
+ | * The C programming guide | ||
+ | * Matt Haas | ||
+ | =====Background===== | ||
+ | The idea of this project is to create the queue functions enqueue and dequeue, and implement them in a function. To make this project a little more fun, i will include a headerfile(that has a node struct in it) in the header file of my queue functions so that i can reference that headerfile in order to create nodes. I will also create a queue struct that contains the head and tail of the queue. | ||
+ | |||
+ | |||
+ | =====Scope===== | ||
+ | I will be implementing these queue functions to contain character data. My main function will create a list with given values, and will then prompt to either enqueue or dequeue, and prompt for a value accordingly. The main function will then print out the resulting queue and exit. | ||
+ | |||
+ | Functions used: | ||
+ | |||
+ | *Enqueue - add a node to the front | ||
+ | *Dequeue - remove a node from the end | ||
+ | =====Attributes===== | ||
+ | State and justify the attributes you'd like to receive upon successful approval and completion of this project. | ||
+ | |||
+ | * Queue attribute: this function will implement a queue | ||
+ | * Linked list: This project will implement a linked list. | ||
+ | * Doubly linked list: this project will not only implement a linked list, but a doubly linked list | ||
+ | * pointer: this project will use pointers | ||
+ | * Libraries: This project will implement non-core libraries | ||
+ | |||
+ | =====Code===== | ||
+ | ====Main==== | ||
+ | <code c> | ||
+ | |||
+ | # | ||
+ | int main() | ||
+ | { | ||
+ | char input; | ||
+ | char junk; | ||
+ | int choice; | ||
+ | int i=0; | ||
+ | |||
+ | Queue *myqueue1; | ||
+ | myqueue1 = (Queue *) malloc (sizeof(Queue)); | ||
+ | myqueue1 -> head = myqueue1 -> tail = myqueue1 -> tmp = NULL; | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | while(input != '#' | ||
+ | { | ||
+ | if(myqueue1 -> head == NULL) | ||
+ | { | ||
+ | myqueue1 -> head = (Node *) malloc (sizeof(Node)); | ||
+ | myqueue1 -> tail = myqueue1 -> head; | ||
+ | myqueue1 -> tail -> next = NULL; | ||
+ | myqueue1 -> head -> prev = NULL; | ||
+ | myqueue1 -> tmp = myqueue1 -> head; | ||
+ | myqueue1 -> head -> value = input; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | enqueue(myqueue1, | ||
+ | } | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | } | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | if(choice == 1) | ||
+ | { | ||
+ | scanf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | enqueue(myqueue1, | ||
+ | } | ||
+ | else if(choice == 2) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | myqueue1 -> tmp = myqueue1 -> head; | ||
+ | i=0; | ||
+ | while(myqueue1 -> tmp != NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | i++; | ||
+ | myqueue1 -> tmp = myqueue1 -> tmp -> next; | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====Enqueue==== | ||
+ | <code c> | ||
+ | /* | ||
+ | This is a simple function to enqueue a node to a queue. | ||
+ | Accepts: A que struct containing the head, tail and a tmp node of the queue, | ||
+ | Returns: nothing | ||
+ | Requires the " | ||
+ | */ | ||
+ | # | ||
+ | |||
+ | void enqueue(Queue *que, char val) | ||
+ | { | ||
+ | Node *tmp1; | ||
+ | tmp1 = (Node *) malloc (sizeof(Node)); | ||
+ | tmp1 -> value = val; | ||
+ | que -> head -> prev = tmp1; | ||
+ | tmp1 -> next = que -> head; | ||
+ | que -> head = que -> head -> prev; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====Dequeue==== | ||
+ | <code c> | ||
+ | /* | ||
+ | Deueue is a simple program to dequeue a node from a list. | ||
+ | Node *dequeue(Queue *) | ||
+ | Accepts: a queue struct | ||
+ | Returns: the dequeued node | ||
+ | */ | ||
+ | # | ||
+ | |||
+ | Node *dequeue(Queue *que) | ||
+ | { | ||
+ | que -> tmp = que -> tail; | ||
+ | if(que -> tail != que -> head) | ||
+ | { | ||
+ | que -> tail = que -> tail -> prev; | ||
+ | que -> tmp -> prev = NULL; | ||
+ | que -> tail -> next = NULL; | ||
+ | } | ||
+ | return (que -> tmp); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====Header File==== | ||
+ | <code c> | ||
+ | #ifndef QUE_LIST_H | ||
+ | #define QUE_LIST_H | ||
+ | # | ||
+ | |||
+ | struct queue{ | ||
+ | struct node *head; | ||
+ | struct node *tail; | ||
+ | struct node *tmp; | ||
+ | }; | ||
+ | typedef struct queue Queue; | ||
+ | |||
+ | Node *dequeue(Queue *); | ||
+ | void enqueue(Queue *, char); | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | =====Execution===== | ||
+ | Again, if there is associated code with the project, and you haven' | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | Enter a character, (# to exit): f | ||
+ | Enter a character, (# to exit): 4 | ||
+ | Enter a character, (# to exit): @ | ||
+ | Enter a character, (# to exit): & | ||
+ | Enter a character, (# to exit): 7 | ||
+ | Enter a character, (# to exit): j | ||
+ | Enter a character, (# to exit): ; | ||
+ | Enter a character, (# to exit): # | ||
+ | To enqueue a node, enter 1 | ||
+ | To dequeue a node, enter 2 | ||
+ | : 1 | ||
+ | Enter a character, (# to exit): e | ||
+ | [0] value: e | ||
+ | [1] value: ; | ||
+ | [2] value: j | ||
+ | [3] value: 7 | ||
+ | [4] value: & | ||
+ | [5] value: @ | ||
+ | [6] value: 4 | ||
+ | [7] value: f | ||
+ | lab46: | ||
+ | Enter a character, (# to exit): f | ||
+ | Enter a character, (# to exit): 5 | ||
+ | Enter a character, (# to exit): @ | ||
+ | Enter a character, (# to exit): 5 | ||
+ | Enter a character, (# to exit): b | ||
+ | Enter a character, (# to exit): # | ||
+ | To enqueue a node, enter 1 | ||
+ | To dequeue a node, enter 2 | ||
+ | : 2 | ||
+ | You removed node memory address [0x2433030] | ||
+ | [0] value: b | ||
+ | [1] value: 5 | ||
+ | [2] value: @ | ||
+ | [3] value: 5 | ||
+ | lab46: | ||
+ | </ | ||
+ | =====Reflection===== | ||
+ | |||
+ | Though this project did not make a substantial impact, i still was able to take away a few key things. Queue' | ||
+ | =====References===== | ||
+ | No references were needed. |