day5 - COP 3503 Computer Science II CLASS NOTES - DAY #5...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3503 – Computer Science II CLASS NOTES - DAY #5 Introduction to Data Structures Most algorithms require a proper representation of the data in order to achieve efficiency. This representation and the operations that are allowed are called a data structure . Data structures are an important part of OOP as they allow for component reuse (recall that this is a major goal of the OO paradigm). Once the data structure has been designed and implemented, it can be used over and over again in various applications. Separation of the interface to and the implementation of a data structure is also central to the OO paradigm. The user of the data structure does not need to see the implementation of the data structure, only the operations that are available to operate on the structure. (Recall our discussions about encapsulation and information hiding in OOP.) Abstraction is another important part of the OO paradigm. This means that more thought must be given to design of the data structure because programs will need to make use of the structure without having knowledge of its implementation. All this makes the interface to the structure much “cleaner” and enhances the ability to reuse the structure. Stacks A stack is a data structure that operates on a LIFO policy – Last In, First Out. Access to the structure is restricted to the last element that was put into the stack, called the top element. Only three operations are supported by this data structure. They are push (insert operation) , pop (delete operation) , and top (combined pop followed by push – state of the stack remains unchanged by this operation – see below). Push – adds a new element to the top of the stack. Pop – removes the top element from the stack. Top – returns a copy of the top element without removing it from the stack. Note that the allowed operations do not permit access to any element in the stack other than the top element. If you wish to examine the third element in the stack, then the top two elements must be removed (popped) first in order to bring the third element to the top at which point it can be examined. Useful for reversing the order of lists of items and expression evaluation. Day 5 - 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms: (We will assume for now that our stack will be implemented using static memory. Later we will examine implementations using dynamic memory.) Assume that the stack is represented by an array with a top of stack marker with the convention that tos == -1 implies the stack is empty. int MAX = 10, stack[MAX], tos = -1; push(x) { if (tos == MAX-1) // stack is full ERROR – stack full else // stack is not full stack[++tos] = x; //increment tos prior to use and push x on the stack } pop(x) { if (tos == -1) // stack is empty ERROR – stack empty else // stack contains elements x = stack[tos -- ]; } top(x) { if (tos == -1) //stack is empty the stack is empty else //stack contains at least one element x = stack[tos]; } Stack Examples
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

day5 - COP 3503 Computer Science II CLASS NOTES - DAY #5...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online