This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:tgalpin2:portfolio:stack [2011/12/16 18:29] – [Execution] tgalpin2 | user:tgalpin2:portfolio:stack [2011/12/16 23:20] (current) – [References] tgalpin2 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | |||
+ | A project for Data Structures by Tyler Galpin during the Fall 2011. | ||
+ | |||
+ | This project took about one day to complete and was completed on November 16th, 2011. | ||
+ | |||
+ | =====Objectives===== | ||
+ | The point of this project is to make a usable Stack library that can be used not only in a test implementation, | ||
+ | |||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * Understanding of linked list | ||
+ | * Understanding of doubly linked list | ||
+ | * Basic understanding of what a stack is | ||
+ | * Understanding of pointers | ||
+ | |||
+ | =====Background===== | ||
+ | As previously stated, a working Stack library is the goal. It should be able to manipulate the data in the stack however we need it to. As is such,the following functions must be in it: | ||
+ | |||
+ | * **Push**--Add a node, FILO style | ||
+ | * **Pop**-- Remove a node, FILO style | ||
+ | * **Display**-- Display stack, make note of where top and bottom is | ||
+ | |||
+ | =====Scope===== | ||
+ | The implementation is relatively straightforward. I just need it to demonstrate that the logic of a stack is present. For this, a menu is included in the main part of the program to let you choose which function you'd like to test. | ||
+ | |||
+ | =====Attributes===== | ||
+ | |||
+ | |||
+ | * linked list: The stack is based off of a linked list | ||
+ | * doubly linked list: The stack uses a doubly linked list. | ||
+ | * pointers: make up the backbone of the data structure | ||
+ | * malloc: needed in the creation of new stacks/ | ||
+ | * stack: ...Hey, this is a stack, isn't it? | ||
+ | * library: That is what is being made/used! | ||
+ | * File I/O: Used in main.c for test implementation | ||
+ | |||
+ | =====Code===== | ||
+ | |||
+ | ===stacklib.h=== | ||
+ | <code c> | ||
+ | /* | ||
+ | * Stack implementation -- header file | ||
+ | * | ||
+ | * Fall2011 Data Structures | ||
+ | */ | ||
+ | |||
+ | #ifndef _STACKLIB_H | ||
+ | #define _STACKLIB_H | ||
+ | |||
+ | // Include files | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Create our node structure | ||
+ | struct node { | ||
+ | int value; | ||
+ | struct node *next; | ||
+ | struct node *prev; | ||
+ | }; | ||
+ | typedef struct node Node; | ||
+ | |||
+ | Node *stack, *top, *tmp; | ||
+ | |||
+ | void push(int); | ||
+ | void pop(); | ||
+ | void displayStack(); | ||
+ | //int isEmpty(); (Figure this out later) | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | ===main.c=== | ||
+ | <code c> | ||
+ | /* | ||
+ | * Stack implimentation -- main | ||
+ | * | ||
+ | * Fall2011 Data Structures | ||
+ | */ | ||
+ | |||
+ | #include " | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int i, input, choice; | ||
+ | puts(" | ||
+ | scanf(" | ||
+ | while(input != -1) | ||
+ | { | ||
+ | push(input); | ||
+ | puts(" | ||
+ | scanf(" | ||
+ | } | ||
+ | puts(" | ||
+ | scanf(" | ||
+ | while (choice != 0) | ||
+ | { | ||
+ | if (choice == 1) | ||
+ | { | ||
+ | input=0; | ||
+ | puts(" | ||
+ | scanf(" | ||
+ | while(input != -1) | ||
+ | { | ||
+ | push(input); | ||
+ | puts(" | ||
+ | scanf(" | ||
+ | } | ||
+ | input=0; | ||
+ | } | ||
+ | else if (choice == 2) | ||
+ | { | ||
+ | pop(); | ||
+ | } | ||
+ | else if (choice == 3) | ||
+ | { | ||
+ | displayStack(); | ||
+ | } | ||
+ | puts(" | ||
+ | scanf(" | ||
+ | } | ||
+ | /* | ||
+ | while((tmp=pop())!=NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | free(tmp); | ||
+ | } | ||
+ | */ | ||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===push.c=== | ||
+ | <code c> | ||
+ | #include " | ||
+ | |||
+ | void push(int value) | ||
+ | { | ||
+ | if (stack==NULL) | ||
+ | { | ||
+ | stack = (Node *) malloc(sizeof(Node)); | ||
+ | top=stack; | ||
+ | stack-> | ||
+ | stack-> | ||
+ | tmp=stack; | ||
+ | stack-> | ||
+ | } | ||
+ | else if (stack == top) | ||
+ | { | ||
+ | tmp= (Node *) malloc(sizeof(Node)); | ||
+ | top-> | ||
+ | tmp-> | ||
+ | top=top-> | ||
+ | top-> | ||
+ | top-> | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | tmp=stack; | ||
+ | int i=0; | ||
+ | | ||
+ | { | ||
+ | i++; | ||
+ | tmp=tmp-> | ||
+ | | ||
+ | tmp = (Node *) malloc (sizeof(Node)); | ||
+ | top -> next = tmp; | ||
+ | tmp -> prev = top; | ||
+ | top = top -> next; | ||
+ | top -> value = value; | ||
+ | top -> next = NULL; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===pop.c=== | ||
+ | <code c> | ||
+ | # | ||
+ | |||
+ | void pop() | ||
+ | { | ||
+ | tmp=top; | ||
+ | tmp-> | ||
+ | top=top-> | ||
+ | tmp-> | ||
+ | free(tmp); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===displayStack.c=== | ||
+ | <code c> | ||
+ | # | ||
+ | |||
+ | void displayStack() | ||
+ | { | ||
+ | int n=0; | ||
+ | tmp=top; | ||
+ | printf(" | ||
+ | while(tmp != NULL) | ||
+ | { | ||
+ | printf(" | ||
+ | n++; | ||
+ | tmp=tmp-> | ||
+ | } | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | =====Execution===== | ||
+ | This is how to compile and execute: | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | I was able to complete this project with relative ease. It was an interesting project to work on, and I learned a good bit about stacks from it. It's a short jump from the stack project to a queue, and all it takes is some manipulation of the functions from here to do that. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | * Matt Haas + Class lecture | ||
+ | * Fellow classmates (simple discussion about the aspects of our personal implementations) | ||
+ | * C Programming Language O' | ||