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

Cpds Lab Manual

System Software Testing tools Automation- Question Bank

Uploaded by

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

Cpds Lab Manual

System Software Testing tools Automation- Question Bank

Uploaded by

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

CP&DS LAB Manual

C Programming and Data Structures (Anna University)

Downloaded by Amala Yesupatham


CS3362 C PROGRAMMING AND DATA STRUCTURES LABORATORY

LIST OF EXPERIMENTS

1. Practice of C programming using statements, expressions, decision making and iterative


statements
2. Practice of C programming using Functions and Arrays
3. Implement C programs using Pointers and Structures
4. Implement C programs using Files
5. Development of real time C applications
6. Array implementation of List ADT
7. Array implementation of Stack and Queue ADTs
8. Linked list implementation of List, Stack and Queue ADTs
9. Applications of List, Stack and Queue ADTs
10. Implementation of Binary Trees and operations of Binary Trees
11. Implementation of Binary Search Trees
12. Implementation of searching techniques
13. Implementation of Sorting algorithms: Insertion Sort, Quick Sort, Merge Sort
14. Implementation of Hashing – any two collision techniques

Downloaded by Amala Yesupatham


EX.NO:1(a) C PROGRAMMING USING STATEMENTS

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");
}
}
2

Downloaded by Amala Yesupatham


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

Downloaded by Amala Yesupatham


To implement c programming using expressions

ALGORITHM
STEP1: Start the program
STEP2: Declare the variables
STEP3: Enter the variable value
STEP4: Perform the arithmetic operations
STEP5: Print the values
STEP6: Stop 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;
4

Downloaded by Amala Yesupatham


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
Enter 2 numbers for arithmetic operation
10
5

======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
===========================
5

Downloaded by Amala Yesupatham


RESULT
Thus the c program using expression was executed successfully.

Downloaded by Amala Yesupatham


EX.NO:1(c) 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;
7

Downloaded by Amala Yesupatham


}
if(s == c)
{
printf("The number %d is a palindrome", c);
}
else
{
printf("The number %d is not a palindrome", c);
}
getch();
}

OUTPUT

Enter the number: 121


The number 121 is a palindrome

RESULT
Thus the c program using decision making and iterative statements was executed
successfully.
8

Downloaded by Amala Yesupatham


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;

for (int i = 0; i < 6; ++i)


9

Downloaded by Amala Yesupatham


{
sum += num[i];
}
return sum;
}

OUTPUT
Result = 162.50

RESULT
Thus the c program using functions and arrays was executed successfully.

10

Downloaded by Amala Yesupatham


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


11

Downloaded by Amala Yesupatham


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

12

Downloaded by Amala Yesupatham


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");

// Printing what is written in file


// character by character using loop.
do
13

Downloaded by Amala Yesupatham


{
ch = fgetc(ptr);
printf("%c", ch);
// Checking if character is not EOF.
// If it is EOF stop reading.
}
while (ch != EOF);
// Closing the
file fclose(ptr);
return 0;
}

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?
14

Downloaded by Amala Yesupatham


The C language is a high-level, general-purpose programming language. It provides a
straightforward, consistent, powerful interface for programming systems. That's why the C
language is widely used for developing system software, application software, and embedded
systems.

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.

And C is an excellent choice for system programming, for example, developing operating
systems, compilers, and network drivers. Despite its popularity, C is not without its criticisms.
Some have argued that its syntax could be more complex and easier to learn, while others have
noted its lack of standardization as a significant issue. Nevertheless, C remains a widely used and
influential language and will probably continue for many years.

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.

15
Downloaded by Amala Yesupatham
Key Features of C Programming Language

C is a general-purpose computer programming language for system administration, network


programming, and embedded software.

It has several features that make it desirable for these applications:

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

Use of the C programming language is not limited to the development of operating systems
and applications. It is also used in GUI development, IDE development, etc.

Some uses of C programming language:-

1. Operating Systems
2. Assemblers
3. Text Editors
4. Print Spoolers
5. Modern Programs
6. Databases
7. Utilities
16

Downloaded by Amala Yesupatham


