Join us in Developer++: DSA Series which is an C++ workshop series organized to upskill Data Structures & Algorithms concepts and problem solving in C++ Language. This event is going to be hosted by Prof. Piyush Kumar Soni who is an assistant professor at NMIMS MPSTME Shirpur campus with an experience of 9 year. This session is going to cover core topics of DSA like Vector, LinkedList, Stack and Queue, along with their implementation using STL Library.
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
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