எளிய தமிழில் – Data Structures & Algorithms C++ / Python – 05

By | November 27, 2025

C++ List

Descriptions

List holds the data of same type and dynamically increase in size

விரிவுரை

பட்டியல் ஒரே மாதிரியான தரவைக் கொண்டுள்ளது.

அளவு மாறும் வகையில் தன்மை கொண்டது.

To use a list in C++, you have to include the <list> header file:

C++ இல் பட்டியலைப் பயன்படுத்த, <list> தலைப்பு கோப்பை நீங்கள் சேர்க்க வேண்டும்:

 

Create a List

Things to considered when using list, use the list keyword, and specify the type of values it should store within angle brackets <> and then the name of the list, like: list<type> listName.

பட்டியலைப் பயன்படுத்தும் போது கருத்தில் கொள்ள வேண்டிய விஷயங்கள், list முக்கிய சொல்லைப் பயன்படுத்தி, கோண அடைப்புக்குறிக்குள் வகையைக் குறிப்பிடவும், பின்னர் பட்டியலின் name குறிப்பிடவும், எடுத்துக்காட்டாக: list<type> listName.

 

Syntax

// Include the list library

<list>

list <string> foods;

 

type     listName

 

Example / உதாரணம்

<iostream>

<list>

using namespace std;

 

int main() {

// Create a vector called cars that will store strings

list<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};

// Print list data

for (string food : foods) {

cout << food << “\n”;

}

return 0;

}

Output:

Idly

Pongal

Vadai

Dosa

Access a list

You cannot access list data’s  by indexing, like with arrays and vectors.

 

However, you can access the first or the last element with the .front() and .back() functions, respectively:

வரிசைகள் மற்றும் வெக்டர்களைப் போல குறியீட்டு எண்களைக் குறிப்பிடுவதன் மூலம் பட்டியல் தரவுகளை அணுக முடியாது.

 

இருப்பினும், நீங்கள் முறையே .front() மற்றும் .back() செயல்பாடுகளைப் பயன்படுத்தி முதல் அல்லது கடைசி உறுப்பை அணுகலாம்:

Example / உதாரணம்

// Create a list called foods that will store strings

list<string> food = {“Idly”, “Pongal”, “Vadai”, “Dosa”};

// Get the first element

cout << foods.front();  // Outputs Idly

// Get the last element

cout << foods.back();  // Outputs Dosa

 

Add / Remove / Change a List Data’s

To add / remove data’s in a list, use .push_front() / .pop_front()  to insert / remove a data at the beginning of the list and .push_back() /.pop_back() to add /remove a data at the end. To change a data in the list with the .front() and .back() functions.

பட்டியலில் தரவைச் சேர்க்க / நீக்க, பட்டியலின் தொடக்கத்தில் ஒரு தரவைச் சேர்க்க / நீக்க .push_front() / .pop_front() ஐப் பயன்படுத்தவும், இறுதியில் ஒரு தரவைச் சேர்க்க / நீக்க .push_back() /.pop_back() ஐப் பயன்படுத்தவும். பட்டியலில் உள்ள தரவை மாற்ற .front() மற்றும் .back() செயல்பாடுகளை  பயன்படுத்தவும்.

Example / உதாரணம்

list<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};

// Add an element at the beginning

foods.push_front(“RavaDosa”);

// Add an element at the end

foods.push_back(“Kesari”);

// Now the foods will be {“RavaDosa”, “Pongal”, “Vadai”, “Kesari”};

// Remove the first element

foods.pop_front();

cout << foods.front(); // Outputs Pongal

// Remove the last element

foods.pop_back();

cout << foods.back();  // Now outputs Vadai instead of Kesari

// Change the value of the first element

foods.front() = “MasalDosa”;

 

// Change the value of the last element

foods.back() = “Idly”;

cout << foods.front(); // Now outputs MasalDosa instead of Pongal

cout << foods.back();  // Now outputs Vadai instead of Idly

 

List Size

The .size() function is used to find the data count on a list

பட்டியலில் உள்ள தரவு எண்ணிக்கையைக் கண்டறிய .size() செயல்பாடு பயன்படுத்தப்படுகிறது.

Example / உதாரணம்

list<string> foods = {“Idly”, “Pongal”, “Vadai”, “Dosa”};

cout << foods.size();  // Outputs 4

List is Empty

The list is empty or not can be find by using .empty() function.

The .empty() function returns 1 (true) if the list is empty and 0 (false)

பட்டியல் காலியாக உள்ளதா இல்லையா என்பதை .empty() செயல்பாட்டைப் பயன்படுத்திக் கண்டறியலாம்.

பட்டியல் காலியாக இருந்தால் .empty() செயல்பாடு 1 (true) ஐயும் 0 (false) ஐயும் வழங்கும்.

Example / உதாரணம்

list<string> foods;

cout << foods.empty();  // Outputs 1 (The list is empty)

list<string> foods = {“Idly”};

cout << foods.empty();  // Outputs 0 (The list is not empty)

Difference between list and vector

  1. You can add and remove data’s from both the beginning and at the end of a list, while vectors are generally optimized for adding and removing at the end.
  2. In vectors indexing possible, indexing not supported in list

பட்டியல் மற்றும் வெக்டருக்கு இடையிலான வேறுபாடு

  1. ஒரு பட்டியலின் தொடக்கத்திலும் முடிவிலும் நீங்கள் தரவைச் சேர்க்கலாம் மற்றும் அகற்றலாம், அதே நேரத்தில் வெக்டார்கள் பொதுவாக இறுதியில் சேர்ப்பதற்கும் அகற்றுவதற்கும் உகந்ததாக இருக்கும்.
  2. வெக்டார்கள் அட்டவணைப்படுத்தலில் சாத்தியம், பட்டியலில் அட்டவணைப்படுத்தல் ஆதரிக்கப்படாது.

Leave a Reply