1#include <iostream>
2
3using namespace std;
4
5struct node
6{
7 int data;
8 node *next;
9};
10
11class linked_list
12{
13private:
14 node *head,*tail;
15public:
16 linked_list()
17 {
18 head = NULL;
19 tail = NULL;
20 }
21
22 void add_node(int n)
23 {
24 node *tmp = new node;
25 tmp->data = n;
26 tmp->next = NULL;
27
28 if(head == NULL)
29 {
30 head = tmp;
31 tail = tmp;
32 }
33 else
34 {
35 tail->next = tmp;
36 tail = tail->next;
37 }
38 }
39};
40
41int main()
42{
43 linked_list a;
44 a.add_node(1);
45 a.add_node(2);
46 return 0;
47}
1
2#include <bits/stdc++.h>
3#include <iostream>
4#include <list>
5#include <iterator>
6
7#define ll long long
8
9using namespace std;
10
11//function to print all the elements of the linked list
12void showList(list <int> l){
13 list <int> :: iterator it; //create an iterator according to the data structure
14 for(it = l.begin(); it != l.end(); it++){
15 cout<<*it<<" ";
16 }
17
18}
19
20
21int main(){
22
23 list <int> l1;
24 list <int> l2;
25
26 for(int i=0; i<10; i++){
27 l1.push_back(i*2); //fill list 1 with multiples of 2
28 l2.push_back(i*3); //fill list 2 with multiples of 3
29 }
30
31 cout<<"content of list 1 is "<<endl;
32 showList(l1);
33 cout<<endl;
34
35 cout<<"content of list 2 is "<<endl;
36 showList(l2);
37 cout<<endl;
38
39 //reverse the first list
40 l1.reverse();
41 showList(l1);
42 cout<<endl;
43
44 //sort the first list
45 l1.sort();
46 showList(l1);
47 cout<<endl;
48
49 //removing an element from both sides
50 l2.pop_front();
51 l2.pop_back();
52
53 //adding an element from both sides
54 l2.push_back(10);
55 l2.push_front(20);
56
57
58 return 0;
59}
1template<typename T>
2class list {
3 public:
4
5 struct node {
6 T value;
7 node* next;
8 };
9
10 list() : hd(nullptr), tl(nullptr) {}
11
12 node* head() { return hd; }
13 node* tail() { return tl; }
14
15 void insert(node* prior, T value);
16
17 node* at(int i);
18
19 void erase(node* prior);
20 void clear();
21
22 void push_back(T value);
23 void pop_back();
24
25 void push_front(T value);
26 void pop_front();
27
28 int size();
29
30 private:
31 node* hd, tl;
32}
33