Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views9 pages

CPP Lab Print (14&15)

lab material
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

CPP Lab Print (14&15)

lab material
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Exercise -14 a)

AIM: Write a Program to implement List and List Operations


Program:
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
int myints[] = {5, 6, 3, 2, 7};
list<int> l;
list<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"List Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element at the Front"<<endl;
cout<<"2.Insert Element at the End"<<endl;
cout<<"3.Delete Element at the Front"<<endl;
cout<<"4.Delete Element at the End"<<endl;

cout<<"5.Remove Elements with Specific Values"<<endl;


cout<<"6.Remove Duplicate Values"<<endl;
cout<<"7.Reverse the order of elements"<<endl;
cout<<"8.Sorted List"<<endl;

cout<<"9.Display List"<<endl;
cout<<"10.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted at the front: ";
cin>>item;
l.push_front(item);
break;
case 2:
cout<<"Enter value to be inserted at the end: ";
cin>>item;
l.push_back(item);
break;
case 3:
item = l.front();
l.pop_front();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 4:
item = l.back();
l.pop_back();
cout<<"Element "<<item<<" deleted"<<endl;
break;

case 5:
cout<<"Enter element to be deleted: ";
cin>>item;
l.remove(item);
break;
case 6:
l.unique();
cout<<"Duplicate Items Deleted"<<endl;
break;
case 7:
l.reverse();
cout<<"List reversed"<<endl;
break;
case 8:
l.sort();
cout<<"List Sorted"<<endl;
break;
case 9:
cout<<"Elements of the List: ";
for (it = l.begin(); it != l.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 10:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
Exercise -14 b)

AIM: a) Write a Program to implement Vector and Vector Operations


Program:
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
vector<int> ss;
vector<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Vector Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Vector"<<endl;
cout<<"2.Delete Last Element of the Vector"<<endl;
cout<<"3.Size of the Vector"<<endl;
cout<<"4.Display by Index"<<endl;
cout<<"5.Dislplay by Iterator"<<endl;
cout<<"6.Clear the Vector"<<endl;
cout<<"7.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
ss.push_back(item);
break;
case 2:
cout<<"Delete Last Element Inserted:"<<endl;
ss.pop_back();
break;
case 3:
cout<<"Size of Vector: ";
cout<<ss.size()<<endl;
break;
case 4:
cout<<"Displaying Vector by Index: ";
for (int i = 0; i < ss.size(); i++)
{
cout<<ss[i]<<" ";
}
cout<<endl;
break;
case 5:
cout<<"Displaying Vector by Iterator: ";
for (it = ss.begin(); it != ss.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
break;
case 6:
ss.clear();
cout<<"Vector Cleared"<<endl;
break;
case 7:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
Exercise -15 a)

AIM: Write a Program to implement Dequeue and Dequeue Operations

Program:
#include <iostream>
#include <deque>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
deque<int> dq;
deque<int>::iterator it;
int choice, item;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Deque Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element at the End"<<endl;
cout<<"2.Insert Element at the Front"<<endl;
cout<<"3.Delete Element at the End"<<endl;
cout<<"4.Delete Element at the Front"<<endl;
cout<<"5.Front Element at Deque"<<endl;
cout<<"6.Last Element at Deque"<<endl;
cout<<"7.Size of the Deque"<<endl;
cout<<"8.Display Deque"<<endl;
cout<<"9.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted at the end: ";
cin>>item;
dq.push_back(item);
break;
case 2:
cout<<"Enter value to be inserted at the front: ";
cin>>item;
dq.push_front(item);
break;
case 3:
item = dq.back();
dq.pop_back();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 4:
item = dq.front();
dq.pop_front();
cout<<"Element "<<item<<" deleted"<<endl;
break;
case 5:
cout<<"Front Element of the Deque: ";
cout<<dq.front()<<endl;
break;
case 6:
cout<<"Back Element of the Deque: ";
cout<<dq.back()<<endl;
break;
case 7:
cout<<"Size of the Deque: "<<dq.size()<<endl;
break;
case 8:
cout<<"Elements of Deque: ";
for (it = dq.begin(); it != dq.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 9:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
Exercise -15 b)

AIM: Write a Program to implement Map and Map Operations


Program:

#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
map<char,int> mp;
map<char, int>::iterator it;
int choice, item;
char s;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"Map Implementation in Stl"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the Map"<<endl;
cout<<"2.Delete Element of the Map"<<endl;
cout<<"3.Size of the Map"<<endl;
cout<<"4.Find Element at a key in Map"<<endl;
cout<<"5.Dislplay by Iterator"<<endl;
cout<<"6.Count Elements at a specific key"<<endl;
cout<<"7.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
cout<<"Enter the key: ";
cin>>s;
mp.insert(pair<char,int>(s ,item));
break;
case 2:
cout<<"Enter the mapped string to be deleted: ";
cin>>s;
mp.erase(s);
break;
case 3:
cout<<"Size of Map: ";
cout<<mp.size()<<endl;
break;
case 4:
cout<<"Enter the key at which value to be found: ";
cin>>s;
if (mp.count(s) != 0)
cout<<mp.find(s)->second<<endl;
else
cout<<"No Element Found"<<endl;
break;
case 5:
cout<<"Displaying Map by Iterator: ";
for (it = mp.begin(); it != mp.end(); it++)
{
cout << (*it).first << ": " << (*it).second << endl;
}
break;
case 6:
cout<<"Enter the key at which number of values to be counted: ";
cin>>s;
cout<<mp.count(s)<<endl;
break;
case 7:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

You might also like