Solved programming problem

Convert a Single Linked List to Doubly Linked list, then find the min value and Print alternative fr

Source code

#include <iostream>

using namespace std;

struct single_list ///create single lined list

{

    int s_data;

    struct single_list *s_next;

};

typedef struct single_list s_node;

s_node *start=NULL;

struct doubley_list ///create Doubly Linked list

{

    int d_data;

    struct doubley_list *d_next,*d_prev;

};

typedef struct doubley_list d_node;

d_node *head=NULL;

void insert_S() ///insert single lined list

{

    int n;

    while(1)

    {

         cout<<"Press 1 to Insert
Press 0 to End
";

         cin>>n;

         if(n!=1) return;

         s_node *temp=new s_node();

         cout<<"Data: ";

         cin>>temp->s_data;

         temp->s_next=NULL;

         if(start==NULL)

         {

             start=temp;

         }

         else

         {

             s_node *last=start;

             while(last->s_next != NULL)

             {

                 last = last->s_next;

             }

             last->s_next=temp;

         }

    }

}

void print_s() ///print single linked list

{

    cout<<"Your Single Linked List: 
";

    s_node *temp=start;

    while(temp!=NULL)

    {

        cout<<temp->s_data<<" ";

        temp=temp->s_next;

    }

    cout<<endl;

}

void create_d()///create single to doubly linked list

{

    s_node *temp=start,*t;

    while(temp!=NULL)

    {

        t=temp;

        delete(temp);

        temp=temp->s_next;

        d_node *new_node= new d_node();

        new_node->d_data=t->s_data;

        new_node->d_next=NULL;

        new_node->d_prev=NULL;

        if(head==NULL)

        {

            head=new_node;

        }

        else

        {

            new_node->d_next=head;

            head->d_prev=new_node;

            head=new_node;

        }

    }

}

void   print_d() ///print doubly linked list

{

   cout<<"Your Doubly Linked List: 
";

    d_node *temp=head;

    while(temp!=NULL)

    {

        cout<<temp->d_data<<" ";

        temp=temp->d_next;

    }

    cout<<endl;

}

void find_min() ///find min

{

    int min=head->d_data;

    d_node *temp=head;

    while(temp!=NULL)

    {

        if(temp->d_data<min)

        {

            min = temp->d_data;

        }

        temp=temp->d_next;

    }

    cout<<"
Minimum Value: "<<min<<endl;

}

void  print_alter()///print alternative from the back

{

    d_node *temp=head,*t;

    int count=0;

    while(temp->d_next!=NULL)

    {

        temp=temp->d_next;

    }

    t=temp;

    cout<<"Your Alternative Liked List: ";

    while(t!=NULL)

    {

        count++;

      ///  cout<<count<<endl;

        if(count%2!=0)

        {

            cout<<t->d_data<<" ";

        }

        t = t->d_prev;

    }

    cout<<endl;

}

int main()

{

    cout<<"Insert Single Linked List: "<<endl;

    insert_S();

    print_s();

    create_d();

    print_d();

    find_min();

    print_alter();

    return 0;

}

Output

Insert Single Linked List:

Press 1 to Insert

Press 0 to End

1

Data: 1

Press 1 to Insert

Press 0 to End

1

Data: 2

Press 1 to Insert

Press 0 to End

1

Data: 3

Press 1 to Insert

Press 0 to End

1

Join the discussion

Add your comment