This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:nbrimme1:portfolio:oct09 [2013/10/24 22:06] – nbrimme1 | user:nbrimme1:portfolio:oct09 [2013/10/24 22:41] (current) – nbrimme1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====October 9, 2013==== | ||
+ | Was in the hospital a few days. Matt went over " | ||
+ | - **pop()**: Removes the top element off the stack. | ||
+ | - **push()**: Adds an element to the top of the stack. | ||
+ | - **peek()**: Shows the top of the stack. It does this by poping an element from the stack then pushing the same element back onto the stack. | ||
+ | Stacks also have an important pointer, top(), that always points to the top of the stack. This is sometimes called the "Stack Pointer" | ||
+ | \\ | ||
+ | \\ | ||
+ | **pop.c** | ||
+ | <code c> | ||
+ | #include " | ||
+ | Node *pop(Stack **myStack) | ||
+ | { | ||
+ | Node *tmp = NULL; | ||
+ | |||
+ | if ((*myStack) != NULL) | ||
+ | { | ||
+ | tmp = removeNode((*myStack) -> data, (*myStack) -> data -> end); | ||
+ | (*myStack) -> top = (*myStack) -> data -> end; | ||
+ | } | ||
+ | |||
+ | return (tmp); | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | **push.c** | ||
+ | <code c> | ||
+ | #include " | ||
+ | |||
+ | Stack *push(Stack *myStack, Node *newNode) | ||
+ | { | ||
+ | if ((myStack -> size <= 0) || ((myStack -> data -> qty) < (myStack -> size))) | ||
+ | { | ||
+ | myStack -> data = append(myStack -> data, myStack -> data -> end, newNode); | ||
+ | myStack -> top = myStack -> data -> end; | ||
+ | } | ||
+ | |||
+ | return(myStack); | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | **peek.c** | ||
+ | <code c> | ||
+ | #include " | ||
+ | |||
+ | Node *peek(Stack *myStack) | ||
+ | { | ||
+ | // exercise left to the implementer | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | **stackops.c** | ||
+ | <code c> | ||
+ | #include " | ||
+ | |||
+ | Stack *mkstack(int size) | ||
+ | { | ||
+ | Stack *myStack; | ||
+ | |||
+ | myStack = (Stack *) malloc (sizeof(Stack)); | ||
+ | |||
+ | myStack -> data = mklist(); | ||
+ | myStack -> size = size; | ||
+ | myStack -> top = myStack -> data -> end; | ||
+ | |||
+ | return (myStack); | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | **stack.h** | ||
+ | <code c> | ||
+ | #ifndef _STACK_H | ||
+ | #define _STACK_H | ||
+ | |||
+ | #include " | ||
+ | |||
+ | struct stack { | ||
+ | List *data; | ||
+ | Node *top; | ||
+ | int size; | ||
+ | }; | ||
+ | typedef struct stack Stack; | ||
+ | |||
+ | Stack *mkstack(int); | ||
+ | Stack *push | ||
+ | Node *pop (Stack **); | ||
+ | Node *peek | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | **A copy of the code is also available in the /// | ||
+ | \\ | ||
+ | \\ | ||
+ | Matt also added a few new things that need to be implemented into the doubly linked list program: | ||
+ | - Add a qty variable (int) to your Linked List struct. | ||
+ | - Update linked list functions to appropriately update qty so it contains an accurate list count | ||
+ | - Add a mklist() function, in the spirit of create() function on the knowledge assessments, |