Answers for "c++ list implementation"

C++
28

linkedlist implementation in c++

#include <iostream>

using namespace std;

struct node
{
    int data;
    node *next;
};

class linked_list
{
private:
    node *head,*tail;
public:
    linked_list()
    {
        head = NULL;
        tail = NULL;
    }

    void add_node(int n)
    {
        node *tmp = new node;
        tmp->data = n;
        tmp->next = NULL;

        if(head == NULL)
        {
            head = tmp;
            tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
    }
};

int main()
{
    linked_list a;
    a.add_node(1);
    a.add_node(2);
    return 0;
}
Posted by: Guest on March-18-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

Code answers related to "c++ list implementation"

Browse Popular Code Answers by Language