Single LInked list

 

Single linked list






OURSHOPKEEPER
























#include <iostream>
#include <cstdlib>
using namespace std;

//insertion at beg, end, poss
struct node
{
    int data;
    struct node *next;
};

void insertbegin();
void display();
void insertend();
int length();
void insertatpos();
void deletebegin();
void deletepos();
void deleteEnd();

struct node *head = NULL;

int main()
{
    int choice;
    while (1)
    {
        cout << "1.Insert node at beginning \n2. insert a end \n 3. insert at pos \n 4.display\n 6. delete beginning\n 7.Delete position\n 8. delete ends\n 9.exit\n";
        cout << "Enter the choice : ";
        cin >> choice;

        switch (choice)
        {
        case 1:
            insertbegin();
            break;

        case 2:
            insertend();
            break;

        case 3:
            insertatpos();
            break;
        case 4:
            display();
            break;
        case 5:
            cout << length();
            break;
        case 6:
            deletebegin();
            break;
        case 7:
            deletepos();
            break;
        case 8:
            deleteEnd();
            break;

        case 9:
            exit(0);

        default:
            cout << "Invalid";
        }
    }
    return 0;
}

void insertbegin()
{
    int data;
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
    cout << "Enter Data To insert : ";
    cin >> data;
    temp->data = data;
    temp->next = NULL;

    if (head == NULL)
    {
        head = temp;
        cout << "Node is Inserted Success Fully \n\n";
    }
    else

    {
        temp->next = head;
        head = temp;
    }
}

void display()
{
    struct node *temp;
    temp = head;
    if (head == NULL)
    {
        head = temp;
        cout << "Linked list is empty \n\n";
    }
    else
        while (temp != NULL)
        {
            cout << temp->data << "->";
            temp = temp->next;
        }
    cout << "\n\n";
}

void insertend()
{
    int data, i = 0;
    int len = length();
    struct node *temp, *p;

    temp = (struct node *)malloc(sizeof(struct node));
    cout << "Enter the data: ";
    cin >> data;
    temp->data = data;
    temp->next = NULL;
    p = head;

    if (head == NULL)
    {
        head = temp;
        cout << "Node is Inserted Success Fully \n";
    }
    else
    {
        while (i < len - 1)
        {
            p = p->next;
            i++;
        }
        p->next = temp;
        cout << "Node inserted successfully\n";
    }
}

int length()
{
    int length = 0;
    struct node *temp;
    temp = head;

    while (temp != NULL)
    {
        length++;
        temp = temp->next;
    }
    return length;
    cout << "\n";
}

void insertatpos()
{
    int data, loc, i = 1;
    struct node *temp, *p;
    cout << "Enter position where you want to ensert data\n";
    cin >> loc;
    temp = (struct node *)malloc(sizeof(struct node));

    cout << "Enter Data To insert : ";
    cin >> data;
    temp->data = data;
    temp->next = NULL;
    p = head;

    if (loc > length())
    {
        cout << "Invalid Location\n";
    }
    else
    {
        while (i < loc - 1)
        {
            p = p->next;
            i++;
        }
        temp->next = p->next;
        p->next = temp;
    }
}

void deletebegin()
{

    struct node *temp;
    temp = head;

    if (head == NULL)
    {
        cout << "No Element present in Linked List\n";
    }
    else
    {
        head = temp->next;
        temp->next = NULL;
        cout << "First Node of Linked List has been deleted \n";
    }
}

void deleteEnd()
{
    int len = 1;
    struct node *temp;
    temp = head;

    if (head == NULL)
    {

        cout << "No Element present in Linked List \n";
    }
    else if (length() == 1)
    {
        head = NULL;
        cout << "Last Node has been deleted \n";
    }
    else
    {
        while (len < length() - 1)
        {
            temp = temp->next;
            len++;
        }
        temp->next = NULL;
        cout << "Last Node has been deleted \n";
    }
}

void deletepos()
{
    struct node *temp, *p;
    int pos, len = 1;
    temp = head;

    cout << "Enter the position of Node which do you want to delete \n";
    cin >> pos;
    if (pos > length())
    {
        cout << "Invlide Position \n";
    }
    else
    {
        while (len < pos - 1)
        {
            temp = temp->next;
            len++;
        }
        p = temp->next;
        temp->next = p->next;
        p->next = NULL;
        cout << "Node of Location has been deleted \n";
        cout << pos;
    }
}





    


Click on the above button to download the code.



Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post