Lists - Are Sequence Containers That Allow Constant Time Insert and Erase Operations Anywhere

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

DATA STRUCTURES AND ALGORITHM

Lists - are sequence containers that allow constant time insert and erase operations anywhere
within the sequence, and iteration in both directions. As compared to vector (a type of array that
is one dimensional), list has slow traversal, but once a position has been found, insertion and
deletion are quick.
Below program show how to create a List in C++.
#include <iostream>
#include <list>//List library to be included in the program
using namespace std;

//function for printing the elements in a list


void showlist(list <int> g)
{
list <int> :: iterator it;
for(it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}

int main()
{
list <int> gqlist1; //declare your list container
//Use for loop to iterate numbers from 1 to 10 and list each number to the list.
for (int i = 1; i <= 10; ++i)
{
gqlist1.push_back(i); //insert the value of i to list
}
cout << "\nList 1 (gqlist1) is : ";
showlist(gqlist1); //call the function that will print items of our list
return 0;
}

02 Handout Page 1 of 4
DATA STRUCTURES AND ALGORITHM

Output:

Common functions/methods used with List:


front() – Returns the value of the first element in the list.
Syntax: listcontainername.front();
Example: cout << gqlist1.front(); //this will display 1 as 1 is the first element of list gqlist1.

back() – Returns the value of the last element in the list .


Syntax: listcontainername.back();
Example: cout << gqlist1.back(); //this will display 10 as 10 is the last element of list gqlist1.

push_front(g) – Adds a new element ‘g’ at the beginning of the list .


Syntax: listcontainername.push_front(valuetoinsert);
Example: gqlist1.push_front(0); //this will insert 0 at the start of the list gqlist1.

push_back(g) – Adds a new element ‘g’ at the end of the list.


Syntax: listcontainername.push_back(valuetoinsert);
Example: gqlist1.push_back(11); //this will insert 11 at the end of the list gqlist1.

pop_front() – Removes the first element of the list, and reduces size of the list by 1.
Syntax: listcontainername.pop_front();
Example: gqlist1.pop_front(); //this will remove the first element of list gqlist1 which is 1.

pop_back() – Removes the last element of the list, and reduces size of the list by 1
Syntax: listcontainername.pop_back();
Example: gqlist1.pop_back(); //this will remove the last element of list gqlist1 which is 10.

02 Handout Page 2 of 4
DATA STRUCTURES AND ALGORITHM

empty() – Returns whether the list is empty(1) or not(0).


Syntax: listcontainername.empty();
Example: gqlist1.empty(); //this will return 0 since list gqlist is not empty.

insert() – Inserts new elements in the list before the element at a specified position.
Syntax: listcontainername.insert(iterator,valuetoinsert);
Example: list<int>::iterator x; //declare first an iterator
x=gqlist1.begin(); //set iterator to the beginning position of the list which will
be 0.
advance(x,2); //if you would like to insert after beginning position,
advance the iterator to position you would like to insert.
gqlist1.insert(x,7); // 7 will be inserted at index 2 of the list gqlist1.

erase() – Removes a single element or a range of elements from the list.


Syntax: listcontainername.erase(iterator);
Example: list<int>::iterator x; //declare first an iterator
x=gqlist1.begin(); //set iterator to the beginning position of the list which will
be 0.
advance(x,2); //if you would like to erase item after beginning position,
advance the iterator to position you would like to erase.
gqlist1.erase(x); // element at index 2 of the list gqlist1 will be erased.

reverse() – Reverses the list.


Syntax: listcontainername.reverse();
Example: gqlist1.reverse(); //reverse order of the list gqlist1. Display will start from 10
to 1.

sort() – Sorts the list in increasing order.


Syntax: listcontainername.sort();
Example: gqlist.sort(); //sorting list items in ascending order. Display will now start
from 1 to 10

02 Handout Page 3 of 4
DATA STRUCTURES AND ALGORITHM

size() – Returns the number of elements in the list.


Syntax: listcontainername.size();
Example: cout << gqlist1.size(); //displays the size of the list which is now 10.

assign() – Assigns new elements to list by replacing current elements and resizes the list.
Syntax: listcontainername.assign(numofitems, valuetoassign);
Example: gqlist.assign(3,7); //assign a new items to list. Now we have three (3) number
7 in our list.

remove() – Removes all the elements from the list, which are equal to given element.
Syntax: listcontainername.remove(valuetoremove);
Example: gqlist.remove(7); //removes all 7 in our list. The list is now empty.

References:
class template std::list. Retrieved January 10, 2020 at
http://www.cplusplus.com/reference/list/list/
List in C++ Standard Template Library (STL). Retrieved January 10, 2020 at
https://www.geeksforgeeks.org/list-cpp-stl/

02 Handout Page 4 of 4

You might also like