Limited Period Offer : 20% Discount on online/offline courses, for more details call/whatsapp

linked list

1 min read
1 year ago By Aniket Prajapati

### Linked List

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers . This elements can be accessed by pointer traversing.

insert in linked list:

Linked list class which has a datatype and a pointer which has constructor so that it can be called :

code:

#include<bits/stdc++.h>
using namespace std;

class Node{
    public:
    int data;
    Node* next;
    
    Node(int data)
    {
        this->data=data;
        this->next=NULL;
    }
};

Node* takeinput()
{
    int data;
    cin>>data;
    Node* head = NULL;
    Node*tail = NULL;
    while(data!=-1)
    {
        Node* newnode=new Node(data);
        if(head==NULL)
        {
            head=newnode;
            tail=newnode;
        }
        else{
            tail->next=newnode;
            tail=tail->next;
        }
        cin>>data;
    }
    return head;
}

Node* insert(Node* head)
{
    int n;
    cin>>n;
    int data=4;
    Node* temp=head;
    Node* temp1;
    while(n>1)
    {
        temp=temp->next;
        n--;
    }
    temp1=temp->next;
    Node* newnode=new Node(data);
    temp->next=newnode;
    newnode->next=temp1;
    
    
    return head;
}

void print(Node* head)
{
    while(head!=NULL){
        cout<<head->data;
        head=head->next;
    }
    cout<<endl;
}

int main()
{
    Node*head=takeinput();
    print(head);
    Node* a=insert(head);
    print(a);
}
Take input in linked list:

The take input function in a linked list runs in O(N) time complexity where N is the number of elements in the linked list. code

Node* takeinput()
{
    int data;
    cin>>data;
    Node* head = NULL;
    Node*tail = NULL;
    while(data!=-1)
    {
        Node* newnode=new Node(data);
        if(head==NULL)
        {
            head=newnode;
            tail=newnode;
        }
        else{
            tail->next=newnode;
            tail=tail->next;
        }
        cin>>data;
    }
    return head;
}
Jul 24, 2023 10:12 Back to Articles

Other Articles

Things you should learn to become good web developer for any tech stack

In this articles all concepts covered to become web developer

1 year ago By Santosh Kshirsagar
Introducing ECMAScript 6 (ES6): A New Era for JavaScript Development Introducing ECMAScript 6 (ES6): A New Era for JavaScript Development

In this article, we'll explore the important changes and improvements that came with ECMAScript 6 (ES6), and how they've made JavaScript programming better.

1 year ago By Mitali Gupta
Binary Search top interview questrions.

As we know binary search itself is a imp topic to be learned in Data Structures and Algorithm so, This article contains all the important questions that can be asked in coding interview or online assessment(OA) . Here the questions clear all the concepts and the variety in questions helps to understands the logic behind the concepts .

1 year ago By Aniket Prajapati
For Laravel Setting UP CI/CD Pipeline using GitHub, CodeDeploy, S3 and EC2

Setting ci/cd pipeline for laravel application on aws using github actions then uploading to s3 and using codedeploy application gets installled on EC2 instance

1 year ago By Santosh Kshirsagar