Hold Tight, because I am going to tell you some exciting 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:-

The C programming language helps in developing the popular database management


system, MySQL.

5. Ease of Computation:-

C provides faster computation in programs. The implementation of algorithms and data


structures is swift in C. With the help of C, you can perform high degree calculations such as
MATLAB, Mathematica etc.

6. Gaming:-

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.

17

Downloaded by Amala Yesupatham


7. Development of New languages:-

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:-

Mainly used to translate Assembly language to Machine language. C also helped in


developing GNU assembler.

10. Text Editors:-

C also helped in creating various text editors like Vim, Gedit etc.

11. Drivers:-

Another application of C is to write driver softwares like Keyboard driver, Network driver,
mouse driver etc.

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:-

Another application of C is to design network devices.

18

Downloaded by Amala Yesupatham


14. Compiler Design:-

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.

19

Downloaded by Amala Yesupatham


EX.NO:6 ARRAY IMPLEMENTATION OF LIST ADT
AIM

To write a C program to implement list operations using array

ALGORITHM
STEP1: Start the program
STEP2: Initialize and declare variables using structure and arrays. Define the required size
of header files
STEP3: Enter the operations to perform in list as
a)Create list
b)Insert
c)Delete
d)View
STEP4: Based on the operations choosing, the list elements are structured.
STEP5: Stop the program

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");
A:
printf("\n Enter Ur Option:\t");
scanf("%d",&choice);
20

Downloaded by Amala Yesupatham


switch(choice)
{
case 1:
if(L==NULL)
L=Createlist(5);
else
printf("\nList is already created");
break;
case 2:
if(L==NULL)
printf("\nList is not yet created");
else
{
printf("\nEnter the Element to insert:\t");
scanf("%d",&element);
if(L->size==0)
Insert(element,L,0);
else
{
printf("\n where u want to insert?\t1:Front\t2:Back\t3:middle\t::: ");
scanf("%d",&ch);
if(ch==1)
Insert(element,L,0);
else
if(ch==2)
Insert(element,L,L->size);
else
if(ch==3)
{
21

Downloaded by Amala Yesupatham


printf("\nWhere you want to insert:\t");
scanf("%d",&a);
P=Find(a,L);
if(P<L->size)
Insert(element,L,P);
else
printf("\nElement is not in the list");
}
else
printf("\n Ur choice is not available");
}
}
break;
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
22

Downloaded by Amala Yesupatham


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;
23

Downloaded by Amala Yesupatham


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);
24

Downloaded by Amala Yesupatham


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

25

Downloaded by Amala Yesupatham


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 ::::: 1
List is Full

Enter Ur Option: 4
Elements present in the list are
100
200
300
400
500
26

Downloaded by Amala Yesupatham


Enter Ur Option: 7
List is not empty

Enter Ur Option: 6
Which element is to find: 500
Element at 4 [0 to 4 – present] [5 – not present]

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
27

Downloaded by Amala Yesupatham


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
28

Downloaded by Amala Yesupatham


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

Downloaded by Amala Yesupatham


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 ");
30

Downloaded by Amala Yesupatham


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);
31

Downloaded by Amala Yesupatham


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

32

Downloaded by Amala Yesupatham


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

Downloaded by Amala Yesupatham


33

Downloaded by Amala Yesupatham


RESULT
Thus push and pop operations of a stack was demonstrated using arrays.

EX.NO:7(b) ARRAY IMPLEMENTATION OF QUEUE ADT


AIM

To write a C program to implement queue operations using array.

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
34

