0% found this document useful (0 votes)
22 views4 pages

Lab Dsa

This document contains code for implementing a linked list and merge sort in C++. The linked list code defines Node and LinkedList classes to implement a basic linked list with methods for insertion, deletion, finding elements and displaying the list. The merge sort code uses a divide and conquer approach, recursively splitting the vector into halves and then merging the sorted halves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Lab Dsa

This document contains code for implementing a linked list and merge sort in C++. The linked list code defines Node and LinkedList classes to implement a basic linked list with methods for insertion, deletion, finding elements and displaying the list. The merge sort code uses a divide and conquer approach, recursively splitting the vector into halves and then merging the sorted halves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Linkedlist

#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 insertAthead(int data){


Node* tmp = new Node(data);
if(head==nullptr){
head=tmp;
tail=tmp;
}
else{
tmp->next = head;
head = tmp;
}

void insertAttail(int data){


Node* tmp = new Node(data);
if(head==nullptr){
head=tmp;
tail=tmp;
}
else{
tail->next = tmp;
tail = tmp;
}
}

void insertatIndex(int ind,int data){


Node* tmp = new Node(data);
Node* header = head;
if(ind==0){
insertAthead(data);
return;
}
if(ind==sz()){
insertAttail(data);
return;
}
if(ind>sz() || ind<0){
cout << "This is not a valid indext " << endl;
return;
}
for(int i=1;i<=ind-1;i++){
header = header->next;
}
tmp->next = header->next;
header->next = tmp;

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;
}

void deleteat(int ind){


Node* header = head;
if(ind<0 || ind>=sz()){
cout << "Invalid index " << endl;
return;
}
if(ind==0){
head = head->next;
return;
}
for(int i=1;i<=ind-1;i++){
header = header->next;
}
if(ind==sz()-1){
header->next = nullptr;
}
else{
header->next = header->next->next;
}

void findelement(int val){


Node* header = head;
int ind=-1;
while(header!=nullptr){
ind++;
if(val==header->data){
cout << "Index is : " << ind << " and value founded " << endl;
break;
}
header = header->next;
}

};

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();

cout << endl << endl;

lst.findelement(5);

mergesort

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

void mergesort(vector<int>&v,int low,int mid,int high){


vector<int>tmp;
int left = low;
int right = mid+1;
while(left<=mid && right<=high){
if(v[left]<=v[right]){
tmp.push_back(v[left]);
left++;
}
else{
tmp.push_back(v[right]);
right++;
}

}
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];
}
}

void sortt(vector<int>&v,int low,int high){


if(low>=high) return;
int mid = (low+high)/2;
sortt(v,low,mid);
sortt(v,mid+1,high);
mergesort(v,low,mid,high);
}

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 << " ";
}

You might also like