Skip to Content
C++ Data Structures and Algorithms
book

C++ Data Structures and Algorithms

by Wisnu Anggoro
April 2018
Intermediate to advanced content levelIntermediate to advanced
322 pages
6h 57m
English
Packt Publishing
Content preview from C++ Data Structures and Algorithms

Finding out the index of a selected item in the List ADT

The Search() method will also iterate each items' list until it finds the matched value. The return value of this method will be the index of List. It will return -1 if no result is found. The implementation should be as follows:

int List::Search(int val){    // Looping through the array elements    // return the array index if value is found    for(int i=0; i < m_count; ++i)    {        if(m_items[i] == val)        {            return i;        }    }    return -1;}

As you can guess, the complexity of this method will be O(n) for the average and worst case scenarios, since it will iterate through all list elements. However, in the best case, it can be O(1) if val is found at the first position.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Data Structures and Algorithms Using C++

Data Structures and Algorithms Using C++

Ananda Rao Akepogu, Radhika Raju Palagiri
C++ Data Structures and Algorithm Design Principles

C++ Data Structures and Algorithm Design Principles

John Carey, Anil Achary, Shreyans Doshi, Payas Rajan
C++ Plus Data Structures, 6th Edition

C++ Plus Data Structures, 6th Edition

Nell Dale, Chip Weems, Tim Richards

Publisher Resources

ISBN: 9781788835213Supplemental Content