data structures - How to insert a item in sorted linked list within constant time complexity? -
i have problem in sorted linked list .i can't insert item in constant time. if possible how can solve it?
and function time complexity big-o(n)
template <class itemtype> void sortedtype<itemtype>::insertitem(itemtype item) { nodetype<itemtype>* newnode; nodetype<itemtype>* predloc; nodetype<itemtype>* location; bool moretosearch; location = listdata; predloc = null; moretosearch = (location != null); while (moretosearch) { if (location->info < item) { predloc = location; location = location->next; moretosearch = (location != null); } else moretosearch = false; } newnode = new nodetype<itemtype>; newnode->info = item; if (predloc == null) { newnode->next = listdata; listdata = newnode; } else { newnode->next = location; predloc->next = newnode; } length++; }
it not possible inset item in sorted linked list within constant time complexity. can insert item in o(log n) time complexity.
Comments
Post a Comment