Dec 11, 2013 at 11:10pm UTC
hi all
if i want to sort my list in ascending order how can i do it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#ifndef ORDLIST_H
#define ORDLIST_H
#include<iostream>
using namespace std;
template <class T>
class ordlist
{
public :
ordlist(int size1);
~ordlist();
void insert(const T& item);
void remove(const T& item);
void removeat(int location);
int search(const T& item);
bool isEmpty();
bool isFull();
ordlist(const ordlist<T>&);
const ordlist<T>& operator =(const ordlist<T>&);
void print();
private :
int max_size;
int size;
T *list;
};
template <class T>
ordlist<T>::ordlist(int size1)
{
if (size1<0)
{cout<<"The size must be positive" <<endl;
max_size=100;}
else
max_size=size1;
size=0;
list=new T[max_size];
}
template <class T>
ordlist<T>::~ordlist()
{
delete [] list;
}
template <class T>
void ordlist<T>::insert(const T& item)
{
}
template <class T>
void ordlist<T>::remove(const T& item)
{
int location;
if (size==0)
cout<<"can't delete from an empty array" <<endl;
else
{location=search(item);
if (location!=-1)
removeat(location);
else
cout<<"The item is not found" <<endl;}
}
template <class T>
void ordlist<T>::removeat(int location)
{
if (location<0 || location>=size)
cout<<"cannot delete" <<endl;
else
{for (int i=location;i<size-1;i++)
list[i]=list[i+1];
size--;
}
}
template <class T>
int ordlist<T>::search(const T& item)
{
int location;
bool found=false ;
for (location=0;location<size;location++)
if (list[location]==item)
{
found=true ;
break ;
}
if (found)
return location;
else
return -1;
}
template <class T>
bool ordlist<T>::isEmpty()
{
return (size==0);
}
template <class T>
bool ordlist<T>::isFull()
{
return (size==max_size);
}
template <class T>
ordlist<T>::ordlist(const ordlist<T>&a)
{
max_size=a.max_size;
size=a.size;
list= new T[max_size];
for (int i=0;i<size;i++)
list[i]=a.list[i];
}
template <class T>
const ordlist<T>& ordlist<T>::operator =(const ordlist<T>&a)
{
if (this !=&a)
{
delete []list;
max_size=a.max_size;
size=a.size;
list=new T[max_size];
for (int i=0;i<size;i++)
list[i]=a.list[i];
}
return *this ;
}
template <class T>
void ordlist<T>::print()
{
for (int i=0;i<size;i++)
cout<<list[i]<<" " <<endl;
}
#endif
and i want to use it with insert
Last edited on Dec 12, 2013 at 12:20am UTC
Dec 12, 2013 at 12:31am UTC
You might as well just search for the position at which the element should be inserted (i.e. the point at which the previous element is less than or equal and the next element is greater than or equal to the element being inserted). Re-sorting the entire list after adding just one element at the end will result in a lot of unnecessary comparisons.
Dec 12, 2013 at 12:33am UTC
mmmm can you wrote to me in insert function ?