Lab Dsa
Lab Dsa
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int data){
this->data = data;
this->next = nullptr;
}
};
class LinkedList{
public:
Node* head = nullptr;
Node* tail = nullptr;
void display(){
Node* tmp = head;
while(tmp!=nullptr){
cout << tmp->data << " " ;
tmp = tmp->next;
}
cout << endl;
}
int sz(){
int cnt=0;
Node* tmp = head;
while(tmp!=nullptr){
cnt++;
tmp = tmp->next;
}
return cnt;
}
};
int main(){
LinkedList lst;
lst.insertAthead(3);
lst.insertAthead(2);
lst.insertAthead(1);
lst.insertAthead(0);
lst.display();
lst.insertAttail(5);
lst.insertAttail(6);
lst.insertAttail(7);
lst.display();
cout << lst.sz() << endl;
lst.insertatIndex(4,4);
lst.display();
cout << lst.sz() << endl;
lst.deleteat(8);
lst.display();
cout << lst.sz();
lst.findelement(5);
mergesort
#include<bits/stdc++.h>
using namespace std;
}
while(left<=mid){
tmp.push_back(v[left]);
left++;
}
while(right<=high){
tmp.push_back(v[right]);
right++;
}
for(int i = low; i<=high;i++){
v[i]= tmp[i-low];
}
}
int main(){
int n = 7 ;
vector<int> v ={4,3,2,1,9,8,7};
cout << "Before sorting : ";
for(auto i:v){
cout << i << " ";
}
cout << endl;
sortt(v,0,n-1);
cout << "After sorting : ";
for(auto i:v){
cout << i << " ";
}