NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include<iostream>

using namespace std;

class LinkedList;

class Node
{


public:
int data;
Node *next;




Node()
{
data=0;
next=NULL;
}

Node(int a)
{
data=a;
next=NULL;
}

void setdata(int data)
{
this->data=data;
this->next=NULL;
}

int getdata()
{
return data;
}
};


class LinkedList:Node
{
public:
Node *head;
void add(Node *n);
void display();
void selectionsort();
void insertionsort();
void bubblesort();
void search();
void merge(LinkedList l1, LinkedList l2);



LinkedList()
{
head=NULL;
}
};

void LinkedList::add(Node *n)
{
if(head==NULL)
{
head=n;
n->next=NULL;
}
else
{
Node *ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=n;
}
};

void LinkedList::display()
{
Node *ptr = head;
while(ptr !=NULL){
cout<<ptr->data<<" ";
ptr = ptr ->next;
}
cout<<endl;
}
void LinkedList::selectionsort()
{
Node *temp=new Node();
Node *min=new Node();
Node *ptr=new Node();
Node *curr_ptr=new Node();
ptr=head;

while(ptr->next!=NULL)
{//iterate from first to the second last element.
min=ptr;
curr_ptr=ptr->next;//set curr_ptr point at the next element.
while(curr_ptr!=NULL)//iterate from next of ptr element to the last element.
{
if(min->data>curr_ptr->data)//if minimum data is greater than ptr data set new minimum.
{
min=curr_ptr;

}
curr_ptr=curr_ptr->next;
}
temp->data=min->data;
min->data=ptr->data;
ptr->data=temp->data;
ptr=ptr->next;
}
}
void LinkedList::merge(LinkedList l1, LinkedList l2)
{
Node *ptr = l1.head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=l2.head;
this->head=l1.head;
l1.head=NULL;
l2.head=NULL;
}
void LinkedList::search()
{
int number;
int count=0;
Node *ptr=head;

if(head==NULL)
{
cout<<"LinkedList is Empty";

}
else
{
cout<<"Enter the number to search: ";
cin>>number;

while(ptr!=NULL)
{
count++;
if((ptr->data,number)==0)
{
cout<<"Number Foundn";
cout<<"Position = "<<count<<endl;
break;
}
ptr=ptr->next;
if(ptr==NULL)
{
cout<<"Number not foundn";
break;
}
}
}

}
void LinkedList::insertionsort()
{
Node *ptr=head;
int i=0;
int j;
while(ptr!=NULL)
{
Node *tmp=head;
for(j=0;j<i;j++)
{
if(ptr->data<tmp->data)
{
//move data front.
int temp=ptr->data;
ptr->data=tmp->data;
tmp->data=temp;
}
tmp=tmp->next;
}
i++;
ptr=ptr->next;
}
}
void LinkedList::bubblesort()
{
Node *base, *cur;
Node *ptr=this->head;
while(ptr->next!=NULL)
{
base=this->head;
cur=this->head->next;
while(cur!=NULL)
{
if(base->data>cur->data)
{
int temp= base->data;
base->data=cur->data;
cur->data=temp;
}
base=cur;//always compare next two elements.
cur=cur->next;
}
ptr=ptr->next;
}
}



int main()
{
LinkedList l1;
LinkedList l2;
Node *n1;
Node n2;
int number;
char choice='Y';
int ch;
while(1){
cout<<"n1. Create Linked List.n";
cout<<"2. Display.n";
cout<<"3. Selection Sort.n";
cout<<"4. Insertion Sort.n";
cout<<"5. Bubble Sort.n";
cout<<"6. Search the numbern";
cout<<"7. Merge Linked Lists.nn";
cout<<"Enter your choice: ";
// cout<<"n";
cin>>ch;

switch(ch)
{
case 1:
{
while(choice=='Y' || choice=='y')
{

cout<<"Enter a number: ";
cin>>number;
n1=new Node();
n1->setdata(number);
l1.add(n1);
cout<<"Do you want to add another node?(Y/N)";
cin>>choice;

}
break;
}
case 2:
{
if(l1.head==NULL){
cout<<"The linked list is NULLnn";
}
else{
l1.display();
}
break;
}
case 3:
{
if(l1.head==NULL)
{
cout<<"The Linked List is empty.nn";
}
else
{
l1.selectionsort();
cout<<"Selection sort completed successfully.nn";
l1.display();
}
break;
}
case 4:
{
if(l1.head==NULL)
{
cout<<"The Linked List is empty.nn";
}
else
{
l1.insertionsort();
cout<<"Insertion sort completed successfully.nn";
l1.display();
}
break;
}

case 5:
{
if(l1.head==NULL)
{
cout<<"The Linked List is empty.nn";
}
else
{
l1.bubblesort();
cout<<"nBubblesort completed successfully.n";
l1.display();
}
break;
}
case 6:
{
l1.search();
break;
}

case 7:

while(choice=='Y' || choice=='y')
{

cout<<"Enter a number: ";
cin>>number;
n1=new Node();
n1->setdata(number);
l2.add(n1);
cout<<"Do you want to add another node?(Y/N)";
cin>>choice;

}
l1.merge(l1,l2);
break;

default:
break;
}

}

return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.