DS Assignment Final
DS Assignment Final
Array
Q. 1 Write a program using array.
#include <cstdlib>
#include <iostream>
#define NUM_EMPLOYEE 10
using namespace std;
int main()
{
int Salary[NUM_EMPLOYEE], lCount=0,gCount=0,i=0;
cout << "Enter employee salary (Max 10) " << endl;
for (i=0; i<NUM_EMPLOYEE; i++){
cout << "Enter employee salary: - " << i+1 << endl;
cin >> Salary[i];
}
cout << "There are " << gCount << " employee with salary more than
3000" << endl
<< "There are " << lCount << " employee with salary less than 3000" <<
endl;
return 0;
}
Output-
Enter employee salary (Max 10)
Enter employee salary: - 1
2000
Enter employee salary: - 2
5000
Enter employee salary: - 3
1000
Enter employee salary: - 4
4000
Enter employee salary: - 5
2000
Enter employee salary: - 6
2500
Enter employee salary: - 7
7000
Enter employee salary: - 8
8000
Enter employee salary: - 9
1000
Enter employee salary: - 10
4000
There are 5 employee with salary more than 3000
There are 5 employee with salary less than 3000
Assignment No-2
Polynomials
Q. 1 Write a program to representation of Polynomials using array
#include <iostream>
using namespace std;
struct poly
{
int coeff;
int pow_val;
poly* next;
};
class add
{
poly *poly1, *poly2, *poly3;
public:
add() { poly1 = poly2 = poly3 = NULL; }
void addpoly();
void display();
};
void add::addpoly()
{
int i, p;
poly *newl = NULL, *end = NULL;
cout << "Enter highest power for x\n";
cin >> p;
cout << "\nFirst Polynomial\n";
for (i = p; i >= 0; i--)
{
newl = new poly;
newl->pow_val = p;
cout << "Enter Co-efficient for degree" << i << ":: "; cin >>newl-
>coeff;
newl->next = NULL;
if (poly1 == NULL)
poly1 = newl;
else
end->next = newl;
end = newl;
}
cout << "\n\nSecond Polynomial\n"; end = NULL;
for (i = p; i >= 0; i--)
{
newl = new poly;
newl->pow_val = p;
cout << "Enter Co-efficient for degree" << i << ":: "; cin >>
newl->coeff;
newl->next = NULL;
if (poly2 == NULL)
poly2 = newl;
else
end->next = newl;
end = newl;
}
poly *p1 = poly1, *p2 = poly2;
end = NULL;
while (p1 != NULL && p2 != NULL) {
if (p1->pow_val == p2->pow_val) {
newl = new poly;
newl->pow_val = p--;
newl->coeff = p1->coeff + p2->coeff;
newl->next = NULL;
if (poly3 == NULL)
poly3 = newl;
else
end->next = newl;
end = newl;
}
p1 = p1->next;
p2 = p2->next;
}
}
void add::display()
{
poly* t = poly3;
cout << "\n\nAnswer after addition is : ";
while (t != NULL) {
cout.setf(ios::showpos);
cout << t->coeff;
cout.unsetf(ios::showpos);
cout << "X" << t->pow_val;
t = t->next;
}
}
int main()
{
add obj;
obj.addpoly();
obj.display();
}
Output-
First Polynomial
Enter Co-efficient for degree2:: 1
Enter Co-efficient for degree1:: 2
Enter Co-efficient for degree0:: 3
Second Polynomial
Enter Co-efficient for degree2:: 3
Enter Co-efficient for degree1:: 2
Enter Co-efficient for degree0:: 1
num[j+1]=x;
}
cout<<"\n\nArray after Insertion Sort\n";
for(i=0; i<7; i++)
{
cout<<num[i]<<" ";
}
return 0;
}
Output
num[lnp]=t;
}
}
cout<<"\n\nArray after Selection Sort\n";
for(i=0; i<7; i++)
{
cout<<num[i]<<" ";
}
return 0;
}
Output :
Array before Selection Sort
12 9 37 86 2 17 5
Array after Selection Sort
86 37 17 12 9 5 2
Assignment No – 4
Searching
Q1.Write a program using linear search and binary search
#include <iostream>
using namespace std;
class Search
{
protected:
int arr[10],n;
public:
Search()
{
n=sizeof(arr)/sizeof(arr[0]);
}
void Input();
void Display();
virtual int search(int)=0;
};
void Search::Input()
{
cout<<endl<<"Enter "<<n<<" integer elements :";
for(int i=0;i<n;i++)
cin>>arr[i];
}
void Search::Display()
{
cout<<endl<<"List : ";
for(int i=0;i<n;i++)
cout<<arr[i]<<"("<<i<<") ";
}
class LinearSearch:public Search
{
public:
int search(int);
};
int LinearSearch::search(int Tok)
{
Display();
for(int i=0;i<n;i++)
if(arr[i]==Tok)
return i;
return -1;
}
class BinarySearch:public Search
{
public:
int search(int);
};
int BinarySearch::search(int Tok)
{
for(int It=1;It<n;It++)
for(int i=0;i<n-It;i++)
if(arr[i]>arr[i+1])
{
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
Display();
int l=0,u=n-1;
while(l<=u)
{
int mid=(l+u)/2;
if(arr[mid]==Tok)
return mid;
if(arr[mid]>Tok)
u=mid-1;
else
l=mid+1;
}
return -1;
}
int main(int argc, char *argv[]) {
Search *ptr;
int pos;
do
{
int ch;
cout<<endl<<"Searching"<<endl<<"Menu"<<endl<<"
1:Linear"<<endl
<<" 2:Binary"<<endl<<"Else:Exit";
cout<<endl<<endl<<"Enter choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<endl<<"Linear Search";
ptr=new LinearSearch;
break;
case 2:
cout<<endl<<"Binary Search";
ptr=new BinarySearch;
break;
default:
cout<<endl<<"Exit...";
return 0;
}
ptr->Input();
cout<<endl<<"Enter token : ";
cin>>ch;
if((pos=ptr->search(ch))!=-1)
cout<<endl<<"Found at position :"<<pos;
else
cout<<endl<<"Not a member of given list";
delete ptr;
ptr=NULL;
}
while(1);
return 0;
}
Output:
Searching
Menu
1:Linear
2:Binary
Else:Exit
Enter choice : 1
Linear Search
Enter 10 integer elements :1 2 7 8 0 4 7 3 5 9
Enter token : 4
List : 1(0) 2(1) 7(2) 8(3) 0(4) 4(5) 7(6) 3(7) 5(8) 9(9)
Found at position :5
Searching
Menu
1:Linear
2:Binary
Else:Exit
Enter choice : 2
Binary Search
Enter 10 integer elements :2 1 4 6 8 7 0 3 6 4
Enter token : 6
List : 0(0) 1(1) 2(2) 3(3) 4(4) 4(5) 6(6) 6(7) 7(8) 8(9)
Found at position :7
Searching
Menu
1:Linear
2:Binary
Else:Exit
Enter choice :
Assignment No-5
Stack
top=-1;
}
int Peek()
{
if(Isempty())
{
cout<<endl<<"Empty";
return 0;
}
return(A[top]);
}
int Isempty()
{
return(top==-1);
}
int Isfull()
{
return(top==n-1);
}
void Push(int x)
{
if(!Isfull())
A[++top]=x;
}
int Pop()
{
if(Isempty())
{
cout<<endl<<"Empty";
return 0;
}
return(A[top--]);
}
void Display();
};
void Stackk::Display()
{
if(Isempty())
{
cout<<endl<<endl<<"Stack : Empty";
return;
}
cout<<endl<<endl<<"Stack : ";
for(int d=top;d>=0;d--)
cout<<A[d]<<" ";
}
int main(int argc, char *argv[])
{
Stackk Stk;
do
{
int ch;
cout<<endl<<"Operations on Stack"<<endl<<"Menu"<<endl<<"
1:Push"<<endl
<<" 2:Pop"<<endl<<" 3:IsEmpty"<<endl<<" 4:IsFull"<<endl
<<" 5:Peek"<<endl<<" 6:Display"<<endl<<"Else:Exit";
cout<<endl<<endl<<"Enter choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<endl<<"Push Operation";
cout<<endl<<"Enter number";
cin>>ch;
Stk.Push(ch);
break;
case 2:
cout<<endl<<"Pop operation";
if(!Stk.Isempty())
cout<<endl<<"Number Popped : "<<Stk.Pop();
else
cout<<endl<<"Empty";
break;
case 3:
if(Stk.Isempty())
cout<<endl<<"Stack is empty";
else
cout<<endl<<"Stack is not empty";
break;
case 4:
if(Stk.Isfull())
cout<<endl<<"Stack is full";
else
cout<<endl<<"Stack is not full";
break;
case 5:
if(Stk.Isempty())
cout<<endl<<"Stack is empty";
else
cout<<endl<<"Element at peek ="<<Stk.Peek();
break;
case 6:
Stk.Display();
break;
default:
cout<<endl<<"Exit...";
return 0;
}
}
while(1);
return 0;
}
Output:-
Operations on Stack
Menu
1:Push
2:Pop
3:IsEmpty
4:IsFull
5:Peek
6:Display
Else:Exit
Enter choice : 1
Push Operation
Enter number10
Operations on Stack
Menu
1:Push
2:Pop
3:IsEmpty
4:IsFull
5:Peek
6:Display
Else:Exit
Enter choice : 1
Push Operation
Enter number20
Operations on Stack
Menu
1:Push
2:Pop
3:IsEmpty
4:IsFull
5:Peek
6:Display
Else:Exit
Enter choice : 2
Pop operation
Number Popped : 20
Operations on Stack
Menu
1:Push
2:Pop
3:IsEmpty
4:IsFull
5:Peek
6:Display
Else:Exit
Enter choice : 6
Stack : 10
Assignment No- 6
Queue
switch(ch)
{
case 1:
cout<<endl<<"Enque Operation";
cout<<endl<<"Enter number";
cin>>ch;
Que.Enque(ch);
break;
case 2:
cout<<endl<<"Deque operation";
if(!Que.Isempty())
cout<<endl<<"Number Deleted :
"<<Que.Deque();
else
cout<<endl<<"Empty";
break;
case 3:
if(Que.Isempty())
cout<<endl<<"Queue is empty";
else
cout<<endl<<"Queue is not empty";
break;
case 4:
if(Que.Isfull())
cout<<endl<<"Queue is full";
else
cout<<endl<<"Queue is not full";
break;
case 5:
if(!Que.Isempty())
cout<<endl<<"Element at peek =
"<<Que.Peek();
else
cout<<endl<<"Empty";
break;
case 6:
Que.Display();
break;
case 7:
Que.Compaction();
break;
default:
cout<<endl<<"Exit...";
return 0;
}
}while(1);
return 0;
}
Output:-
Operations on Queue
Menu
1:Enque
2:Deque
3:IsEmpty
4:IsFull
5:Peek
6:Display
7:Compaction
Else:Exit
Enter choice : 1
Enque Operation
Enter number20
Operations on Queue
Menu
1:Enque
2:Deque
3:IsEmpty
4:IsFull
5:Peek
6:Display
7:Compaction
Else:Exit
Enter choice : 1
Enque Operation
Enter number30
Operations on Queue
Menu
1:Enque
2:Deque
3:IsEmpty
4:IsFull
5:Peek
6:Display
7:Compaction
Else:Exit
Enter choice : 6
Queue : 20(0) 30(1)
Assignment No-7
Linked List
Q. 1 Write a program using linked list
#include <iostream>
using namespace std;
class LSLili
{
struct Node
{
int no;
struct Node *next;
};
struct Node *head;
public:
LSLili()
{
head=NULL;
}
~LSLili();
int Isempty()
{
return(head==NULL);
}
void Insert(int,int);
int Delete(int);
void Display();
int Search(int);
};
int LSLili::Search(int n)
{
int cnt=0;
if(Isempty())
{
cout<<"List : Empty";
return -1;
}
for(Node *temp=head;temp!=NULL;temp=temp->next,cnt++)
if(temp->no==n)
return cnt;
return -1;
}
void LSLili::Display()
{
int cnt=0;
if(Isempty())
{
cout<<"List : Empty";
return;
}
cout<<"List : ";
for(Node *temp=head;temp!=NULL;temp=temp->next,cnt++)
cout<<"["<<temp->no<<"("<<cnt<<")]->";
cout<<"NULL";
}
void LSLili::Insert(int n,int pos)
{
if(pos<0)
return;
if(pos==0)
{
Node *temp;
temp=new Node;
temp->no=n;
temp->next=head;
head=temp;
return;
}
int p=1;
for(Node *t=head;t!=NULL;t=t->next)
if(pos==p++)
{
Node *temp;
temp=new Node;
temp->no=n;
temp->next=t->next;
t->next=temp;
return;
}
cout<<"Illegal Position";
}
int LSLili::Delete(int pos)
{
int n;
if(Isempty())
{
cout<<"Empty";
return 0;
}
if(pos<0)
{
cout<<"Illegal Position";
return 0;
}
if(pos==0)
{
Node *temp;
temp=head;
n=temp->no;
head=head->next;
delete temp;
return n;
}
int p=1;
for(Node *t=head;t->next!=NULL;t=t->next)
if(pos==p++)
{
Node *temp;
temp=t->next;
n=temp->no;
t->next=temp->next;
delete temp;
return n;
}
cout<<"Illegal Position";
return 0;
}
LSLili::~LSLili()
{
while(head!=NULL)
{
Node *temp;
temp=head;
head=head->next;
delete temp;
}
}
int main(int argc, char *argv[]) {
LSLili L;
int n,pos;
do
{
int ch;
cout<<endl<<"Operations on Linear Singly Linked-
List"<<endl<<"Menu"
<<endl<<" 1:Insert"<<endl<<" 2:Delete"<<endl<<"
3:Search"<<endl
<<" 4:Display"<<endl<<" 5:IsEmpty"<<endl<<"Else:Exit";
cout<<endl<<endl<<"Enter choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<endl<<"Insert Operation";
cout<<endl<<"Enter number : ";
cin>>n;
cout<<endl<<"Enter Position : ";
cin>>pos;
L.Insert(n,pos);
break;
case 2:
cout<<endl<<"Delete operation";
cout<<endl<<"Enter Position : ";
cin>>pos;
if(!L.Isempty())
cout<<endl<<"Deleted Number:
"<<L.Delete(pos);
else
cout<<endl<<"Empty";
break;
case 5:
if(L.Isempty())
cout<<endl<<"Linked-List is empty";
else
cout<<endl<<"Linked-List is not empty";
break;
case 3:
cout<<endl<<"Search operation";
cout<<endl<<"Enter Number : ";
cin>>n;
pos=L.Search(n);
if(pos==-1)
cout<<n<<" is not a member of current list";
else
cout<<n<<" is at position "<<pos<<" in a
current list";
break;
case 4:
L.Display();
break;
default:
cout<<endl<<"Exit...";
return 0;
}
}while(1);
return 0;
}
Output-
Operations on Linear Singly Linked-List
Menu
1:Insert
2:Delete
3:Search
4:Display
5:IsEmpty
Else:Exit
Enter choice : 1
Insert Operation
Enter number : 10
Enter Position : 0
Enter choice : 4
List : [10(0)]->NULL
Operations on Linear Singly Linked-List
Menu
1:Insert
2:Delete
3:Search
4:Display
5:IsEmpty
Else:Exit
Enter choice :