ECE 103 Engineering Programming Chapter 62 Stack Implementation

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CS261 Data Structures Linked Lists - Introduction.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures Using C++ 2E
Lists, Stacks and Queues in C
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
CC 215 Data Structures Stack ADT
Data Structures and Algorithms
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Stack and Queue APURBO DATTA.
Data Structures – Week #3
CSCE 3110 Data Structures & Algorithm Analysis
Pointers and Dynamic Variables
Pointers and Linked Lists
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Abstract Data Types (ADTs)
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Chapter 16-2 Linked Structures
Circular Buffers, Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Object Oriented Programming COP3330 / CGS5409
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Programming Linked Lists.
Data Structures and Algorithms
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 56 Runtime Errors
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks Data structure Elements added, removed from one end only
Pointers & Dynamic Data Structures
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Stacks CS-240 Dick Steflik.
ECE 103 Engineering Programming Chapter 63 Queue Implementation
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack.
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Data Structures – Week #3
Structures and List Processing
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

ECE 103 Engineering Programming Chapter 62 Stack Implementation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Array – Version 1 Array – Version 2 Pointer Version

Stack : Array – Version 1 A stack can be implemented as an array of structures. A separate “stack index” variable determines the index of the stack’s top element. A B C top D Stack “push” operation “pop” operation 2

Define structure of stack element struct element_def { int val; /* Element value */ }; typedef struct element_def ELEMENT; typedef enum { FALSE, TRUE } boolean; typedef enum { E_OK, E_STK_EMPTY, E_STK_FULL } e_stat; 3

Create and initialize the stack #define MAX_STACK_SIZE 5 int main (void) { ELEMENT stack[MAX_STACK_SIZE]; /* Stack as raw array */ int stk_idx = -1; /* Top-of-stack index */ int v1 = 7, v2; /* Stack value */ /* Perform some stack operations */ if (push(stack, &stk_idx, v1) == E_STK_FULL) printf("Stack is full.\n"); if (!is_empty(stk_idx)) pop(stack, &stk_idx, &v2) return 0; } stk_idx -1 4 {?} 3 2 1 stack 4

Check if the stack is empty boolean is_empty (const int stk_idx) { return (stk_idx == -1) ? TRUE : FALSE; } Pre-State: stack empty Check if stack empty stk_idx -1 stk_idx -1 returns 1 4 {?} 3 2 1 stack 4 {?} 3 2 1 stack 5

Push a value on top of the stack e_stat push (ELEMENT stack[], int *stk_idx, int value) { if (*stk_idx >= MAX_STACK_SIZE-1) return E_STK_FULL; (*stk_idx)++; /* Update top-of-stack index */ stack[*stk_idx].val = value; /* Push value */ return E_OK; } stk_idx→ -1 value 7 stk_idx→ value 7 4 {?} 3 2 1 stack→ Pre-State: empty stack Want to push 7 on stack 4 {?} 3 2 1 stack→ {7} 6

Pop a value off the top of the stack e_stat pop (ELEMENT stack[], int *stk_idx, int *value) { if (is_empty(*stk_idx)) return E_STK_EMPTY; *value = stack[*stk_idx].val; /* Pop value */ (*stk_idx)--; /* Update top-of-stack index */ return E_OK; } stk_idx→ value→ ? stk_idx→ -1 value→ 7 4 {?} 3 2 1 stack→ {7} Pre-State: 7 on stack Want to pop 7 off stack 4 {?} 3 2 1 stack→ {7} 7

Retrieve value at the top of the stack e_stat peek (const ELEMENT stack[], int const stk_idx, int *value) { if (is_empty(stk_idx)) return E_STK_EMPTY; *value = stack[stk_idx].val; /* Retrieve value */ return E_OK; } Note: The stack is not changed. The top-of-stack value is retrieved but not popped. 8

Stack : Array – Version 2 In this version, the stack array and the stack index variable are stored together in a structure. This helps to “hide” some of the internal workings of the stack implementation. A B C top D Stack “push” operation “pop” operation 9

Define structure of stack element and stack #define MAX_STACK_SIZE 5 /* Stack element */ struct element_def { int val; /* Element value */ }; typedef struct element_def ELEMENT; /* Stack itself */ struct stack_def int n; /* Top-of-stack index */ ELEMENT A[MAX_STACK_SIZE]; /* Stack array */ typedef struct stack_def STACK; 10

Create the stack int main (void) { STACK stack; /* Stack as array within structure */ int v1 = 7, v2; /* Stack value */ init_stack(&stack); /* Initialize stack */ /* Perform some stack operations */ if (push(&stack, v1) == E_STK_FULL) printf("Stack is full.\n"); if (!is_empty(&stack)) pop(&stack, &v2) return 0; } stack→ n -1 4 {?} 3 2 1 A 11

Initialize the stack void init_stack (STACK *stack) { stack->n = -1; } stack→ n {?} stack→ n -1 4 {?} 3 2 1 A 4 {?} 3 2 1 A 12

Check if the stack is empty boolean is_empty (const STACK *stack) { return (stack->n == -1) ? TRUE : FALSE; } Pre-State: empty stack Check if stack empty stack→ n -1 stack→ n -1 returns 1 4 {?} 3 2 1 A 4 {?} 3 2 1 A 13

