Amala C Pro
Amala C Pro
Amala C Pro
LAB RECORD
STUDENT NAME :
SUBJECT CODE :
SUBJECT NAME :
YEAR / SEM :
ASAN MEMORIAL
COLLEGE OF ENGINEERING & TECHNOLOGY
(Approved by AICTE & Affiliated to Anna University)
BONAFIDE CERTIFICATE
REGISTER NUMBER:
of III semester II Year B.E Degree in Electronics & Communication Engineering in the CS3362 C
………………………….
Date :
AIM
To implement c programming using statements
ALGORITHM
STEP1: Start the program
STEP2: Declare the variables
STEP3: Put the x and y value
STEP4: Check the condition, if the condition true print true statements
STEP5: Otherwise print false statements
STEP6: Stop the program
PROGRAM
#include <stdio.h>
void main( )
{
int x,
y; x =
15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
OUTPUT
Y is greater than x
RESULT
Thus the c program using statement was executed successfully.
EX.NO:1(b) C PROGRAMMING USING EXPRESSIONS
Aim:
To implement c programming using expressions
ALGORITHM
STEP1: Start the program
PROGRAM
#include
<stdio.h> int
main()
{
int a,b,result;
printf("Enter 2 numbers for Arithmetic operation \n");
scanf("%d\n%d",&a,&b);
printf("=======ARITHMETIC EXPRESSIONS=======\n");
result = a+b;
printf("Addition of %d and %d is = %d \n",a,b,result);
result = a-b;
printf("Subtraction of %d and %d is = %d \
n",a,b,result); result = a*b;
printf("Multiplication of %d and %d is = %d \n",a,b,result);
result = a/b;
printf("Division of %d and %d is = %d \n",a,b,result);
result = a%b;
printf("Modulus(Remainder) when %d divided by %d = %d \n",a,b,result);
int c=a;
result = a++;
printf("Post Increment of %d is = %d \
n",c,result); result = ++a;
printf("Pre Increment of %d is = %d \
n",c,result); result=a--;
printf("Post decrement of %d is = %d \
n",c,result); result=--a;
printf("Pre decrement of %d is = %d \n",c,result);
printf("===========================");
return 0;
}
OUTPUT
======ARITHMETIC EXPRESSIONS=======
Addition of 10 and 5 is = 15
Subtraction of 10 and 5 is = 5
Multiplication of 10 and 5 is = 50
Division of 10 and 5 is = 2
Modulus(Remainder) when 10 divided by 5 = 0
Post Increment of 10 is = 10
Pre Increment of 10 is =
12 Post decrement of 10
is = 12 Pre decrement of
10 is = 10
===========================
RESULT
Thus the c program using expression was executed successfully.
EX.NO:1(b) C
PROGRAMMING USING DECISION MAKING AND ITERATIVE
STATEMENTS
AIM
To implement c programming using decision making and iterative statements
ALGORITHM
STEP1: Start the program
STEP2: Declare the variables
STEP3: Enter the variable
value
STEP4: Check the condition, if it is true print palindrome
STEP5: Otherwise print not palindrome
STEP6: Stop the program
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, s = 0;
clrscr();
printf("Enter a number:\t");
scanf("%d", &a);
c = a;
while(a > 0)
{
b = a%10;
s = (s*10)+b;
a = a/10;
}
if(s == c)
{
printf("The number %d is a palindrome", c);
}else
{
printf("The number %d is not a palindrome", c);
}getch();
}
OUTPUT
RESULT
Thus the c program using decision making and iterative statements was
executed successfully.
EX.NO:2 C PROGRAMMING USING FUNCTIONS AND ARRAYS
AIM
To implement c programming using functions and arrays
ALGORITHM
STEP1: Start the program
STEP2: Declare the function with variables
STEP3: Enter the variable value
STEP4: Calculate the formula to sum the given values
STEP5: Print the result value
STEP6: Stop the program
PROGRAM
// Program to calculate the sum of array elements by passing to a function
#include <stdio.h>
float calculateSum(float
num[]); int main()
{
float result, num[] = {23.4, 55, 22.6, 3, 40.5,
18}; result = calculateSum(num);
printf("Result = %.2f",
result); return 0;
}
float calculateSum(float num[])
{
float sum = 0.0;
RESULT
Thus the c program using functions and arrays was executed successfully.
EX.NO:3 C PROGRAMS USING POINTERS AND STRUCTURES
AIM
To implement c programming using pointers and structures
ALGORITHM
STEP1: Start the program
STEP2: Declare the structure
STEP3: Enter the variable
values STEP4: Print the
result
STEP5: Stop the program
PROGRAM
#include
<stdio.h>
#include
<stdlib.h>
struct person
{
int age;
float weight;
char
name[30];
};
int main()
{
struct person
*ptr; int i, n;
printf("Enter the number of persons: ");
scanf("%d", &n);
// allocating memory for n numbers of struct person
ptr = (struct person*) malloc(n * sizeof(struct person)); for(i = 0; i < n; ++i)
{
printf("Enter first name and age respectively: ");
scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
}
printf("Displaying Information:\n");
for(i = 0; i < n; ++i)
printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)-
>age); return 0;
}
OUTPUT
Enter the number of persons: 2
Enter first name and age respectively: Harry 24
Enter first name and age respectively: Gary 32
Displaying Information:
Name: Harry Age: 24
Name: Gary Age: 32
RESULT
Thus the c program using pointers and structures was executed successfully.
EX.NO:4 C PROGRAMS USING FILES
AIM
To implement c programming using files
ALGORITHM
STEP1: Start the program
STEP2: Open the file using file name with read mode
STEP3: Read the content of file
STEP4: Print the content of file
STEP5: After reading the content of file close the opening file
STEP6: Stop the program
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
FILE* ptr;
char ch;
// Opening file in reading mode
ptr = fopen("test.txt", "r");
if (NULL = = ptr)
{
printf("file can't be opened \n");
}
printf("content of this file are \n");
INPUT FILE
ECE | C programming and Data Structure
OUTPUT
C: \Users\Desktop>gcc fgetcexp.c –o
fgetcexp C: \Users\Desktop>fgetcexp
Content of this file are
ECE | C programming and Data Structure
RESULT
Thus the c program using files was executed successfully.
EX.NO:5 DEVELOPMENT OF REAL TIME C APPLICATIONS
CASE STUDY
What is the C Language?
The C programming language has been highly influential, and many other
languages have been derived from it. For example, C++ and Java are two popular modern
dialects of C.
History of C language
The C programming language was created at Bell Laboratories in the early 1970s,
mainly by Ken Thompson and Dennis Ritchie. For the UNIX operating system, which at
the time required applications to be written in assembly language, programmers needed a
more user-friendly set of instructions. Assembly programmes, which communicate
directly with a computer's hardware, are lengthy and complex to debug, and adding new
functionality requires a lot of time and effort.
Thompson's first high-level language was named B after the BCPL system
programming language on which it was built. Thompson rewrote B to better match the
demands of the modern time, better system hardware after Bell Labs purchased a Digital
Equipment Corporation (DEC) UNIX system model PDP-11. As a result C, the B's
successor, was created. By 1973, C had matured to the point that it could be used to
rewrite the UNIX operating system.
Key Features of C Programming Language
o C program syntax is easy to learn and read; this makes debugging code more accessible
and faster.
o C programs are relatively short compared to other languages, which reduces the time
needed to complete them.
o C is a powerful programming language that enables developers to create sophisticated
software systems.
o The language is fast, efficient, and easy to learn, making it a popular choice for many
applications.
o C is also portable, meaning that programs written in C can be easily ported to other
platforms.
o C has been around for many years (it was first released in 1979), so many libraries and
tools are available that facilitate its use.
REAL-WORLD APPLICATIONS OF C
1. Operating Systems
2. Assemblers
3. Text Editors
4. Print Spoolers
5. Modern Programs
6. Databases
7. Utilities
Real-world applications of C.
1. Operating Systems:-
What is better than writing your own operating system? And yes, with the help of the C
programming language, you can write your own operating system. Windows Kernel, Linux
Kernel and Apple’s OS X kernel are mostly written in C.
2. GUI:-
It stands for Graphical User Interface. The C programming language also helps in
developing popular adobe softwares like Photoshop, Premier Pro, Illustrator etc.
3. Embedded Systems:-
In daily life, we use different embedded systems like coffee machines, microwaves,
climate control systems etc. These all are mostly programmed in C.
4. Database:-
5. Ease of Computation:-
C programming is relatively faster than Java or Python. It has been used in various
gaming applications and graphics. C programming language also helps in creating many
popular childhood games like Tic-Tac-Toe, The Snake game etc.
Due to the fast execution and simplicity, many languages like Java, C++, Python, PHP,
PERL, JavaScript, etc were influenced by the development of C. In Python, C is used for
building standard libraries. The syntax and control structures of PERL, PHP and C++ are based
upon the C programming language.
8. Google:-
In the Google open source community, the projects are being handled by C/C++. And C/C+
+ also helped in developing google file system and chromium browser.
9. Assemblers:-
C also helped in creating various text editors like Vim, Gedit etc.
11.Drivers:-
12. Interpreters:-
With the help of C programming language, you can create language interpreters. C
helped in developing different programming language interpreters like Python and MATLAB
interpreters etc.
13. Network Devices:-
C also helped in designing several popular compilers like Clang C, MINGW, Apple C etc.
This is one of the most popular uses of C language.
RESULT
Thus the case study was studied successfully.
EX.NO:6 ARRAY IMPLEMENTATION OF LIST ADT
PROGRAM
#include<stdlib.h>
void main()
{
LIST L=NULL;
POSITION P;
int a,choice,ch,element;
clrscr();
printf("\n\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.MakeEmpty\n6.Find\n 7.IsEmpty\
n8.IsFull\n9.Deletelist\n10.Exit\n");
case 3:
if(L==NULL)
printf("\nList is not yet created");
if(Isempty(L))
printf("\nList is empty");
else
{
printf("\nEnter the element to delete:\t");
scanf("%d",&a);
Delete(a,L);
}
break;
case 4:
if(L==NULL)
printf("\nList is not yet created");
else
if(Isempty(L)) printf("\nList is
empty");
else
{
printf("\nElements present in the list are:");
Display(L);
}
break;
case 5:
if(L==NULL)
printf("\n List is not yet created ");
else
MakeEmpty(L);
break;
case 6:
if(L==NULL)
printf("\n Not yet created");
else
if(Isempty(L))
printf("\n List is empty");
else
{
printf("\n which element is to find:\t");
scanf("%d",&a);
P=Find(a,L);
printf("\n Element is at %d\t[0 to 4 means present]\t[5 means not present]",P);
}
break;
case 7:
if(L==NULL)
printf("\n Not yet created");
else
if(Isempty(L))
printf("\n List is empty");
else
printf("\n List is not empty");
break;
case 8:
if(L==NULL)
printf("\n Not yet created");
else
if(Isfull(L))
printf("\n List is FULL");
else
printf("\n List is not FULL");
break;
case 9:
if(L==NULL)
printf("\n Not yet created");
else
{
L=Deletelist(L);
printf("\n List is Deleted");
}
break;
case 10:
exit (0);
break;
default:
printf("\n\n *******WRONG ENTRY*******");
break;
}
goto A;
}
OUTPUT
1.Create
2.Insert
3.Delete
4.Display
5.MakeEmpty
6.Find
7.IsEmpty
8.IsFull
9.Deletelist
10.Exit
Enter Ur Option: 1
List is created successfully
Enter Ur Option: 2
Enter the element to insert:300
Enter Ur Option: 2
Enter the element to insert:100
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 1
Enter Ur Option: 2
Enter the element to insert:200
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 3
Enter Ur Option: 2
Enter the element to insert:400
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 2
Enter Ur Option: 2
Enter the element to insert:500
Where U want to insert? 1.Front 2.Back 3.Middle ::::: 2
Enter Ur Option: 2
Enter the element to insert:600
Where U want to insert?1.Front 2.Back 3.Middle :::::
Enter Ur Option: 3
Enter the element to delete: 300
Enter Ur Option: 4
Elements present in the list are: 100
200
400
500
Enter Ur Option:
8 List is not Full
Enter Ur Option: 5
Now List becomes
Empty
Enter Ur Option:9
List is Deleted
Enter Ur Option: 2
List is not yet created
Enter Ur Option: 12
*******WRONG ENTRY*******
Enter Ur Option: 10
RESULT
Thus operations on list was demonstrated using arrays successfully.
EX.NO:7(a) ARRAY IMPLEMENTATION OF STACK ADT
AIM
To write a C program to implement stack operations using array.
ALGORITHM
STEP 1: Start the program
STEP2: Define an array stack of size max = 5
STEP 3: Initialize top = -1
STEP 4: Display a menu listing stack operation
STEP 5: Accept choice
STEP 6: If choice = 1 then
If top < max -1
Increment top
Store element at current position of top
Else
Print Stack overflow
Else If choice = 2 then If top < 0
then
Print Stack underflow
Else
Display current top element Decrement top
Else If choice = 3 then
Display stack elements starting from top
STEP 7: Stop the program
PROGRAM
#include <stdio.h>
#include <conio.h>
#define max 5
Static int
stack[max];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return (stack[top--]);
}
void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \
n"); else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
{
printf("%4d", stack[i]);
}
printf("\n");
}
}
main()
{
int ch=0, val;
clrscr();
while(ch != 4)
{
printf("\n STACK OPERATION \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("4.QUIT \n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : ");
scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n"); break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n");
else
{
val = pop();
printf("\n Popped element is %d\n", val);
}
break;
case 3:
view(); break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
}
OUTPUT
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 23
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 34
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Enter Stack element : 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3 Top--> 45 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element is 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
Top--> 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 4
RESULT
Thus push and pop operations of a stack was demonstrated using arrays.
ALGORITHM
STEP1: Start the program
STEP2: Define an array queue of size max = 5
STEP3: Initialize front = rear = –1
STEP4: Display a menu listing queue operation
STEP 5: Accept choice
STEP 6: If choice = 1 then
If rear < max -1
Increment rear
Store element at current position of rear
Else
Print Queue Full Else If choice = 2 then If front = –1
then
Print Queue empty
Else
Display current front element Increment front
Else If choice = 3 then
Display queue elements starting from front to rear.
STEP 7: Stop the program
PROGRAM
#include<stdio.h>
#include<conio.h>
#define max 5
static int
queue[max];
int front = -1;
int rear = -1;
void insert(int x)
{
queue[++rear= x;
clrscr();
while(ch != 4)
{
printf("\n QUEUE OPERATION \n");
printf("1.INSERT ");
printf("2.DELETE ");
printf("3.VIEW ");
printf("4.QUIT\n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(rear < max-1)
{
printf("\n Enter element to be inserted : ");
scanf("%d", &val);
insert(val);
}
else
printf("\n Queue Full \
n"); break;
case 2:
if(front == -1)
printf("\n Queue Empty \n");
else
{
val = remove();
printf("\n Element deleted : %d \n", val);
}
break;
case 3:
view();
break;
case 4:
exit(0);
default:
printf("\n Invalid Choice \n");
}
}
}
OUTPUT
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 12
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 23
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 34
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 45
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Enter element to be inserted : 56
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 1
Queue Full
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT
Enter Choice : 3
Front--> 12 23 34 45 56 <--Rear
RESULT
Thus insert and delete operations of a queue was demonstrated using arrays.
EX.NO:8(a) LINKED LIST IMPLEMENTATION OF STACK ADT
AIM
To implement stack operations using linked list.
ALGORITHM
STEP 1: Start the program
STEP 2: Define a singly linked list node for stack
STEP 3: Create Head node
STEP 4: Display a menu listing stack operation
STEP 5: Accept choice
STEP 6: If choice = 1 then
Create a new node with data Make new node point to first node
Make head node point to new node Else If
choice = 2 then
Make temp node point to first node
Make head node point to next of temp node Release memory
Else If choice = 3 then
Display stack elements starting from head node till null
STEP 7: Stop the program
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>
struct node
{
int label;
struct node *next;
};
main()
{
int ch = 0;
int k;
struct node *h, *temp, *head;
/* Head node construction */
head = (struct node*) malloc(sizeof(struct
node)); head->next = NULL;
while(1)
{
printf("\n Stack using Linked List \
n"); printf("1->Push ");
printf("2->Pop ");
printf("3->View
"); printf("4-
>Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
/* Create a new node */
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp-
>label); h = head;
temp->next = h-
>next; h->next =
temp; break;
case 2:
/* Delink the first
node */ h = head-
>next;
head->next = h->next;
printf("Node %s deleted\n", h-
>label); free(h);
break; case 3:
printf("\n HEAD ->
"); h = head;
/* Loop till last node */
while(h->next != NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL \
n"); break;
case 4:
exit(0);
}}}
OUTPUT
Stack using Linked List
1->Push 2->Pop 3->View 4->Exit
Enter your choice : 1
Enter label for new node :
23 New node added
RESULT
Thus push and pop operations of a stack was demonstrated using linked list.
EX.NO:8(b) LINKED LIST IMPLEMENTATION OF QUEUE ADT
AIM
To implement queue operations using linked list.
ALGORITHM
STEP 1: Start the program
STEP 2: Define a singly linked list node for stack
STEP 3: Create Head node
STEP 4: Display a menu listing stack operation
STEP 5: Accept choice
STEP 6: If choice = 1 then
Create a new node with data Make new
node point to first node
Make head node point to new node Else
If choice = 2 then
Make temp node point to first node
Make head node point to next of temp node Release
memory
Else If choice = 3 then
Display stack elements starting from head node till null
STEP 7: Stop the program
PROGRAM
#include <conio.h>
#include <process.h>
#include <alloc.h>
struct node
{
int label;
struct node *next;
};
main()
{
int ch=0; int k;
struct node *h, *temp, *head;
head = (struct node*) malloc(sizeof(struct node));
head->next = NULL;
while(1)
{
printf("\n Queue using Linked List \n");
printf("1->Insert ");
printf("2->Delete ");
printf("3->View ");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch); switch(ch)
{
case 1:
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : ");
scanf("%d", &temp->label);h = head;
while (h->next != NULL)
h = h->next;
h->next = temp;
temp->next = NULL;
break;
case 2:
h = head->next;
head->next = h->next;
printf("Node deleted \n");
free(h);
break;
case 3:
printf("\n\nHEAD -> ");
h=head;
while (h->next!=NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL \n");
break;
case 4: exit(0);
}
}
}
OUTPUT
Queue using Linked List
1->Insert 2->Delete 3->View 4-
>Exit Enter your choice : 1
Enter label for new node : 12
RESULT
Thus insert and delete operations of a queue was demonstrated using linked list.
EX.NO:9(a) POLYNOMIAL ADDITION
AIM
To add any two given polynomial using linked lists.
ALGORITHM
STEP 1: Start the program
STEP 2: Create a structure for polynomial with exp and coeff terms.
STEP 3: Read the coefficient and exponent of given two polynomials p and q.
STEP 4: While p and q are not null, repeat step 4.
If powers of the two terms are equal then
Insert the sum of the terms into the sum Polynomial Advance p and q
Else if the power of the first polynomial> power of second then Insert the
term from first polynomial into sum polynomial Advance
Else
STEP 5: Copy the remaining terms from the non-empty polynomial into
the Sum polynomial
STEP 6: Stop the program.
PROGRAM
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
struct link
{
int coeff; int pow;
struct link *next;
};
struct link
*poly1=NULL,*poly2=NULL,*poly=NULL; void
create(struct link *node)
{
char ch; do
{
printf("\nEnter coefficient: ");
scanf("%d", &node->coeff);
printf("Enter exponent: ");
scanf("%d", &node->pow);
node->next = (struct link*)malloc(sizeof(struct link));
node = node->next;
node->next = NULL;
printf("\n continue(y/n):
"); fflush(stdin);
ch=getch();
} while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d", node->coeff, node-
>pow); node=node->next;
if(node->next!=NULL)
printf(" + ");
}
}
void polyadd(struct link *poly1, struct link *poly2, struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
else if(poly1->pow < poly2->pow)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
else
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct
link)); poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->pow = poly2-
>pow; poly->coeff =
poly2->coeff; poly2 =
poly2->next;
}
poly->next = (struct link *)malloc(sizeof(struct link));
poly = poly->next;
poly->next = NULL;
}
}
main()
{
poly1 = (struct link *)malloc(sizeof(struct link));
poly2 = (struct link *)malloc(sizeof(struct link));
poly = (struct link *)malloc(sizeof(struct link));
printf("Enter 1st Polynomial:");
create(poly1);
printf("\nEnter 2nd Polynomial:");
create(poly2);
printf("\nPoly1: "); show(poly1);
printf("\nPoly2: "); show(poly2);
polyadd(poly1, poly2, poly);
printf("\nAdded Polynomial:
"); show(poly);
}
OUTPUT
Enter 1st Polynomial:
Enter coefficient: 5
Enter exponent: 2
continue(y/n): y
Enter coefficient: 4
Enter exponent: 1
continue(y/n): y
Enter coefficient: 2
Enter exponent: 0
continue(y/n): n
RESULT
Thus the polynomial operations using linked list was executed successfully and the
output was verified successfully.
EX.NO:9(B) INFIX TO POSTFIX CONVERSION
Aim:To convert infix expression to its postfix form using stack operations.
#include <conio.h>
#include <string.h>
#define MAX 20
RESULT
Thus the given infix expression was converted into postfix form using stack.
EX.NO:10 IMPLEMENTATION OF BINARY TREES
AIM
To insert and delete nodes in a binary tree.
ALGORITHM
STEP1: Start the program
STEP2: Create a structure with key and 2 pointer variable left and right.
STEP3: Read the node to be inserted.
If (root==NULL)
root=node
else if (root->key<node->key) root->right=NULL
else
Root->left=node
STEP4: For Deletion
if it is a leaf node
Remove immediately
Remove pointer between del node & child if it is having
one child
Remove link between del node&child Link delnode is child with delnodes
parent
If it is a node with two children
Find min value in right subtree Copy min value
to delnode place Delete the duplicate
STEP5: Stop the program
PROGRAM
#include<stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node* left;
struct node*
right;
};
// Inorder traversal
void inorderTraversal(struct node* root)
{
if (root == NULL) return;
inorderTraversal(root-
>left); printf("%d ", root-
>item);
inorderTraversal(root-
>right);
}
// Preorder traversal
void preorderTraversal(struct node* root)
{
if (root == NULL) return;
printf("%d ", root->item);
preorderTraversal(root-
>left);
preorderTraversal(root-
>right);
}
// Postorder traversal
void postorderTraversal(struct node* root)
{
if (root == NULL) return;
postorderTraversal(root-
>left);
postorderTraversal(root-
>right); printf("%d ", root-
>item);
}
// Create a new Node
struct node* create(int value)
{
struct node* newNode = malloc(sizeof(struct
node)); newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value)
{
root->left =
create(value); return
root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value)
{
root->right = create(value);
return root->right;
}
int main()
{
struct node* root = create(1);
insertLeft(root, 4);
insertRight(root, 6);
insertLeft(root->left, 42);
insertRight(root->left, 3);
insertLeft(root->right, 2);
insertRight(root->right, 33);
printf("Traversal of the inserted binary tree \n");
printf("Inorder traversal \n");
inorderTraversal(root);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
}
OUTPUT
Traversal of the inserted binary tree
Inorder traversal
42 4 3 1 2 6 33
Preorder
traversal 1 4
42 3 6 2 33
Postorder
traversal 42 3
4 2 33 6 1
RESULT
Thus the implementation of binary tree was executed and the output was verified
successfully.
EX.NO:11 IMPLEMENTATION OF BINARY SEARCH TREES
AIM
To insert and delete nodes in a binary search tree.
ALGORITHM
STEP1: Start the program
STEP2: Create a structure with key and 2 pointer variable left and right.
STEP3: Read the node to be inserted.
If (root==NULL)
root=node
else if (root->key<node->key) root-
>right=NULL
else
Root->left=node
STEP4: For Deletion
if it is a leaf node
Remove immediately
Remove pointer between del node & child if it is having
one child
Remove link between del node&child Link delnode is
child with delnodes parent
If it is a node with two children
Find min value in right subtree Copy min value
to delnode place Delete the duplicate
STEP5: Stop the program
PROGRAM
#include <stdio.h>
#include <stdlib.h>
// structure of a node
struct node
{
int data;
struct node *left;
struct node
*right;
};
struct node *root = NULL;
struct node
*create_node(int); void
insert(int);
struct node *delete (struct node *,
int); int search(int);
void inorder(struct node
*); void postorder();
void preorder();
struct node *smallest_node(struct node *);
struct node *largest_node(struct node *);
int get_data();
int main()
{
int userChoice;
int userActive =
'Y'; int data;
struct node* result = NULL;
while (userActive == 'Y' || userActive == 'y')
{
printf("\n\n------- Binary Search Tree- -\n");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Get Larger Node Data");
printf("\n5. Get smaller Node data");
printf("\n\n-- Traversals --"); printf("\n\
n6. Inorder ");
printf("\n7. Post Order ");
printf("\n8. Pre Oder ");
printf("\n9. Exit"); printf("\n\
nEnter Your Choice: ");
scanf("%d", &userChoice);
printf("\n");
switch(userChoice)
{
case 1:
data =
get_data();
insert(data);
break;
case 2:
data = get_data();
root = delete(root, data);
break;
case 3:
data = get_data();
if (search(data) == 1)
{
printf("\nData was found!\n");
}
else
{
printf("\nData does not found!\n");
}
break;
case 4:
result =
largest_node(root); if
(result != NULL)
{
printf("\nLargest Data: %d\n", result->data);
}
break;
case 5:
result =
smallest_node(root); if
(result != NULL)
{
printf("\nSmallest Data: %d\n", result->data);
}
break;
case 6:
inorder(root);
break;
case 7:
postorder(root);
break;
case 8:
preorder(root);
break;
case 9:
printf("\n\nProgram was terminated\n");
break;
default:
printf("\n\tInvalid Choice\n");
break;
}
printf("\n \nDo you want to continue? ");
fflush(stdin);
scanf(" %c", &userActive);
}
return 0;
}
// creates a new node
struct node *create_node(int data)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL)
{
printf("\nMemory for new node can't be allocated");
return NULL;
}
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
// inserts the data in the BST
void insert(int data){
struct node *new_node = create_node(data);
if (new_node != NULL)
{
// if the root is empty then make a new node as the root
node if (root == NULL)
{
root = new_node;
printf("\n* node having data %d was inserted\n",
data); return;
}
struct node *temp = root;
int get_data()
{
int data;
printf("\nEnter Data: ");
scanf("%d", &data);
return data;
}
OUTPUT
------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. In Order
7. Post Order
8. Pre Order
9. Exit
Enter Your Choice: 1
Enter Data: 20
* node having data 20 was inserted
Enter Your Choice: 1
Enter Data: 15
* node having data 15 was inserted
RESULT
Thus the implementation of binary search tree was executed and the output was verified
successfully.
Ex.No:12(a) IMPLEMENTATION OF LINEAR SEARCH
AIM
To perform linear search of an element on the given array.
ALGORITHM
STEP1: Start the program
STEP2: Read number of array elements n
PROGRAM
#include<stdio.h>
#include <conio.h>
main()
{
int a[50],i, n, val, found;
clrscr();
printf("Enter number of elements : ");
scanf("%d", &n);
printf("Enter Array Elements : \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("Enter element to locate : ");
scanf("%d", &val);
found = 0;
for(i=0; i<n;
i++)
{
if (a[i] == val)
{
printf("Element found at position %d",
i); found = 1;
break;
}
}
if (found == 0)
printf("\n Element not found");
getch();
OUTPUT
Enter number of elements : 7
Enter Array Elements :
23 6 12 5 0 32 10
Enter element to locate : 5
Element found at position 3
RESULT
Thus an array was linearly searched for an element's existence.
Ex.No:12(b) IMPLEMENTATION OF BINARY SEARCH
AIM
To locate an element in a sorted array using Binary search method
ALGORITHM
STEP1: Start the program
STEP2: Read number of array elements, say n
STEP3: Create an array arr consisting n sorted elements
STEP4: Get element, say key to be located
STEP5: Assign 0 to lower and n to upper
STEP6: While (lower < upper)
Determine middle element mid = (upper+lower)/2
If key = arr[mid] then
Print mid Stop
Else if key > arr[mid] then lower = mid + 1
else
upper = mid – 1
STEP7: Print "Element not found"
STEP8: Stop the program
PROGRAM
#include<stdio.h>
#include <conio.h>
main()
{
int a[50],i, n, upper, lower, mid, val, found;
clrscr();
printf("Enter array size : ");
scanf("%d", &n);
for(i=0; i<n; i++)
a[i] = 2 * i;
printf("\n Elements in Sorted Order \n");
for(i=0; i<n; i++)
printf("%4d", a[i]);
printf("\n Enter element to locate :
"); scanf("%d", &val);
upper = n;
lower = 0;
found = -1;
while (lower <= upper)
{
mid = (upper + lower)/2;
if (a[mid] == val)
{
printf("Located at position %d",
mid); found = 1;
break;
}
else if(a[mid] > val)
OUTPUT
Enter array size : 9
Elements in Sorted
Order 0 2 4 6 8 10 12
14 16
Enter element to locate : 12
Located at position 6
Enter array size : 10
Elements in Sorted
Order 0 2 4 6 8 10 12
14 16 18
Enter element to locate : 13
Element not found
RESULT
Thus an element is located quickly using binary search method.
Ex.No:13(a) IMPLEMENTATION OF INSERTION SORT
ALGORITHM
STEP1: Start the program
STEP2: Read number of array elements n
STEP3: Read array elements Ai
STEP4: Sort the elements using insertion sort
In pass p, move the element in position p left until its correct place is found among the first
p + 1 elements.
Element at position p is saved in temp, and all larger elements (prior to position p) are
moved one spot to the right. Then temp is placed in the correct spot.
STEP5: Stop the program
PROGRAM
main()
{
int i, j, k, n, temp, a[20], p=0;
printf("Enter total elements:
"); scanf("%d",&n);
printf("Enter array elements: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(i=1; i<n; i++)
{
temp = a[i]; j = i - 1;
while((temp<a[j]) && (j>=0))
{
a[j+1] = a[j]; j = j - 1;
}
a[j+1] =
temp; p++;
printf("\n After Pass %d: ", p);
for(k=0; k<n; k++)
printf(" %d", a[k]);
}
printf("\n Sorted List : ");
for(i=0; i<n; i++)
printf(" %d", a[i]);
}
OUTPUT
Enter total elements: 6
Enter array elements: 34 8 64 51 32 21
After Pass 1: 8 34 64 51 32 21
After Pass 2: 8 34 64 51 32 21
After Pass 3: 8 34 51 64 32 21
After Pass 4: 8 32 34 51 64 21
After Pass 5: 8 21 32 34 51 64
Sorted List: 8 21 32 34 51 64
RESULT
Thus array elements was sorted using insertion sort.
Ex.No:13(b) IMPLEMENTATION OF QUICK SORT
AIM
To sort an array of N numbers using Quick sort
ALGORITHM
STEP1: Start the program
STEP2: Read number of array elements n
STEP 3: Read array elements Ai
STEP 4: Select an pivot element x from Ai
STEP 5: Divide the array into 3 sequences: elements < x, x, elements > x
STEP 6: Recursively quick sort both sets (Ai < x and Ai > x)
STEP 7: Stop the program
PROGRAM
#include <stdio.h>
#include <conio.h>
void qsort(int arr[20], int fst, int
last); main()
{
int arr[30]; int i, size;
printf("Enter total no. of the elements : ");
scanf("%d", &size);
printf("Enter total %d elements : \n", size);
for(i=0; i<size; i++)
scanf("%d", &arr[i]); qsort(arr,0,size-1);
printf("\n Quick sorted elements \
n"); for(i=0; i<size; i++)
printf("%d\t", arr[i]);
getch();
}
void qsort(int arr[20], int fst, int last)
{
int i, j, pivot,
tmp; if(fst < last)
{
pivot = fst;
i = fst;
j = last;
while(i < j)
{
while(arr[i] <=arr[pivot] && i<last)
i++;
while(arr[j] > arr[pivot]) j--;
if(i <j )
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
tmp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = tmp;
qsort(arr, fst, j-1);
qsort(arr, j+1, last);
}
}
OUTPUT
Enter total no. of the elements : 8
Enter total 8 elements :
1
2
7
-1
0
4
-2
3
Quick sorted elements
-2 -1 0 1 2 3 4 7
RESULT
Thus an array was sorted using quick sort's divide and conquer method.
Ex.No:13(c) IMPLEMENTATION OF MERGE SORT
AIM
To sort an array of N numbers using Merge sort (Divide and Conquer method).
ALGORITHM
STEP1: Start the program
STEP2: Read number of array elements n
STEP3: Read array elements Ai
STEP4: Divide the array into sub-arrays with a set of elements
STEP5: Recursively sort the sub-arrays
STEP6: Merge the sorted sub-arrays onto a single sorted array.
STEP7: Stop the program
PROGRAM
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
void main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
OUTPUT
Enter no of elements: 5
Enter array elements: 10 7 3 6 2
RESULT
Thus array elements was sorted using merge sort's divide and conquer method.
Ex.No:14(a) IMPLEMENTATION OF HASHING USING LINEAR PROBING
ALGORITHM
Algorithm to insert a value in linear probing Hash table is an array of
size = TABLE_SIZE Step 1: Read the value to be inserted, key
Step 2: let i = 0
Step 3: hkey = key% TABLE_SIZE
Step 4: compute the index at which the key has to be inserted in hash table
index = (hkey + i) % TABLE_SIZE
Step 5: if there is no element at that index then insert the value at index
and STOP
Step 6: If there is already an element at that index i = i+1
step 7: if i < TABLE_SIZE then go to step 4
OUTPUT
Press 1. Insert2. Display 3. Search 4.Exit
1
enter a value to insert into hash table 12
12 value is found at
index 2
Press 1. Insert2. Display 3. Search 4.Exit
3
enter search element 23
value is not found
Press 1. Insert2. Display 3. Search 4.Exit
4
RESULT
Thus the program for implementation of linear searching was executed and verified
successfully.
Ex.No:14(b) IMPLEMENATATION OF HASHING USING QUADRATIC PROBING
AIM
To perform the implementation of hashing using Quadratic probing method.
ALGORITHM
Algorithm to insert a value in quadratic probing
Hash table is an array of size =
TABLE_SIZE Step 1: Read the value to
be inserted, key Step 2: let i = 0
Step 3: h(key)=key % TABLE_SIZE
Step 4: compute the index at which the value has to be inserted in hash
table index = (h(key)+ i * i) % TABLE_SIZE
Step 5: if there is no element at that index then insert the value at index
and STOP
Step 6: If there is already an element at that index i = i+1
step7: if i < TABLE_SIZE then go to step 4
OUTPUT
Press 1. Insert2. Display 3. Search 4.Exit
1
enter a value to insert into hash table 12
RESULT
Thus the program for implementation of quadratic probing was executed and verified
successfully.