linkedlist

 #include <iostream>

#include <cstddef>

using namespace std;


class node{

    public:

    int data;

    node*next;

    

    node(int data){

        this -> data=data;

        next=NULL;

    }

};


void print(node *head){

    while(head!=NULL){

        cout<<head->data<<" ";

        head=head->next;

    }

}

int main() {

    //linked list of 5 elements

    node n1(1);

    node*head=&n1;

    node n2(2);

    n1.next=&n2;

    node n3(3);

    

    node n4(4);

    node n5(5);

    n2.next=&n3;

    n3.next=&n4;

    n4.next=&n5;

    print(head);

    

    

    //n5.next=&n2;

    

   /* node n1(1);

    //node*head=&n1;//define head

    node n2(2);

    n1.next=&n2;

   // cout<<head->data;

    //Dynamic

    

    node*n3=new node(10);

    node*head=n3;

    node*n4=new node(20);

    n3->next=n4;

    cout<<head->data;*/

    

    

    

    return 0;

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings