SlideShare a Scribd company logo
3
Most read
5
Most read
35
Unit-II
Data Structure
Arrays, Stacks, Queues And Linked List Chapter: 06
In Computer Science, a data structure is a particular way of storing and organizing data in a
computer so that it can be used efficiently. Different kinds of data structures are suited to different
kinds of applications, and some are highly specialized to specific tasks.
Simple Data Structure: These data structures are normally built from primitive data types like
integers, floats, characters. For example arrays and structure.
Compound Data Structure: simple data structures can be combined in various ways to form
more complex structure called compound structures. Linked Lists, Stack, Queues and Trees are
examples of compound data structure.
Searching methods in array
Linear Search: In this method each Binary Search Method
element of the array is compared with the Binary search algorithm is applicable for
number to be searched in linear order (from already sorted array only. In this algorithm,
first to last). And where the number is to search for the given item from the sorted
matched the position is displayed. array (in ascending order), the item is
#include<iostream.h> compared with the middle element of the array.
#include<conio.h> If the middle element is equal to the item then
void main() index of the middle element is returned,
{ otherwise, if item is less than the middle item
int lsearch(int[],int,int); then the item is present in first half segment of
int a[50],item,n,index; the array (i.e. between 0 to middle-1), so the
clrscr(); next iteration will continue for first half only, if
cout<<"n Enter size of array"; the item is larger than the middle element then
cin>>n; the item is present in second half of the array
cout<<"n Enter array elements"; (i.e. between middle+1 to size-1), so the next
for(int i=0;i<n;i++) iteration will continue for second half segment
cin>>a[i]; of the array only. The same process continues
cout<<"Enter the item to be searched"; until either the item is found (search
cin>>item; successful) or the segment is reduced to the
index=lsearch(a,n,item); single element and still the item is not found
if(index= = -1) (search unsuccessful).
cout<<"n Element not found";
else #include<iostream.h>
cout<<"n Element found at position #include<conio.h>
"<<index+1;
getch(); void main()
} {
int bsearch(int[],int,int);
int lsearch(int a[],int size,int item) int a[50], item, n, index;
{ clrscr();
int found=0; cout<<"n Enter total elements";
for(int i=0;i<size;i++) cin>>n;
{ cout<<"n Enter array elements in sorted
if(a[i]==item) form:";
{ for(int i=0;i<n;i++)
return i; cin>>a[i];
found=1;
break;
}
}
if(found==0)
return -1;
}
cout<<"Enter the item to be searched";
cin>>item;
index=bsearch(a, n, item);
if(index= = -1)
cout<<"n Element not found";
else
cout<<"n Element found at position
"<<index+1;
getch();
}
int bsearch(int a[], int size, int item)
{
int beg, med, last;
beg=0,found=0;
last=size-1;
int mid=(last+beg)/2;
while(beg<=last)
{
mid=(beg+last)/2;
if(item= =a[mid])
{
return mid;
found=1;
}
else if(item>a[mid])
beg=mid+1;
else
last=mid-1;
}
if(found==0)
return -1;
}
Sorting operation in the array
Sorting means to arrange the array elements in Ascending order or in Descending order. There are
various methods to do this but for the ease sake Bubble sort method is displayed here.
#include<iostream.h>
#include<conio.h>
void bubblesort (int[],int);
void main()
{
int a[50],n;
clrscr();
cout<<"nHow many elements do you want to create array with? ";
cin>>n;
cout<<"nEnter array elementsn";
for(int i=0;i<n;i++)
cin>>a[i];
bubblesort(a,n);
cout<<"nnThe sorted array is as shown belown";
for(i=0;i<n;i++)
36
cout<<a[i]<<"n";
getch();
}
void bubblesort(int a[],int n) //Function to perform bubble sort
{
int temp;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
Some Questions Based on Array
Q1. Write a function in C++ which accepts an integer array and its size as arguments/parameters and
reverses the array
example : if the array is 1,2,3,4,5 then rearrange the array as 5,4,3,2,1
Ans : void reverse(int arr[ ], int n)
{
int temp;
for(int i=0,j=n-1; i<=j; i++,j--)
{
temp= arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Q2. Write a function in C++ which accepts an integer array and its size as arguments/parameters and
exchange the array in the given manner
example : if the array is 1,2,3,4,5,6,7,8,9,10 then rearrange the array as2,1,4,3,6,5,8,7,10,9
Ans : void change(int arr[ ], int n)
{
int temp;
for(int i=0; i<n; i=i+2)
{
temp= arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
Q3 Write a function in C++ to merge the contents of two sorted arrays A & B into third array C.
Assuming array A is sorted in ascending order, B is sorted in descending order, the resultant array is
required to be in ascending order.
Q 4 Write a function in C++ which accepts an integer array and its size as arguments and assign the
elements into a two dimensional array of integers in the following format
37
If the array is 1,2,3,4,5,6
The resultant 2D array is
1 2 3 4 5 6
0 1 2 3 4 5
0 0 1 2 3 4
0 0 0 1 2 3
0 0 0 0 1 2
0 0 0 0 0 1
if the array is 1,2,3
The resultant 2D array is
1 2 3
0 1 2
0 0 1
Question based on Two dimensional Array
Q1. Write a function in C++ that will accept a 2-D array and its row and column size as argument
and find sum of rows and columns
Ans : void rowcolsum(int A[ ][ ],int N, int M)
{
for (int i=0;i<N;i++)
{
int SumR=0;
for (int j=0;j<M;j++)
SumR+=A[i][j];
cout<<SumR<<endl;
}
for (int i=0;i<N;i++)
{
int SumC=0;
for (int j=0;j<M;j++)
SumC+=A[j][i];
cout<<SumC<<endl;
}
}
Q2. Write a function in C++ to find the sum of both left and right diagonal elements from a two
dimensional array (matrix).
Ans : void DiagSum(int A[ ][ ], int N)
{
int SumD1=0,SumD2=0;
for (int I=0;I<N;I++)
{
SumD1+=A[I][I];
SumD2+=A[N-I-1][I];
}
}
Address Calculation in Two Dimensional Array
Two dimensional array can be arranged in two manner
1. Row Major Order
2. Column Major Order
To find the address of a particular row and column the formula in Row Major Order is
Address of A[row][column]=B +w*(n(row)+column)
Where
B= Base address of the array
w= Word size
38
39
n= total no of columns in the array
To find the address of a particular row and column the formula in Column Major Order is
Address of A[row][column]=B +w*(n(Column)+row)
Where
B= Base address of the array
w= Word size
n= total no of rows in the array
Q1. An array x[30][10] is stored in the memory with each element requiring 4 bytes of storage. If the
base address of x is 4500, find out memory locations of x[12][8] if the content is stored along the
row.
Ans: Here the array is stored in Row Major Order so
B=4500
W= 4
N= 10
As per the formula
Address of A[row][column]=B +w*(n(row)+column)
=4500+4*(10(12)+8)
=4500+4*(128)
=4500+512
=5012
Q 2. An array P[20][30] is stored in the memory along the column with each of the element
occupying 4 bytes, find out the Base Address of the array, if an element P[2][20] is stored at the
memory location 5000.
Ans : Given, W=4, N=20, M=30, Loc(P[2][20])=5000
Column Major Formula:
Loc(P[I][J]) =Base(P)+W*(N*J+I)
Loc(P[2][20]) =Base(P)+4*(20*20+2)
Base(P) =5000 4*(400+2)
=5000 1608
=3392
Q3. An array S[40][30] is stored in the memory along the row with each of the element occupying 2
bytes, find out the memory location for the element S[20][10], if an element S[15][5] is stored at the
memory location 5500.
Ans. Given, W=2, N=40, M=30, Loc(S[15][5])=5500
Row Major Formula:
Loc(S[I][J]) =Base(S)+W*(M*I+J)
Loc(S[15][5]) =Base(S)+2*(30*15+5)
5500 =Base(S) + 2*(450+5)
Base(S) =5500 910 = 4590
Loc(S[20][10]) =4590+2*(30*20+10)
=4590+2*(600+10)
=4590+1220 = 5810
STACKS, QUEUES AND LINKED LIST
Stack
In computer science, a stack is a Last in, First out (LIFO) data structure. It simply means that an
element that is inserted at the end will be deleted first. To Manage a stack all the insertion and
deletion takes place from one posi
One of the common uses of stack is in function call.
Operations on the Stack
There are two fundamental operations
Push
Pop
Push means to insert an element
Pop means to delete an element
Queue
In computer science, a Queue is a First in, First out (FIFO) data structure. It simply means that an
element that is inserted at the beginning will be deleted first. To Manage a queue all the insertion and
Every element is inserted from the rear position and deleted from the front position in the queue.
Linked List
A linked list is a data structure consisting of a group of nodes which together represent a sequence.
Under the simplest form, each node is composed of a data and a reference (in other words, a link) to
the next node in the sequence; more complex variants add additional links. This structure allows for
efficient insertion or removal of elements from any position in thesequence.
Here in the figure is an example of a linked list whose nodes contain two fields: an integer value and
a link to the next node. The last node is linked to a terminator used to signify the end of thelist.
Linked lists are among the simplest and most common data structures. They can be used to
implement several other common abstract data types, stacks, queues etc though it is not uncommon to
implement the other data structures directly without using a list as the basis of implementation.
The principal benefit of a linked list over an array is that the list elements can easily be inserted or
removed without reallocation or reorganization of the entire structure because the data items need not
be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at
any point in the list, and can do so with a constant number of operations if the link previous to the
link being added or removed is maintained during list traversal.
Linked list are dynamic structure where memory allocation takes place at run time.
Operation on a linked list
There are three basic operations on a linked list
Insertion
Deletion
Traversal
Inserting a node or element into Linked list :
Inserting an element into linked list contains 3 types .
1. Insertion at beginning of the Linked list
2. Insertion after/before any element of the linked list
3. Insertion at the end of the linked list
Deleting a node from the Linked list.
A node can be deleted in 3 ways similar to Insertion.
1. Deleting a Node from the beginning of the Linked List
40

More Related Content

PDF
2-D array
Swarup Kumar Boro
 
PPTX
Lecture 9 data structures and algorithms
Aakash deep Singhal
 
PPT
Arrays and structures
Mohd Arif
 
PPTX
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
PPT
Data Structure and Algorithms Arrays
ManishPrajapati78
 
PPTX
Multi-Dimensional Lists
primeteacher32
 
PPTX
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
PPT
Fundamentals of data structures
Niraj Agarwal
 
Lecture 9 data structures and algorithms
Aakash deep Singhal
 
Arrays and structures
Mohd Arif
 
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Multi-Dimensional Lists
primeteacher32
 
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Fundamentals of data structures
Niraj Agarwal
 

What's hot (20)

PPTX
Row major and column major in 2 d
nikhilarora2211
 
PPTX
Lecture 5 data structures and algorithms
Aakash deep Singhal
 
PPT
Unit 4 tree
kalyanineve
 
PPTX
Array
HarshKumar943076
 
PDF
1-D array
Swarup Kumar Boro
 
PPTX
هياكلبيانات
Rafal Edward
 
PDF
Data structure ppt
Prof. Dr. K. Adisesha
 
PPT
Arrays
SARITHA REDDY
 
PPTX
Set data structure
Tech_MX
 
PPT
Unit 1 introduction to data structure
kalyanineve
 
PDF
Introduction to Data Structure
Prof Ansari
 
PPTX
2D Array
Ehatsham Riaz
 
PPT
Data structure
viswanathV8
 
PPT
02 Arrays And Memory Mapping
Qundeel
 
PPTX
Array ppt
Kaushal Mehta
 
PPT
Chapter 3 ds
Hanif Durad
 
PPTX
Data structures
Pranav Gupta
 
PPTX
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
PDF
Data Structures (BE)
PRABHAHARAN429
 
PDF
2nd puc computer science chapter 3 data structures 1
Aahwini Esware gowda
 
Row major and column major in 2 d
nikhilarora2211
 
Lecture 5 data structures and algorithms
Aakash deep Singhal
 
Unit 4 tree
kalyanineve
 
هياكلبيانات
Rafal Edward
 
Data structure ppt
Prof. Dr. K. Adisesha
 
Set data structure
Tech_MX
 
Unit 1 introduction to data structure
kalyanineve
 
Introduction to Data Structure
Prof Ansari
 
2D Array
Ehatsham Riaz
 
Data structure
viswanathV8
 
02 Arrays And Memory Mapping
Qundeel
 
Array ppt
Kaushal Mehta
 
Chapter 3 ds
Hanif Durad
 
Data structures
Pranav Gupta
 
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Data Structures (BE)
PRABHAHARAN429
 
2nd puc computer science chapter 3 data structures 1
Aahwini Esware gowda
 
Ad

Similar to DATA STRUCTURE CLASS 12 COMPUTER SCIENCE (20)

PPT
DATASTRUCTURES UNIT-1
Malikireddy Bramhananda Reddy
 
PPTX
arrays.pptx
NehaJain919374
 
PDF
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
PPTX
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
PPTX
III_Data Structure_Module_1.pptx
shashankbhadouria4
 
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
PPTX
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Data structure
Arvind Kumar
 
PPTX
Data structure using c module 1
smruti sarangi
 
PPTX
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
PPTX
unit-2-dsa.pptx
sayalishivarkar1
 
PPT
Arrays
Komal Singh
 
PPT
III_Data Structure_Module_1.ppt
shashankbhadouria4
 
PPT
Array 31.8.2020 updated
vrgokila
 
PPTX
unit1Intro_final.pptx
DEEPAK948083
 
PDF
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPT
Algorithm
sultanarun
 
PPTX
Bca ii dfs u-1 introduction to data structure
Rai University
 
DOCX
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
DATASTRUCTURES UNIT-1
Malikireddy Bramhananda Reddy
 
arrays.pptx
NehaJain919374
 
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
III_Data Structure_Module_1.pptx
shashankbhadouria4
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
Data structure array
MajidHamidAli
 
Data structure
Arvind Kumar
 
Data structure using c module 1
smruti sarangi
 
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
unit-2-dsa.pptx
sayalishivarkar1
 
Arrays
Komal Singh
 
III_Data Structure_Module_1.ppt
shashankbhadouria4
 
Array 31.8.2020 updated
vrgokila
 
unit1Intro_final.pptx
DEEPAK948083
 
Algorithm
sultanarun
 
Bca ii dfs u-1 introduction to data structure
Rai University
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
Ad

More from Dev Chauhan (17)

PDF
GTU induction program report
Dev Chauhan
 
PDF
2 States Book Review
Dev Chauhan
 
PPTX
STACK, LINKED LIST ,AND QUEUE
Dev Chauhan
 
PPTX
NETWORKING AND COMMUNICATION TECHNOLOGIES
Dev Chauhan
 
PPTX
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
PPTX
BASIC CONCEPTS OF C++ CLASS 12
Dev Chauhan
 
PPTX
BOOLEAN ALGEBRA
Dev Chauhan
 
PPTX
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
PPTX
Communication and Network Concepts
Dev Chauhan
 
PPTX
What is bullying
Dev Chauhan
 
PPT
Properties Of Water
Dev Chauhan
 
PPTX
बहुव्रीहि समास
Dev Chauhan
 
PPTX
अव्ययीभाव समास
Dev Chauhan
 
PPTX
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)
Dev Chauhan
 
PPTX
कर्मधारय, द्विगु समास
Dev Chauhan
 
PPTX
द्वन्द्व समास
Dev Chauhan
 
PPTX
Class 10 Farewell Presentation Topic:- Nostalgia
Dev Chauhan
 
GTU induction program report
Dev Chauhan
 
2 States Book Review
Dev Chauhan
 
STACK, LINKED LIST ,AND QUEUE
Dev Chauhan
 
NETWORKING AND COMMUNICATION TECHNOLOGIES
Dev Chauhan
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
BASIC CONCEPTS OF C++ CLASS 12
Dev Chauhan
 
BOOLEAN ALGEBRA
Dev Chauhan
 
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
Communication and Network Concepts
Dev Chauhan
 
What is bullying
Dev Chauhan
 
Properties Of Water
Dev Chauhan
 
बहुव्रीहि समास
Dev Chauhan
 
अव्ययीभाव समास
Dev Chauhan
 
तत्पुरुष (विभक्ति, उपपद ऽ नञ्)
Dev Chauhan
 
कर्मधारय, द्विगु समास
Dev Chauhan
 
द्वन्द्व समास
Dev Chauhan
 
Class 10 Farewell Presentation Topic:- Nostalgia
Dev Chauhan
 

Recently uploaded (20)

PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 

DATA STRUCTURE CLASS 12 COMPUTER SCIENCE

  • 1. 35 Unit-II Data Structure Arrays, Stacks, Queues And Linked List Chapter: 06 In Computer Science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. Simple Data Structure: These data structures are normally built from primitive data types like integers, floats, characters. For example arrays and structure. Compound Data Structure: simple data structures can be combined in various ways to form more complex structure called compound structures. Linked Lists, Stack, Queues and Trees are examples of compound data structure. Searching methods in array Linear Search: In this method each Binary Search Method element of the array is compared with the Binary search algorithm is applicable for number to be searched in linear order (from already sorted array only. In this algorithm, first to last). And where the number is to search for the given item from the sorted matched the position is displayed. array (in ascending order), the item is #include<iostream.h> compared with the middle element of the array. #include<conio.h> If the middle element is equal to the item then void main() index of the middle element is returned, { otherwise, if item is less than the middle item int lsearch(int[],int,int); then the item is present in first half segment of int a[50],item,n,index; the array (i.e. between 0 to middle-1), so the clrscr(); next iteration will continue for first half only, if cout<<"n Enter size of array"; the item is larger than the middle element then cin>>n; the item is present in second half of the array cout<<"n Enter array elements"; (i.e. between middle+1 to size-1), so the next for(int i=0;i<n;i++) iteration will continue for second half segment cin>>a[i]; of the array only. The same process continues cout<<"Enter the item to be searched"; until either the item is found (search cin>>item; successful) or the segment is reduced to the index=lsearch(a,n,item); single element and still the item is not found if(index= = -1) (search unsuccessful). cout<<"n Element not found"; else #include<iostream.h> cout<<"n Element found at position #include<conio.h> "<<index+1; getch(); void main() } { int bsearch(int[],int,int); int lsearch(int a[],int size,int item) int a[50], item, n, index; { clrscr(); int found=0; cout<<"n Enter total elements"; for(int i=0;i<size;i++) cin>>n; { cout<<"n Enter array elements in sorted if(a[i]==item) form:"; { for(int i=0;i<n;i++) return i; cin>>a[i];
  • 2. found=1; break; } } if(found==0) return -1; } cout<<"Enter the item to be searched"; cin>>item; index=bsearch(a, n, item); if(index= = -1) cout<<"n Element not found"; else cout<<"n Element found at position "<<index+1; getch(); } int bsearch(int a[], int size, int item) { int beg, med, last; beg=0,found=0; last=size-1; int mid=(last+beg)/2; while(beg<=last) { mid=(beg+last)/2; if(item= =a[mid]) { return mid; found=1; } else if(item>a[mid]) beg=mid+1; else last=mid-1; } if(found==0) return -1; } Sorting operation in the array Sorting means to arrange the array elements in Ascending order or in Descending order. There are various methods to do this but for the ease sake Bubble sort method is displayed here. #include<iostream.h> #include<conio.h> void bubblesort (int[],int); void main() { int a[50],n; clrscr(); cout<<"nHow many elements do you want to create array with? "; cin>>n; cout<<"nEnter array elementsn"; for(int i=0;i<n;i++) cin>>a[i]; bubblesort(a,n); cout<<"nnThe sorted array is as shown belown"; for(i=0;i<n;i++) 36
  • 3. cout<<a[i]<<"n"; getch(); } void bubblesort(int a[],int n) //Function to perform bubble sort { int temp; for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } Some Questions Based on Array Q1. Write a function in C++ which accepts an integer array and its size as arguments/parameters and reverses the array example : if the array is 1,2,3,4,5 then rearrange the array as 5,4,3,2,1 Ans : void reverse(int arr[ ], int n) { int temp; for(int i=0,j=n-1; i<=j; i++,j--) { temp= arr[i]; arr[i] = arr[j]; arr[j] = temp; } } Q2. Write a function in C++ which accepts an integer array and its size as arguments/parameters and exchange the array in the given manner example : if the array is 1,2,3,4,5,6,7,8,9,10 then rearrange the array as2,1,4,3,6,5,8,7,10,9 Ans : void change(int arr[ ], int n) { int temp; for(int i=0; i<n; i=i+2) { temp= arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } Q3 Write a function in C++ to merge the contents of two sorted arrays A & B into third array C. Assuming array A is sorted in ascending order, B is sorted in descending order, the resultant array is required to be in ascending order. Q 4 Write a function in C++ which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format 37
  • 4. If the array is 1,2,3,4,5,6 The resultant 2D array is 1 2 3 4 5 6 0 1 2 3 4 5 0 0 1 2 3 4 0 0 0 1 2 3 0 0 0 0 1 2 0 0 0 0 0 1 if the array is 1,2,3 The resultant 2D array is 1 2 3 0 1 2 0 0 1 Question based on Two dimensional Array Q1. Write a function in C++ that will accept a 2-D array and its row and column size as argument and find sum of rows and columns Ans : void rowcolsum(int A[ ][ ],int N, int M) { for (int i=0;i<N;i++) { int SumR=0; for (int j=0;j<M;j++) SumR+=A[i][j]; cout<<SumR<<endl; } for (int i=0;i<N;i++) { int SumC=0; for (int j=0;j<M;j++) SumC+=A[j][i]; cout<<SumC<<endl; } } Q2. Write a function in C++ to find the sum of both left and right diagonal elements from a two dimensional array (matrix). Ans : void DiagSum(int A[ ][ ], int N) { int SumD1=0,SumD2=0; for (int I=0;I<N;I++) { SumD1+=A[I][I]; SumD2+=A[N-I-1][I]; } } Address Calculation in Two Dimensional Array Two dimensional array can be arranged in two manner 1. Row Major Order 2. Column Major Order To find the address of a particular row and column the formula in Row Major Order is Address of A[row][column]=B +w*(n(row)+column) Where B= Base address of the array w= Word size 38
  • 5. 39 n= total no of columns in the array To find the address of a particular row and column the formula in Column Major Order is Address of A[row][column]=B +w*(n(Column)+row) Where B= Base address of the array w= Word size n= total no of rows in the array Q1. An array x[30][10] is stored in the memory with each element requiring 4 bytes of storage. If the base address of x is 4500, find out memory locations of x[12][8] if the content is stored along the row. Ans: Here the array is stored in Row Major Order so B=4500 W= 4 N= 10 As per the formula Address of A[row][column]=B +w*(n(row)+column) =4500+4*(10(12)+8) =4500+4*(128) =4500+512 =5012 Q 2. An array P[20][30] is stored in the memory along the column with each of the element occupying 4 bytes, find out the Base Address of the array, if an element P[2][20] is stored at the memory location 5000. Ans : Given, W=4, N=20, M=30, Loc(P[2][20])=5000 Column Major Formula: Loc(P[I][J]) =Base(P)+W*(N*J+I) Loc(P[2][20]) =Base(P)+4*(20*20+2) Base(P) =5000 4*(400+2) =5000 1608 =3392 Q3. An array S[40][30] is stored in the memory along the row with each of the element occupying 2 bytes, find out the memory location for the element S[20][10], if an element S[15][5] is stored at the memory location 5500. Ans. Given, W=2, N=40, M=30, Loc(S[15][5])=5500 Row Major Formula: Loc(S[I][J]) =Base(S)+W*(M*I+J) Loc(S[15][5]) =Base(S)+2*(30*15+5) 5500 =Base(S) + 2*(450+5) Base(S) =5500 910 = 4590 Loc(S[20][10]) =4590+2*(30*20+10) =4590+2*(600+10) =4590+1220 = 5810
  • 6. STACKS, QUEUES AND LINKED LIST Stack In computer science, a stack is a Last in, First out (LIFO) data structure. It simply means that an element that is inserted at the end will be deleted first. To Manage a stack all the insertion and deletion takes place from one posi One of the common uses of stack is in function call. Operations on the Stack There are two fundamental operations Push Pop Push means to insert an element Pop means to delete an element Queue In computer science, a Queue is a First in, First out (FIFO) data structure. It simply means that an element that is inserted at the beginning will be deleted first. To Manage a queue all the insertion and Every element is inserted from the rear position and deleted from the front position in the queue. Linked List A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in thesequence. Here in the figure is an example of a linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of thelist. Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types, stacks, queues etc though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation. The principal benefit of a linked list over an array is that the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal. Linked list are dynamic structure where memory allocation takes place at run time. Operation on a linked list There are three basic operations on a linked list Insertion Deletion Traversal Inserting a node or element into Linked list : Inserting an element into linked list contains 3 types . 1. Insertion at beginning of the Linked list 2. Insertion after/before any element of the linked list 3. Insertion at the end of the linked list Deleting a node from the Linked list. A node can be deleted in 3 ways similar to Insertion. 1. Deleting a Node from the beginning of the Linked List 40