What is Data Structures? · What is Data Structures? Example:library is composed of elements...

Preview:

Citation preview

CHAPTER 0: INTRODUTION

What is Data Structures? A data structure is defined by

(1) the logical arrangement of data elements, combined with

(2) the set of operations we need to access the elements.

Atomic Variables

Atomic variables can only store one value at a time.int num;

float s;

A value stored in an atomic variable cannot be subdivided.

What is Data Structures?

Example:library is composed of elements (books)

Accessing a particular book requires knowledge of the arrangement of the books

Users access books only through the librarian

the logical arrangement of data elements, combined withthe set of operations we need to access the elements.

Basic Data Structures

Structures include linked lists

Stack, Queue

binary trees

…and others

Need for Data Structures

Data structures organize data ⇒ more efficient programs.

More powerful computers ⇒ more complex applications.

More complex applications demand more calculations.

Organizing Data

Any organization for a collection of records that can be searched, processed in any order, or modified.

The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.

Efficiency

A solution is said to be efficient if it solves the problem within its resource constraints.

Space Time

The cost of a solution is the amount of resources that the solution consumes.

Selecting a Data Structure

Select a data structure as follows:1. Analyze the problem to determine the resource constraints a

solution must meet.2. Determine the basic operations that must be supported. Quantify

the resource constraints for each operation.3. Select the data structure that best meets these requirements.

Data Structure Philosophy

Each data structure has costs and benefits.

Rarely is one data structure better than another in all situations.

A data structure requires: space for each data item it stores,

time to perform each basic operation,

programming effort.

What is Algorithm?

Algorithm: A computable set of steps to achieve a desired result

Ralationship to Data Structure Example: Find an element

1 2 3 4 5 6 7

1

2

3

4

5

6

7

C LANGUAGE

1. ADDRESS2. POINTERS3. ARRAYS4. ADDRESS OF EACH ELEMENT IN AN ARRAY5. ACCESSING & MANIPULATING AN ARRAY USING

POINTERS6. ANOTHER CASE OF MANIPULATING AN ARRAY

USING POINTERS7. TWO-DIMENSIONAL ARRAY8. POINTER ARRAYS9. STRUCTURES10.STRUCTURE POINTERS

The C++ Preprocessor

C++ is based on C, which was written in the early 1970s

Any command starting with a # in the first column is not a C/C++ statement, but rather a preprocessor statement The preprocessor performed very basic text-based (or

lexical) substitutions

The output is sent to the compiler

The C++ Preprocessor

The sequence is:

file (filename.cpp) → preprocessor → compiler (g++)

Note, this is done automatically by the compiler: no additional steps are necessary

At the top of any C++ program, you will see one or more directives starting with a #, e.g.,

#include <iostream>

Libraries

You will write your code in a file such as Single_list.h where you will implement a data structure

This file will be included in our tester file Single_list_tester.h with a statement such as:

#include "Single_list.h"

The file Single_list_int_driver.cpp then includes the tester file:

#include "Single_list_tester.h"

Libraries

You will note the difference:

#include <iostream>

#include "Single_list.h"

The first looks for a file iostream.h which is shipped with the compiler (the standard library)

The second looks in the current directory

Libraries

In this class, you will put all code in the header file

This is not normal practice: Usually the header (.h) file only contains declarations

The definitions (the actual implementations) are stored in a related file and compiled into an object file

The C++ Preprocessor

The C++ Preprocessor

This class definition contains only the signatures (or prototypes) of the operations

The actual member function definitions may be defined elsewhere, either in: The same file, or

Another file which is compiled into an object file

We will use the first method

The File as the Unit of Compilation

In C/C++, the file is the base unit of compilation: Any .cpp file may be compiled into object code

Only files containing an int main() function can be compiled into an executable

The signature of main is:int main () {

// does some stuff

return 0;

}

The operating system is expecting a return value Usually 0

The File as the Unit of Compilation

This file (example.cpp) contains two functions

#include<iostream>

int sqr( int n ) { // Function declaration and definition

return n*n;

}

int main() {

cout << "The square of 3 is " << sqr(3) << endl;

return 0;

}

Operators

Operators have similar functionality for built-in datatypes:

Assignment =

Arithmetic + - * / %

+= -= *= /= %=

Autoincrement ++

Autodecrement --

Logical && || !

Relational == != < <= >= >

Comments /* */

// to end of line

Bitwise & | ^ ~

&= |= ^=

Bit shifting << >>

<<= >>=

C LANGUAGE

1. ADDRESSFor every variable there are two attributes: address and

value

cout << "Value of 'y' is: " << y << "\n"; cout << "Address of 'y' is: " << &y << "\n\n";

