Data Structure Intro
Data Structure Intro
Stack is a linear data structure which works on LIFO order. So that Last In First
Out .
In stack element is always added at top of stack and also removed from top of
the stack.
● POP Operation :
Pop operation refers to the removal of an element. Again, since we
only have access to the element at the top of the stack, there’s only
one element that we can remove. We just remove the top of the
stack. We can choose to return the value of the popped element
back, it is at the choice of the programmer to implement this.
● PEEK Operation :
Peek operation allows the user to see the element on the top of the
stack. The stack is not modified in any manner in this operation.
● isEmpty Operation :
This Operation checks whether the stack is empty or not. To prevent
performing operations on an empty stack, the programmer is
required to internally maintain the size of the stack which will be
updated during push and pop operations accordingly. isEmpty()
conventionally returns a boolean value: True if size is 0, else False.
WHAT IS A QUEUE ?
A queue is a collection of entities that are
maintained in a sequence and can be modified
by the addition of entities at one end of the
sequence and the removal of entities from the
other end of the sequence.
● Queue is also a linear data structure which works onFIFO order.
So that First In First Out .
● Sequential execution
○ Statements executed in order
● Transfer of control
○ Next statement executed not next one in
sequence 19
○ Selection structures
○ Repetition structures
● Flowchart
○ Graphical representation of an algorithm
○ Oval symbol 20
● Selection structure
○ Choose among alternative courses of action
○ Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
Print “Passed”
if ( grade >= 60 )
cout << "Passed";
● Diamond symbol (decision symbol)
○ Indicates decision is to be made 22
false
if/else Selection Structure
● if
○ Performs action if condition true
● if/else
○ Different actions if conditions true or false
● Pseudocode
if student’s grade is greater than or equal to 60
print “Passed” 24
else
print “Failed”
● C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
if/else Selection Structure
● Nested if/else structures
else
if student’s grade is greater than or equal to 80
Print “B”
else 25
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
if/else Selection Structure
● Example
true
examGrade < 60
28
false true
quizGrade < 10
29
int examGrade,quizGrade;
int examGrade, quizGrade; “We have a real
if (examGrade
(examGrade< <60)
60) {
System.out.println(“We have a problem”);
problem”
cout << “We have a problem” << endl;
if (quizGrade < 10)< 10)
Case if false have true/false
(quizGrade
System.out.printl(“We “Ok”
a real problem”);
cout << “We have a real problem” << endl;
3}elseSystem.out.println(“Ok”);
else
cout << “Ok”;
boolean Operators
● Combines multiple boolean expressions
● If person’s age greater than or equal to 13 and
less than 17, he can go to G and PG-13 rated
movies, but not R rated movies
● if (age >= 13 && age < 17)
cout << “You can go to G and PG-13”
<< “ rated movies, but not R” +
<< “ rated movies.”) << endl;
● boolean operators 31
Let age = 17
opera operan operand1 &&
nd1 d2 operand2
true true true
Let age = 16
true false false
false true false
false false false
Let age = 12
32
Expression Combinations
true false
false true
Playing Cards
● Exercise with playing cards
○ Numbers represent the rank and suit of cards
// Codes for suits
const int SPADES = 0;
const int HEARTS = 1;
const int DIAMONDS = 2;
const int CLUBS = 3;
34
● switch
○ Test variable for multiple values
case value2:
36
case value3: // taken if variable == value2 or ==
value3
statements
break;
true
case a case a action(s) break
false
true
case b case b action(s) break
false 37
.
.
.
true
case z case z action(s) break
false
default action(s)
Converting if/else to a switch
switch (rank)
{
case JACK:
cout << "Jack";
if break;
(rank == JACK)
case QUEEN:
cout
cout<<
<<"Queen";
"Jack";
break;
case KING:
cout << "King";
elsebreak;
if (rank == QUEEN)
case ACE:
cout <<
cout << "Queen";
"Ace";
break;
default:
cout << rank;
} else if (rank == KING; 38
else
cout << rank;
Complexity of an algorithm is a measure of the amount of time
and/or space required by an algorithm for an input of a given size (n).
Three types of complexity could be considered when analyzing algorithm
performance. These are worst-case complexity, best-case complexity, and
average-case complexity. Only worst-case complexity has found to be
useful.
Time complexity of an algorithm quantifies the amount of time taken by an
algorithm to run as a function of the length of the input. Similarly, Space
complexity of an algorithm quantifies the amount of space or memory
taken by an algorithm to run as a function of the length of the input.
or any defined problem, there can be N number of solution. This is true in
general. If I have a problem and I discuss about the problem with all of my
friends, they will all suggest me different solutions. And I am the one who has
to decide which solution is the best based on the circumstances.
Similarly for any problem which must be solved using a program, there can be
infinite number of solutions. Let's take a simple example to understand this.
Below we have two different algorithms to find square of a number(for some
time, forget that square of any number n is n*n):
One solution to this problem can be, running a loop for n times, starting with
the number n and adding n to it, every time.
/*
*/
for i=1 to n
do n = n + n
return n
Or, we can simply use a mathematical operator * to find the square.
/*
*/
return n*n
generator, all the balls have whole numbers on them. The difference between short integers, integers and long integers is the number of bytes (see the number bases section for
Declaring a variable gives the variable a name, and, in most
Short integer details) used to store them. This will vary according to the operating system and hardware you're using, but these days you can assume that an integer will be at least 16 bits,
container that stores your value. See the Type section for examples of
Long integer integers unless you specify a short one.
declarations.
When you define a variable, you are simply giving it a value.
Type
Most procedural programming languages support some sort of typing -
that is variables can only store one type of value. Anyone who has
created a database will be familiar with this idea - each field in an
Access database is also given a type, be it number, text, memo, date,
etc. The types supported will vary from language to language, but will
include some, or all, of the following (and maybe more!):
Float Floating point numbers are ones that contain fractional parts - i.e. they are not
whole numbers. The single and double quantifiers are analagous to the short and
Single long quantifiers used with integers - i.e. they indicate how many bits are used to
store the variable. Floating point arithmetic can lead to problems with rounding
Double and precision, so if you're dealing with a limited number of decimal places, it is
probably more efficient to use integers and multiply all your values by a power of
10. For example, if you're dealing with money, it's probably better to work in pence
and use integers than to work in pounds and use floating point variables.
Char A char variable is a common sight in C or C++ programs (which can't handle
strings), and is used to store a single text character. The value it actually stores is
an integer representing the code (e.g. ASCII) for the character represented.
Boolean A boolean variable can store one of two values - either TRUE or FALSE.
Like char, this is usually an integer - in VisualBASIC, for example, FALSE is 0 and
TRUE is -1, and the TRUE and FALSE values themselves are constants (see
below).
Fixed- Strings are variables that contain text, and they come in two sorts. With a fixed-
length length string, you declare how many characters the string is going to hold. Certain
string API calls in Windows require the use of fixed-length strings, but generally they are
not used in BASIC. In C they are implemented as an array (or vector) of chars.
Variable- A variable-length string is one where you don't define the length. This is the
length default type in BASIC, and is useful for taking user input where you don't know
string what the response will be. The maximum length of the string will depend on your
environment, but it should be at least 255 characters.
These types may go by different names in different languages, for example an integer in one might be a short in another, or
a single in one might be a float elsewhere.
Note that there is no string type in C, but that you can use a char pointer
(char* - I'm not going to go into the complexities of pointers and
references here!), and nor is there a Boolean type. I've used a fixed-
length string in the BASIC example to represent a single character
(either M or F).
BASIC also allows you to declare the type of a variable by using a suffix
on its name, e.g. $ for strings, % for integers, etc.. In the above
example, I could have defined name$ and age%, and the variables
would have been set to set to a string and an integer automatically.
See how variables and types are used in Python on the
Advanced ICT YouTube channel.
Some languages, such as JavaScript, are not typed, but still have a
declaration statement, e.g.:
var name, age, height, sex, married;
This might sound easier, but can lead to confusing results when it can't
decide whether your variables are numbers or strings. For example, say
you have two variables, a = 123 and b = 456 - what would you expect c
= a + b to give you? The + operator can also be used to concatenate
strings, so the value of c will depend on the context. Sometimes c will
be 579, and sometimes it might be 123456! To be sure, you'd have to
use something like c = (a * 1) + (b * 1) (for addition) or c = a + "" + b (for
string concatenation).
Scope
A variable can't always be used throughout the whole of
your program - they have something called a scope which
determines where a value can be read or changed.
Global variables are variables that can be used through
your program - that is, the scope of a global variable is the
entire application. Most variables, however, will be local -
local variables can only be used in the function (or
procedure) in which they were declared, or any other
function called by that function. Scope, therefore, is
hierarchical, and generally only applies downwards (from
the main body of the program, to the functions it calls, and
from functions down to further sub-functions).
This means that if you give a variable at the top of your
program, you can't declare another variable with the same
name in a function. However, if you declare a variable in
one function, you can declare another variable with the
same name in another function, and they will effectively be
different variables and can have different variables.
Finally, by default a variable declared in a function only
exists for the time that the function is running - each time
you call the function, the variable is re-declared and reset. If
this isn't what you want, you can declare your variable as
a static - this means that its value will persist after the
function finishes, and its value will be the same next time
the function is run. Static variables still have a type (e.g.
integer, float, etc.).