'Sorting linked list in ascending order' program is given below:
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////
struct node{
int data;
node *next;
};
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
class ll{
public:
ll();
void insert();
void print();
private:
node *head, *tail;
};
/////////////1////////////////////////////////////////////////////////////////////////////////
ll::ll(){
head = tail = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////
void ll::insert(){
node *temp = new node;
temp->next = NULL;
cout << "Id: "; cin >> temp->data;
if (head == 0 && tail == 0){
head = temp;
tail = temp;
}
else{
if (temp->data <= head->data){
//Insert at head
temp->next = head;
head = temp;
}
else{
if (temp->data >= tail->data){
//insert at tail
tail->next = temp;
tail = temp;
}
else{// Insert some where in the middle.
node *p = head;
while (temp->data > p->data){//finding where to insert.
p = p->next;
}
//we've to insert at node before p.
//so
for (node *b = head; b->next != p; b = b->next);
//insert after bth node.
temp->next = b->next;
b->next = temp;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
void ll::print(){
for (node *p = head; p!=NULL; p=p->next){
cout << p->data << endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
//*******************************************************************************************
int main(){
ll o;
cout << "Do you want to insert? (y/n) "; char opt; cin >> opt;
while (opt == 'y' || opt == 'Y'){
o.insert();
cout << "Do you want to insert? (y/n) "; cin >> opt;
}
o.print();
return 0;
}
//*******************************************************************************************
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////
struct node{
int data;
node *next;
};
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
class ll{
public:
ll();
void insert();
void print();
private:
node *head, *tail;
};
/////////////1////////////////////////////////////////////////////////////////////////////////
ll::ll(){
head = tail = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////
void ll::insert(){
node *temp = new node;
temp->next = NULL;
cout << "Id: "; cin >> temp->data;
if (head == 0 && tail == 0){
head = temp;
tail = temp;
}
else{
if (temp->data <= head->data){
//Insert at head
temp->next = head;
head = temp;
}
else{
if (temp->data >= tail->data){
//insert at tail
tail->next = temp;
tail = temp;
}
else{// Insert some where in the middle.
node *p = head;
while (temp->data > p->data){//finding where to insert.
p = p->next;
}
//we've to insert at node before p.
//so
for (node *b = head; b->next != p; b = b->next);
//insert after bth node.
temp->next = b->next;
b->next = temp;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
void ll::print(){
for (node *p = head; p!=NULL; p=p->next){
cout << p->data << endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
//*******************************************************************************************
int main(){
ll o;
cout << "Do you want to insert? (y/n) "; char opt; cin >> opt;
while (opt == 'y' || opt == 'Y'){
o.insert();
cout << "Do you want to insert? (y/n) "; cin >> opt;
}
o.print();
return 0;
}
//*******************************************************************************************
No comments:
Post a Comment