In memory with address 3: value: 45. In memory with address 2: value "Dave"

C LANGUAGE

2. POINTERS1. is a variable whose value is also an address.

2. A pointer to an integer is a variable that can store the address of that integer

ia: value of variable

&ia: address of ia

*ia means you are printing the value at the location specified by ia

C LANGUAGE

int i; //Aint * ia; //Bcout<<"The address of i "<< &i << " value="<<i <<endl; cout<<"The address of ia " << &ia << " value = " << ia<< endl;

i = 10; //Cia = &i; //D

cout<<"after assigning value:"<<endl;cout<<"The address of i "<< &i << " value="<<i <<endl; cout<<"The address of ia " << &ia << " value = " << ia<< " point to: "<< *ia;

C LANGUAGE

Points to Remember

• Pointers give a facility to access the value of a variable indirectly.

• You can define a pointer by including a * before the name of the variable.

• You can get the address where a variable is stored by using &.

C LANGUAGE

3. ARRAYS1. An array is a data structure 2. used to process multiple elements with the

same data type when a number of such elements are known.

3. An array is a composite data structure; that means it had to be constructed from basic data types such as array integers.

1. int a[5]; 2. for(int i = 0;i<5;i++)

1. {a[i]=i; }

What is Array Name?

‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.

For example, if we have the code

int a, b;

then we can write

b = 2;a = b;a = 5;

But we cannot write

2 = a;

Array Layout

x[1]

x[2]

x[3]

x[4]

x[5]

x[0]

Array cells are contiguous in computer memory

The memory can be thought of as an array

C LANGUAGE

4. ADDRESS OF EACH ELEMENT IN AN ARRAYEach element of the array has a memory address.

