// Linked Lists in classes(excludi ng structures) without using tail pointer
# include<iostrea m.h>
# include<stdlib. h>
void Swap(int num1, int num2)
{
int a = num1;
num2 = num1;
num2 = a;
}
class node
{
private:
int data;
node* next;
node* headptr;
public:
node()
{headptr = NULL;}
void insert_at_head( int d)
void insert_at_tail( int d);
void display_all();
void sort_ascending_ order(); // complicated
void sort_descending _order(); // complicated
};
///////////////////////////////////////////////////////////////////////////////
void node::insert_at _head(int d)
{
node* ptr = new node;
ptr->data = d;
ptr->next = headptr;
headptr = ptr;
}
void node::display_a ll()
{
node* tempptr = headptr;
if(headptr == NULL)
{cout<<"The list is empty"<<endl;}
while(tempptr->next != NULL)
{
cout<<tempptr->data<<" , ";
tempptr = tempptr->next;
}
if(tempptr->next == NULL)
{cout<<tempptr->data;}
}
void node::insert_at _tail(int d)
{
node* temptr = new node;
temptr->data = d;
temptr->next = NULL;
node* ptrCurrent = headptr;
while(ptrCurren t->next != NULL)
{
ptrCurrent = ptrCurrent->next;
}
ptrCurrent->next = temptr;
}
void node::sort_asce nding_order()
{
node* realptr = headptr;
node* ptrCurrent = headptr;
while(ptrCurren t->next != NULL)
{
if(ptrCurrent->data > ptrCurrent->next->data)
{
realptr->data = ptrCurrent->data;
cout<<realptr->data<<endl;
Swap(ptrCurrent->data,ptrCurren t->next->data);
}
ptrCurrent = ptrCurrent->next;
realptr = realptr->next;
}
}
void main()
{
node n1;
n1.insert_at_he ad(1);
n1.insert_at_he ad(2);
n1.insert_at_he ad(3);
n1.insert_at_ta il();
n1.insert_at_ta il();
n1.display_all( );
cout<<endl<<end l;
cout<<"Testing Sorting"<<endl< <endl;
n1.sort_ascendi ng_order();
cout<<endl<<end l;
n1.display_all( );
}
//void node::sort_asce nding_order() this is not working
Please help me out using singly linked list and do keep the code less complicated. Thankx
# include<iostrea m.h>
# include<stdlib. h>
void Swap(int num1, int num2)
{
int a = num1;
num2 = num1;
num2 = a;
}
class node
{
private:
int data;
node* next;
node* headptr;
public:
node()
{headptr = NULL;}
void insert_at_head( int d)
void insert_at_tail( int d);
void display_all();
void sort_ascending_ order(); // complicated
void sort_descending _order(); // complicated
};
///////////////////////////////////////////////////////////////////////////////
void node::insert_at _head(int d)
{
node* ptr = new node;
ptr->data = d;
ptr->next = headptr;
headptr = ptr;
}
void node::display_a ll()
{
node* tempptr = headptr;
if(headptr == NULL)
{cout<<"The list is empty"<<endl;}
while(tempptr->next != NULL)
{
cout<<tempptr->data<<" , ";
tempptr = tempptr->next;
}
if(tempptr->next == NULL)
{cout<<tempptr->data;}
}
void node::insert_at _tail(int d)
{
node* temptr = new node;
temptr->data = d;
temptr->next = NULL;
node* ptrCurrent = headptr;
while(ptrCurren t->next != NULL)
{
ptrCurrent = ptrCurrent->next;
}
ptrCurrent->next = temptr;
}
void node::sort_asce nding_order()
{
node* realptr = headptr;
node* ptrCurrent = headptr;
while(ptrCurren t->next != NULL)
{
if(ptrCurrent->data > ptrCurrent->next->data)
{
realptr->data = ptrCurrent->data;
cout<<realptr->data<<endl;
Swap(ptrCurrent->data,ptrCurren t->next->data);
}
ptrCurrent = ptrCurrent->next;
realptr = realptr->next;
}
}
void main()
{
node n1;
n1.insert_at_he ad(1);
n1.insert_at_he ad(2);
n1.insert_at_he ad(3);
n1.insert_at_ta il();
n1.insert_at_ta il();
n1.display_all( );
cout<<endl<<end l;
cout<<"Testing Sorting"<<endl< <endl;
n1.sort_ascendi ng_order();
cout<<endl<<end l;
n1.display_all( );
}
//void node::sort_asce nding_order() this is not working
Please help me out using singly linked list and do keep the code less complicated. Thankx
Comment