ronith
9/3/2018 - 9:17 AM

Linked List using classes

#include <iostream>
using namespace std;

class node {
public:
    int data;
    node *next;

    node() {
        data = 0;
        next = NULL;
    }
    node (int x) {
        data = x;
        next= NULL;
    }
};

class ll {
public:
    node *head;
    ll() {
        head = NULL;
    }

    bool isEmpty(); //checks if the ll is empty
    node* getLastNode(); //returns address of the last node of ll
    void printList(); //prints entire ll
    void addAtEnd(int x); //adds an element at the end of ll
    void addAtFront(int x); //adds an element at the front of ll
    void inserter(int x,int n); //inserts x at nth node's position indexing from 1
    node* searcher(int x); //searches for element x and returns its node address
    bool delAtEnd(); //deletes last node and tells if deletion is successfully performed
    bool delAtFront(); //deletes head node and tells if deletion is successfully performed
    bool deleter(int n); //deletes the nth node indexing from 1 and tells if deletion is successfully performed


};

bool ll :: isEmpty() {
    if (head == NULL)
        return true;
    else
        return false;
}

node* ll :: getLastNode() {
    node *temp = head;
    while (temp->next != NULL)
        temp = temp->next;
    return temp;
}

void ll :: printList() {
    node *temp = head;
    while (temp) {
        cout<< temp->data<< "->";
        temp= temp->next;
    }
    cout<< "NULL";
}

void ll::addAtEnd (int x) {
    node *n = new node(x);
    if (head == NULL)
        head = n;
    else {
        node *temp = head;
        while (temp->next != NULL)
            temp = temp->next;
        temp->next = n;
    }
}

void ll::addAtFront (int x) {
    node *n = new node(x);
    if (head != NULL)
        n->next = head;
    head = n;
}

void ll::inserter (int x, int n) {
    if (n==1)
        addAtFront (x);
    node *temp = head;
    while (--n>1) {
        temp= temp->next;
    }
    node *n1 = new node(x);
    node *temp1 = temp->next;
    temp->next = n1;
    n1->next = temp1;
}

node * ll::searcher (int x) {
    node *temp = head;
    while (temp != NULL) {
        if (temp->data == x)
            return temp;
        temp = temp->next;
    }
}

bool ll :: delAtFront(){
    if(head==NULL){
        return false;
    }else{
        node* temp=head;
        head=temp->next;
        delete(temp); // to delete the previous head node from memory
        return true;
    }
}

bool ll :: deleter(int n){
    if(n==1){
        return delAtFront();
    }else{
        int pos=1;
        node* temp=head;
        while(pos<n-1){
            temp=temp->next;
            pos++;
        }
        node* temp1=temp->next;
        temp->next=temp->next->next;
        delete(temp1); // to delete the previous node from memory
        return true;
    }
}

int main() {
    ll h;
    h.addAtEnd(5);
    h.addAtEnd(4);
    h.addAtEnd(3);
    h.addAtEnd(2);
    h.addAtEnd(1);
    h.addAtEnd(0);
    h.inserter(6,3);
    h.printList();
    cout<< (h.searcher(6))->data;
}