Wednesday, 10 February 2016

Insertion in Double Linked List

#include<iostream>
using namespace std;


struct node
{
    int data;
    node*n, *p;
};
class double_llist
{
private:node*start, *cur, *temp;
    public:
        double_llist()
        {
            start=NULL;
        }
        void Append(int val)
        {
            if(start==NULL)
            {
                start=new node;
                start->p=NULL;
                start->n=NULL;
                start->data=val;
            }
            else
            {
                cur=start;
                while(cur->n!=NULL)
                {
                cur=cur->n;
                }
                temp=new node;
                temp->n=NULL;
                temp->p=cur;
                temp->data=val;
                cur->n=temp;
               
            }
        }
        void print()
        {
            if(start==NULL)
            cout<<"List is empty";
            else
            {
                cur=start;
                while(cur->n!=NULL)
                {
                    cout<<cur->data;
                    cur=cur->n;
                }
                cout<<cur->data;
            }
           
        }
    };
    int main()
    {
        double_llist obj;
        int value;
        cout<<"Please,Enter 10 values"<<endl;
        for(int i=0;i<=9;i++)       
        {
            cout<<"Enter number at loc"<<i<<endl;
            cin>>value;
            obj.Append(value);
        }
        cout<<endl;
        obj.print();
    }

No comments:

Post a Comment

Tree Insertion

#include <iostream> using namespace std; struct node {     int data;     node*left;     node* right; }; class tree { priva...