All About C++

Get Pure Concepts of C++

Wednesday, 10 February 2016

how to add,remove and display data from singly link list in data structure

An example of singly link list with the function of insertion, deletion and display
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
            int data;
            node *next;

};
//Defined a Structure


class List      // where we create the links and combin them in a list
{
private:
            node *first;         // Head start or here its name is first...
public:
            List()
            {
                        first = NULL;          // null as link list is empty
            }
            void add(int d)
            {
                        node *ptr, *temp;
                        if (first == NULL)// will execute only once as when we will add first element as first is null.
                        {
                                    temp = new node;     // vvimp steps. Create a structure, and reference will updated have a close look at first.
                                    temp->data = d;      // assign value of d come from main function to first of data.
                                    temp->next = NULL;   // Assign null
                                    first = temp;
                        }
                        else
                        {
                                    ptr = first;
                                    while (ptr->next != NULL)
                                    {
                                                ptr = ptr->next;// same as increment statement as it moves toward null
                                    }
                                    temp = new node;            // these three lines add new elements
                                    temp->data = d;
                                    temp->next = NULL;
                                    ptr->next = temp;           // link the nodes with previous ones....
                        }
            }
                       
                        void delnode()           //deleting
                        {
                                    node *temp1, *temp2, *temp3;

                                    char d;
                                    cout << "press 's' to delete from start,'m' for midd , 'e' for end" << endl;
                                    cin >> d;
                                    switch (d)
                                    {
                                    case's':               //delete startif(start==NULL)
                                    { if (first == NULL)
                                    {
                                                cout << "no node to delete" << endl;
                                    }
                                    else
                                    {
                                                temp1 = first;
                                                first = temp1->next;
                                                delete temp1;
                                    }
                                    break;
                                    case'e':            //delete endif(start==NULL)
                                    { if (first == NULL)

                                    {
                                                cout << "no node to delete" << endl;
                                    }
                                    else
                                    {
                                                temp1 = first;
                                                while (temp1->next != NULL)
                                                {
                                                            temp2 = temp1;
                                                            temp1 = temp1->next;
                                                }
                                                delete temp1;
                                                temp2->next = NULL;
                                    }}
                                                break;
                                    case'm':
                                                int num;//delete midint num;
                                                cout << "enter node you want to delete" << endl;
                                                cin >> num;

                                                temp1 = first;
                                                for (int i = 1; i < num; i++)
                                                {
                                                            if (first == NULL)
                                                            {
                                                                        cout << "given node does not exist" << endl;
                                                                        return;
                                                            }
                                                            else
                                                            {                       temp2 = temp1;
                                                                        temp1 = temp1->next;
                                                            }
                                                }}
                                                            temp3 = temp1->next;
                                                temp2->next = temp3;
                                                delete temp1;
                                                break;
                                    }
                                    };

                        void show()
                        {
                                    node *temp;
                                    temp = first;
                                    cout << "The list follows:" << endl << endl;
                                    while (temp != NULL)  // move towards null
                                    {
                                                cout << temp->data << " ";
                                                temp = temp->next; // same as increment statement as it moves toward null
                                    }
                        }
            };
void main(){
                        List l;
                        l.add(20);
                        l.add(30);
                        l.add(40);
                        l.add(50);
                        l.delnode();
                        l.show();
                                                system("pause");

            }