News:

Loading...

FoodForFutureGeeks

Monday, 7 January 2013

LinkedListImplementationUsing Structures

Singly Linked List with its operations :
/*************************************************************
* Author: Aniruddha S N
* 
*/

#include<iostream>
using namespace std;

// structure node
struct node
{
    int info;
    struct node* next;
};

//function declarations
 void display_list();
 void Insert_Element_List(int ele);
 void DeleteElementAtPos(int pos);

 //global variable first
 node* first = NULL;

 //main function
int main()
{    
    int ele = 0;
    int ch  = 0;
    int pos = 0;

//Menu to display single linked list operations
    while(1)
    {
        cout<<"**************LinkedListOperationsMenu*************\n";
        cout<<"1---->Insert an element in list\n";
        cout<<"2---->displayList\n";
        cout<<"3---->DeleteAnElement at specified position in list\n";
        cout<<"4---->exit\n";
        cout<<"***************************************************\n";
        cout<<"\n\n";
        cout<<"Enter your choice:\n";
        cin>>ch;

        //using switch case call respective functions for respective choices
        switch(ch)
        {
        case 1: cout<<"Enter the element to be inserted into list\n";
                cin>>ele;
                Insert_Element_List(ele);
                break;
        case 2:display_list();
               break;
        case 3:cout<<"Enter the position to delete elements\n";
               cin>>pos;
               DeleteElementAtPos(pos);
               break;
        default: exit(0);
        }

    }
     return 0;
}//end of main

//function to insert an element in singly linked list
void  Insert_Element_List(int ele)
{
    node* temp;
     if(first == NULL)
     {
         first= new node();
         first->info=ele;
         first->next=NULL;
     }
     else
     {
         node* temp1;
         temp1 = new node();
         temp=first;

     //find the end of list
         while( temp->next != NULL)
             temp=temp->next;

         temp1->info=ele;
         temp1->next=NULL;
     //attach the new node at the end of list
         temp->next=temp1;
     }
}


//function to display the elements in the list
void  display_list()
{
    if ( first == NULL)
        cout<<"list doesnt have any elements";

    else if(first->next == NULL)
    {
        cout<<"List Contents:\n";
        cout<<first->info<<"\n";
    }

    else
    {
        node* temp;
        temp=first;
        cout<<"List Contents:\n";
        while( temp != NULL)
        {
            cout<<temp->info<<"\n";
            temp=temp->next;
        }
    }
}

//function to delete element at specified position
void DeleteElementAtPos(int pos)
{
    int flag = 0;
    node* prev;
    node* temp;


    if( first == NULL )
    { cout<<"List Empty\n";
    }

    else if( pos >1 && first->next == NULL )
    {
        cout<<"No element at specified position in list\n";
    }
            
    else if( pos ==1)
      {
            temp=first;
            prev=temp;
            temp=temp->next;

            //re initiate first
            first=temp;
            delete prev;
            prev= NULL;
            
        }

    // for all other positions
    else
    {

        temp=first;
        flag=1;
        while(flag < pos && temp->next != NULL)
        {
           prev=temp;
           temp=temp->next;
           flag++;
        }
        //check whether the specified position exists
        if( flag < pos)
        {
            cout<<"No element at specified position in list\n";
        }
        //delete the node at the specified position
        else
        {
            prev->next = temp->next;
            delete temp;
            temp=NULL;
        }
    
    }//end of else for all other positions
}

No comments:

Post a Comment