Lists and Arrays in C++

Lists in C++

  • Definition: In C++, a list container is implemented as a doubly linked list.
  • Doubly Linked List: Each element in the list stores the address of both the next and the previous element.

Sample Code

  • Includes:
    • <iostream>: For input/output operations.
    • <list>: For using the list container.
  • Namespace: using namespace std;
  • List Initialization:
    • list<int> myList = {10, 20, 30, 40, 50};
  • Output:
    • Iterates through the list and prints each element followed by a space.

Adding and Deleting Elements in a List

  • Functions:
    • push_front(): Inserts element at the beginning.
    • push_back(): Adds element at the end.
    • pop_front(): Removes the first element.
    • pop_back(): Removes the last element.

List Functions

  • push_front(): Inserts an element at the beginning, making it the new first element.
  • push_back(): Adds an element at the end, making it the new last element.
  • pop_front(): Removes the first element, shifting the head to the next element.
  • pop_back(): Removes the last element, adjusting the tail to the previous element.

Sample Code

  • Initial List: list<int> myList = {10, 20, 30};
  • Operations:
    • myList.push_front(5);
    • myList.push_back(40);
    • myList.pop_front();
    • myList.pop_back();
  • Output: Shows the list after each operation.

List Operators

  • Assignment Operator (=):
    • Copies the contents of one list to another.
    • Syntax: list2 = list1;
  • Equality (==) and Inequality (!=) Operators:
    • Compares two lists for equality or inequality.
    • Syntax: list1 == list2, list1 != list2
  • Relational Operators (
    • Compares list elements lexicographically.
    • Syntax: list1 < list2, list1 > list2

Sample Code

  • List Initialization:
    • list<int> list1 = {10, 20, 30};
    • list<int> list2 = {10, 20, 30};
    • list<int> list3 = {5, 15, 25};
  • Assignment Operator:
    • list4 = list1; (Copies list1 to list4)
  • Equality/Inequality Operators:
    • Compares list1 with list2 and list3.
  • Relational Operators:
    • Compares list1 with list3 using <, >.
    • Compares list1 with list2 using <=.

Arrays

  • Definition: Arrays store multiple values in a single variable.
  • Declaration:
    • variable_type array_name[number_of_elements]; Example: string cars[4];

Array Initialization

  • Array Literal:
    • Values are placed in a comma-separated list inside curly braces.
    • Example: string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
    • int myNum[3] = {10, 20, 30};

Sample Code

  • Array Declarations:
    • int intArray[] = {10, 20, 30, 40, 50};
    • double doubleArray[] = {10.5, 20.75, 30.1, 40.2, 50.6};
    • string stringArray[] = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
    • char charArray[] = {'A', 'B', 'C', 'D', 'E'};
  • Output:
    • Prints each array with its elements.

Array Operators and Function

  • Subscript Operator ([]):
    • Accesses individual elements by index.
    • Syntax: array[index]
  • Assignment Operator (=):
    • Assigns a value to an element.
    • Syntax: array[index] = value
  • Comparison Operators (==, !=):
    • Compares individual elements.
    • Arrays themselves cannot be directly compared.
    • Syntax: array1[index] == array2[index]
  • Relational Operators (
    • Compares elements of arrays.
    • Arrays cannot be directly compared; elements must be compared individually.

Sample Code

  • Array Initialization:
    • int arr1[] = {10, 20, 30, 40, 50};
    • int arr2[] = {10, 21, 30, 39, 50};
  • Array Element Modification:
    • arr1[1] = 25;
  • Comparison:
    • Compares arr1[1] and arr2[1] using ==.
    • Compares arr1[2] and arr2[2] using >, <.`
  • Note: Arrays cannot be directly compared using == or != in C++.