Linkedlist Basic examples for understanding


Struct in C++ 

Its is used to store different attributes of different datatype in a block

Below Shows the example of n teacher data

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct teacher{
    string name;
    string subject;
    string gender;
    void teacherDetails(string name,string subject,string gender){
        cout<<name<<" "<<subject<<" "<<gender;
    }
};
int main()
{
    teacher trecord[2];
    
    for(int i =0;i<2;i++){
        cout<<"Enter Details of Teacher"<<i+1;
        cin>>trecord[i].name>>trecord[i].subject>>trecord[i].gender;
    }
    for(int i =0;i<2;i++){
        trecord[i].teacherDetails(trecord[i].name,trecord[i].subject,trecord[i].gender);
    }
  return 0;

}

LinkedList


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node *next;

};
    void printmyList(node *h){
        while(h!=NULL){
            cout<<h->data<<" "<<h->next<<endl;
            h = h->next;
        }
    }
int main()
{
    node *head = new node();
    node *second = new node();
    node *third = new node();
    
    head->data = 100;
    head->next = second;
    
    second->data = 3;
    second->next = third;
    
    third->data = 90;
    third->next = NULL;
    
    printmyList(head);
    return 0;
}

Two ways to Add node at front 

1.) using head pointer of node





#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node *next;

};
    void printmyList(node *h){
        while(h!=NULL){
            cout<<h->data<<" "<<h->next<<endl;
            h = h->next;
        }
    }
    
node *push(node *head,int newData){
    node *newNode = new node();
    newNode->data = newData;
    newNode->next = head;
    head = newNode;
    return head;
    
}
int main()
{
    node *head = NULL;
    head = push(head,5);
    head = push(head,90);
    head = push(head,50);
    
    printmyList(head);

    return 0;
}

OP:
50 front
90 
5 end

2.) using head reference 



**headref will store and give the address of head pointer which is OXIJ
and *headref indicates  the value present in the head which is OXAB which is address of the node...


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node *next;

};
    void printmyList(node *h){
        while(h!=NULL){
            cout<<h->data<<" "<<h->next<<endl;
            h = h->next;
        }
    }
    
void push(node **headref,int newData){
    node *newNode = new node();
    newNode->data = newData;
    newNode->next = *headref;  //stores the OXAB address in the next of newnode
    *headref = newNode; // OXEF gets store in the head wh
    
}
int main()
{
    node *head = NULL;
    push(&head,5);
    push(&head,90);
    
    printmyList(head);

    return 0;
}

Inserting After the head in the LinkedList


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node *next;

};
void printmyList(node *h){
        while(h!=NULL){
            cout<<h->data<<" "<<h->next<<endl;
            h = h->next;
        }
    }
    
void push(node **headref,int newData){
    node *newNode = new node();
    newNode->data = newData;
    newNode->next = *headref;
    *headref = newNode;
    
}
void insertAfter(node *prevNode,int newData){
    node *newNode = new node();
    newNode->data = newData;
    newNode->next = prevNode->next;
    prevNode->next = newNode;
}
int main()
{
    node *head = NULL;
    push(&head,5); 
    push(&head,90);
    push(&head,870);
    insertAfter(head->next,75);
    
    
    printmyList(head);

    return 0;
}

Append a node at the last of Linked List


To append at last do this two steps

 1.) traverse through ll and find the last node which points to null and his address

2.) Now append your newnode at last.


for appending
we will check two things
1.) whether the head itself is NULL if yes then just append the element on head -> next
if no then we will traverse through the LL and find that node whose next is NULL and thereafter append there.


#include <iostream>

using namespace std;
struct node{
    int data;
    node *next;
    
};
void print(node *n){
    while(n!=NULL){
        cout<<n->data<<endl;
        n= n->next;
    }
}
void push(node **head_ref,int newdata){
    node *newNode = new node();
    newNode->data = newdata;
    newNode->next = *head_ref;
    *head_ref = newNode;
}
void append(node **head_ref,int newdata){
    node *newNode = new node();
    newNode->data = newdata;
    newNode->next = NULL;
    
    node *last = *head_ref;
    if(*head_ref==NULL){
        *head_ref = newNode;
        return;
    }
    while(last->next==NULL){
        last = last->next;
    }
    
    last->next = newNode;
    return;
}
int main()
{
    node *head = new node();
    push(&head,5);//yeh last hai filhal
    push(&head,75);//head is pointing this
       push(&head,81);//now this
    append(&head,68);
    
    print(head);

    return 0;
}
OP:
81 head
75
5
68 end


Delete a node in a LinkedLIST








#include <iostream>

using namespace std;
struct node{
    int data;
    node *next;
    
};
void print(node *n){
    while(n!=NULL){
        cout<<n->data<<endl;
        n= n->next;
    }
}
void push(node **head_ref,int newdata){
    node *newNode = new node();
    newNode->data = newdata;
    newNode->next = *head_ref;
    *head_ref = newNode;
}
void deleteNode(node **head_ref,int dataTobeDelete){
    node *prev ,*temp;
    temp = *head_ref; //abhi joh bhi mai temp mai change karunga woh head mai bhi hoga
    
    // if first node mai hi data joh delete karna h woh hai toh 
    if(temp!=NULL && temp->data == dataTobeDelete){
        temp->next = NULL;
        free(temp);
    }
    // agar first node mai nhi hai toh traverse 
    while(temp!=NULL && temp->data!=dataTobeDelete){
        prev = temp;
        temp = temp->next;
    }
    // at this moment joh data hai woh temp mai hai aur usse ek previous node prev mai hai
    if(temp==NULL){
        return;
    }
    // deleting
    prev->next = temp->next;
    free(temp);
    
}
int main()
{
    node *head =NULL;
    push(&head,51);//yeh last hai filhal
    push(&head,5);
    push(&head,89);
    push(&head,74);
    push(&head,84);//head is pointing this
    
    print(head);
    
    deleteNode(&head,51);
    
    print(head);

    return 0;
}


OP:
84
74
89
5

















Comments