Академический Документы
Профессиональный Документы
Культура Документы
1. Using circular representation for a polynomial, design, develop, and execute a program in C to
accept two polynomials, add them, and then print the resulting polynomial.
Description:
A Polynomial is can be expressed in terms that only have positive integer exponents and the operations
of addition, subtraction, and multiplication.
A polynomial equation is an equation that can be written in the form
axn + bxn-1 + . . . + r x + s = 0, where a, b, . . . , r and s are constants.
We call the largest exponent of x appearing in a non-zero term of a polynomial the degree of that
polynomial.
Examples
1. 3x + 1 = 0 has degree 1, since the largest power of x that occurs is x = x1. Degree 1 equations are called
linear equations.
2. x2 - x - 1 = 0 has degree 2, since the largest power of x that occurs is x2. Degree 2 equations are also
called quadratic equations, or just quadratics.
3. x3 = 2x2 + 1 is a degree 3 polynomial (or cubic) in disguise. It can be rewritten as x3 - 2x2 - 1 = 0,
which is in the standard form for a degree 3 equation.
4. x4 - x = 0 has degree 4. It is called a quartic.
Simplify (3x3 + 3x2 4x + 5) + (x3 2x2 + x 4)
Can be added horizontally:
(3x3 + 3x2 4x + 5) + (x3 2x2 + x 4)
= 3x3 + 3x2 4x + 5 + x3 2x2 + x 4
= 3x3 + x3 + 3x2 2x2 4x + x + 5 4
= 4x3 + 1x2 3x + 1
...or vertically:
Program:
#include<malloc.h>
#include<conio.h>
#include<stdio.h>
struct link
{
int coeff;
Dept of Information Science & Engineering, SCE
Sample Output
enter 1st number:
enter coeff:4
enter power:3
continue(y/n):y
enter coeff:
2
enter power:3
continue(y/n):y
Dept of Information Science & Engineering, SCE
(After all characters are scanned, we have to add any character that the stack may have to the
Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this
step as long as stack is not empty.
Return the Postfix string.
Example :
Let us see how the above algorithm will be imlemented using an example.
Infix String : a+b*c-d
Initially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'.
'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the
stack.
Postfix String
Stack
Next character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an
operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be
pushed to the stack.
Postfix String
Stack
The next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost
character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the
stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the
stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The
'-' will be pushed to the stack.
Postfix String
Stack
Next character is 'd' which is added to Postfix string. Now all characters have been scanned so we must
pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-'
in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is
how the stack and Postfix string will be :
Postfix String
Stack
End result :
Program:
#include<conio.h>
#include<stdio.h>
#include<string.h>
Sample Output
Enter a valid infix expression
a+b/c
Postfix expression is abc/+
Description:
Infix Expression :
Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.
Postfix Expression : The Postfix(Postorder) form of the above expression is "23*45/-".
Postfix Evaluation :
The algorithm to evaluate the postfix expression is as follows :
After all characters are scanned, we will have only one element in the stack. Return topStack.
Example :
Let us see how the above algorithm will be imlemented using an example.
Postfix String : 123*+4Initially the Stack is empty. Now, the first three characters scanned are 1,2 and 3, which are operands.
Thus they will be pushed into the stack in that order.
Expression
Stack
Next character scanned is "*", which is an operator. Thus, we pop the top two elements from the stack
and perform the "*" operation with the two operands. The second operand will be the first element that is
popped.
10
Expression
Stack
The value of the expression(2*3) that has been evaluated(6) is pushed into the stack.
Expression
Stack
Next character scanned is "+", which is an operator. Thus, we pop the top two elements from the stack
and perform the "+" operation with the two operands. The second operand will be the first element that is
popped.
Expression
Stack
The value of the expression(1+6) that has been evaluated(7) is pushed into the stack.
Expression
Stack
Next character scanned is "4", which is added to the stack.
Expression
Stack
11
Next character scanned is "-", which is an operator. Thus, we pop the top two elements from the stack and
perform the "-" operation with the two operands. The second operand will be the first element that is
popped.
Expression
Stack
The value of the expression(7-4) that has been evaluated(3) is pushed into the stack.
Expression
Stack
Now, since all the characters are scanned, the remaining element in the stack (there will be only one
element in the stack) will be returned.
End result :
Program:
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
int s[20],top=-1;
int evaluate(char *);
void push(int);
int pop();
void main()
{
int res;
char post[30];
Dept of Information Science & Engineering, SCE
12
13
Sample Output
Case 1 :
Enter a valid postfix expression
63/
Result of expression is 2
Case 2 :
Enter a valid postfix expression
ab+
Enter value for a:10
Enter value for b:5
Result of expression is 15
14
Front(deletion)
rear end(insertion)
Program:
#include<stdio.h>
#include<conio.h>
#define QSIZE 3
#include<stdlib.h>
int choice,item,f,r,q[10];
void insert_rear()
{
if(r==QSIZE-1)
{
printf("over flow\n");
return;
}
printf("Enter the elements to be inserted:\n");
scanf("%d",&item);
q[++r]=item;
}
void delete_front()
{
if(f>r)
{
printf("q is empty\n");
return;
}
printf("The elements deleted is:%d",q[f++]);
}
15
void display()
{
int i;
if(f>r)
{
printf("Q is empty\n");
return;
}
printf("The contents of queue:\n");
for(i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
}
void main()
{
f=0;r=-1;
clrscr();
for(; ;)
{
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
printf("\n enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:insert_rear();
break;
case 2:delete_front();
break;
case 3:display();
break;
default :exit(0);
}
}
}
Sample Output
1.Insert
2.Delete
3.Display
4.Exit
enter your choice:2
Dept of Information Science & Engineering, SCE
16
1.Insert
2.Delete
3.Display
4.Exit
enter your choice:1
Enter the elements to be inserted:
63
1.Insert
2.Delete
3.Display
4.Exit
enter your choice:1
Enter the elements to be inserted:
20
1.Insert
2.Delete
3.Display
4.Exit
enter your choice:1
over flow
1.Insert
2.Delete
3.Display
4.Exit
enter your choice:2
The elements deleted is:63
1.Insert
2.Delete
3.Display
4.Exit
enter your choice:3
The contents of queue:
20
1.Insert
2.Delete
3.Display
4.Exit
enter your choice:4
Dept of Information Science & Engineering, SCE
17
Description:
Object: Objects are the run time entities in an object oriented system.
Class:A class is an expanded concept of a data structure. The object of same data structure and beheviour
are grouped in to class. Instead of holding only data, it can hold both data members and member
functions.
Once the class has been defined we can create any number of objects belonging to that class.
Example: Mango, Orange, Apple are the members of class fruit. The syntax used to create object is
Fruit mango;
The above statement will create an object mango belonging to the class fruit.
In this program a Class EMPLOYEE is created with the above mentioned data members and member
functions. Object is an instance of class. Required number of objects are created to the EMPLOYEE
class.
Program:
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_no;
char emp_name[20];
float basic,allowances,gross,net,it;
public:
void read();
void calculate();
void display();
};
18
void employee::read()
{
cout<<"Enter employee number"<<endl;
cin>>emp_no;
cout<<"Enter employee name"<<endl;
cin>>emp_name;
cout<<"Enter employee salary"<<endl;
cin>>basic;
}
void employee::calculate()
{
allowances=0.123*basic;
gross=basic+allowances;
it=0.3*gross;
net=gross-it;
}
void employee::display()
{
cout<<"\n Employee number is:"<<emp_no<<endl;
cout<<"\n Employee name is:"<<emp_name<<endl;
cout<<"\n basic salary is:"<<basic<<endl;
cout<<"\n gross salary is:"<<gross<<endl;
cout<<"\n net salary is:"<<net<<endl;
}
void main()
{
int n,i;
clrscr();
employee emp[20];
cout<<"enter number of employee:\n"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter employee details:"<<endl;
emp[i].read();
emp[i].calculate();
}
19
Sample Output
enter number of employee
3
enter employee details:
Enter employee number
10
sanjana
Enter employee salary
6000
enter employee details:
Enter employee number
20
Enter employee name
swathi
Enter employee salary
7000
enter employee details:
Enter employee number
101
Enter employee name
roopa
Enter employee salary
5000
20
21
Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char name[20];
public:string()
{
name[20]='\0';
}
string(char s[])
{
Dept of Information Science & Engineering, SCE
22
Sample Output
first string= VTU
second string= BELGAUM
concatenated third string=
VTUBELGAUM
23
Top
Program:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define max 5
class stack
{
int a[max],top;
public: stack()
{
top=-1;
}
stack operator+(int);
stack operator--();
friend void operator<<(ostream&,stack&);
};
24
25
Sample Output
enter the choice of operation:
1.push
2.pop
3.display
4.exit
2
stack empty
1.push
2.pop
3.display
4.exit
3
stack empty
enter the choice of operation:
1.push
2.pop
3.display
4.exit
1
enter the elements:34
enter the choice of operation:
1.push
2.pop
3.display
4.exit
1
enter the elements:67
26
27
Linked list is a dynamic data structure whose length can be increased or decreased at run time.
Types of linked lists are : 1. Singly linked list
2. Doubly linked list
In this program we learn SLL. Drawback of SLL is that it can be traversed in only one direction(forward).
Bidirectional or backward traversal is not possible.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
node*link;
};
typedef struct node* NODE;
class list
{
NODE first;
public:
list()
{
first=NULL;
}
void insert(int);
Dept of Information Science & Engineering, SCE
28
29
for(;;)
{
cout<<"\nenter the choice"<<endl;
cout<<"1.insert\n2.delete\n3.display\n4.exit"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:cout<<"enter an element:";
int item;
cin>>item;
object.insert(item);
break;
chase 2:object.delet();
break;
case 3:object.display();
break;
case 4:exit(0);
}
}
getch();
}
Sample Output
enter the choice
1.insert
2.delete
3.display
4.exit
2
the list is empty
enter the choice
1.insert
2.delete
3.display
4.exit
1
enter an element:23
30
31
Description:
A sparse matrix can be vaguely defined as a matrix with few nonzeros.
Consider a matrix in which most of the entries are zeros. In this case large amount of memory is wasted to
explicitly store all those zeros. when performing operations on the matrices such as addition, subtraction
and multiplication (especially), a large number of the operations have two operands of zero (0) which
results in a large amount of wasted time.
If a matrix is sparse time and space, we can store the elements that are non-zero only and implicitly
assuming any element not stored is 0. This greatly reduces the number of times to be stored and the
number of operations that must be performed for addition, subtraction and multiplication etc.. So instead
of using 2D, we can use some other data structures to store elements of matrices and the data structure
used in this program is Coordinate list(COO)
COO stores a list of (row, column, value) tuples. Ideally, the entries are sorted (by row index, then
column index) to improve random access times..
Description:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_TERMS 10
struct term
{
int row,col,val;
}
a[MAX_TERMS];
void main()
{
int i,item;
clrscr();
printf("enter total number of rows,columns,&values\n");
scanf("%d%d%d", &a[0].row,&a[0].col,&a[0].val);
printf("enter the values along with row &coloumn");
for(i=1;i<=a[0].val;i++)
scanf("%d%d%d", &a[i].row,&a[i].col,&a[i].val);
Dept of Information Science & Engineering, SCE
32
Sample Output
enter total number of rows,columns,&values
7
3
5
enter the values along with row &coloumn
124
231
336
512
7 2 10
enter the element to be searched 6
element found at 3 row,3 col
33
Basic Calculations
Assuming that values are stored starting at subscript 1, then
34
35
36
37
Sample Output
1.insert 2.display
2
heap is empty
1.insert 2.display
1
enter the item to be inserted:
2
1.insert 2.display
1
enter the item to be inserted:
4
1.insert 2.display
1
enter the item to be inserted:
67
Dept of Information Science & Engineering, SCE
38
39
Description:
To traverse a list both forwards and backwards efficiently, or given a list element, to determine the
preceding and following elements quickly, then the doubly-linked list comes in handy. A list element
contains the data plus pointers to the next and previous list items as shown in the picture below.
Of course we need a pointer to some link in the doubly-linked list to access list elements. It is convenient
for doubly-linked lists to use a list header, or head, that has the same structure as any other list item and is
actually part of the list data structure. The picture below shows an empty and nonempty doubly-linked
list. By following arrows it is easy to go from the list header to the first and last list element, respectively.
40
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int info;
struct node*llink;
struct node*rlink;
};
typedef struct node*NODE;
NODE first=NULL;
void ins_front(int item);
void ins_left(int key, int item);
void del(int key);
void display();
void main()
{
41
42
void display()
{
NODE temp=first;
if(first==NULL)
{
printf("list is empty\n");
return;
}
if(first->llink==NULL&&first->rlink==NULL)
{
printf("The element present is %d", first->info);
return;
}
printf("elements of the list are:\n");
while (temp!=NULL)
{
printf("%d->", temp->info);
temp=temp->rlink;
}
}
void ins_left(int key, int item)
{
NODE temp, prev;
NODE nn=(NODE)malloc(sizeof(struct node));
nn->info=item;
nn->rlink=nn->llink=NULL;
if(first==NULL)
{
printf("list is empty\n");
return;
}
temp=first;
while(temp!=NULL)
{
if(temp->info==key)
43
44
Sample Output
1.insert front
2.insert left
3.del key
4.display
5.exit
Enter your choice 1
enter the item to be inserted
23
1.insert front
2.insert left
3.del key
4.display
5.exit
Enter your choice 1
enter the item to be inserted
27
1.insert front
2.insert left
3.del key
4.display
5.exit
Enter your choice 4
elements of the list are:
27->23->
1.insert front
2.insert left
3.del key
4.display
5.exit
Enter your choice 2
left of which node?:
23
enter the item to be inserted
5
new node is successfully inserted
Dept of Information Science & Engineering, SCE
45
1.insert front
2.insert left
3.del key
4.display
5.exit
Enter your choice4
elements of the list are:
27->5->23->
1.insert front
2.insert left
3.del key
4.display
5.exit
Enter your choice 3
enter the item to be deleted 23
deleted node is 23elements of the list are:
27->5->
1.insert front
2.insert left
3.del key
4.display
5.exit
Enter your choice 5
46
47
48
49
50
Description:
The octal numeral system, or oct for short, is the base-8
8 number system, and uses the digits 0 to 7. Octal
numerals can be made from binary numerals by grouping consecutive binary digits into groups of three
(starting from the right). For example, the binary representation for decimal 74 is 1001010,
1001010 which can be
grouped into (00)1 001 010 so the octal representation is 112. In the decimal system each decimal place
is a power of ten. For example:
51
52
Sample Output
Enter integer value in decimal:45
The corresponding octal value for(45) is:55
Enter the value added to octal value:9
55(octal)+9(decimal)=54(decimal)
53
The depth of a node is the number of edges from the root to the node.
The height of a node is the number of edges from the node to the deepest leaf.
The height of a tree is a height of the root.
A full binary tree.is a binary tree in which each node has exactly zero or two children.
A complete binary tree is a binary tree, which is completely filled, with the possible exception of
the bottom level, which is filled from left to right.
54
Traversals
A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there
is no unique traversal. We will consider several traversal algorithms with we group in the following two
kinds
depth-first traversal
breadth-first traversal
PreOrder traversal - visit the parent first and then left and right children;
InOrder traversal - visit the left child, then the parent and the right child;
PostOrder traversal - visit left child, then the right child and then the parent;
There is only one kind of breadth-first traversal--the level order traversal. This traversal visits nodes by
levels from top to bottom and from left to right.
55
In the next picture we demonstarte the order of node visitation. Number 1 denote the first node in a
particular traversal and 7 denote the last node.
These common traversals can be represented as a single algorithm by assuming that we visit each node
three times. An Euler tour is a walk around the binary tree where each edge is treated as a wall, which
you cannot cross. In this walk each node will be visited either on the left, or under the below, or on the
right. The Euler tour in which we visit nodes on the left produces a preorder traversal. When we visit
nodes from the below, we get an inorder traversal. And when we visit nodes on the right, we get a
postorder traversal.
56
57
58
if(dir=='L' || dir=='l')
prev->left=nn;
else
prev->right=nn;
cout<<"continue with next node(1/0)\t";
cin>>opt;
}
}while(opt);
}
};
void main()
{
BINTREE bin;
clrscr();
int choice;
for(;;)
{
cout<<"1.insert 2.inorder 3.preorder 4.postorder 5.exit"<<endl;
cout<<"enter choice:";
cin>>choice;
switch(choice)
{
case 1:bin.insert();
break;
case 2:if(bin.root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Inorder traversal is:"<<endl;
bin.inorder(bin.root);
cout<<endl;
}
break;
case 3:if(bin.root==NULL)
cout<<"Tree is empty"<<endl;
else
{
cout<<"Preorder traversal is:"<<endl;
bin.preorder(bin.root);
cout<<endl;
59
Sample Output
left/right of 34:r
left/right of 5:r
60
61