Saturday 19 August 2017

Link list examples

main
#include"data.h"
void main(){
    data d;
   
    d.addstart();
    d.addend();
    d.addbydata();
    d.transverse();
    d.dele();
    d.transverse();
}
.h
#pragma once
#include<iostream>
#include<fstream>
using namespace std;
struct node{
    int n;
    node* next;
    node() :next(NULL), n(0){ }
};
class data
{
protected:
    node* cur;
    node* first;

public:
    data();
    void transverse();
    void addstart();
    void addend();
    void addbydata();
    void dele();
};

.cpp
#include "data.h"


data::data() :first(NULL), cur(NULL)
{
}


void data::addend(){
    node* temp = new node;
   
   
    cout << "enter the number to add at the end " << endl;
    cin >> temp->n;
    if (first == NULL){
        first = temp;
        cur = #include "data.h"


data::data() :head(NULL), cur(NULL)
{
}


void data::addend(){
    node* temp = new node;
   
   
    cout << "enter the number to add at the end " << endl;
    cin >> temp->n;
    if (head == NULL){
        head = temp;
        cur = head;
    }
    else {
        cur->next = temp;
        cur = temp;
    }

}
void data::addstart(){
   

    node* temp = new node;
    cout << "enter the number you want to add at the start" << endl;
    cin >> temp->n;
    temp->next = head;
    head = temp;
   
}
void data::addbydata(){
   
    int ch=1, c;
    node* t = NULL;
    cur = head;
    cout << "enter the place where you want to add the number" << endl;
    cin >> c;
    while (ch != c){
        t = cur;
        cur = cur->next;
        ch++;
    } ;
    node * temp = new node;
    t->next = temp; temp->next = cur;
    cout << "enter the number" << endl;
    cin >> temp->n;


}
void data::transverse(){
    {
       
        node* temp = head;
        cout <<"your data is" << endl;
        do
        {
       
           
            cout << temp->n << "\n" << endl;
            temp = temp->next;
        } while (temp != NULL);
    }

}
void data::dele(){
    {
        int n;
        cout << "\nWhich Node Do You Want to Delete? ";
        cin >> n;
        int s_n = 0;
        node* T = NULL, *prev = NULL;
        cur = head;
        while (s_n != n)
        {
            prev = T;
            T = cur;
            s_n++;
            cur = cur->next;
        }
        if (T == head)
        {
            head = cur;
            cout << " Deleting " << T->n << endl;
            delete T;
        }
        else
        {
            prev->next = cur;
            cout << " Deleting " << T->n << endl;
            delete T;
       
        }
       
    }

}
    }
    else {
        cur->next = temp;
        cur = temp;
    }

}
void data::addstart(){
   

    node* temp = new node;
    cout << "enter the number you want to add at the start" << endl;
    cin >> temp->n;
    temp->next = first;
    first = temp;
   
}
void data::addbydata(){
   
    int ch=1, c;
    node* t = NULL;
    cur = first;
    cout << "enter the place where you want to add the number" << endl;
    cin >> c;
    while (ch != c){
        t = cur;
        cur = cur->next;
        ch++;
    } ;
    node * temp = new node;
    t->next = temp; temp->next = cur;
    cout << "enter the number" << endl;
    cin >> temp->n;


}
void data::transverse(){
    {
       
        node* temp = first;
        cout <<"your data is" << endl;
        do
        {
       
           
            cout << temp->n << "\n" << endl;
            temp = temp->next;
        } while (temp != NULL);
    }

}
void data::dele(){
    {
        int n;
        cout << "\nWhich Node Do You Want to Delete? ";
        cin >> n;
        int s_n = 0;
        node* T = NULL, *prev = NULL;
        cur = #include "data.h"


data::data() :head(NULL), cur(NULL)
{
}


void data::addend(){
    node* temp = new node;
   
   
    cout << "enter the number to add at the end " << endl;
    cin >> temp->n;
    if (first == NULL){
        head = temp;
        current = head;
    }
    else {
        current->next = temp;
        current = temp;
    }

}
void data::addstart(){
   

    node* temp = new node;
    cout << "enter the number you want to add at the start" << endl;
    cin >> temp->n;
    temp->next = first;
    first = temp;
   
}
void data::addbydata(){
   
    int ch=1, c;
    node* t = NULL;
    cur = first;
    cout << "enter the place where you want to add the number" << endl;
    cin >> c;
    while (ch != c){
        t = cur;
        cur = cur->next;
        ch++;
    } ;
    node * temp = new node;
    t->next = temp; temp->next = cur;
    cout << "enter the number" << endl;
    cin >> temp->n;


}
void data::transverse(){
    {
       
        node* temp = first;
        cout <<"your data is" << endl;
        do
        {
       
           
            cout << temp->n << "\n" << endl;
            temp = temp->next;
        } while (temp != NULL);
    }

}
void data::dele(){
    {
        int n;
        cout << "\nWhich Node Do You Want to Delete? ";
        cin >> n;
        int s_n = 0;
        node* T = NULL, *prev = NULL;
        current = first;
        while (s_n != n)
        {
            prev = T;
            T = current;
            s_n++;
            current = current->next;
        }
        if (T == head)
        {
            first = current;
            cout << " Deleting " << T->n << endl;
            delete T;
        }
        else
        {
            prev->next = current;
            cout << " Deleting " << T->n << endl;
            delete T;
       
        }
       
    }

};
        while (s_n != n)
        {
            prev = T;
            T = cur;
            s_n++;
            cur = cur->next;
        }
        if (T == head)
        {
            head = cur;
            cout << " Deleting " << T->n << endl;
            delete T;
        }
        else
        {
            prev->next = cur;
            cout << " Deleting " << T->n << endl;
            delete T;
       
        }
       
    }

}

1 comment: