Sunday 18 September 2016

Linked list program to store negative and positive number


Group B: (Assignment No:04)(SPPU Syllabus assignment No :18)
Problem Statement: Write C++ program to store set of negative and positive numbers using linked list. Write functions to 
a) Insert numbers
b) Delete nodes with negative numbers
c) Create two more linked lists using this list, one containing all positive numbers and other containing negative numbers
d) For two lists that are sorted; Merge these two lists into third resultant list that is sorted.

Program Code:
//Author: Prof.Anjali Almale
#include<iostream>
using namespace std;
struct node   //node defined to allocate memory
{
    int num; //To store data
    node *next; //To store address of next node.
};

class numbers  //class name is number
{
 public:

    node *head,*head1,*head2; //data memebers

    numbers() //default constructor
    {
        head=NULL;
        head1=NULL;
        head2=NULL;
    }
    void create();       //member function declaration.
    void display(node *);
    void insert();
    void sort(node *);
    void display2();
    void remove();
    void seperate();
    void merge();
}; //end of class

void numbers::create()
{

  node *current,*new_node;
    char c;
 cout<<"\n------------CREATION OF NUMBERS LIST-------------\n\n ";
    do
    {

    new_node=new node; //dynamic memory allocation
    cout<<"\n Enter the no: \n ";
    cin>>new_node->num; //To store data

    new_node->next=NULL; //make node next null(init)
    if(head==NULL) //check header if null
    {
        head=new_node;
        current=new_node;
    }
    else
    {
        current->next=new_node;
        current=new_node;

    }
    cout<<"\nDo you want to add new member";
    cin>>c;
       }while(c!='n');
}
void numbers::seperate()//To separate the positive numbers and negative numbers.
{
    node *temp,*current1,*current2,*n1,*n2;
    temp=head;

    while(temp!=NULL)
    {
        if((temp->num)<0)
        {
            n1=new node;
            n1->num=temp->num;
            n1->next=NULL;

            if(head1==NULL)
            {
                current1=n1;
                head1=n1;
            }
            else
            {
            current1->next=n1;
            current1=n1;
            }

        }
        else if((temp->num)>=0)
        {
            n2=new node;
            n2->num=temp->num;
            n2->next=NULL;
            if(head2==NULL)

            {
                current2=n2;
                head2=n2;
            }
            else
            {
              current2->next=n2;
              n2->next=NULL;
              current2=n2;
            }
        }
          temp=temp->next;
     }
}

void numbers::display(node *head)//To display the list
{
    node *p;
    p=head;
    if(p==NULL)
    {
        cout<<"\nThe list is Empty";
    }
    else
    {
        while(p!=NULL)
        {
            cout<<p->num<<"\t";
             p=p->next;
        }
    }
}

void numbers::sort(node *head_d) //Before merge u hv to sort them
{
    node *temp1,*temp2,*temp3;
    temp1 = head_d;

 for( ;temp1->next!=NULL;temp1=temp1->next)
 {
     for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
     {
         if(temp1->num>temp2->num)
         {
             int temp= temp1->num;
             temp1->num = temp2->num;
             temp2->num = temp;
         }
     }
 }
 temp3 = head_d;

while (temp3!=NULL)
{
cout<<"\t"<< temp3->num;
temp3 =temp3->next;
}
}

void numbers::merge()//To merge both the list
{
    node *temp;
    temp=head1;

    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    temp->next=head2;
}
void numbers::insert()
{
    node *p,*temp;
        p=new node;
        cout<<"\n\n Enter New Number to be insert  :  ";
        cin>>p->num;
        p->next=NULL;
        temp=head;
        while(temp->next!=NULL)
        {
    temp=temp->next;
        }
    temp->next=p;
}

int main()
{
    numbers n;
    n.create();
    cout<<"\n \nTHE LIST OF +VE AND -VE NUMBERS IS: \n";
    n.display(n.head);
    n.insert();
    cout<<"\n \nTHE LIST AFTER INSERTION IS: \n";
    n.display(n.head);
    n.seperate();
    cout<<"\n\n THE LIST OF ONLY -VE NUMBERS IS: \n";
    n.display(n.head1);
    cout<<"\n\n THE LIST OF ONLY +VE NUMBERS IS: \n";
    n.display(n.head2);
    cout<<"\n\n THE LIST OF ONLY SORTED -VE NUMBERS IS : \n";
    n.sort(n.head1);
    cout<<"\n\n THE LIST OF ONLY SORTED +VE NUMBERS IS : \n";
    n.sort(n.head2);
    n.merge();
    cout<<"\n AFTER MERGE .....THE LIST IS AS FOLLOWS: \n";
    n.display(n.head1);
    cout<<"\n\n THE LIST OF SORTED +VE AND -VE NUMBERS IS : \n";
    n.display(n.head1);
    return 0;
}

/*   OUTPUT *************************************************************


------------CREATION OF NUMBERS LIST-------------


 Enter the no:
 1

Do you want to add new membery

 Enter the no:
 -5

Do you want to add new membery

 Enter the no:
 6

Do you want to add new membery

 Enter the no:
 -7

Do you want to add new membery

 Enter the no:
 -3

Do you want to add new membery

 Enter the no:
 -4

Do you want to add new membery

 Enter the no:
 7

Do you want to add new membern


THE LIST OF +VE AND -VE NUMBERS IS:
1    -5    6    -7    -3    -4    7

 Enter New Number to be insert  :  10


THE LIST AFTER INSERTION IS:
1    -5    6    -7    -3    -4    7    10

 THE LIST OF ONLY -VE NUMBERS IS:
-5    -7    -3    -4

 THE LIST OF ONLY +VE NUMBERS IS:
1    6    7    10

 THE LIST OF ONLY SORTED -VE NUMBERS IS :
    -7    -5    -4    -3

 THE LIST OF ONLY SORTED +VE NUMBERS IS :
    1    6    7    10
 AFTER MERGE .....THE LIST IS AS FOLLOWS:
-7    -5    -4    -3    1    6    7    10

 THE LIST OF SORTED +VE AND -VE NUMBERS IS :
-7    -5    -4    -3    1    6    7    10

********************************************************************************************/
data structures and algorithms Web Developer

1 comment:

  1. Program is running well but at last where the list of +ve and -ve should be sorted it's not happening

    ReplyDelete