datastructure
datastructure
Arrays
Types of Arrays
DATA STRUCTURE
Data structure is a particuAr way to
store and organize data in a computer so
that it can be used effectively.
In DS, Any Organization for collection of
amount of:
Space
Time
Programming Effort
Data Structure mainly specifies the following
four things
Organization of Data
Accessing methods
Degree of associativity
Processing alternatives for information
Character
Boolean
Stack
Queue
void main() {
int i = 5;
int j;
cout<<“Please enter j value: “;
cin>>j;
FOR LOOP
For loop is used to print the set of statement
repeatedly until met the condition
Syntax
for(initialization;condition;increament/
decreament operator){
//Repeated statement
}
Ex:
for(int i=0;i<5;i++){
cout<<i;
}
ARRAYS
memory locations
TYPES OF ARRAYS
variables.
One-dimensional array can be decAred as
follows :
Datatype varname[Expression];
int a[5];
ARRAY REPRESENTATION
Element − Each item stored in an array is called an
element.
Index − Each location of an element in an array has
a numerical index, which is used to identify the
element.
Arrays can be decAred in various ways in different
Anguages
int arr[10]={31,23,34,64,55,26,37,98,92,10};
Index:0 to 9
int:- datatype
arr:- arrayname
[ ]:- size of the array
EXAMPLE: C++ ARRAY
C++ PROGRAM TO STORE AND CALCUATE THE SUM OF
5 NUMBERS ENTERED BY THE USER USING ARRAYS.
#include <iostream>
using namespace std;
int main() {
int numbers[5]; Output
Enter 5 numbers:
int sum = 0; 3
cout << "Enter 5 numbers: "; 4
5
for (int i = 0; i < 5; ++i) { 4
cin >> numbers[i]; 2
Sum = 18
sum += numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
TWO DIMENSIONAL ARRAY
Two dimensional arrays are also called table or
matrix, two dimensional arrays have two
subscripts.
Two dimensional array in which elements are
DecAration:
int arr[10] ;//one dimensional
int arr2d[10][10] ;
Initialization:
int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
#include <iostream>
using namespace std;
int main(){
int test[3][2] ={ {2, -5}, {4, 0}, {9, 1} };
for(int j = 0; j < 2; ++j){
for(int i = 0; i < 3; ++i){
cout<< "test[" << i << "][" << j << "] = " <<
test[i][j] << endl;
Output
} test[0][0] = 2
} test[0][1] = -5
test[1][0] = 4
return 0; test[1][1] = 0
test[2][0] = 9
}
test[2][1] = 1
DAY 3
BASIC OPERATIONS ON ARRAY
index.
Deletion − Deletes an element at the given
index.
Search − Searches an element using the
index.
TRAVERSE OPERATION
This operation is used to traverse through the
elements of an array.
Example
Following program traverses and prints the elements of an array:
#include <iostream>
using namespace std;
main() {
int A[] = {1,3,5,7,8};
cout<<"The original array elements are :”<<endl;
for(int i = 0; i<5; i++) {
cout<< “A[“<<i<<“]=”<<A[i]<<endl;
}
return 0;
}
OUTPUT
When we compile and execute the above
program, it produces the following result −
A[0] = 1
A[1] = 3
A[2] = 7
A[3] = 8
SEARCH OPERATION
Applications of
Queues
CircuAr Queue
Operations on Queues
Queue Applications.
STACKS IN DATA STRUCTURE
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
//POP THE ELEMENT
void pop() {
if(top<=-1)
cout<<"Stack
Underflow"<<endl;
else {
cout<<"The popped element is
"<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i-
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty“;
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do{
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be
pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
dispAy();
break
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid
Choice"<<endl
}
}
}
while(ch!=4);
return 0;
}
OUTPUT
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice: 1
Enter value to be pushed: 2
Enter choice: 1
Enter value to be pushed: 6
Enter choice: 1
Enter value to be pushed: 8
Enter choice: 3
The stack elements are :8 6 2
Enter choice: 2
The popped element is 8
Enter choice: 3
Stack elements are:6 2
Enter choice: 5
Invalid Choice
Enter choice: 4
Exit
APPLICATION OF STACKS
Reversing a list
Parentheses checker
postfix expression
Evaluation of a postfix expression
Recursion
Tower of Hanoi
CONVERSION OF INFIX TO
POSTFIX EXPRESSION USING
STACK
To convert Infix expression to Postfix
expression, we will use the stack data
structure.
Infix Expression : Infix Expression contains
Postfix Expression : 3 4 5 * 6 / +
#include<iostream>
#include<stack>
using namespace std;
void Postfix(char *a)
{ stack <char> s;
char output[50],t;
for(int i=0;a[i]!='\0';i++)
{ char ch = a[i];
switch(ch)
{
case '^':
case '-':
case '+':
case '/':
case '*': s.push(ch);
break;
case ')': t=s.top();
s.pop();
cout<<t;
break;
CONTINUE…
if (isalpha(ch))
cout<<ch;
}
}
int main()
{
char a[] = "(((a*b)+(c/d))-e)";
Postfix(a);
return 0;
}
Output:
ab*cd/+e-
QUEUES IN DATA STRUCTURES
A Queue is also a non-primitive linear data
structure.
It is an ordered list follows particular order