Push a value on top of the stack e_stat push (STACK *stack, const int value) { if (stack->n >= MAX_STACK_SIZE-1) return E_STK_FULL; stack->n++; /* Update top-of-stack index */ stack->A[stack->n].val = value; /* Push value */ return E_OK; } stack→ n -1 value 7 stack→ n value 7 4 {?} 3 2 1 A Pre-State: empty stack Want to push 7 on stack 4 {?} 3 2 1 A {7} 14

Pop a value off the top of the stack e_stat pop (STACK *stack, int *value) { if (is_empty(stack)) return E_STK_EMPTY; *value = stack->A[stack->n].val; /* Pop value */ stack->n--; /* Update top-of-stack index */ return E_OK; } stack→ n value→ ? stack→ n -1 value→ 7 4 {?} 3 2 1 A {7} Pre-State: 7 on stack Want to pop 7 off stack 4 {?} 3 2 1 A {7} 15

Retrieve value at the top of the stack e_stat peek (const STACK *stack, int *value) { if (is_empty(stack)) return E_STK_EMPTY; *value = stack->A[stack->n].val; /* Retrieve value */ return E_OK; } Note: The stack is not changed. The top-of-stack value is retrieved but not popped. 16

Stack : Pointer Version In this version, the stack is implemented as a linked list using pointers. Nodes are created at runtime using malloc(). A B C top D Stack “push” operation “pop” operation 17

Define structure of stack element struct stack_element { int val; /* Element value */ struct stack_element *next; /* Pointer to next element */ }; typedef struct stack_element stack_e; typedef enum { FALSE, TRUE } boolean; typedef enum { E_OK, E_STK_EMPTY, E_STK_FULL } e_stat; 18

Create the stack int main (void) { stack_e *tos = NULL; /* Top-of-stack pointer */ int v1 = 7, v2; /* Element value */ if (push(&tos, v1) == E_STK_FULL) printf("Stack is full.\n"); if (push(&tos, 42) == E_STK_FULL) if (pop(&tos, &v2) == E_STK_EMPTY) printf("Stack is empty.\n"); return 0; } 19

After initialization, the stack now looks like this: tos NULL 20

Check if the stack is empty boolean is_empty (const stack_e *top) { return (top == NULL) ? TRUE : FALSE; } 21

Push a value on top of the stack e_stat push (stack_e **top, const int value) { stack_e *new_element; /* Pointer to new element */ /* Dynamically create new element */ if ( (new_element = (stack_e *) malloc(sizeof(stack_e))) == NULL ) return E_STK_FULL; new_element->val = value; /* Push value */ new_element->next = *top; /* Create link to current top-of-stack */ *top = new_element; /* Update top-of-stack pointer */ return E_OK; } 22

e_stat push (stack_e **top, const int value) ← called with push(&tos, v1) where v1=7 NULL value 7 stack_e *new_element; ← create a pointer to a stack element new_element ? new_element = (stack_e *) malloc(sizeof(stack_e)) ← dynamically allocate storage for one element new_element val ? next new_element->val = value; ← val is 7 new_element->next = *top; ← next points to whatever *top (i.e. tos) points to new_element val 7 next NULL *top = new_element; ← tos now points to the newly allocated element. top tos val 7 next NULL top and new_element disappear once the function is done. The dynamically allocated element still exists and is pointed to by tos. new_element 23

By performing one push operation, the stack now looks like this: tos val 7 next NULL 24

e_stat push (stack_e **top, const int value) ← called with push(&tos, 42) 7 next NULL value 42 stack_e *new_element; ← create a pointer to a stack element new_element ? new_element = (stack_e *) malloc(sizeof(stack_e)) ← dynamically allocate storage for one element new_element val ? next new_element->val = value; ← val is 42 new_element->next = *top; ← next points to whatever *top (i.e. tos) points to new_element val 42 next val 7 next NULL *top = new_element; ← tos now points to the newly allocated element. top tos val 42 next val 7 next NULL new_element 25

By performing another push operation, the stack now looks like this: tos val 42 next val 7 next NULL 26

Pop a value off the top of the stack e_stat pop (stack_e **top, int *value) { stack_e *p; /* Work pointer to current top-of-stack */ if (is_empty(*top)) return E_STK_EMPTY; *value = (*top)->val; /* Pop value */ p = *top; /* Remember current top-of-stack */ *top = (*top)->next; /* Update top-of-stack pointer */ free(p); /* Deallocate old top-of-stack element */ return E_OK; } 27

28 e_stat pop (stack_e **top, int *value) ← called with pop(&tos, &v2) 42 next val 7 next NULL value v2 ? *value = (*top)->val; ← retrieve the value on top of the stack value v2 42 p = *top; ← make p point to the element on the top of the stack p val 42 next val 7 next NULL top tos *top = (*top)->next; ← reset the top-of-stack pointer p val 42 next val 7 next NULL top tos free(p); deallocate the former top-of-stack memory block p val 7 next NULL top tos 28

By performing one pop operation, the stack now looks like this: tos val 7 next NULL 29

Retrieve value at the top of the stack e_stat peek (const stack_e *top, int *value) { if (is_empty(top)) return E_STK_EMPTY; *value = top->val; /* Retrieve value */ return E_OK; } Note: The stack is not changed. The top-of-stack value is retrieved but not popped. 30