Loading....
Coupon Accepted Successfully!

 

Lists

The list is a frequently used feature. It allows a bi-directional linear list. Element can be inserted or deleted at both the ends. The elements in the list can be accessed sequentially. Iterators are used for accessing individual elements of the list.

The list class supports various functions and they are listed in Table 20.8.

Table 20.8 Functions of list class

Function

Task

clear( )

Erases entire elements

back( )

Provides reference to the end elements

empty( )

Determines if the list is vacant or not

begain( )

Provides reference to the first element

erase( )

Erases given element

end( )

Provides reference of the end element of the list

merge( )

Combines two sorted lists

pop_fornt( )

Erases the first elements

pop_back( )

Erases the last elements

remove( )

Erases elements as specified

reverse( )

Reverse the list elements

insert( )

Adds given element

push_back( )

Appends an element to the end

push_front( )

Appends an element to the front

size( )

Provides the sizes of the list

unique( )

Erases the identical elements in the list

resize( )

Changes the size of the list

swap( )

Swaps the element of a list with those in the calling list

sort( )

Sorts the list elements

 

20.9 Write a program to create list of four numbers and display them.

#include<iostream>

#include<list>

using namespace std;

void show( list <int> &num)

{ list<int> :: iterator n;

for (n=num.begin();n!=num.end(); ++n)

cout<<*n<<“”;

}

int main()

{

list <int> list;

list.push_back(5);

list.push_back(10);

list.push_back(15);

list.push_back(20);

cout<<“Numbers are”;

show(list);

return 0;

}

OUTPUT

Numbers are 5 10 15 20

 

 

 

Explanation: In the above program, list is the object of list container class that is able to hold integer values. The push_back() functions adds elements to the list. Four integer numbers are added in the list. The show() function is used to display the numbers on the screen. The num reference variable receives the reference of the first element. The iterator n is used to display the numbers. It is used exactly as a pointer.

 

20.10 Write a program to add element to both ends of the list and display the numbers.

#include<iostream>

#include<list>

using namespace std;

void show( list <int> &num)

{

list<int> :: iterator n;

for (n=num.begin();n!=num.end(); ++n)

cout<<*n<<“”;

}

int main()

{

list <int> list;

list.push_back(5);

list.push_back(10);

list.push_back(15);

list.push_back(20);

cout<<“\nNumbers are”;

show(list);

list.push_front(1);

list.push_back(25);

cout<<“\nAfter adding Numbers are”;

show(list);

return 0;

}

OUTPUT

Numbers are 50 10 15 20

After adding Numbers are 1 5 10 15 20 25

 

 

 

Explanation: The above program is similar to the previous one. In addition, here two elements are added to the front and rear ends of the list. The function push.front() adds elements to the front end of the list, and the function push_back() appends elements to the rear end of the list. The output shows all the elements of the list.

 

20.11 Write a program to delete elements from the list.

#include<iostream>

#include<list>

using namespace std;

void show( list <int> &num)

{

list<int> :: iterator n;

for (n=num.begin();n!=num.end(); ++n)

cout<<*n<<“”;

}

int main()

{

list <int> list;

list.push_back(5);

list.push_back(10);

list.push_back(15);

list.push_back(20);

cout<<“\nNumbers are ”;

show(list);

list.pop_front();

list.pop_back();

cout<<“\nAfter deleting Numbers are”;

show(list);

return 0;

}

 

OUTPUT

Numbers are 5 10 15 20

After deleting Numbers are 10 15

 

 

Explanation: In the above program, the member function pop_front( ) is used to delete the front element of the list, and the function pop.back() is used to delete the back element of the list. The output shows the list of numbers.

 

20.12 Write a program to sort the list elements and display them.

 

#include<iostream>

#include<list>

using namespace std;

void show( list <int> &num)

{

list<int> :: iterator n;

for (n=num.begin();n!=num.end(); ++n)

cout<<*n<<“ ”;

}

int main()

{

list <int> list;

list.push_back(23);

list.push_back(19);

list.push_back(5);

list.push_back(15);

list.push_back(25);

list.push_back(20);

cout<<“\n Unsorted list:”;

show(list);

cout<<“\n Sorted list:”;

list.sort();

show(list);

return 0;

}

 

OUTPUT

Unsorted list :23 19 5 15 25 20

Sorted list : 5 15 19 20 23 25

 

 

Explanation: In the above program, the push_back() function is used to add elements to the list object. The function sort() is used to sort the list element in ascending order. The program shows the list of elements in sorted and unsorted elements using the show() function.

 

20.13 Write a program to display the elements in reverse order.

#include<iostream>

#include<list>

using namespace std;

void show( list <int> &num)

{

list<int> :: iterator n;

for (n=num.begin();n!=num.end(); ++n)

cout<<*n<<“ ”;

}

int main()

{

list <int> list;

list.push_back(23);

list.push_back(19);

list.push_back(5);

list.push_back(15);

list.push_back(25);

list.push_back(20);

cout<<“\n Original list:”;

show(list);

cout<<“\n Reverse list:”;

list.reverse();

show(list);

return 0;

}

 

OUTPUT

Original list :23 19 5 15 25 20

Reverse list : 20 25 15 5 19 23

 

 

Explanation: The above program is similar to a previous one. Here, the reverse() function arranges the list elements in reverse order. The output shows the list of elements in original and reversed order.

 

20.14 Write a program to copy elements of one list object to another object. Display the contents of both the objects.

#include<iostream>

#include<list>

using namespace std;

void show( list <int> &num)

{

list<int> :: iterator n;

for (n=num.begin();n!=num.end(); ++n)

cout<<*n<<“”;

}

int main()

{

list <int> listX,listY;

listX.push_back(23);

listX.push_back(19);

listX.push_back(5);

listX.push_back(15);

listX.push_back(25);

listX.push_back(20);

listY=listX;

cout<<“\n Elements of listX:”;

show(listX);

cout<<“\n Elements of listY:”;

show(listY);

return 0;

}

 

OUTPUT

Elements of listX :23 19 5 15 25 20

Elements of listY :23 19 5 15 25 20

 

 

Explanation: In the above program, listX and listY are two objects of the list container. The listX is initialized with six integer elements using the push_back() functions. The statement listY = listX copies elements of the listX object to the listY object.

 

20.15 Write a program to merge two lists and display the merged list.

#include<iostream>

#include<list>

using namespace std;

void show( list <int> &num)

{

list<int> :: iterator n;

for (n=num.begin();n!=num.end(); ++n)

cout<<*n<<“”;

}

int main()

{

list <int> listX,listY;

listX.push_back(23);

listX.push_back(19);

listX.push_back(5);

listY.push_back(15);

listY.push_back(25);

listY.push_back(20);

cout<<»\n Elements of listX:»;

show(listX);

cout<<»\n Elements of listY:»;

show(listY);

listX.merge(listY);

cout<<»\n Merged list: «;

show(listX);

return 0;

}

 

OUTPUT

Elements of listX :23 19 5

Elements of listY :15 25 20

Merged list : 15 23 19 5 25 20

 

 

Explanation: In the above program, the objects listX and listY are initialized with three objects each with the function push_back(). The merge() function merges the elements of two list objects into one. The elements are merged in the calling object. Thus, in this program,listX calls the function, and listY is sent as an argument. The listX contains its own elements and the elements of listY. In the output, the contents of objects listX and listY are displayed.





Test Your Skills Now!
Take a Quiz now
Reviewer Name