top of page

Singly Linked List.


Singly Linked List operation through structure:


#include<iostream>
using namespace std;

typedef struct nodetype
{
    int info;
    struct nodetype *next;
} node;

node *head = 0;

node* createList(node* head)
{
    node *newnode , *temp;
    int item;
    newnode = (node*)malloc(sizeof(node));
    cout<<"Enter the element"<<endl;
    cin>>item;
    newnode->info = item;
    if(head == 0)
    {
        cout<<"Head is NULL"<<endl;
        newnode->next = NULL;
        head = newnode;
    }
    else
    {
        cout<<"Head is not NULL"<<endl;
        temp = head;
        while (temp -> next != NULL)  
        {  
                temp = temp -> next;  
        } 
        temp->next = newnode;
        newnode->next = NULL;
    }
    return head;
    
}
void displaylist(node* head)
{
    node *temp;
    temp = head;
    while(temp!=NULL)
    {
        cout<<temp->info<<endl;
        temp = temp->next;
    }
}
node * reverse(node *head)
{
    node *temp,*temp1 = NULL,*temp2;
    temp = head;
    while(temp!=NULL)
    {
        temp2 = temp->next;
        temp->next = temp1;
        temp1 = temp;
        temp = temp2;  
    }
    return temp1;
}

void deleteList(node *head)
{
    node *temp;
    while(head!=NULL)
    {
        temp = head;
        head = head->next;
        free(temp);
    }
}

node * addElementEnd(node *head)
{
    node *temp,*newnode;
    int item;
    cout<<"Enter the item"<<endl;
    cin>>item;
    newnode = (node*)malloc(sizeof(node));
    newnode->next = NULL;
    newnode->info = item;
    if(head == 0)
    {
        head = newnode;
    }
    else
    {  
        temp = head;
        while(temp->next !=NULL)
        {
            temp = temp->next;
        }
        temp->next = newnode;
    }
      return head;
}

node * addElementBegin(node *head)
{
    node *newnode;
    newnode = (node*)malloc(sizeof(node));
    int item;
    cout<<"Enter the element"<<endl;
    cin>>item;
    newnode->info = item;
    newnode->next = head;
    head = newnode;
    return head;
}

node * insertAfterElement(node *head,int element)
{
    
    node *temp,*loc,*newnode;
    int item;
    temp =head;
    if(head == NULL)
    {
        cout<<"Element not avilable in the list"<<endl;
    }
    else
    {
        while(temp != NULL)
        {
            if(temp->info == element)
            {
                cout<<"Enter the item"<<endl;
                cin>>item;
                loc = temp->next;
                newnode = (node*)malloc(sizeof(node));
                newnode->info = item;
                temp->next = newnode;
                newnode->next = loc;
                return head;
            }
            temp = temp->next;
        }
        cout<<"Element not avilable in the list"<<endl;
    }
    return head;
}

node * insertBeforeElement(node *head,int element)
{
    
    node *temp,*loc=NULL,*newnode;
    int item;
    temp = head;
    if(head == NULL)
    {
        cout<<"Element not avilable in the list"<<endl;
    }
    else
    {
        while(temp != NULL)
        {
            if(temp->info == element)
            {
                cout<<"Enter the item"<<endl;
                cin>>item;
                newnode = (node*)malloc(sizeof(node));
                newnode->info = item;
                newnode->next = temp;
                (loc == NULL) ? head = newnode : loc->next = newnode;
                
                return head;
            }
            loc = temp;
            temp = temp->next;
        }
        cout<<"Element not avilable in the list"<<endl;
    }
    return head;
}

int main()
{
    node *head = NULL;
    int choice;
    while(1)
    {
        cout<<"1. Create the list"<<endl;
        cout<<"2. Display the list"<<endl;
        cout<<"3. Reverse the list"<<endl;
        cout<<"4. Add a element at end of list"<<endl;
        cout<<"5. Add a element at begin of list"<<endl;
        cout<<"6. Add a element after given element" <<endl;
        cout<<"7. Add a element before given element" <<endl;
        cout<<"8. delete the list" <<endl;
        cout<<"9. Exit from program"<<endl;
        cout<<"Enter the choice"<<endl;
        cin>>choice;
        switch(choice)
        {
            int element;
            case 1 :
                head = createList(head);
                break;
            case 2:
                if (head == NULL)
                    cout<<"List is empty"<<endl;
                else
                    displaylist(head);
                break;
            case 3:
                head = reverse(head);
                break;
            case 4:
                head = addElementEnd(head);
                break;
            case 5:
                head = addElementBegin(head);
                break;
            case 6:
                
                cout<<"Enter the element"<<endl;
                cin>>element;
                head = insertAfterElement(head,element);
                break;
            case 7:
                cout<<"Enter the element"<<endl;
                cin>>element;
                head = insertBeforeElement(head,element);
                break;
            case 8:
                deleteList(head);
                break;
            case 9:exit (1);
            default:
                cout<<"Please choose correct option"<<endl;
                    
        }
    }
}

Comments


bottom of page