0% found this document useful (0 votes)
2 views

coLabfile

The document provides an overview of various programming concepts and implementations, including data structures, leap year checks, Fibonacci series, array minimum and maximum values, character frequency, sorting, factorial calculation, linked list operations, a basic calculator, and palindrome checking. Each section includes code snippets and concludes with a statement about the successful execution of the respective task. The explanations emphasize the importance of data organization and efficient algorithms in programming.

Uploaded by

2faltukaam3377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

coLabfile

The document provides an overview of various programming concepts and implementations, including data structures, leap year checks, Fibonacci series, array minimum and maximum values, character frequency, sorting, factorial calculation, linked list operations, a basic calculator, and palindrome checking. Each section includes code snippets and concludes with a statement about the successful execution of the respective task. The explanations emphasize the importance of data organization and efficient algorithms in programming.

Uploaded by

2faltukaam3377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1. What is a Data Structure?

What is the purpose of it


explain in brief ?
A data structure is like a toolbox used by developers to store and
manage their valuable resources of data in an easy accessible format.
By way of example, we regard playing with a lot toys. When you try
to put all your toys into a big box, it becomes really hard for when
ever you want to take it out. Instead, if we had smaller baskets for
action gures and cars and puzzles all seperated.. it would save us the
hassle of sorting through everything! This is literally concept what
data structures do inside comp uses, just storing and organizing the
information so that computer can easily access them.
Those who organize data well in the rst place are able to access that
same information faster. This makes it easier to snag the one we need
when, say, books are arranged by subject.
• Memory ef cient: If your data structure uses less memory, then it
can store more things precisely similar to when you organize the
shelf.

• Simple Management : Keep massive amounts of information in


their place without it getting lost or complex.

Examples of some common data structures are as follows:-


Obviously, this refers to the various types of arrays i.e a row of lockers
with numbers written on it and each locker holds an item.
Lists: A single le to keep the items in shopping list as a one after
another.
Stacks and Queues: Imagine a stack of plates or people lining up for
their turn; the one that entered rst should come out, either from top
(stack) ,used last in First-Out manner or bottom-queueing last-in- rst-
out.

1
fi
fi
fi
fi
fi
fi
2. Take an input year, and check leap year or not.
Code :
#include<iostream>
using namespace std;

bool leapYearCheck(int n){


bool flag = false;
if(n%4 == 0) return flag = true;
else return flag;
}
int main(){
int n;
cout << "Enter the year : ";
cin >> n; cout << endl;
if (leapYearCheck(n) == true) cout<<"It's a
leap year";
else cout <<"It's NOT a leap year";
}

Output :

Conclusion:

Correctly identified leap years based on


divisibility rules.
2
3. Fibonacci series, print it and calculate sum.
Code :

#include<iostream>
using namespace std;
// 1 1 2 3 5 8 13 21 34 55 89 ...
int fibo(int n){
if(n==2||n==1) return 1;
return fibo(n-1)+fibo(n-2); // double call
follows Euler our Tree
}

int main(){
int n, sum = 0;
cin>>n;
for(int i= 1;i<=n;i++){
sum += fibo(i);
cout<<fibo(i)<<" ";
}
cout<<"\nSum : "<<sum;
}

Output :

Conclusion:

Generated Fibonacci sequence and calculated its


sum successfully.

3
4. Find the minimum and maximum of a number in an
array.
Code :
#include<iostream>
#include<climits>
using namespace std;
int minOfArray(int arr[] ,int len){
int min = INT_MAX;
for (int i = 0; i < len; i++){
if(arr[i]< min) min = arr[i];
}
return min;
}
int maxOfArray(int arr[] ,int len){
int max = INT_MIN;
for (int i = 0; i < len; i++){
if(arr[i] > max) max = arr[i];
}
return max;
}
int main(){
int arr[10] = {12,34,5,2,56,87,26,88,54,21};
cout << "Minimum : "<<minOfArray(arr,10)<<
endl;
cout <<"Maximum : " <<maxOfArray(arr,10)<<
endl;}
Output :

Conclusion:

Identified minimum and maximum values in the array


accurately.
4
5. Find the frequency of character in a word.
Code :

#include<iostream>
#include<string>
using namespace std;

void freq(char a, string str){


int count = 0;
for (int i = 0; i < size(str); i++){
if( a == str[i]) count++;
}
cout << "Frequency is : "<<count;
}

int main(){
string str;
char s;
cin >> str >> s;
freq( s , str);
}

Output :

Conclusion:

Accurately calculated character frequencies in a


word.

