You are on page 1of 14

Lab no # 01

Submitted by:
Name Alisha
Roll no 21011556-115
Section C
Batch 2021

Submitted to:
Sir Saqib Ali Shah
Department:
Information Technology
ACTIVITY NO # 01
Question No 01:
Different data structures and their applications in computer science and
IT(Computing)?
Data structures are fundamental concepts in computer science and have numerous applications in
various fields of IT. They provide a way to organize and store data efficiently, which is crucial
for the development of algorithms and software applications. Here are some common
applications of data structures in computer science and IT:
1. Arrays: Arrays is an essential data structure in computer programming and are used
in many applications and websites.
 An array can be used to store a list of usernames and passwords in a secure manner. Each
username and password can be stored as a separate element in the array, and the array can be
accessed to authenticate users when they log in.
For example:
E-commerce websites ,game development ,social media platforms,
Chat applications etc.
In general, arrays are useful whenever there is a need to store and manipulate a collection of
related data elements.

1. Linked lists: Linked lists are a fundamental data structure used in many applications and
websites. They are commonly used in situations where a collection of data needs to be stored,
accessed, and manipulated efficiently.
Memory management: Linked lists are used by operating systems to manage memory by
maintaining a list of free memory blocks that can be allocated when needed.
Web browsers: Linked lists are used to implement the back and forward navigation feature in
web browsers, allowing users to navigate between visited pages.
Text Editors: Linked lists can be used to implement undo/redo functionality, where each change
to the text is represented by a node in the linked list.
2. Stack: In computer science and data structures, a stack is a collection of elements that
supports two main operations: push, which adds an element to the top of the stack, and pop,
which removes the most recently added element from the top of the stack. This ordering of
elements is known as Last In First Out (LIFO).
Stacks are used in many different applications, including but not limited to:
Function call stack: used by programming languages to keep track of function calls and their
parameters.
Web browsing history: used by web browsers to keep track of the pages visited in the order
they were accessed.
Undo/redo functionality: used by many applications to allow users to undo or redo their
actions.
3. Queue: In computer science and data structures, a queue is a linear data structure that
represents a sequence of elements arranged according to a particular order of operations. It
operates on a first-in, first-out (FIFO) basis, meaning that the element that is added first is the
first one to be removed.
Operating systems: A queue can be used to manage a list of processes waiting to be performed
by the CPU.
Online ticket booking systems: A queue can be used to manage customers waiting to buy
tickets.
Online shopping: A queue can be used to manage orders waiting to be processed and shipped.
4. Trees: In computer science, a tree is a hierarchical data structure that consists of nodes
connected by edges. Each node in the tree represents a value or an object, and the edges
represent the relationships between them.
In a tree, there is a single node called the root node, which has zero or more child nodes
connected to it. Each child node may have zero or more child nodes of its own, forming a
hierarchical structure.
Trees are used in a variety of applications in computer science, including:
File systems: A file system uses a tree data structure to organize files and directories on a
computer's storage devices.
Web development: HTML documents are represented as trees, with the root element as the root
node and its child elements as its children.
Programming languages: Abstract syntax trees (ASTs) are used to represent the structure of
source code in programming languages.
Networking: Trees are used in network routing algorithms to efficiently route data between
nodes.
Overall, trees are useful in many applications such as searching and sorting algorithms, database
systems, and compilers.
5. Graphs: In computer science, a graph is a non-linear data structure that consists of a set of
vertices (also called nodes) and a set of edges that connect them. A vertex represents an
object or a concept, while an edge represents the relationship or connection between two
vertices.

Graphs can be used to model a wide range of real-world scenarios, such as social networks,
transportation systems, and computer networks. For example, in a social network, each user can
be represented by a vertex, and the edges between them can represent their friendships or
connections.
Graphs can be implemented in various programming languages and can be applied in many
different applications, including:
Social media networks: Graphs can be used to represent connections between users in social
media networks such as Facebook, Twitter, and LinkedIn.
Recommendation systems: Graphs can be used to model user behavior and interests, which can
be used to make personalized recommendations.
Knowledge representation: Graphs can be used to represent knowledge in various domains,
such as medicine, finance, and law.
Graphs are useful in many applications such as network routing algorithms, social network
analysis, and web search algorithms.

Question no 02:
Representation of each data structure pros and cons?
Here are some pros and cons of commonly used data structures:
1. Arrays:
Pros:
Provide constant-time access to any element in the array.
Require less memory overhead than some other data structures, such as linked lists.
Can be used to implement other data structures.
Cons:
Resizing an array can be expensive, especially if the array is large.
Inserting or deleting elements in the middle of an array can be expensive, as all subsequent
elements must be shifted.
2. Linked Lists:
Pros:
Allow for efficient insertion and deletion of elements, even in the middle of the list.
Can be easily resized by adding or removing nodes.
Do not require contiguous memory allocation, which can be useful in situations where memory
is fragmented.
Cons:
Accessing elements in the middle of a linked list can be slow, as each node must be traversed
sequentially.
Require more memory overhead than arrays, as each node contains a pointer to the next node.
Cannot be used to implement constant-time random access.
3. Stacks:
Pros:
Allow for efficient insertion and deletion of elements at one end (the top).
Can be used to implement other data structures, such as a depth-first search algorithm.
Cons:
Do not allow for efficient access to elements in the middle or at the bottom of the stack.
Cannot be used to implement constant-time random access.
4. Queues:
Pros:
Allow for efficient insertion and deletion of elements at opposite ends (the front and the back).
Can be used to implement other data structures, such as a breadth-first search algorithm.
Cons:

