Linklist all concepts using Dynamic arrays(using nodes)

 



Double Link List (Add and Show nodes)

#include <iostream>

using namespace std;

struct node

{

    int data;

    node* pre, *nxt;

};

class Link_List

{

        node *head, *current;

    public:

        Link_List()

        {

            head=current=NULL; // Means at the most link list has no node.

        }

       void List_Add(int n)

        {

            node*tempNode;

            tempNode=new node;

            tempNode->data=n;

            tempNode->pre=NULL;

            tempNode->nxt=NULL;

            if(head==NULL)

            {

                current=head=tempNode;

            }

            else

            {

                current->nxt=tempNode;

                tempNode->pre=current;

                current=tempNode;

            }

        }

        void show_Forward()

        {

            node *tempNode=head;

            while(1)

            {

                cout<<"\t"<<tempNode->data<<endl;

                if(tempNode->nxt==NULL)

                    break;

                tempNode=tempNode->nxt;

            }

        }

        void show_Reversed()

        {

            node *tempNode=current;

            while(1)

            {

                cout<<"\t"<<tempNode->data<<endl;

                if(tempNode->pre==NULL)

                    break;

                tempNode=tempNode->pre;

            }

        }

};

int main()

{

Link_List L1;

for(int i=0;i<=5;i++)

{

L1.List_Add(i);

 

}

cout<<"===In Ascending Order==="<<endl;

L1.show_Forward();

cout<<"===In Descending Order==="<<endl;

L1.show_Reversed();

    return 0;

}

Insertion and Deletion of nodes

//double link list

 

#include<iostream>

using namespace std;

struct node{

                int data;

                node *nxt;

                node *prv;

};

class LinkList{

                                              node *head, *current;

                                                                int size;

                                                                public:

                                                                                LinkList()

                                                                                {head = NULL;

                                                                                current = NULL;

                                                                                size = 0;

                                                                                }

                                                                                void insert_at_end(int x)

                                                                                {

                                                                                                node *tmpnode;

                                                                                                tmpnode = new node;

                                                                                                tmpnode->data = x;

                                                                                                tmpnode->nxt = NULL;

                                                                                                tmpnode->prv = NULL;

                                                                                                if(head == NULL)

                                                                                                {head = current = tmpnode;

                                                                                                size++;

                                                                                                }

                                                                                                else{

                                                                                                                size++;

                                                                                                                current->nxt = tmpnode;

                                                                                                                tmpnode->prv = current;

                                                                                                                current = current->nxt;

                                                                                                }

                                                                                                 

                                                                                }

                                                                                void insert_at_start(int x)

                                                                                {

                                                                                                node *tmpnode;

                                                                                                tmpnode = new node;

                                                                                                tmpnode->data = x;

                                                                                                tmpnode->nxt = NULL;

                                                                                                tmpnode->prv = NULL;

                                                                                                if(head == NULL)

                                                                                                {

                                                                                                                size++;

                                                                                                                head = current =tmpnode;

                                                                                                }

                                                                                               

                                                                                else {

                                                                                                size++;

                                                                                                head->prv = tmpnode;

                                                                                                tmpnode->nxt = head;

                                                                                           head = head->prv;                                                                                          

                                                                                }

                                                                                }

                                                                                void insert_at_pos(int x, int pos)

                                                                                {

                                                                                                node *tmpnode;

                                                                                                tmpnode = new node;

                                                                                                tmpnode->data = x;

                                                                                                tmpnode->nxt = NULL;

                                                                                                tmpnode->prv = NULL;

                                                                                                if(head == NULL || pos > size)

                                                                                                {

                                                                                                                cout<<"position does not exist";

                                                                                                }

                                                                                                else if(pos == 1)

                                                                                                {

                                                                                                                //size++;

                                                                                                                insert_at_start(x);

                                                                                                }

                                                                                                else if(pos == size)

                                                                                                {

                                                                                                //            size++;

                                                                                                insert_at_end(x);

                                                                                                }

                                                                                                else{

                                                                                                                node *tmp1;

                                                                                                                tmp1 = head;

                                                                                                                int i=1;

                                                                                                                while(i<pos-1)

                                                                                                                {

                                               

                                                                                                                                tmp1 = tmp1->nxt;

                                                                                                                                i++;

                                                                                                                }

                                                                                                                node *tmp2;

                                                                                                                tmp2 = tmp1->nxt;

                                                                                                                tmp1->nxt = tmpnode; 

                                                                                                                tmpnode->prv = tmp1;

                                                                                                                tmp2->prv = tmpnode;

                                                                                                                tmpnode->nxt = tmp2;

                                                                                                                size++;

                                                                                                }

                                                                                }

