Write C++ program to store set of negative and positive numbers using linked list.
Second Year Computer Engineering Data Structure Programs:
Data Structure Lab:
Practical B18:UHG MN
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
#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node*next;
}*start,*start1,*start2;
class single_llist
{
public:
node*create_node(int);
void insert_begin();
void delete_pos();
void display();
void sort();
void split();
void merge();
};
int main()
{
single_llist s1;
start=NULL;
int choice;
while(1)
{
cout<<"1.Insert integer "<<endl;
cout<<"2.Display integer list"<<endl;
cout<<"3.Delete Integers"<<endl;
cout<<"4.Create two link list"<<endl;
cout<<"5.Merge List"<<endl;
cout<<"6.Exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Integers:"<<endl;
s1.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Display Integers:"<<endl;
s1.display();
cout<<endl;
break;
case 3:
cout<<"Delete Integers :"<<endl;
s1.delete_pos();
cout<<endl;
break;
case 4:
cout<<"split Integers :"<<endl;
s1.split();
cout<<endl;
break;
case 5:
cout<<"Merge Integers :"<<endl;
s1.merge();
cout<<endl;
break;
case 6:
cout<<"Exiting...."<<endl;
exit(1);
break;
default:
cout<<"WRONG CHOICE...";
return 0;
}
}
}
node *single_llist::create_node(int value)
{
struct node *temp;
temp=new(struct node);
if(temp==NULL)
{
cout<<"Memory not allowed"<<endl;
return 0;
}
else
{
temp->info=value;
temp->next=NULL;
return temp;
}
}
void single_llist::insert_begin()
{
int value,n;
struct node*temp,*p;
cout<<"Enter the no. of integers"<<endl;
cin>>n;
cout<<"Enter Integers"<<endl;
for(int i=0;i<n;i++)
{
cin>>value;
temp=create_node(value);
if(start==NULL)
{
start=temp;
start->next=NULL;
p=start;
}
else
{
p->next=temp;
p=p->next;
}
}
cout<<" Inserted "<<endl;
}
void single_llist::display()
{
struct node*temp;
if(start==NULL)
{
cout<<"the list is empty"<<endl;
return ;
}
temp=start;
cout<<"element list are:"<<endl;
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
void single_llist::sort()
{
struct node *ptr, *s;
int value;
if (start == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
ptr = start;
while (ptr != NULL)
{
for (s = ptr->next;s !=NULL;s = s->next)
{
if (ptr->info > s->info)
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
void single_llist::delete_pos()
{
struct node *p;
sort();
p=start;
while(p->info<0)
{
start=p->next;
p=p->next;
}
}
void single_llist::split()
{
struct node *ptr1,*ptr2;
sort();
for(ptr1=start;ptr1->info<0;ptr1=ptr1->next)
{
ptr2=ptr1;
}
start1=start;
ptr2->next=NULL;
start2=ptr1;
cout<<"List of Negative Numbers :"<<endl;
start=start1;
display();
cout<<"List of Positive Number :"<<endl;
start=start2;
display();
}
void single_llist::merge()
{
struct node*p;
for(p=start1;p->next!=NULL;p=p->next)
{}
p->next=start2;
start=start1;
display();
}
----------------------------------------------------------------------------------------------------------------------------------
/**************************************OUTPUT**********************************
********************************************
Enter your choice among the following:
1.Insert the numbers.
2.Display the list.
3.Seperate the positive & negative numbers.
4.Remove negative numbers from list.
5.Merge the two lists in sorted form.
6.Exit.
********************************************
1
Enter the number for 1st list: 23
Do you want to enter more numbers?(y/n)
y
Enter the number for 1st list: -14
Do you want to enter more numbers?(y/n)
y
Enter the number for 1st list: 36
Do you want to enter more numbers?(y/n)
y
Enter the number for 1st list: -77
Do you want to enter more numbers?(y/n)
y
Enter the number for 1st list: -21
Do you want to enter more numbers?(y/n)
n
********************************************
Enter your choice among the following:
1.Insert the numbers.
2.Display the list.
3.Seperate the positive & negative numbers.
4.Remove negative numbers from list.5.Merge the two lists in sorted form.
6.Exit.
********************************************
2
23
-14
36
-77-21
********************************************
Enter your choice among the following:
1.Insert the numbers.
2.Display the list.
3.Seperate the positive & negative numbers.
4.Remove negative numbers from list.
5.Merge the two lists in sorted form.
6.Exit.
********************************************
3
The positive numbers in the list are:
23
36
The negative numbers in the list are:
-14
-77
-21
********************************************
Enter your choice among the following:
1.Insert the numbers.
2.Display the list.
3.Seperate the positive & negative numbers.
4.Remove negative numbers from list.
5.Merge the two lists in sorted form.
6.Exit.
********************************************
4
The list without negative numbers is as follows:
23
36
********************************************
Enter your choice among the following:
1.Insert the numbers.
2.Display the list.
3.Seperate the positive & negative numbers.
4.Remove negative numbers from list.
5.Merge the two lists in sorted form.
6.Exit.
********************************************
5Enter the number for 2nd list: -45
Do you want to enter more numbers?(y/n)
y
Enter the number for 2nd list: -25
Do you want to enter more numbers?(y/n)
y
Enter the number for 2nd list: 47
Do you want to enter more numbers?(y/n)
y
Enter the number for 2nd list: 13Do you want to enter more numbers?(y/n)
n
The two lists have been merged in the sorted form as follows:
-77
-45
-25
-21
-14
13
23
36
47
********************************************
Enter your choice among the following:
1.Insert the numbers.
2.Display the list.
3.Seperate the positive & negative numbers.
4.Remove negative numbers from list.
5.Merge the two lists in sorted form.
6.Exit.
********************************************/
Comments
Post a Comment