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/17/2011.
The purpose of this project is to pursue a better understanding of Queue's and their uses. I hope to walk away with a better understanding of the structure of a queue, the functionality of enqueueing and dequeueing, and library creation/use.
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
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.
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:
State and justify the attributes you'd like to receive upon successful approval and completion of this project.
#include"quelist.h" 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("Enter a character, (# to exit): "); scanf("%c", &input); 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, input); } scanf("%c", &junk); printf("Enter a character, (# to exit): "); scanf("%c", &input); } printf("To enqueue a node, enter 1 \n"); printf("To dequeue a node, enter 2 \n: "); scanf("%d", &choice); if(choice == 1) { scanf("%c", &junk); printf("Enter a character, (# to exit): "); scanf("%c", &input); enqueue(myqueue1, input); } else if(choice == 2) { printf("You removed node memory address [0x%x]\n", dequeue(myqueue1)); } else { printf("Wrong choice, %d was not an option\n", input); exit(1); } myqueue1 -> tmp = myqueue1 -> head; i=0; while(myqueue1 -> tmp != NULL) { printf("[%d] value: %c \n", i, myqueue1 -> tmp -> value); i++; myqueue1 -> tmp = myqueue1 -> tmp -> next; } return 0; }
/* 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, and a character value. Returns: nothing Requires the "linkedlist.h" header file, which is in "quelist.h" */ #include"quelist.h" 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; }
/* Deueue is a simple program to dequeue a node from a list. Node *dequeue(Queue *) Accepts: a queue struct Returns: the dequeued node */ #include"quelist.h" 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); }
#ifndef QUE_LIST_H #define QUE_LIST_H #include"linkedlist.h" struct queue{ struct node *head; struct node *tail; struct node *tmp; }; typedef struct queue Queue; Node *dequeue(Queue *); void enqueue(Queue *, char); #endif
Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code:
lab46:~/src/DataS/Project2$ ./mainque 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:~/src/DataS/Project2$ ./mainque 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:~/src/DataS/Project2$
Though this project did not make a substantial impact, i still was able to take away a few key things. Queue's are useful because of how little is involved in they're code, they are short sweet and to the point.
No references were needed.