This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:kkrauss1:portfolio:stack_library [2011/12/12 20:25] – [Code] kkrauss1 | user:kkrauss1:portfolio:stack_library [2011/12/12 20:27] (current) – [Code] kkrauss1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | |||
+ | A project for C/C++, Data Structures, and System Programming by Karl Krauss for fall 2011. | ||
+ | |||
+ | Approximately 3 hours including taking the time to full grasp all concepts that were used, this was much simpler than doubly linked list as the " | ||
+ | |||
+ | =====Objectives===== | ||
+ | The purpose of this project was to create a library for stacks. | ||
+ | |||
+ | =====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 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 stack {// predefined structure for use of returns in functions allow for easy access to stack. | ||
+ | |||
+ | Node *start; | ||
+ | |||
+ | Node *end; | ||
+ | |||
+ | }; | ||
+ | |||
+ | typedef struct stack Stack; | ||
+ | |||
+ | |||
+ | |||
+ | Stack *push(Stack *myStack, int input); | ||
+ | |||
+ | Node *pop(Stack *myStack); | ||
+ | |||
+ | Node *peek(Stack *myStack); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | //minimum variables needed in main: int input, Node *tmp, Node *tmp2, List *myList. | ||
+ | |||
+ | |||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | This is the code: | ||
+ | <code c> | ||
+ | typedef struct list List; | ||
+ | |||
+ | in List: * start will be the stack and *end the top of the stack. | ||
+ | |||
+ | */ | ||
+ | List *push(List *myList, Node *tmp, int input); | ||
+ | List *pop(List *myList, Node *tmp, int i); | ||
+ | void peek(List *myList); | ||
+ | int main() | ||
+ | { | ||
+ | |||
+ | List *myList; | ||
+ | //Node *stack, *top | ||
+ | Node *tmp; | ||
+ | int i=0, input; | ||
+ | |||
+ | //tmp = stack = top = NULL; | ||
+ | myList=(List *)malloc(sizeof(List)); | ||
+ | myList-> | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | scanf(" | ||
+ | |||
+ | while (input != -1) | ||
+ | { | ||
+ | push(myList, | ||
+ | i++; | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | } | ||
+ | |||
+ | peek(myList); | ||
+ | getchar(); | ||
+ | getchar(); | ||
+ | while (i != 0)//tmp = (Node *)pop(myList, | ||
+ | { | ||
+ | |||
+ | myList=pop(myList, | ||
+ | i--; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | List *push(List *myList, Node *tmp, int input)// pushing a new value onto the top of the stack | ||
+ | { | ||
+ | myList = newNode(myList, | ||
+ | |||
+ | return myList; | ||
+ | } | ||
+ | |||
+ | List *pop(List *myList, Node *tmp, int i)// popping value off the top of the stack | ||
+ | { | ||
+ | |||
+ | int x; | ||
+ | tmp = myList-> | ||
+ | |||
+ | for(x=1; x<i; x++) | ||
+ | { | ||
+ | tmp = tmp-> | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | deleteNode(myList, | ||
+ | |||
+ | return myList; | ||
+ | } | ||
+ | |||
+ | void peek(List *myList)// checking to see whats on the top of the stack | ||
+ | { | ||
+ | if(myList-> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | *As with doubly linked lists, | ||