5
6. Sort the frequency of character of a word and print it
in ascending and descending order.
Code :
#include<bits/stdc++.h>
using namespace std;
string frequencySort(string s) {
unordered_map<char,int>mp;
for(int i=0;i<s.length();i++)
mp[s[i]]++;
priority_queue<pair<int,char>>pq;
for(auto x:mp)
pq.push({x.second,x.first});
string ans="";
while(pq.size())
{
auto x=pq.top();
pq.pop();
for(int i=0;i<x.first;i++)
ans+=x.second;
}
return ans;
}
int main(){
string str;
cout << "Enter the string : “; cin >> str;
cout << endl; cout << frequencySort(str);
}
Output :

Conclusion:
Successfully sorted and displayed character
frequencies.
6
7. Factorial of a number.
Code :

#include<bits/stdc++.h>
using namespace std;

int fac(int n){


if(n == 1) return 1;
return n * fac(n-1);
}

int main(){
int n;
cout << "Enter the number : ";
cin >> n;
cout << "Factorial of "<< n <<" is :
"<<fac(n);
}

Output :

Conclusion:

Correctly calculated the factorial of the number.

7
8. Create a Linked List, print it and do the deletion and
insertion operation on it.
Code :
#include<iostream>
using namespace std;
class node {
public:
int val;
node* next;

node(int val) {
this->val = val;
this->next = NULL;
}
};
class LinkedList {
public:
node* head;
node* tail;
int size;

LinkedList() {
head = NULL;
tail = NULL;
size = 0;
}
void insertAtTail(int val) {
node* temp = new node(val);
if (size == 0) {
head = tail = temp;
} else {
tail->next = temp;
tail = temp;
}
size++;
}
void insertAtHead(int val) {
node* temp = new node(val);
8
if (size == 0) {
head = tail = temp;
} else {
temp->next = head;
head = temp;
}
size++;
}
void insert(int idx, int val) {
if (idx == 1) {
insertAtHead(val);
} else if (idx == size + 1) {
insertAtTail(val);
} else if (idx < 1 || idx > size + 1) {
cout << "INVALID" << endl;
} else {
node* new_node = new node(val);
node* temp = head;
for (int i = 1; i < idx - 1; i++) {
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
size++;
}
}
void deleteAtHead() {
if (size == 0) {
cout << "List is empty" << endl;
} else {
node* temp = head;
head = head->next;
delete temp;
size--;
if (size == 0) tail = NULL;
}
}
void deleteAtTail() {
9
if (size == 0) {
cout << "List is empty" << endl;
} else if (size == 1) {
delete head;
head = tail = NULL;
size--;
} else {
node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
tail = temp;
size--;
}
}
void deleteAt(int idx) {
if (idx == 1) {
deleteAtHead();
} else if (idx == size) {
deleteAtTail();
} else if (idx < 1 || idx > size) {
cout << "INVALID" << endl;
} else {
node* temp = head;
for (int i = 1; i < idx - 1; i++) {
temp = temp->next;
}
node* to_delete = temp->next;
temp->next = temp->next->next;
delete to_delete;
size--;
}
}
void display() {
node* temp = head;
while (temp != NULL) {
10
cout << temp->val << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
LinkedList ll;
ll.insertAtTail(10);
ll.insertAtTail(20);
ll.insertAtTail(30);
ll.insertAtTail(40);
ll.insertAtTail(50);
ll.display();
ll.insert(3,60);
ll.display();
ll.deleteAt(6);
ll.display();
}

Output :

Conclusion:

Implemented insertion and deletion in a Linked


List effectively.

11
9. Program a calculator.
Code :
#include<bits/stdc++.h>
using namespace std;
int main(){
// Calculator
int a, b, calc;
cout << "Enter no. : ";
cin >> a; cout<<endl;
cout << "Enter no. : ";
cin >> b; cout << endl;
cout << "1. Add\n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "4. Divide\n";
cout<<"Calculate : ";
cin >> calc; cout << endl;
switch (calc){
case 1:
cout << "Result : " << a+b; cout << endl;
break;
case 2:
cout << "Result : " << a-b; cout << endl;
break;
case 3:
cout << "Result : " << a*b; cout << endl;
break;
case 4:
cout << "Result : " << a/b; cout << endl;
break;
default:
break;
} }

12
Output :-

Conclusion:

Built a functional calculator for basic


operations.

13
10. Check the string/number is palindrome or not.
Code :
#include<iostream>
#include<string>
using namespace std;
bool check_palindrome(string a){
int len = size(a);
bool flag = true;
for (int i = 0, j = len -1 ; i < j; i++ ,j--){
if(a[i] == a[j]) flag = true;
else flag = false; break;
}
return flag;
}
int main(){
string str ;
cin >> str;
cout << "Palindrome :" <<
check_palindrome(str);
// 1 = true,0 = false
}
Output :

Conclusion:

Accurately checked for palindromic strings and


numbers.
14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy