0% found this document useful (0 votes)
57 views

DS Assignment Final

The document contains programs demonstrating the use of arrays, polynomials, sorting algorithms, searching algorithms, and stacks. The array program takes employee salaries as input and counts those above and below 3000. The polynomial program represents polynomials using linked lists and adds two polynomials. The sorting programs demonstrate insertion sort, bubble sort, and selection sort. The searching programs perform linear and binary search on arrays. The stack program implements basic stack operations.

Uploaded by

Chetan Arkade
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)
57 views

DS Assignment Final

The document contains programs demonstrating the use of arrays, polynomials, sorting algorithms, searching algorithms, and stacks. The array program takes employee salaries as input and counts those above and below 3000. The polynomial program represents polynomials using linked lists and adds two polynomials. The sorting programs demonstrate insertion sort, bubble sort, and selection sort. The searching programs perform linear and binary search on arrays. The stack program implements basic stack operations.

Uploaded by

Chetan Arkade
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/ 35

Assignment No-1

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];
}

for(i=0; i<NUM_EMPLOYEE; i++){


if(Salary[i]<3000)
lCount++;
else
gCount++;
}

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-

Enter highest power for x


2

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

Answer after addition is : +4X2+4X1+4X0


Assignment No – 3
Sorting
Q1.Write a program using insertion sort.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num[]= {12,9,37,86,2,17,5};
int i,j,x;
cout<<"Array before Insertion Sort"<<endl;
for(i=0; i<7; i++)
{
cout<<num[i]<<" ";
}
for(i=1; i<7; i++)
{
x=num[i];
j=i-1;
while(j>=0)
{
if(x<num[j])
{
num[j+1]=num[j];
}
else
{
break;
}
j=j-1;
}

num[j+1]=x;
}
cout<<"\n\nArray after Insertion Sort\n";
for(i=0; i<7; i++)
{
cout<<num[i]<<" ";
}
return 0;
}
Output

Array before Insertion Sort


12 9 37 86 2 17 5

Array after Insertion Sort


2 5 9 12 17 37 86
Q.2 Write a program using bubble sort.
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp,pass=0;
int a[10] = {10,2,0,14,43,25,18,1,5,45};
cout <<"Input list ...\n";
for(i = 0; i<10; i++)
{
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++)
{
cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}
Output
Input list ...
10 2 0 14 43 25 18 1 5 45
Sorted Element List ...
0 1 2 5 10 14 18 25 43 45
Number of passes taken to sort the list:10
Q.3 Write a program using Selection sort (descending order).
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num[]= {12,9,37,86,2,17,5};
int i,j,t,lnp;
cout<<"Array before Selection Sort"<<endl;
for(i=0; i<7; i++)
{
cout<<num[i]<<" ";
}
for(i=0; i<6; i++)
{
lnp=i;
for(j=i+1; j<7; j++)
{
if(num[j]>num[lnp])
{
lnp=j;
}
}
if(num[i]<num[lnp])
{
t=num[i];
num[i]=num[lnp];

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

Q. 1 Write a program using stack.


#include <iostream>
using namespace std;
class Stackk
{
int A[20];
int n;
int top;
public:
Stackk()
{
n=sizeof(A)/sizeof(A[0]);

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

Q. 1 Write a program using queue.


#include <iostream>
using namespace std;
class Queuee
{
int A[20];
int n;
int front,rear;
public:
Queuee()
{
n=sizeof(A)/sizeof(A[0]);
rear=-1;
front=0;
}
int Peek()
{
if(Isempty())
{
cout<<endl<<"Empty";
return 0;
}
return(A[front]);
}
int Isempty()
{
return(front>rear);
}
int Isfull()
{
return(rear==n-1);
}
void Enque(int x)
{
if(!Isfull())
A[++rear]=x;
}
int Deque()
{
if(Isempty())
{
cout<<endl<<"Empty";
return 0;
}
return(A[front++]);
}
void Display();
void Compaction();
};
void Queuee::Display()
{
if(Isempty())
{
cout<<endl<<endl<<"Queue : Empty";
return;
}
cout<<endl<<endl<<"Queue : ";
for(int d=front;d<=rear;d++)
cout<<A[d]<<"("<<d<<") ";
}
void Queuee::Compaction()
{
int d,i;
for(d=front,i=0;d<=rear;d++,i++)
A[i]=A[d];
front=0;
rear=i-1;
}
int main(int argc, char *argv[])
{
Queuee Que;
do
{
int ch;
cout<<endl<<"Operations on
Queue"<<endl<<"Menu"<<endl<<" 1:Enque"<<endl
<<" 2:Deque"<<endl<<" 3:IsEmpty"<<endl<<"
4:IsFull"<<endl
<<" 5:Peek"<<endl<<" 6:Display"<<endl<<"
7:Compaction"<<endl<<"Else:Exit";
cout<<endl<<endl<<"Enter choice : ";
cin>>ch;

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

Operations on Linear Singly Linked-List


Menu
1:Insert
2:Delete
3:Search
4:Display
5:IsEmpty
Else:Exit

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 :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy