MS Rumi. Powered by Blogger.

Tuesday 21 October 2014

Free Download Project Of Linked List In C++

By Unknown  |  06:50 No comments

Linked List Basic And Simple Program 

In this program user can have six functions to select. User can Add  Node In Beginning,  In Last  can display link list, user can delete the node from specific position and also can add node for specific position in linked list and user can reverse complete linked list, finally user will have exit option to exit program.   

Header Files For Linked List Function 

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip.h>   // For Set word

// structre for node

struct node{
    int value;
    node *next;
}*start=NULL;

int count=0;// Global Integer For Counting

// Function To Create Node In Link List (C++)

node* creatNode(int data)
{
    node *temp;
    temp= new node;  // Creating Node
    temp->value=data; //assiging value
    temp->next=NULL;
    return temp;
}

// Function To Add Node At Given Position In  Linked List C++

void addNode()
{
    int value,pos=0,key;
    node *temp=start;
    node *newNode;
    node *pre=start;
    if(start==NULL)
    {
        cout<<"Enter value:";
        cin>>value;
        temp=creatNode(value);
        start=temp;
        start->next=NULL;
        return;
    }
    cout<<"Enter Position To Add New Node:";
    cin>>key;
    if(pos==key)
    {
        cout<<"Enter value:";
        cin>>value;
        temp=creatNode(value);
        start=temp;
        start->next=pre;
        return;
    }
    else
    {
        int i=1;
        while(temp!=NULL)
        {   
            pre=temp;
            temp=temp->next;
            if(i==key)
            {
                cout<<"Enter value:";
                cin>>value;
                newNode=creatNode(value);
                newNode->next=temp;
                pre->next=newNode;
                return;
            }
            i++;
        }
    }
    cout<<"Node Position Is Not Available";   
   

}

// Function To Add Node At Beginning of Linked List C++


void insertBeg()
{
    int value;
    node *temp, *pre;
    cout<<"Enter value:";
    cin>>value;
    temp=creatNode(value);
    if(start==NULL)
    {
        start=temp;
        start->next=NULL;
    }
    else
    {
        pre=start;
        start=temp;
        start->next=pre;

    }

}

// Function To Add Node At Last of Linked List C++

void insertLast()
{
    int value;
    node *temp, *last;
    cout<<"Enter value:";
    cin>>value;
    temp=creatNode(value);//Storing addres of created Node
    if(start==NULL) // If there is no node
    {
        start=temp; 
    }
    else
    {   
        last=start;
        while(last->next!=NULL) // Checking Adress in Next
        {
            last=last->next;   //Moving Pointer to Last
        }
        temp->next=NULL; // To Make It Last
        last->next=temp;    // Connecting Last Node To New Node
    }
       

   

}



// Function To Delete Node From Linked List C++




void deleteNode(int num)
{
    int pos=1;
    node *temp, *pre;
    temp=start;            // assigning start node address to temp
    if(temp==NULL)   // Checking list is empty?
    {
        cout<<"*************List Is empty************";
        return;
    }
    if(num==pos)                    // If user want to delte first node
    {
        if(temp->next==NULL)    // checking that is first node is last?
        {
            start=NULL;        // List Will Become Empty
            cout<<"There Was Only One Node ... Now List Is Empty";
            return;
        }
        else
        {
            start=temp->next;
            return;
        }
    }
    while (temp!=NULL)
        {
            pos++;
            pre=temp;
            temp=temp->next;
           
            if(pos==num)
            {
                if(temp->next=NULL)
                {
                    pre->next=NULL;
                    cout<<"You Deleted Last Node Of Your List..."<<endl;
                    return;
                }
                else
                {
                    pre->next=temp->next;
                    return;
                }
            }
        }
}



// Displaying Linked List Function In C++

void display()
{
    node *temp;
    int num=0;
    temp=start;
    if(temp==NULL)
    {
        cout<<"Link List Is Empty...."<<endl;
        return;
    }
    cout<<"Node #"<<setw(10)<<"Value"<<endl;
    while(temp!=NULL)
    {
        cout<<setw(2)<<num<<setw(11)<<temp->value<<endl;
        temp=temp->next;
        num++;
        count++;
    }
    cout<<endl;
}


// Function Reverse Linked List C++

void reverse ()
{
    node *frw;
    node *pre=NULL;
    node *curent=start;
    while(curent!=NULL)
    {
        frw=curent->next;
        curent->next=pre;
        pre=curent;
        curent=frw;
    }
    start=pre;

}


// Main Function
int main ()
{
    int option,num;
    while(1)
    {
        system("CLS");
        cout<<endl;
        cout<<"*****Select Option********"<<endl;
        cout<<"* 1. Add In Begining     *"<<endl;
        cout<<"* 2. Add In Last         *"<<endl;
        cout<<"* 3. Display Link List   *"<<endl;
        cout<<"* 4. Delete Node         *"<<endl;
        cout<<"* 5. Reverse Node        *"<<endl;
        cout<<"* 6. Add Node At Nth Pos *"<<endl;
        cout<<"* 7. Exit                *"<<endl;
        cout<<"**************************"<<endl;
        cout<<"Enter Option From Above:";
        cin>>option;
        switch(option)
        {
        case 1:
            {
                insertBeg();
            }
            break;
        case 2:
            {
                insertLast();
            }
            break;
        case 3:
            {
                display();
                cout<<"\n Total Nodes"<<count;
                system("pause");
            }
            break;
        case 4:
            {
                display();
                if(start)
                {
                    cout<<"Enter Node Number To Delete:";
                    cin>>num;
                    deleteNode(num);
                    display();
                    system("pause");
                }
                else
                    system("pause");
            }
            break;
        case 5:
            {
                cout<<"Before Reverse"<<endl;
                display();
                reverse();
                cout<<"After Reverse"<<endl;
                display();
                system("pause");
            }
            break;
        case 6:
            {
                display();
                addNode();
            }
            break;
        case 7:
            {
                return 0;
            }
            break;
        default:
            cout<<"Invalid Option"<<endl;
        }
    }
}


If you have any issue regarding to this linked list program, please ask in the comment section.
Or if you have any question about linked list coding or other c++ programing issue post it here I will give you solution of your problem with explanation. 

Author: Unknown

Hello, I am Author, thanks for visiting my blog. Bookmark this....

0 comments:

E-mail Newsletter

Sign up now to receive breaking news and to hear what's new with us.

Recent Articles

TOP