Downloaded by Amala Yesupatham


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;
if (front == -1)
front = 0;
}
int remove()
{
int val;
35

Downloaded by Amala Yesupatham


val = queue[front];
if (front==rear && rear==max-1)
front = rear = -1;
else
front ++;
return (val);
}
void view()
{
int i;
if (front == -1)
printf("\n Queue Empty \n");
else
{
printf("\n Front-->");
for(i=front; i<=rear; i++)
printf("%4d", queue[i]);
printf(" <--Rear\n");
}
}
main()
{
int ch= 0,val;
clrscr();
while(ch != 4)
{
printf("\n QUEUE OPERATION \n");
printf("1.INSERT ");
printf("2.DELETE ");
36

Downloaded by Amala Yesupatham


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:
37

Downloaded by Amala Yesupatham


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
38

Downloaded by Amala Yesupatham


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

39

Downloaded by Amala Yesupatham


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
40

Downloaded by Amala Yesupatham


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);
41

Downloaded by Amala Yesupatham


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;
42

Downloaded by Amala Yesupatham


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

Stack using Linked List


1->Push 2->Pop 3->View 4->Exit
Enter your choice : 1
Enter label for new node : 34

Stack using Linked List


1->Push 2->Pop 3->View 4->Exit
Enter your choice : 3
HEAD -> 34 -> 23 -> NULL

43

Downloaded by Amala Yesupatham


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>
44

Downloaded by Amala Yesupatham


#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);
45

Downloaded by Amala Yesupatham


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

46

Downloaded by Amala Yesupatham


OUTPUT
Queue using Linked List
1->Insert 2->Delete 3->View 4->Exit
Enter your choice : 1
Enter label for new node : 12

Queue using Linked List


1->Insert 2->Delete 3->View 4->Exit
Enter your choice : 1
Enter label for new node : 23

Queue using Linked List


1->Insert 2->Delete 3->View 4->Exit
Enter your choice : 3
HEAD -> 12 -> 23 -> NULL

RESULT
Thus insert and delete operations of a queue was demonstrated using linked list.
47

Downloaded by Amala Yesupatham


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 p
Else
Insert the term from second polynomial into sum polynomial
Advance q
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
{
48

Downloaded by Amala Yesupatham


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)
49

Downloaded by Amala Yesupatham


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;

Downloaded by Amala Yesupatham


poly->next=NULL;
50

Downloaded by Amala Yesupatham


}
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:");

Downloaded by Amala Yesupatham


create(poly2);
51

Downloaded by Amala Yesupatham


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

Enter 2nd Polynomial:


Enter coefficient: 5
Enter exponent: 1
continue(y/n): y
Enter coefficient: 5
Enter exponent: 0
continue(y/n): n
Poly1: 5x^2 + 4x^1 + 2x^0
52

Downloaded by Amala Yesupatham


Poly2: 5x^1 + 5x^0

Added Polynomial: 5x^2 + 9x^1 + 7x^0

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.

ALGORITHM
53

Downloaded by Amala Yesupatham


STEP 1: Start the program
STEP 2: Define a array stack of size max = 20
STEP 3: Initialize top = -1
STEP 4: Read the infix expression character-by-
character If character is an operand print it
If character is an operator
Compare the operator’s priority with the stack[top] operator.
If the stack [top] has higher/equal priority than the input operator,
Pop it from the stack and print it.
Else
Push the input operator onto the stack
If character is a left parenthesis, then push it onto the stack.
If character is a right parenthesis, pop all operators from stack and print it until a left
parenthesis is encountered. Do not print the parenthesis.
If character = $ then Pop out all operators, Print them and Stop
STEP 5: Stop the program.

PROGRAM
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 20
int top = -1;
char stack[MAX];
char pop();
void push(char item);
int prcd(char symbol)
{
switch(symbol)
54

Downloaded by Amala Yesupatham


{
case '+':
case '-':
return 2;
break;
case '*':
case '/':
return 4;
break;
case '^':
case '$':
return 6;
break;
case '(':
case ')':
case '#':
return 1;
break;
}
}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
55

Downloaded by Amala Yesupatham


case '$':
case '(':
case ')':
return 1;
break;
default:
return 0;
}
}
void convertip(char infix[],char postfix[])
{
int i,symbol,j = 0; stack[+
+top] = '#';
for(i=0;i<strlen(infix);i++)
{
symbol = infix[i];
if(isoperator(symbol) == 0)
{
postfix[j] = symbol;
j++;
}
else
{
if(symbol == '(')
push(symbol);
else if(symbol == ')')
{
while(stack[top] != '(')
{
56

Downloaded by Amala Yesupatham


postfix[j] = pop(); j+
+;
}
pop(); //pop out (.
}
else
{
if(prcd(symbol) > prcd(stack[top]))
push(symbol);
else
{
while(prcd(symbol) <= prcd(stack[top]))
{
postfix[j] = pop(); j+
+;
}
push(symbol);
}
}
}
}
while(stack[top] != '#')
{
postfix[j] = pop(); j+
+;
}
postfix[j] = '\0';
}
main()
57