Do not allow for efficient access to elements in the middle of the queue.
Cannot be used to implement constant-time random access
5. Trees:
Pros:
Searching in a binary search tree is very efficient, with a time complexity of O(log n).
Insertion and deletion operations in a binary search tree are efficient.
Trees use memory efficiently.
Cons:
If a binary search tree becomes unbalanced, the time complexity of operations can increase
significantly.
Trees only support a limited set of operations.
6. Graphs:
Pros:
Graphs can model a wide range of relationships between objects.
Graphs are used to solve problems in a wide range of fields, including computer networking,
social networks, and transportation networks.
Graphs can represent other data structures, such as trees, by using special types of graphs.
Cons:
Graphs can require a lot of memory to store the nodes and edges.
Many graph algorithms have high computational complexity, which can be a challenge when
working with large graphs.

ACTIVITY NO # 02
Question no 03:
Write a program in C++ to insert an element at the end of an array.
#include<iostream>
using namespace std;
int main(){
int arr[6], i, elem;
cout<<"Enter 5 Array Elements: ";
for(i=0; i<5; i++)
cin>>arr[i];
cout<<"\nEnter Element to Insert: ";
cin>>elem;
arr[i] = elem;
cout<<"\nThe New Array is:\n";
for(i=0; i<6; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}

Output:

Question no 04:
To insert an element in an array in C++ programming, you have to ask the
user to enter the size and elements of the array. And then ask to enter the
element to insert and at what position.
#include<iostream>
using namespace std;
int main(){
int arr[50], i, elem, pos, tot;
cout<<"Enter the Size for Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
cout<<"\nEnter Element to Insert: ";
cin>>elem;
cout<<"At What Position ? ";
cin>>pos;
for(i=tot; i>=pos; i--)
arr[i] = arr[i-1];
arr[i] = elem;
tot++;
cout<<"\nThe New Array is:\n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}

Output:
ACTIVITY NO # 03
Question no # 05:
#include<stdio.h>
int main()
{
int i,t,a[10],n,m,s,j=0,b[10];
printf("\nEnter the Limit:");
scanf("%d",&n);
printf("\nEnter the Values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nGiven values are:");
for(i=0;i<n;i++)
{
printf("a[%d]=%d",i,a[i]);
}
printf("\nEnter the position to be update:");
scanf("%d",&t);
printf("\nEnter the value to be update:");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(i==t)
{
a[i]=s;
}
}
printf("\nUpdated value is:");
for(i=0;i<n;i++)
{
printf("\na[%d]=%d",i,a[i]);
}
return 0;
}
Convert the given code into C++ language.
#include<iostream>
using namespace std;
int main()
{
int i,t,a[10],n,m,s,j=0,b[10];
cout<<"\nEnter the Limit:";
cin>>n;
cout<<"\nEnter the Values:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nGiven values are:";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
cout<<"\nEnter the position to be update:";
cin>>t;
cout<<"\nEnter the value to be update:";
cin>>s;
for(i=0;i<n;i++)
{
if(i==t)
{
a[i]=s;
}
}
cout<<"\nUpdated value is:";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<endl;
}
return 0;
}

ACTIVITY NO # 04
Write a code of Factorial by using loop. Also its algorithm..
 Algorithm:
1. Start the program.
2. Declare and initialize the variables number and factorial. Set factorial to 1 initially.

3. Print the line "Enter any number: " to prompt the user to enter a number.

4. Read the input from the user and store it in the variable number using cin.

5. Check if the entered number is less than 0. If it is, print the message "Can't find the factorial
for negative numbers."

6. If the number is not negative, check if it is less than or equal to 1. If it is, output the factorial
as number! = 1.

7. If the number is greater than 1, enter the else block.

8. Initialize a loop with a counter variable counter set to the value of number. Repeat the loop as
long as counter is greater than or equal to 2. Decrement counter by 1 in each iteration.

9. Multiply the factorial variable by the current value of counter in each iteration and update the
factorial.

10. After the loop ends, output the factorial as number! = factorial.

11. End the program.

 Code of given program:


#include<iostream>
using namespace std;
int main(){
int number,factorial=1;
cout<<"Enter any number: ";
cin>>number;

if(number<0)
cout<<"Cant find the factorial for negative numbers.";

else if(number<= 1)
cout<<number<<"!="<<factorial<<endl;

else{
for(int counter=number; counter>=2; counter--){

factorial=factorial * counter;
}
cout<<number<<"!="<<factorial<<endl;
}
return 0;
}

Output:

You might also like