SlideShare a Scribd company logo
1 of 26
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Day-1
18th February 2023
Data Structures- C++ Implementation
1
Instructor
Piyush Kumar Soni
Assistant Professor,
Information Technology Department
Workshop on
Data Structures &
Algorithms
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Contents
2
• Data structures and Algorithms
• Data Structures- C++ implementation
• Arrays
• Linked Lists
• Stacks
• Queues
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Data structures and Algorithms?
3
• Data structure
• Structure of data.
• Is a particular way of storing and organizing data in a computer.
• Is a way of organizing all data items that consider not only the elements stored but also
their relationship to each other.
• The definition of a data structure includes:
• Memory representation of data elements.
• Representation of the relationship existing between data elements(represented in
the form of operations on data elements).
• The definition of operations.
• Algorithm
• In computer science, an algorithm is a finite sequence of well-defined, computer-
implementable instructions, typically to solve a class of problems or to perform a
computation.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Arrays
4
• An array is a collection of items of the same data type stored at contiguous
memory locations.
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
List/Ordered List
5
• List/ordered list defines a sequential set of elements to which you can add
new elements and retrieve, remove or change existing ones.
• Ex. List of contact numbers, names, courses, cities etc.
• The list data structure typically has two very distinctive implementations –
• Array implementation
• Linked implementation
• Operations
• Search
• Insertion
• Deletion
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Array implementation of List
6
• Elements of the list are stored in a one-dimensional array. The first
element is at index 0 and the nth element is at index n-1.
• We are also required to maintain an integer variable N, which represents
the total number of elements in the list.
• Eg. List  {10,20,30,40,50}
• Array representation 
0 1 2 3 4 Indexes
• Value of N is 5
10 20 30 40 50
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Operations
7
• Search
Algorithm LinSrch(A,n,x)
{
for i:=1 to n do
if A[i]=x then
return i;
return 0;
}
Algorithm BinSrch(A,l,h,x)
{
if(l<=h) then
{
mid=(l+h)/2;
if(A[mid]=x) then
return mid;
else if(A[mid]<x) then
BinSrch(A,mid+1,h,x);
else
BinSrch(A,l,mid-1,x);
}
return 0;
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Operations
8
• Insertion
Algorithm Insertion(A,n,pos,x)
{
for i:=n to pos step -1 do
{
A[i+1]:=A[i];
}
A[pos]:=x;
n:=n+1;
}
• Complexity?
• Deletion
Algorithm Deletion(A,n,pos)
{
for i:=pos+1 to n do
{
A[i-1]:=A[i];
}
n:=n-1;
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Issues with Array Representation
9
• Static Allocation-Contiguous memory requirement
• Fixed Size
• Upper bound on the number of elements
• Wastage of memory
• Complexity of insertion and deletion operation
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Solution- Linked List
10
• Dynamic memory allocation only for the element to be inserted, different
elements may be stored at different locations
• No upper bound on the number of elements- as long as memory is available
• No wastage of memory- dynamic memory allocation only for the element to
be inserted
• Reduced complexity of insertion and deletion operation
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Structure of Node and Graphical Representation
11
• Linearity is implicit in array representation but we have to maintain linearity
explicitly in a linked list.
• Hence with every element, we also store the address of the next element.
With the last element, we store NULL.
• Element + address of next element is termed as the node and graphically
represented as-
• Also we maintain a Start pointer to store the beginning of the linked list(i. e.
address of the first node). A linked list is graphically represented as-
Start
Data Link
X
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Operations
12
• Referring to individual fields of a node
• Data(I)
• Link(I)
• Search
Algorithm(Start, e)
{
Temp:=Start;
while(Data(Temp)!=e and Temp!=NULL) do
{
Temp:=Link(Temp);
}
if (Temp=NULL) then
print(Element e not present);
else
print(Element e is present at address Temp);
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Operations
13
• Creating a node
• Concept of available list
• Getnode(I) function
• Insertion
• At the beginning
• At the end
• After a given node
Algorithm Insafgiv(Start, ele,x)
{
getnode(I);
Data(I):=ele;
Temp:=Start;
while(Data(Temp)!=x) do
Temp:=Link(Temp);
Link(I):=Link(Temp);
Link(Temp):=I;
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Operations
14
• Releasing memory
• Free(I)
• Deletion
• From the beginning
• From the end
• Of a given node
Algorithm Delnode(Start, x)
{
Temp1:=Start;
while(Data(Temp1)!=x and
Temp1 !=NULL) do {
Temp2:=Temp1;
Temp1:=Link(Temp1); }
if (Temp1=NULL)
print(x is not present);
else {
Link(Temp2):=Link(Temp1);
Free(Temp1); }
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Doubly Linked List
15
• We can move only in the forward direction in a singly linked list.
• We will also store the address of the previous element.
• Address of the previous element will be termed as the Left Link and the
address of the next element will be termed as Right Link.
• Left Link of the first element and the Right Link of the last element will be
NULL.
• Structure of node will be-
• Graphical representation of doubly linked list-
LLink Data RLink
X X
Start
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Stack
16
• A stack is a list with the restriction that insertions and deletions can be
performed in only one position, namely the end of the list, called the top.
• Stacks are called LIFO lists.
• Operations-
• Insertion(Push)
• Deletion(Pop)
C
B
A
D
E
Push Pop
Top
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Stack-Array Implementation
17
• We store the stack as a one-dimensional array.
• The bottommost element is stored at the index 0 (1 in case of pseudo-code
notations).
• If an element A is stored at index p, then the element just above A and just
beneath A will be stored at index p+1 and p-1 respectively.
• A variable top is also maintained, it stores the index of the top element and is
initialized with -1(0 in case of pseudo-code notations).
• The stack of the previous slide can be stored as follows:
top
A B C D E
0 1 2 3 4 5 6
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Stack-Array Implementation-Operations
18
Algorithm Push(S, top, n, value)
{
if (top=n)// n-1 in programming
print(“stack is full”);
else
{
top:=top+1;
S[top]:=value;
}
}
Algorithm Pop(S, top, value)
{
if (top=0) // -1 in programming
print(“stack is empty”);
else
{
value:=S[top];
top:=top-1;
}
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Stack-Linked Implementation
19
• The stack’s topmost element will be the 1st node of the linked list.
• Element just beneath the element in the stack will be the next node in the
linked list.
• The bottommost element of the stack will be the last node of the linked list.
• A pointer top is also maintained, which stores the address of the top element.
top
E D X
A
B
C
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Stack-Linked Implementation-Operations
20
Algorithm PUSH(Top, ele)
{
getnode(I);
Data(I):=ele;
Link(I):=Top;
Top:=I;
}
Algorithm POP(Top)
{
Temp:=Top;
Top:=Link(Top);
Free(Temp);
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Queue
21
• A queue is a list with the restriction that insertions can be performed in only
one position, called the rear and deletions can be performed at only one
position, called the front. Queues are called FIFO lists.
• Operations-
• Insertion
• Deletion
front rear
A B C D E
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Queue-Array Implementation
22
• We store the queue as a one-dimensional array.
• If an element A is stored at index p, then the element next to A and before A
will be stored at index p+1 and p-1 respectively.
• Two variables front and rear are also maintained, the rear stores the index of
the rear element and is initialized with -1. (0 in case of pseudocode notation)
• Front stores a value that is 1 less than the index of the front element and is
initialized with -1.
front
rear
A B C D E
0 1 2 3 4 5 6 7
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Queue-Array Implementation-Operations
23
Algorithm Insert(Q, rear, n, value)
{
if (rear=n)//n-1 in case of
//programming language
print(“queue is full”);
else
{
rear:=rear+1;
Q[rear]:=value;
}
}
Algorithm Delete(Q, rear, front, value)
{
if (rear=front)
print(“queue is empty”);
else
{
front:=front+1;
value:=Q[front];
}
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Queue-Linked Implementation
24
• The element at the front end of the queue will be the 1st node of the linked
list.
• Next to the element in the queue will be the next node in the linked list.
• The element at the rear end of the queue will be the last node of the linked
list.
• A pointer front is also maintained, which stores the address of the front
element.
• A pointer rear is also maintained, which stores the address of the rear
element.
Front Rear
B X
E
D
C
A
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Queue-Linked Implementation-Operations
25
Algorithm Insertion( rear, value)
{
Getnode(I);
Data(I)=value;
Link(I)=NULL;
Link(rear)=I;
rear=I;
}
Algorithm Deletion(front)
{
Temp:=front;
front:=Link(front);
Free(Temp);
}
MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR
Thank You…!!!
26

More Related Content

Similar to GDSC MPSTME Shirpur DSA Day 1.pptx

What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxAhmedEldesoky24
 
Data Structures and Files
Data Structures and FilesData Structures and Files
Data Structures and FilesKanchanPatil34
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
10994103.ppt
10994103.ppt10994103.ppt
10994103.pptSushmaG48
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURESUsha Mahalingam
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structuresDeepa Rani
 
Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...abhaysingh19149
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using Ccpjcollege
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptxSonaPathak4
 
Lecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdfLecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdfssuser400105
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 

Similar to GDSC MPSTME Shirpur DSA Day 1.pptx (20)

DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Queues
Queues Queues
Queues
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptx
 
Data Structures and Files
Data Structures and FilesData Structures and Files
Data Structures and Files
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
10994103.ppt
10994103.ppt10994103.ppt
10994103.ppt
 
Data Structures
Data StructuresData Structures
Data Structures
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Review of basic data structures
Review of basic data structuresReview of basic data structures
Review of basic data structures
 
Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...Stack and queue power point presentation data structure and algorithms Stack-...
Stack and queue power point presentation data structure and algorithms Stack-...
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Lecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdfLecture 4 - List, Stack, Queues, Deques.pdf
Lecture 4 - List, Stack, Queues, Deques.pdf
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 

Recently uploaded

Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1T.D. Shashikala
 
Lect 2 - Design of slender column-2.pptx
Lect 2 - Design of slender column-2.pptxLect 2 - Design of slender column-2.pptx
Lect 2 - Design of slender column-2.pptxHamzaKhawar4
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamDr. Radhey Shyam
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdfKamal Acharya
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdfKamal Acharya
 
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineLow rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineAftabkhan575376
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdfKamal Acharya
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...Amil baba
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringC Sai Kiran
 
ENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu sweENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu sweMohammadAliNayeem
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5T.D. Shashikala
 
Roushan Kumar Java oracle certificate
Roushan Kumar Java oracle certificate Roushan Kumar Java oracle certificate
Roushan Kumar Java oracle certificate RaushanKumar452467
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdfKamal Acharya
 
An improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technologyAn improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technologyBOHRInternationalJou1
 
Lect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxLect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxMonirHossain707319
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfAyahmorsy
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxMd. Shahidul Islam Prodhan
 
E-Commerce Shopping for developing a shopping ecommerce site
E-Commerce Shopping for developing a shopping ecommerce siteE-Commerce Shopping for developing a shopping ecommerce site
E-Commerce Shopping for developing a shopping ecommerce sitejatinraor66
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectRased Khan
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Prakhyath Rai
 

Recently uploaded (20)

Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Lect 2 - Design of slender column-2.pptx
Lect 2 - Design of slender column-2.pptxLect 2 - Design of slender column-2.pptx
Lect 2 - Design of slender column-2.pptx
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineLow rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdf
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
ENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu sweENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu swe
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
Roushan Kumar Java oracle certificate
Roushan Kumar Java oracle certificate Roushan Kumar Java oracle certificate
Roushan Kumar Java oracle certificate
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
An improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technologyAn improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technology
 
Lect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxLect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptx
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdf
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
E-Commerce Shopping for developing a shopping ecommerce site
E-Commerce Shopping for developing a shopping ecommerce siteE-Commerce Shopping for developing a shopping ecommerce site
E-Commerce Shopping for developing a shopping ecommerce site
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 

GDSC MPSTME Shirpur DSA Day 1.pptx

  • 1. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Day-1 18th February 2023 Data Structures- C++ Implementation 1 Instructor Piyush Kumar Soni Assistant Professor, Information Technology Department Workshop on Data Structures & Algorithms
  • 2. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Contents 2 • Data structures and Algorithms • Data Structures- C++ implementation • Arrays • Linked Lists • Stacks • Queues
  • 3. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Data structures and Algorithms? 3 • Data structure • Structure of data. • Is a particular way of storing and organizing data in a computer. • Is a way of organizing all data items that consider not only the elements stored but also their relationship to each other. • The definition of a data structure includes: • Memory representation of data elements. • Representation of the relationship existing between data elements(represented in the form of operations on data elements). • The definition of operations. • Algorithm • In computer science, an algorithm is a finite sequence of well-defined, computer- implementable instructions, typically to solve a class of problems or to perform a computation.
  • 4. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Arrays 4 • An array is a collection of items of the same data type stored at contiguous memory locations.
  • 5. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR List/Ordered List 5 • List/ordered list defines a sequential set of elements to which you can add new elements and retrieve, remove or change existing ones. • Ex. List of contact numbers, names, courses, cities etc. • The list data structure typically has two very distinctive implementations – • Array implementation • Linked implementation • Operations • Search • Insertion • Deletion
  • 6. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Array implementation of List 6 • Elements of the list are stored in a one-dimensional array. The first element is at index 0 and the nth element is at index n-1. • We are also required to maintain an integer variable N, which represents the total number of elements in the list. • Eg. List  {10,20,30,40,50} • Array representation  0 1 2 3 4 Indexes • Value of N is 5 10 20 30 40 50
  • 7. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Operations 7 • Search Algorithm LinSrch(A,n,x) { for i:=1 to n do if A[i]=x then return i; return 0; } Algorithm BinSrch(A,l,h,x) { if(l<=h) then { mid=(l+h)/2; if(A[mid]=x) then return mid; else if(A[mid]<x) then BinSrch(A,mid+1,h,x); else BinSrch(A,l,mid-1,x); } return 0; }
  • 8. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Operations 8 • Insertion Algorithm Insertion(A,n,pos,x) { for i:=n to pos step -1 do { A[i+1]:=A[i]; } A[pos]:=x; n:=n+1; } • Complexity? • Deletion Algorithm Deletion(A,n,pos) { for i:=pos+1 to n do { A[i-1]:=A[i]; } n:=n-1; }
  • 9. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Issues with Array Representation 9 • Static Allocation-Contiguous memory requirement • Fixed Size • Upper bound on the number of elements • Wastage of memory • Complexity of insertion and deletion operation
  • 10. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Solution- Linked List 10 • Dynamic memory allocation only for the element to be inserted, different elements may be stored at different locations • No upper bound on the number of elements- as long as memory is available • No wastage of memory- dynamic memory allocation only for the element to be inserted • Reduced complexity of insertion and deletion operation
  • 11. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Structure of Node and Graphical Representation 11 • Linearity is implicit in array representation but we have to maintain linearity explicitly in a linked list. • Hence with every element, we also store the address of the next element. With the last element, we store NULL. • Element + address of next element is termed as the node and graphically represented as- • Also we maintain a Start pointer to store the beginning of the linked list(i. e. address of the first node). A linked list is graphically represented as- Start Data Link X
  • 12. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Operations 12 • Referring to individual fields of a node • Data(I) • Link(I) • Search Algorithm(Start, e) { Temp:=Start; while(Data(Temp)!=e and Temp!=NULL) do { Temp:=Link(Temp); } if (Temp=NULL) then print(Element e not present); else print(Element e is present at address Temp); }
  • 13. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Operations 13 • Creating a node • Concept of available list • Getnode(I) function • Insertion • At the beginning • At the end • After a given node Algorithm Insafgiv(Start, ele,x) { getnode(I); Data(I):=ele; Temp:=Start; while(Data(Temp)!=x) do Temp:=Link(Temp); Link(I):=Link(Temp); Link(Temp):=I; }
  • 14. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Operations 14 • Releasing memory • Free(I) • Deletion • From the beginning • From the end • Of a given node Algorithm Delnode(Start, x) { Temp1:=Start; while(Data(Temp1)!=x and Temp1 !=NULL) do { Temp2:=Temp1; Temp1:=Link(Temp1); } if (Temp1=NULL) print(x is not present); else { Link(Temp2):=Link(Temp1); Free(Temp1); } }
  • 15. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Doubly Linked List 15 • We can move only in the forward direction in a singly linked list. • We will also store the address of the previous element. • Address of the previous element will be termed as the Left Link and the address of the next element will be termed as Right Link. • Left Link of the first element and the Right Link of the last element will be NULL. • Structure of node will be- • Graphical representation of doubly linked list- LLink Data RLink X X Start
  • 16. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Stack 16 • A stack is a list with the restriction that insertions and deletions can be performed in only one position, namely the end of the list, called the top. • Stacks are called LIFO lists. • Operations- • Insertion(Push) • Deletion(Pop) C B A D E Push Pop Top
  • 17. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Stack-Array Implementation 17 • We store the stack as a one-dimensional array. • The bottommost element is stored at the index 0 (1 in case of pseudo-code notations). • If an element A is stored at index p, then the element just above A and just beneath A will be stored at index p+1 and p-1 respectively. • A variable top is also maintained, it stores the index of the top element and is initialized with -1(0 in case of pseudo-code notations). • The stack of the previous slide can be stored as follows: top A B C D E 0 1 2 3 4 5 6
  • 18. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Stack-Array Implementation-Operations 18 Algorithm Push(S, top, n, value) { if (top=n)// n-1 in programming print(“stack is full”); else { top:=top+1; S[top]:=value; } } Algorithm Pop(S, top, value) { if (top=0) // -1 in programming print(“stack is empty”); else { value:=S[top]; top:=top-1; } }
  • 19. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Stack-Linked Implementation 19 • The stack’s topmost element will be the 1st node of the linked list. • Element just beneath the element in the stack will be the next node in the linked list. • The bottommost element of the stack will be the last node of the linked list. • A pointer top is also maintained, which stores the address of the top element. top E D X A B C
  • 20. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Stack-Linked Implementation-Operations 20 Algorithm PUSH(Top, ele) { getnode(I); Data(I):=ele; Link(I):=Top; Top:=I; } Algorithm POP(Top) { Temp:=Top; Top:=Link(Top); Free(Temp); }
  • 21. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Queue 21 • A queue is a list with the restriction that insertions can be performed in only one position, called the rear and deletions can be performed at only one position, called the front. Queues are called FIFO lists. • Operations- • Insertion • Deletion front rear A B C D E
  • 22. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Queue-Array Implementation 22 • We store the queue as a one-dimensional array. • If an element A is stored at index p, then the element next to A and before A will be stored at index p+1 and p-1 respectively. • Two variables front and rear are also maintained, the rear stores the index of the rear element and is initialized with -1. (0 in case of pseudocode notation) • Front stores a value that is 1 less than the index of the front element and is initialized with -1. front rear A B C D E 0 1 2 3 4 5 6 7
  • 23. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Queue-Array Implementation-Operations 23 Algorithm Insert(Q, rear, n, value) { if (rear=n)//n-1 in case of //programming language print(“queue is full”); else { rear:=rear+1; Q[rear]:=value; } } Algorithm Delete(Q, rear, front, value) { if (rear=front) print(“queue is empty”); else { front:=front+1; value:=Q[front]; } }
  • 24. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Queue-Linked Implementation 24 • The element at the front end of the queue will be the 1st node of the linked list. • Next to the element in the queue will be the next node in the linked list. • The element at the rear end of the queue will be the last node of the linked list. • A pointer front is also maintained, which stores the address of the front element. • A pointer rear is also maintained, which stores the address of the rear element. Front Rear B X E D C A
  • 25. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Queue-Linked Implementation-Operations 25 Algorithm Insertion( rear, value) { Getnode(I); Data(I)=value; Link(I)=NULL; Link(rear)=I; rear=I; } Algorithm Deletion(front) { Temp:=front; front:=Link(front); Free(Temp); }
  • 26. MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT & ENGINEERING, SHIRPUR Thank You…!!! 26

Editor's Notes

  1. Why study Data Structure and algorithm together?