Untitled

 avatar
unknown
plain_text
20 days ago
1.6 kB
4
Indexable
#include <iostream>
using namespace std;
class node
{
public:
    int data;
    node *start, *next;
    node()
    {
        start = NULL;
    }

    void insertatbeg(int d)
    {
        node *nn = new node();
        nn->data = d;
        if (start == NULL) 
        {
            nn->next = NULL; 
            start = nn;
        }
        else
        {
            nn->next = start;
            start = nn;
        }
    }
    void traverse()
    {
        if (start == NULL)
        {
            cout << "List is empty" << endl;
        }
        else
        {
            node *ptr = start; 
            cout << "Start";
            while (ptr != NULL)
            {
                cout << " -> " << ptr->data;
                ptr = ptr->next;
            }
            cout << endl;
        }
    }
    void deletionatfirst()
    {
        if (start == NULL) {
            cout << "Underflow: List is empty" << endl;
        }
        else
        {
            node *ptr = start; 
            if (start->next == NULL)
            {
                start = NULL;
            }
            else
            {
                start = start->next;
            }
            delete ptr;
        }
    }
};
int main()
{
    node N;
    N.traverse();
    cout << "\n";
    N.insertatbeg(10);
    N.insertatbeg(20);
    N.insertatbeg(30);
    N.traverse();
    cout << "\n";
    N.insertatbeg(40);
    N.traverse();
    cout << "\n";
    N.deletionatfirst();
    N.traverse();
}
Editor is loading...
Leave a Comment