List in C++
xxxxxxxxxx
#include <iostream>
class Node{
public:
Node* next;
int element;
};
int main(){
Node* head = new Node();
head -> next = new Node();
head -> element = 10;
head -> next = new Node();
head -> next -> element = 11;
head -> next -> next = nullptr;
std::cout<<head->element<<std::endl;
std::cout<<head->next->element<<std::endl;
return 0;
}
xxxxxxxxxx
#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";
}
xxxxxxxxxx
#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;
}
xxxxxxxxxx
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> l;
}