void printdetail(int a[]){

for(int i = 0;i<5;i++){

cout<< "value in array “<< a[i] <<“ at address: “ << &a[i]);}

C LANGUAGE 5. ACCESSING & MANIPULATING AN ARRAY USING

POINTERS

You can access an array element by using a pointer.

If an array stores integers->use a pointer to integer to access array elements.

C LANGUAGE

6. ANOTHER CASE OF MANIPULATING AN ARRAY USING POINTERSThe array limit is a pointer constant : cannot change its

value in the program.

int a[5]; int *b;

a=b; //error

b=a; //OK

It works correctly even using a++ ???

C LANGUAGE 7. TWO-DIMENSIONAL ARRAY

int a[3][2];

C LANGUAGE 8. POINTER ARRAYS

You can define a pointer array (similarly to an array of integers).

In the pointer array, the array elements store the pointer that points to integer values.

C LANGUAGE

9. STRUCTURES Structures are used when

you want to process data of multiple data types

But you still want to refer to the data as a single entity

Access data: structurename.membername

C LANGUAGE

10. STRUCTURE POINTERSProcess the structure using a structure pointer

FUNCTION & RECURSION

1. FUNCTION 2. THE CONCEPT OF STACK 3. THE SEQUENCE OF EXECUTION DURING A

FUNCTION CALL 4. PARAMETER PASSING 5. CALL BY REFERENCE 6. RESOLVING VARIABLE REFERENCES 7. RECURSION 8. STACK OVERHEADS IN RECURSION 9. WRITING A RECURSIVE FUNCTION 10. TYPES OF RECURSION

FUNCTION & RECURSION

1. FUNCTION provide modularity to the software

divide complex tasks into small manageable tasks

avoid duplication of work

FUNCTION & RECURSION

2. THE CONCEPT OF STACK A stack is memory in which values are stored and

retrieved in "last in first out" manner by using operations called push and pop.

FUNCTION & RECURSION

3. THE SEQUENCE OF EXECUTION DURING A FUNCTION CALLWhen the function is called, the current

execution is temporarily stopped and the control goes to the called function. After the call, the execution resumes from the point at which the execution is stopped.

To get the exact point at which execution is resumed, the address of the next instruction is stored in the stack. When the function call completes, the address at the top of the stack is taken.

FUNCTION & RECURSION

3. THE SEQUENCE OF EXECUTION DURING A FUNCTION CALL Functions or sub-programs are implemented using a

stack.

When a function is called, the address of the next instruction is pushed into the stack.

When the function is finished, the address for execution is taken by using the pop operation.

FUNCTION & RECURSION

3. THE SEQUENCE OF EXECUTION DURING A FUNCTION CALL

Result:?

FUNCTION & RECURSION

4. PARAMETER * REFERENCE PASSING passing by value

the value before and after the call remains the same

passing by reference changed value after the function completes

FUNCTION & RECURSION

6. RESOLVING VARIABLE REFERENCES

When a variable can be resolved by using multiple references, the local definition is given more preference

FUNCTION & RECURSION

7. RECURSION A method of programming

whereby a function directly or indirectly calls itself

Problems: stop recursion?

FUNCTION & RECURSION

7. RECURSION

FUNCTION & RECURSION

7. RECURSION: Hanoi tower

FUNCTION & RECURSION 7. RECURSION

FUNCTION & RECURSION

8. STACK OVERHEADS IN RECURSION two important results: the depth of recursion and the

stack overheads in recursion

FUNCTION & RECURSION

9. WRITING A RECURSIVE FUNCTION Recursion enables us to write a program in a natural

way. The speed of a recursive program is slower because of stack overheads.

In a recursive program you have to specify recursive conditions, terminating conditions, and recursive expressions.

FUNCTION & RECURSION

10. TYPES OF RECURSION LINEAR RECURSION

TAIL RECURSION

BINARY RECURSION

EXPONENTIAL RECURSION

NESTED RECURSION

MUTUAL RECURSION

FUNCTION & RECURSION

10. TYPES OF RECURSION LINEAR RECURSION

only makes a single call to itself each time the function runs

FUNCTION & RECURSION

10. TYPES OF RECURSION TAIL RECURSION

Tail recursion is a form of linear recursion.

In tail recursion, the recursive call is the last thing the function does. Often, the value of the recursive call is returned.

FUNCTION & RECURSION

10. TYPES OF RECURSION BINARY RECURSION

Some recursive functions don't just have one call to themself, they have two (or more).

FUNCTION & RECURSION

10. TYPES OF RECURSION EXPONENTIAL RECURSION

An exponential recursive function is one that, if you were to draw out a representation of all the function calls, would have an exponential number of calls in relation to the size of the data set

(exponential meaning if there were n elements, there would be O(an) function calls where a is a positive number)

FUNCTION & RECURSION

10. TYPES OF RECURSION EXPONENTIAL RECURSION

FUNCTION & RECURSION

10. TYPES OF RECURSION NESTED RECURSION

In nested recursion, one of the arguments to the recursive function is the recursive function itself

These functions tend to grow extremely fast.

FUNCTION & RECURSION

10. TYPES OF RECURSION MUTUAL RECURSION

A recursive function doesn't necessarily need to call itself.

Some recursive functions work in pairs or even larger groups. For example, function A calls function B which calls function C which in turn calls function A.

FUNCTION & RECURSION

10. TYPES OF RECURSION MUTUAL RECURSION

Recursion Excercises

Write a program to compute: S = 1 + 2 + 3 + …n using recursion.

Write a program to print a revert number Example: input n=12345. Print out: 54321.

Write a program to print this number Example: input n=12345. Print out: 12345.

E4. Write a recursion function to calculate: S=a[0]+a[1]+…a[n-1]

A: array of integer numbers

Stack

LIFO (last in first out)

Stack

Managing Top element

Application of stack

Stack: Expression evaluation a*(b–c)/d => abc–*d/

Use of Stack

Example of use: prefix, infix, postfix expressions. Consider the expression A+B: we think of applying the operator “+” to

the operands A and B. “+” is termed a binary operator: it takes two operands. Writing the sum as A+B is called the infix form of the expression.

Prefix, Infix, Postfix

Two other ways of writing the expression are

+ A B prefixA B + postfix

The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands.

Prefix, Infix, Postfix

Consider the infix expressionA + B * C

We “know” that multiplication is done before addition.

The expression is interpreted as A + ( B * C )

Multiplication has precedence over addition.

Prefix, Infix, Postfix

Conversion to postfix

A + ( B * C ) infix form

Prefix, Infix, Postfix

Conversion to postfix

A + ( B * C ) infix form

A + ( B C * ) convert multiplication

Prefix, Infix, Postfix

Conversion to postfix

A + ( B * C ) infix form

A + ( B C * ) convert multiplication

A ( B C * ) + convert addition

Prefix, Infix, Postfix

Conversion to postfix

A + ( B * C ) infix form

A + ( B C * ) convert multiplication

A ( B C * ) + convert addition

A B C * + postfix form

Prefix, Infix, Postfix

Conversion to postfix

(A + B ) * C infix form

Prefix, Infix, Postfix

Conversion to postfix

(A + B ) * C infix form

( A B + ) * C convert addition

Prefix, Infix, Postfix

Conversion to postfix

(A + B ) * C infix form

( A B + ) * C convert addition

( A B + ) C * convert multiplication

Prefix, Infix, Postfix

Conversion to postfix

(A + B ) * C infix form

( A B + ) * C convert addition

( A B + ) C * convert multiplication

A B + C * postfix form

Converting Infix to Postfix

Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’.

The postfix versions are ‘ABC*+’ and ‘AB+C*’.

The order of operands in postfix is the same as the infix.

In scanning from left to right, the operand ‘A’ can be inserted into postfix expression.

Converting Infix to Postfix

The ‘+’ cannot be inserted until its second operand has been scanned and inserted.

The ‘+’ has to be stored away until its proper position is found. When ‘B’ is seen, it is immediately inserted into the postfix expression. Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot

because * has precedence.

Converting Infix to Postfix

Example: A + B * Csymb postfix stackA A

Converting Infix to Postfix

Example: A + B * Csymb postfix stackA A+ A +

Converting Infix to Postfix

Example: A + B * Csymb postfix stackA A+ A +B AB +

Converting Infix to Postfix

Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *

Converting Infix to Postfix

Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *C ABC + *

Converting Infix to Postfix

Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *C ABC + *

ABC * +

Converting Infix to Postfix

Example: A + B * Csymb postfix stackA A+ A +B AB +* AB + *C ABC + *

ABC * +ABC * +

Precedence of Operators

The five binary operators are: addition, subtraction, multiplication, division and exponentiation.

The order of precedence is (highest to lowest)

Exponentiation ↑

Multiplication/division*, /

Addition/subtraction +, -

Precedence of Operators

For operators of same precedence, the left-to-right rule applies:

A+B+C means (A+B)+C.

For exponentiation, the right-to-left rule applies

A ↑ B ↑ C means A ↑ ( B ↑ C )

Infix to Postfix

Infix Postfix

A + B A B +

12 + 60 – 23 12 60 + 23 –

(A + B)*(C – D ) A B + C D – *

A ↑ B * C – D + E/F A B ↑ C*D – E F/+

Infix to Postfix

Note that the postfix form an expression does not require parenthesis.

Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second.

The postfix forms are:4+3*5 435*+ (4+3)*5 43+5*

Reverse-Polish Notation

Normally, mathematics is written using what we call in-fix notation:

(3 + 4) × 5 – 6

The operator is placed between to operands

One weakness: parentheses are required

(3 + 4) × 5 – 6 = 293 + 4 × 5 – 6= 173 + 4 × (5 – 6) = –1

(3 + 4) × (5 – 6) = –7

Reverse-Polish NotationAlternatively, we can place the operands first, followed by the operator:

(3 + 4) × 5 – 63 4 + 5 × 6 –

Parsing reads left-to-right and performs any operation on thelast two operands:

3 4 + 5 × 6 –

7 5 × 6 –

35 6 –

29

Reverse-Polish Notation

This is called reverse-Polish notation after the mathematician Jan Łukasiewicz As you will see in ECE 222, this forms the basis

of the recursive stack used on all processors

He also made significant contributions tologic and other fields Including humour…

http://www.audiovis.nac.gov.pl/http://xkcd.com/645/

Reverse-Polish NotationOther examples:

3 4 5 × + 6 –3 20 + 6 –

23 6 –17

3 4 5 6 – × +3 4 –1 × +3 –4 +

–1

Reverse-Polish Notation

Benefits: no ambiguity and no brackets are required

this is the same process used by a computer to perform computations: operands must be loaded into registers before operations

can be performed on them

reverse-Polish can be processed using stacks

Reverse-Polish Notation

The easiest way to parse reverse-Polish notation is to use an operand stack: operands are processed by pushing them onto the stack

when processing an operator: pop the last two items off the operand stack,

perform the operation, and

push the result back onto the stack

Reverse-Polish NotationEvaluate the following reverse-Polish expression using a stack:

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

Reverse-Polish Notation

Push 1 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

1

Reverse-Polish Notation

Push 1 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

21

Reverse-Polish Notation

Push 3 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

321

Reverse-Polish Notation

Pop 3 and 2 and push 2 + 3 = 5

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

51

Reverse-Polish Notation

Push 4 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

451

Reverse-Polish Notation

Push 5 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

5451

Reverse-Polish Notation

Push 6 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

65451

Reverse-Polish NotationPop 6 and 5 and push 5 × 6 = 30

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

30451

Reverse-Polish NotationPop 30 and 4 and push 4 – 30 = –26

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–2651

Reverse-Polish Notation

Push 7 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

7–2651

Reverse-Polish NotationPop 7 and –26 and push –26 × 7 = –182

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–18251

Reverse-Polish NotationPop –182 and 5 and push –182 + 5 = –177

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

–1771

Reverse-Polish Notation

Pop –177 and 1 and push 1 – (–177) = 178

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

178

Reverse-Polish Notation

Push 8 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

8178

Reverse-Polish Notation

Push 1 onto the stack

1 2 3 + 4 5 6 × – 7 × + – 8 9× +

98

178

Reverse-Polish NotationPop 9 and 8 and push 8 × 9 = 72

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

72178

Reverse-Polish NotationPop 72 and 178 and push 178 + 72 = 250

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

250

Reverse-Polish Notation

Thus

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

evaluates to the value on the top: 250

The equivalent in-fix notation is

((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))

We reduce the parentheses using order-of-operations:

1 – (2 + 3 + (4 – 5 × 6) × 7) + 8 × 9

Reverse-Polish Notation

Incidentally,

1 – 2 + 3 + 4 – 5 × 6 × 7 + 8 × 9 = –132which has the reverse-Polish notation of

1 2 – 3 + 4 + 5 6 7 × × – 8 9 × +

For comparison, the calculated expression was

1 2 3 + 4 5 6 × – 7 × + – 8 9 × +

Evaluating Postfix

Each operator in a postfix expression refers to the previous two operands.

Each time we read an operand, we push it on a stack.

When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack.

Evaluating Postfix

Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

Evaluating Postfix

Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

Evaluating Postfix

Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

Evaluating Postfix

Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

Evaluating Postfix

Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

Evaluating Postfix

Evaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

* 1 7 7 7

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

* 1 7 7 7

2 1 7 7 7,2

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

* 1 7 7 7

2 1 7 7 7,2

↑ 7 2 49 49

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

* 1 7 7 7

2 1 7 7 7,2

↑ 7 2 49 49

3 7 2 49 49,3

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

* 1 7 7 7

2 1 7 7 7,2

↑ 7 2 49 49

3 7 2 49 49,3

+ 49 3 52 52

Evaluating PostfixEvaluate 6 2 3 + - 3 8 2 / + * 2 ↑ 3 +Input op1 op2 value stack

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 6 5 1 1,3

8 6 5 1 1,3,8

2 6 5 1 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

* 1 7 7 7

2 1 7 7 7,2

↑ 7 2 49 49

3 7 2 49 49,3

+ 49 3 52 52

Queue

An Abstract Queue (Queue ADT) is an abstract data type that emphasizes specific operations: Uses a explicit linear ordering

Insertions and removals are performed individually

There are no restrictions on objects inserted into (pushed onto) the queue—that object is designated the back of the queue

The object designated as the front of the queue is the object which was in the queue the longest

The remove operation (popping from the queue) removes the current front of the queue

QUEUE

FIFO (first in first out)

QUEUE: implement using array

QueueAlso called a first-in–first-out (FIFO) data structure Graphically, we may view these operations as follows:

Queue

Alternative terms may be used for the four operations on a queue, including:

Queue

There are two exceptions associated with this abstract data structure: It is an undefined operation to call either pop or front on

an empty queue

ApplicationsThe most common application is in client-server models Multiple clients may be requesting services from one or

more servers

Some clients may have to wait while the servers are busy

Those clients are placed in a queue and serviced in the order of arrival

Grocery stores, banks, and airport security use queues

The SSH Secure Shell and SFTP are clients

Most shared computer services are servers: Web, file, ftp, database, mail, printers, WOW, etc.

Applications

For example, in downloading these presentations from the web server, those requests not currently being downloaded aremarked as “Queued”

Array ImplementationWe need to store an array: In C++, this is done by storing the address of the first entry

Type *array;

We need additional information, including: The number of objects currently in the queue and the

front and back indices

int queue_size;int ifront; // index of the front entryint iback; // index of the back entry

The capacity of the array

int array_capacity;

Back and front

Before we initialize the values, we will state that iback is the index of the most-recently pushed object

ifront is the index of the object at the front of the queue

To push, we will increment iback and place the new item at that location To make sense of this, we will initialize

iback = -1;

ifront = 0;

After the first push, we will increment iback to 0, place the pushed item at that location, and now

Back and front

Again, we must initialize the values We must allocate memory for the array and initialize the

member variables

The call to new Type[array_capacity] makes a request to the operating system for array_capacity objects

FunctionsSuppose that: The array capacity is 16

We have performed 16 pushes

We have performed 5 pops The queue size is now 11

We perform one further push

In this case, the array is not full and yet we cannot place any more objects in to the array

QUEUE: implement using array#define MAX 10void main(){

int queue[MAX];int bottom,top,count=0;bottom=top=-1;

enqueue(queue,count,top, 100 );int value;dequeue(queue,count,bottom,top,value);

}

Recommended