Home Uncategorized Write a C++ Program to implement List and List Operations

Write a C++ Program to implement List and List Operations


AIM: Write a program to illustrate list and list operations.


STL : Standard Template Library

Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence and iteration in both directions.

List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept internally by the association to each element of a link to the element preceding it and a link to the element following it.

They are very similar to forward_list: The main difference being that forward_list objects are single-linked lists, and thus they can only be iterated forwards, in exchange for being somewhat smaller and more efficient.

Compared to other base standard sequence containers (array, vector and deque), lists perform generally better in inserting, extracting and moving elements in any position within the container for which an iterator has already been obtained, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of lists and forward_lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in a list, one has to iterate from a known position (like the beginning or the end) to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements).


using namespace std;
void show(list<int>&num)
    list<int>::iterator n;
        cout<<*n<<” “;
int main()
    cout<<“\n Numbers are: “;
    cout<<“\n Insert elements at the beginning of list”;
    cout<<“\n After inserting elements are:”;
    cout<<“\n Delete elements at the beginning”;
    cout<<“\n Numbers are: “;
    cout<<“\n Delete least element”;
    cout<<“\n Numbers are: “;
    cout<<“\n Elements in reverse order:”;
    return 0;


Numbers are: 5 10           15           20
Insert elements at the beginning of list
After inserting elements are:
25           5              10           15           20
Delete elements at beginning of list
Numbers are: 5 10           15           20
Delete least element
Numbers are: 5 10           15
Elements in reverse order: 15     10           5

Back to Programs.