23-NTU-CS-1170 (Lab 3)
23-NTU-CS-1170 (Lab 3)
Department of Computer
Science
Subject:
DSA
Submitted to:
Sir ARSAL
Submitted by:
Maha Ather
Reg number:
23-NTU-CS-1170
Lab no.:
3
Semester:3
Exercise 1:
Code:
class node{
private:
int data; //data
node* next; //pointer which points to next node
public:
//set data
void setdata(int data){
this->data=data;
}
//set next
void setnext(node* next){
this->next=next;
}
//get data
int getdata(){
return data;
}
//get next
node* getnext(){
return next;
}
};
list class:
class list{
private:
node* first;
node* last;
public:
list(){
first=nullptr;
last=nullptr;
}
Exercise -2:
1. Insert at start:
Code:
void insertATFirst(int value){
node* newnode=new node();
newnode->setdata(value);
if(first==NULL){
first=newnode;
last=newnode;
}else{
newnode->setnext(first);
first=newnode;
}
}
2. Insert at end
Code:
void InsertATEnd(int value){
node* newnode=new node();
newnode->setdata(value);
if(first==NULL){
first=newnode;
last=newnode;
}else{
last->setnext(newnode);
last=newnode;
}
}
3. Insert at middle
Code:
void InsertATMiddle(int value){
node* newnode=new node();
newnode->setdata(value);
newnode->setnext(nullptr);
//check if list is empty
if(first==NULL){
first=newnode;
last=newnode;
}
//count node to check middle
int count=0;
node* temp=first;
while(temp!=0){
temp=temp->getnext();
count++;
}
//find middle
int middle=count/2;
//traverse list
temp=first;
for(int i=0;i<middle-1;i++){
temp=temp->getnext();
}
//insert newnode at middle
newnode->setnext(temp->getnext());
temp->setnext(newnode);
}
4. Delete:
Code:
void deletelist(int value){
if(first==NULL){
cout<<"list is empty"<<endl;
}
node* temp=first;
node* prev=NULL;
if(temp->getdata()==value){
first=first->getnext();
if(first==NULL)
last=NULL;
delete temp;
}
else{
temp=temp->getnext();
}
//specific node
if(temp!=NULL && temp->getdata()!=value){
prev=temp;
temp=temp->getnext();
}
//value not found
if(temp==NULL){
cout<<"value not found"<<endl;
}
prev->setnext(temp->getnext());
//delete at end
if(temp==last){
last=prev;
delete temp;
}
}
5. Addbetween:
Code:
void addbetween(int val1, int val2, int newdata) {
if (first == NULL) {
cout << "List is empty. Cannot insert." << endl;
return;
}
// Insert newNode
newNode->setnext(temp->getnext());
temp->setnext(newNode);
Exercise-4:
1) Finding Minimum
Code:
int min(){
if(first==NULL){
cout<<"list is empty"<<endl;
}
node* temp=first->getnext();
int min=first->getdata();
while(temp!=0){
if(temp->getdata()<min){
min=temp->getdata();
}
temp=temp->getnext();
}
return min;
}
2) Finding Maximum
Code:
int maxno(){
if(first==NULL){
cout<<"list is empty"<<endl;
return -1;
}
int max=first->getdata();
node* temp=first->getnext();
while(temp!=NULL){
if(temp->getdata()>max){
max=temp->getdata();
}
temp=temp->getnext();
}
return max;
}
3) Searching
Code:
int search(int key){
if(first==NULL){
cout<<"list is empty"<<endl;
}
node* temp=first;
while(temp!=NULL){
if(key==temp->getdata()){
cout<<"element found"<<endl;
return key;
break;
}
else{
temp=temp->getnext();
}
}
cout<<"elemet not found"<<endl;
}
4) Inserting Item at desired location (take location
from user)
Code:
void InsertAtLocation(int value,int location){
node* newnode=new node();
newnode->setdata(value);
newnode->setnext(nullptr);
//check if null
if(first==NULL){
first=newnode;
last=newnode;
}
//count list
int count=0;
node* temp=first;
while(temp!=0){
temp=temp->getnext();
count++;
}
//invalid location
if(location>count){
cout<<"invalid location"<<endl;
}
//at start
if(location==0){
newnode->setnext(first);
first=newnode;
}
//at end
if(location==count){
last->setnext(newnode);
last=newnode;
}
//at specific location
temp=first;
for(int i=0;i<location-1;i++){
temp=temp->getnext();
}
//insert
newnode->setnext(temp->getnext());
temp->setnext(newnode);
}
// Insert newNode
newNode->setnext(temp->getnext());
temp->setnext(newNode);
};
class list{
private:
node* first;
node* last;
public:
list(){
first=nullptr;
last=nullptr;
}
void insertATFirst(int value){
node* newnode=new node();
newnode->setdata(value);
if(first==NULL){
first=newnode;
last=newnode;
}else{
newnode->setnext(first);
first=newnode;
}
}
void InsertATEnd(int value){
node* newnode=new node();
newnode->setdata(value);
if(first==NULL){
first=newnode;
last=newnode;
}else{
last->setnext(newnode);
last=newnode;
}
}
void InsertATMiddle(int value){
node* newnode=new node();
newnode->setdata(value);
newnode->setnext(nullptr);
//check if list is empty
if(first==NULL){
first=newnode;
last=newnode;
}
//count node to check middle
int count=0;
node* temp=first;
while(temp!=0){
temp=temp->getnext();
count++;
}
//find middle
int middle=count/2;
//traverse list
temp=first;
for(int i=0;i<middle-1;i++){
temp=temp->getnext();
}
//insert newnode at middle
newnode->setnext(temp->getnext());
temp->setnext(newnode);
}
void InsertAtLocation(int value,int location){
node* newnode=new node();
newnode->setdata(value);
newnode->setnext(nullptr);
//check if null
if(first==NULL){
first=newnode;
last=newnode;
}
//count list
int count=0;
node* temp=first;
while(temp!=0){
temp=temp->getnext();
count++;
}
//invalid location
if(location>count){
cout<<"invalid location"<<endl;
}
//at start
if(location==0){
newnode->setnext(first);
first=newnode;
}
//at end
if(location==count){
last->setnext(newnode);
last=newnode;
}
//at specific location
temp=first;
for(int i=0;i<location-1;i++){
temp=temp->getnext();
}
//insert
newnode->setnext(temp->getnext());
temp->setnext(newnode);
}
void deletelist(int value){
if(first==NULL){
cout<<"list is empty"<<endl;
}
node* temp=first;
node* prev=NULL;
if(temp->getdata()==value){
first=first->getnext();
if(first==NULL)
last=NULL;
delete temp;
}
else{
temp=temp->getnext();
}
//specific node
if(temp!=NULL && temp->getdata()!=value){
prev=temp;
temp=temp->getnext();
}
//value not found
if(temp==NULL){
cout<<"value not found"<<endl;
}
prev->setnext(temp->getnext());
//delete at end
if(temp==last){
last=prev;
delete temp;
}
}
int search(int key){
if(first==NULL){
cout<<"list is empty"<<endl;
}
node* temp=first;
while(temp!=NULL){
if(key==temp->getdata()){
cout<<"element found"<<endl;
return key;
break;
}
else{
temp=temp->getnext();
}
}
cout<<"elemet not found"<<endl;
}
int maxno(){
if(first==NULL){
cout<<"list is empty"<<endl;
return -1;
}
int max=first->getdata();
node* temp=first->getnext();
while(temp!=NULL){
if(temp->getdata()>max){
max=temp->getdata();
}
temp=temp->getnext();
}
return max;
}
int min(){
if(first==NULL){
cout<<"list is empty"<<endl;
}
node* temp=first->getnext();
int min=first->getdata();
while(temp!=0){
if(temp->getdata()<min){
min=temp->getdata();
}
temp=temp->getnext();
}
return min;
}
//bubble sort
void sortlist(){
if(first==NULL){
cout<<"linked list is empty no need to sort"<<endl;
}
node* temp=first;
while(temp!=0){
node* currentnode=temp->getnext();
while(currentnode!=0){
if(currentnode->getdata()<temp->getdata()){
int swap = temp->getdata();
temp->setdata(currentnode->getdata());
currentnode->setdata(swap);
}
currentnode=currentnode->getnext();
}
temp=temp->getnext();
}
}
//swap two nodes
void swapnode(int key1,int key2){
node* node1=NULL;
node* node2=NULL;
if(key1==key2){
cout<<"both key are same no need to sort"<<endl;
}
node* temp=first;
while(temp!=0){
if(temp->getdata()==key1){
node1=temp;
}
temp=temp->getnext();
}
temp=first;
while(temp!=0){
if(temp->getdata()==key2){
node2=temp;
}
temp=temp->getnext();
}
int swap=node1->getdata();
node1->setdata(node2->getdata());
node2->setdata(swap);
}
//add between
void addbetween(int val1, int val2, int newdata) {
if (first == NULL) {
cout << "List is empty. Cannot insert." << endl;
return;
}
// Find val1
while (temp != NULL && temp->getdata() != val1) {
prev = temp;
temp = temp->getnext();
}
// Insert newNode
newNode->setnext(temp->getnext());
temp->setnext(newNode);