Chapter 1.1 Introduction Data Structure
Chapter 1.1 Introduction Data Structure
Bedasa Wayessa
bedonaf@gmail.com
Here is the code snippet for And here is the sample code snippet
how we can define variables: of how initializing variables works:
int iVar; int iVar = 100;
char32_t cVar; char32_t cVar = 'a';
long long llVar; long long llVar = 9223372036854775805;
bool boVar; bool boVar = true;
2 for loop
– firstly initializes, then, condition check, execute body, update.
for (initialization expr; test expr; update expr){}
3 do-while loop
– firstly, execute the body then condition check
initialization expression;
do { update_expression; } while (test_expression);
Data Structures and Algorithms - CoSc2091 27
Developing Abstract Data Types
An abstract data type (ADT) is a type that consists of a collection of data
and associated operations for manipulating the data.
The ADT will only mention the list of operations that can be performed but
not the implementation.
The implementation itself is hidden, which is why it's called abstract.
E.g.:- Imagine we have a DVD player we usually use in our pleasure time.
Regarding the process flow:
Abstraction is hiding the implementation details of the operations that
are available in the ADT
Information hiding is hiding the data which is being affected by that
Implementation
Encapsulation is grouping all similar data and functions into a group
data data
communications
functions functions
Object C
functions
data
– Class Members:
#include <iostream>
using namespace std; int main () {
public:
//method definitions Output:
int area () { Hello World
return width * width; 100
}
};
Data Structures and Algorithms - CoSc2091 37
Abstract Data Type(ADT)
An ADT is a class that has a defined set of operations and values.
In programming, an ADT has the following features:
An ADT doesn't state how data is organized, and
It provides only what's needed to execute its operations
• An ADT is a prime example of how you can make full use of data
abstraction and data hiding.
• This means that an abstract data type is a huge component of object-
oriented programming methodologies:
– enforcing abstraction, allowing data hiding (protecting information
from other parts of the program), and
– encapsulation (combining elements into a single unit, such as a
class).
class Addition {
public:
int add(int X, int Y){ // Function with parameter
return X+Y; // this function addition of two Integer value
}
int add() { // Function with same name but without parameter
string a= "HELLO";
string b=“C++"; // in this function concatenation is performed
string c= a+b;
cout<<c<<endl;
}
};
Shape
draw()
Values: 2 1 5 3 4
Index: 0 1 2 3 4
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are:\n"; The numbers are:
7
// Printing array elements 5
for (int i = 0; i < 5; ++i) { 6
cout << numbers[i] << "\n"; 12
} 35
return 0;
}
//Creating a node
class Node {
public:
int value;
Node* next;
}; Data Structures and Algorithms - CoSc2091 59
Some important DS: Stack
Stack is a last-in-first-out strategy data structure; this means that the
element stored in last will be removed first.
Stack has specific but very useful applications; some of them are as
follows:
– Solving Recursion - recursive calls are placed onto a stack, and
removed from there once they are processed.
– Evaluating post-fix expressions
– Solving Towers of Hanoi
– to move an entire stack of disks from the
source position to another position.
– Backtracking
– Depth-first search
– Converting a decimal number into a binary number
Data Structures and Algorithms - CoSc2091 60
Some important DS: Queue
Queue is a first-in-first-out data structure.
The element that is added to the queue data structure first, will be
removed from the queue first.
Dequeue, priority queue, and circular queue are the variants of
queue data structure.
Queue has the following application uses:
Access to shared resources (e.g., printer)
Multiprogramming
Message queue
Max-heap Min-heap