Quick actions

cmd+k|ctrl+k

Navigation

Languages

Linked List Core Implementation

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-01-29T19:00:07Z

Updated

2021-01-30T21:08:12Z

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node *next;
};
Node *head = NULL;

Node* createNewNode(int d){
    Node *temp = new Node();
    temp->data = d;
    temp->next = NULL;
    return temp;
}

void addNodeAtBegin(int d){
    if(head == NULL){
        head = createNewNode(d);
        return;
    }
    
    Node* temp1 = createNewNode(d);
    temp1->next = head;
    head = temp1;
    
}

void addNodeAtEnd(int d){
    if(head == NULL){
        head = createNewNode(d);
        return;
    }
    Node* temp3 = head;
    while(temp3->next != NULL){
        temp3 = temp3->next;
    }
    Node* temp4 = createNewNode(d);
    temp3->next = temp4;
}

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

bool search(int d){
    Node *temp = head;
    while(temp->next != NULL){
        if(temp->data == d){
            return true;
            break;
        }
        temp = temp->next;
    }
    if(temp->data == d)
        return true;
    else return false;
}


void del(int n){
    
    Node *temp = head;
    
    if(n==1){
        head = temp->next;
        delete(temp);
        return;
    }
    for(int i=0 ; i<n-2; i++){
        temp = temp->next;
    }
    Node * temp1 = temp->next;
    temp->next = temp1->next;
    delete (temp1);
    
}


void length(){
    Node* temp = head;
    int c =1;
    while(temp->next != NULL){
        temp = temp->next;
        c++;
    }
    cout<<c;
}

void get(int n){
    Node *temp = head;
    for(int i=0; i<n-1 ; i++){
        temp = temp -> next;
    }
    cout<<temp->data;
}

int max(){
    int max = head->data;
    Node *temp = head;
    while(temp->next != NULL){
        temp = temp->next;
        if(temp->data > max){
            max = temp->data;
        }
    }
    return max;
}

int min(){
    int min = head->data;
    Node *temp = head;
    while(temp->next != NULL){
        temp = temp->next;
        if(temp->data < min){
            min = temp->data;
        }
    }
    return min;
}

void convertToCircular(){
    Node *temp = head;
    while(temp->next != NULL){
        temp = temp->next;
    }
    temp->next = head;
}

int countNodesInCircularLinkedList(){
    Node *temp = head;
    int c =1;
    while(temp!=NULL){
        c++;
        temp=temp->next;
        if(temp->next == head){
            break;
        }
    }
    return c;
}

void exchange(){
    Node *temp = head;
    Node* prev;
    while(temp->next != NULL){
        prev = temp;
        temp = temp->next;
    }
    temp->next = head->next;
    prev->next = head;
    head->next = NULL;
    head = temp;
    
}

int main() {
    addNodeAtBegin(3);
    addNodeAtBegin(2);
    addNodeAtBegin(5);
    addNodeAtBegin(6);
    addNodeAtBegin(8);
    print();
    exchange();
    print();
    
    convertToCircular();
    del(5);
    cout<<countNodesInCircularLinkedList();
    
}
INFO