IT 152 Data Structures and Algorithms Tonga Institute of Higher Education
What are Data Structures and Algorithms? Data Structure – An arrangement of data in a computer’s memory Examples: arrays, linked lists, stacks, binary trees and hash tables. Algorithms – A series of steps used to manipulate the data in these structures Examples: Searching, Sorting, Inserting
Why is this important? The correct choice of data structures and algorithms allows major improvements in program efficiency Goals Works correctly Easy implementation Fast Efficient with memory Scalable
Increments and Decrements Increments and Decrements are often used when analyzing data structures and algorithms Special operators are often used for increments and decrements. We must understand how to use these to succeed in this class
Increment / Decrement Operator by Itself Increment Operator: ++ Adds 1 to the variable Example: int x = 4; x++; System.out.println(x);//5 is printed out Decrement Operator: -- Subtracts 1 from the variable Example: int x = 4; --x; System.out.println(x);//3 is printed out When an increment operator or decrement operator is by itself, it doesn’t matter whether it is before or after the variable
Pre-Increment/Decrement and Post-Increment/Decrement - 1 Pre-Increment / Pre-Decrement: Operator is placed before the variable --x ++x Post-Increment / Post-Decrement: Operator is placed after the variable x-- x++ When you see something like this, you must think about whether the ++ or -- is before or after Z = x++; Z = x--; Z = ++x; Z = --x;
When you see something like this, you must think about whether the ++ or -- is before or after Z = x++; Z = x--; Z = ++x; Z = --x; Example: int x = 4; int z; z = ++x; //The value of z is 5 Example: int y = 4; int z; z = y++; //The value of z is 4 The pluses and minus are after. So, put the original value of x in Z Pre-Increment/Decrement and Post-Increment/Decrement - 2 The pluses and minus are before. So, put the incremented/decremented value of x in Z
Plus Equals Operator The += operator is the plus equals operator Example x += 1 Is the same as x = x + 1
Definitions Key – A piece of data used to uniquely identify a record Example: Student ID, Customer ID Pointer / Reference - A variable that contains the address of a location in memory. Example: Record Pointer