Downloaded by Amala Yesupatham


{
char infix[20],postfix[20];
clrscr();
printf("Enter the valid infix string: ");
gets(infix);
convertip(infix, postfix);
printf("The corresponding postfix string is: ");
puts(postfix);
getch();
}
void push(char item)
{
top++;
stack[top] = item;
}
char pop()
{
char a;
a = stack[top];
top--;
return a;
}

OUTPUT
Enter the valid infix string: (a+b*c)/(d$e)
The corresponding postfix string is: abc*+de$/
Enter the valid infix string: a*b+c*d/e
The corresponding postfix string is: ab*cd*e/+
58

Downloaded by Amala Yesupatham


Enter the valid infix string: a+b*c+(d*e+f)*g
The corresponding postfix string is: abc*+de*f+g*+

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
59

Downloaded by Amala Yesupatham


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)
60

Downloaded by Amala Yesupatham


{
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;
61

Downloaded by Amala Yesupatham


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");
62

Downloaded by Amala Yesupatham


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.

63

Downloaded by Amala Yesupatham


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

64

Downloaded by Amala Yesupatham


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')
{
65

Downloaded by Amala Yesupatham


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");
66

Downloaded by Amala Yesupatham


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

Downloaded by Amala Yesupatham


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)
68

Downloaded by Amala Yesupatham


{
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;
struct node *prev = NULL;
// traverse through the BST to get the correct position for insertion
while (temp != NULL)
{
prev = temp;
if (data > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
// found the last node where the new node should insert
if (data > prev->data)
{
69

Downloaded by Amala Yesupatham


prev->right = new_node;
}
else
{
prev->left = new_node;
}
printf("\n* node having data %d was inserted\n", data);
}
}
// deletes the given key node from the BST
struct node *delete (struct node *root, int key)
{
if (root == NULL)
{
return root;
}
if (key < root->data)
{
root->left = delete (root->left, key);
}
else if (key > root->data)
{
root->right = delete (root->right, key);
}
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
70

Downloaded by Amala Yesupatham


free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = smallest_node(root->right);
root->data = temp->data;
root->right = delete (root->right, temp->data);
}
return root;
}
// search the given key node in BST
int search(int key)
{
struct node *temp = root;
while (temp != NULL)
{
if (key == temp->data)
{
return 1;
}
else if (key > temp->data)
{
temp = temp->right;
}
71

Downloaded by Amala Yesupatham


else
{
temp = temp->left;
}
}
return 0;
}
// finds the node with the smallest value in BST
struct node *smallest_node(struct node *root)
{
struct node *curr = root;
while (curr != NULL && curr->left != NULL)
{
curr = curr->left;
}
return curr;
}
// finds the node with the largest value in BST
struct node *largest_node(struct node *root)
{
struct node *curr = root;
while (curr != NULL && curr->right != NULL)
{
curr = curr->right;
}
return curr;
}
// inorder traversal of the BST
void inorder(struct node *root)
72

Downloaded by Amala Yesupatham


{
if (root == NULL)
{
return;
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
// preorder traversal of the BST
void preorder(struct node *root)
{
if (root == NULL)
{
return;
}
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
// postorder travsersal of the BST
void postorder(struct node *root)
{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
73

Downloaded by Amala Yesupatham


printf("%d ", root->data);
}
// getting data from the user
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

74

Downloaded by Amala Yesupatham


Do you want to continue? y
------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 1


Enter Data: 15
* node having data 15 was inserted

Do you want to continue? y


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit
75

Downloaded by Amala Yesupatham


Enter Your Choice: 1
Enter Data: 25
* node having data 25 was inserted

Do you want to continue? y


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 1


Enter Data: 12
* node having data 12 was inserted

Do you want to continue? y


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
76

Downloaded by Amala Yesupatham


-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 1


Enter Data: 18
* node having data 18 was inserted

Do you want to continue? y


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 1


Enter Data: 65
* node having data 65 was inserted

Do you want to continue? n


------- Binary Search Tree ------
77

Downloaded by Amala Yesupatham


1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 6


12 15 18 20 25 65

Do you want to continue? y


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 2


Enter Data: 25
78

Downloaded by Amala Yesupatham


Do you want to continue? y
------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 6


12 15 18 20 65

Do you want to continue? n


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit
79

Downloaded by Amala Yesupatham


Enter Your Choice: 3
Enter Data: 65
Data was found!

Do you want to continue? y


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 3


Enter Data: 64
Data does not found!

Do you want to continue? y


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
80

Downloaded by Amala Yesupatham


-- Traversals --
6. In Order
7. Post Order
8. Pre Order
9. Exit

Enter Your Choice: 4


Largest Data: 65

Do you want to continue? y


------- 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 Oder
9. Exit

Enter Your Choice: 5


Smallest Data: 12

Do you want to continue?


------- Binary Search Tree ------
1. Insert
2. Delete
81

Downloaded by Amala Yesupatham


3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 6


12 15 18 20 25 65

Do you want to continue? n


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 8


20 15 12 18 25 65

Do you want to continue? n


82

Downloaded by Amala Yesupatham


------- Binary Search Tree ------
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit

Enter Your Choice: 7


12 18 15 65 25 20

Do you want to continue? n

83

Downloaded by Amala Yesupatham


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
STEP3: Read array elements Ai, i = 0,1,2,…n–1
STEP4: Read search value
STEP5: Assign 0 to found
STEP6: Check each array element against search
If Ai = search then
found = 1
Print "Element found"
Print position i
Stop
STEP7: If found = 0 then
print "Element not found"
84

Downloaded by Amala Yesupatham


STEP8: Stop the program

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();
85

Downloaded by Amala Yesupatham


}

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

86

Downloaded by Amala Yesupatham


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
87

Downloaded by Amala Yesupatham


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

Downloaded by Amala Yesupatham


else if(a[mid] > val)
upper = mid - 1;
else
lower = mid + 1;
}
if (found == -1)
printf("Element not found");
getch();
}

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

89

Downloaded by Amala Yesupatham


RESULT
Thus an element is located quickly using binary search method.

Ex.No:13(a) IMPLEMENTATION OF INSERTION SORT


AIM

To sort an array of N numbers using 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]);
90

Downloaded by Amala Yesupatham


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
91

Downloaded by Amala Yesupatham


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++)
92

Downloaded by Amala Yesupatham


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

Downloaded by Amala Yesupatham


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.
94

Downloaded by Amala Yesupatham


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); printf("\
nSorted array is :");
for(i=0;i<n;i++)
95

Downloaded by Amala Yesupatham


printf("%d ",a[i]);
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else

temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
96

Downloaded by Amala Yesupatham


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

Sorted array is: 2 3 6 7 10

RESULT
Thus array elements was sorted using merge sort's divide and conquer method.

Ex.No:14(a) IMPLEMENTATION OF HASHING USING


LINEAR PROBING

97

Downloaded by Amala Yesupatham


AIM
To perform the implementation of hashing using linear probing method.

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

Algorithm to search a value in linear probing


Hash table is an array of size = TABLE_SIZE
Step 1: Read the value to be searched, key
Step 2: let i = 0
Step 3: h(key) = key % TABLE_SIZE
Step 4: compute the index at which the key can be found
index = (h(key)+ i) % TABLE_SIZE
Step 5: if the element at that index is same as the search value then print
element found and STOP
Step 6: else i = i+1
step 7: if i < TABLE_SIZE then go to step 4

PROGRAM
98

Downloaded by Amala Yesupatham


#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
{
int key,index,i,flag=0,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void search()
{
int key,index,i,flag=0,hkey; printf("\
nenter search element\n");
scanf("%d",&key); hkey=key
%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
99

Downloaded by Amala Yesupatham


{
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
{
printf("value is found at index %d",index);
break;
}
}
if(i == TABLE_SIZE)
printf("\n value is not found\n");
}
void display()
{
int i;
printf("\nelements in the hash table are \n");
for(i=0;i< TABLE_SIZE; i++)
printf("\nat index %d \t value = %d",i,h[i]);
}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
100

Downloaded by Amala Yesupatham


break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
}
}
}

OUTPUT
Press 1. Insert 2. Display 3. Search 4.Exit
1
enter a value to insert into hash table 12

Press 1. Insert 2. Display 3. Search 4.Exit


1
enter a value to insert into hash table 13

Press 1. Insert 2. Display 3. Search 4.Exit


1
enter a value to insert into hash table 22

Press 1. Insert 2. Display 3. Search 4.Exit


2

elements in the hash table are


101

Downloaded by Amala Yesupatham


at index 0 value = 0
at index 1 value = 0
at index 2 value = 12
at index 3 value = 13
at index 4 value = 22
at index 5 value = 0
at index 6 value = 0
at index 7 value = 0
at index 8 value = 0
at index 9 value = 0

Press 1. Insert 2. Display 3. Search 4.Exit


3
enter search element 12
value is found at index 2

Press 1. Insert 2. Display 3. Search 4.Exit


3
enter search element 23
value is not found

Press 1. Insert 2. Display 3. Search 4.Exit


4

102

Downloaded by Amala Yesupatham


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
103

Downloaded by Amala Yesupatham


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

Algorithm to search a value in quadratic probing


Hash table is an array of size = TABLE_SIZE
Step 1: Read the value to be searched, key
Step 2: let i = 0
Step 3: h(key)=key % TABLE_SIZE
Step 4: compute the index at which the value can be found
index = (h(key) + i * i) % TABLE_SIZE
Step 5: if the element at that index is same as the search value then print
element found and STOP
Step 6: else i = i+1
step7: if i < TABLE_SIZE then go to step 4

PROGRAM
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
104

Downloaded by Amala Yesupatham


{
int key,index,i,flag=0,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i*i)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void search()
{
int key,index,i,flag=0,hkey; printf("\
nenter search element\n");
scanf("%d",&key);
hkey=key % TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
{
index=(hkey+i*i)%TABLE_SIZE;
if(h[index]==key)
{
printf("value is found at index %d",index);
105

Downloaded by Amala Yesupatham


break;
}
}
if(i == TABLE_SIZE)
printf("\n value is not found\n");
}
void display()
{
int i;
printf("\nelements in the hash table are \n");
for(i=0;i< TABLE_SIZE; i++)
printf("\nat index %d \t value = %d",i,h[i]);
}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
106

Downloaded by Amala Yesupatham


search();
break;
case 4:exit(0);
}
}
}

OUTPUT
Press 1. Insert 2. Display 3. Search 4.Exit
1
enter a value to insert into hash table 12

Press 1. Insert 2. Display 3. Search 4.Exit


1
enter a value to insert into hash table 22

Press 1. Insert 2. Display 3. Search 4.Exit


1
enter a value to insert into hash table 32

Press 1. Insert 2. Display 3. Search 4.Exit


2

elements in the hash table are


at index 0 value = 0
at index 1 value = 0
at index 2 value = 12
at index 3 value = 22
at index 4 value = 0
107

Downloaded by Amala Yesupatham


at index 5 value = 0
at index 6 value = 32
at index 7 value = 0
at index 8 value = 0
at index 9 value = 0
Press 1. Insert 2. Display 3. Search 4.Exit
3

enter search element 22


value is found at index 3

Press 1. Insert 2. Display 3. Search 4.Exit


3
enter search element 123
value is not found

Press 1. Insert 2. Display 3. Search 4.Exit


4

108

Downloaded by Amala Yesupatham


RESULT
Thus the program for implementation of quadratic probing was executed and verified
successfully.

109

Downloaded by Amala Yesupatham

You might also like