                                                                                                void del_at_pos(int pos)

                                                                                                {

                                                                                                                node *tmp1;

                                                                                                                tmp1 = head;

                                                                                                                if(head ==  NULL)

                                                                                                                {cout<<"position not exist";

                                                                                                                }

                                                                                                                else if(pos ==  size)

                                                                                                                {

                                                                                                                                 delete_at_end();

                                                                                                                }

                                                                                                                else if(pos ==  1)

                                                                                                                {

                                                                                                                                delete_at_start();

                                                                                                                }

                                                                                                                else {

                                                                                                                                size--;

                                                                                                                                int i = 1;

                                                                                                                while(i<pos-1)

                                                                                                                {

                                               

                                                                                                                tmp1 = tmp1->nxt;

                                                                                                                                i++;

                                                                                                                }

                                                                                                                node *del_wali_tmp,*tmp2;

                                                                                                                del_wali_tmp = tmp1->nxt;

                                                                                                                tmp2 = del_wali_tmp->nxt;

                                                                                                                tmp1->nxt = tmp2;

                                                                                                                tmp2->prv = tmp1;

                                                                                                                delete del_wali_tmp;

                                                                                                                }

                                                                                                }

 

                                                                               

                                                                               

                                                                                void delete_at_end()

                                                                                {

                                                                                                if(head == NULL)

                                                                                                cout<<"list is empty"<<endl;

                                                                                                else if(head == current) {

                                                                                                                size--;

                                                                                                                node *tmp =current;

                                                                                                                current = head =NULL;

                                                                                                                delete tmp;                                                                                                                       

                                                                                                }             

                                                                                                else{

                                                                                                                size--;

                                                                                                                node *tmp;

                                                                                                                tmp = current;

                                                                                                                current = current->prv;

                                                                                                                current->nxt = NULL;

                                                                                                                delete tmp;

                                                                                                }                             

                                                                                }

                                                                                void delete_at_start()

                                                                                {

                                                                                                if(head == NULL)

                                                                                                cout<<"list is empty"<<endl;

                                                                                                else if(head == current) {

                                                                                                                size--;

                                                                                                                node *tmp =current;

                                                                                                                current = head =NULL;

                                                                                                                delete tmp;                                                                                                                       

                                                                                                }                                                                                                                                                                             

                                                                                                else{

                                                                                                                size--;

                                                                                                                node *tmp;

                                                                                                                tmp= head;

                                                                                                                head = head->nxt;

                                                                                                                head->prv = NULL;

                                                                                                                delete tmp;

                                                                                                }

                                                                                }

                                                                                void show()

                                                                                {

                                                                                                node *tmp = head;

                                                                                                while(tmp)

                                                                                                {

                                                                                                                cout<<tmp->data<<endl;

                                                                                                                tmp = tmp->nxt;

                                                                                                }

                                                                                                if(!tmp)

                                                                                            {cout<<"<<<<<<<<<list ended>>>>>>>>\n";

                                                                                                }

                                                                                }

                                                                               

};

 

int main()

{

                LinkList *list1;

                list1 = new LinkList;

                list1->insert_at_end(10);

                list1->insert_at_end(20);

                list1->insert_at_end(30);

                list1->insert_at_end(40);

                //list1->show();

                cout<<"____________\n";

                list1->insert_at_start(5);

                list1->insert_at_start(0);

                list1->insert_at_pos(15,4);

                list1->insert_at_pos(25,6);

                list1->del_at_pos(5);

 

                list1->show();

               

}


Comments

Post a Comment

Popular posts from this blog

Oop(Object Oriented Programming) Project for final year students

Write a C++ program to implement a suitable data structures (using array) of marble plates