Answers for "cpp list"

C++
6

list in cpp

//code by Soumyadeep Ghosh
//ig: @soumyadepp

#include <bits/stdc++.h>

using namespace std;

void display_list(list<int>li)
{
  //auto variable to iterate through the list
  for(auto i:li)
  {
    cout<<i<<" ";
  }
}
int main()
{
  //definition
  list<int>list_1;
  int n,x;
  cin>>n;
  //taking input and inserting using insert function
  for(int i=0;i<n;i++)
  {
    cin>>x;
    list_1.insert(x);
  }
  //if list is not empty display it
  if(list_1.empty()==false)
  {
    display_list(list_1);
  }
  list_1.sort(); //sorts the list
  list_1.reverse(); //reverses the list
  list_1.pop_back(); //deletes last element of the list
  list_1.pop_front(); //deletes the first element of the list
  
  display_list(list_1);  //function to display the list
  
  
  return 0;
}
//in addition , you can use nested lists such as list<list<int>> or list<vector<list>> etc
Posted by: Guest on November-22-2020
1

cpp std list example

#include <algorithm>
#include <iostream>
#include <list>
 
int main()
{
    // Create a list containing integers
    std::list<int> l = { 7, 5, 16, 8 };
 
    // Add an integer to the front of the list
    l.push_front(25);
    // Add an integer to the back of the list
    l.push_back(13);
 
    // Insert an integer before 16 by searching
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
 
    // Print out the list
    std::cout << "l = { ";
    for (int n : l) {
        std::cout << n << ", ";
    }
    std::cout << "};n";
}
Posted by: Guest on January-11-2021
3

linked list in c++ stl

#include <bits/stdc++.h>
#include <iostream>
#include <list>
#include <iterator>

#define ll long long

using namespace std;

//function to print all the elements of the linked list
void showList(list <int> l){
	list <int> :: iterator it; //create an iterator according to the data structure
	for(it = l.begin(); it != l.end(); it++){
		cout<<*it<<" ";
	}
	
}	


int main(){
	
	list <int> l1;
	list <int> l2;
	
	for(int i=0; i<10; i++){
		l1.push_back(i*2); //fill list 1 with multiples of 2
		l2.push_back(i*3); //fill list 2 with multiples of 3
	}
	
	cout<<"content of list 1 is "<<endl;
	showList(l1);
	cout<<endl;
	
	cout<<"content of list 2 is "<<endl;
	showList(l2);
	cout<<endl;
	
	//reverse the first list
	l1.reverse();
	showList(l1);
	cout<<endl;
	
	//sort the first list
	l1.sort();
	showList(l1);
	cout<<endl;
	
	//removing an element from both sides
	l2.pop_front();
	l2.pop_back();
	
	//adding an element from both sides
	l2.push_back(10);
	l2.push_front(20);
	
	
    return 0;
}
Posted by: Guest on December-05-2020
0

list in c++

#include <bits/stdc++.h>
using namespace std;
void display(list<int> &lst){
    list<int> :: iterator it;
    for(it = lst.begin(); it != lst.end(); it++){
         cout<<*it<<" ";
    }
}
int main(){
    list<int> list1;
    int data, size;
    cout<<"Enter the list Size ";
    cin>>size;
    for(int i = 0; i<size; i++){
        cout<<"Enter the element of the list ";
        cin>>data;
        list1.push_back(data);
    }
    cout<<endl;
    display(list1);
  return 0;
}
Posted by: Guest on September-12-2021
0

list in cpp

#include <bits/stdc++.h>

using namespace std;

void display_list(list<int>li)
{
  //auto variable to iterate through the list
  for(auto i:li)
  {
    cout<<i<<" ";
  }
}
int main()
{
  //definition
  list<int>list_1;
  int n,x;
  cin>>n;
  //taking input and inserting using insert function
  for(int i=0;i<n;i++)
  {
    cin>>x;
    list_1.insert(x);
  }
  //if list is not empty display it
  if(list_1.empty()==false)
  {
    display_list(list_1);
  }
  list_1.sort(); //sorts the list
  list_1.reverse(); //reverses the list
  list_1.pop_back(); //deletes last element of the list
  list_1.pop_front(); //deletes the first element of the list
  
  display_list(list_1);  //function to display the list
  
  
  return 0;
}
Posted by: Guest on October-31-2021

Browse Popular Code Answers by Language