Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 92

CS8881-Data Structures Lab Department of CSE 2020-2021

Name:K.V.Thanka kumar Reg.No:312419104152


INDEX

Name of the Lab In charge: Mrs.E.Ajitha

Date of
Ex. No. Date Name of the Experiment Staff Sign
Submission

1 6/10/2020 Array implementation of stack ADT’S 13/10/2020

2 6/10/2020 Array implementation of queue ADT’S 13/10/2020

3 6/10/2020 Array implementation of list ADT’S 13/10/2020

4 6/10/2020 Linked list implementation of list ADT’S 13/10/2020

5 6/10/2020 Linked list implementation of stacks ADT’S 13/10/2020

6 6/10/2020 Linked list implementation of queue ADT’S 13/10/2020

7 6/10/2020 Application of list ADT’S 13/10/2020

8 6/10/2020 Application of stacks ADT’S 13/10/2020

9 6/10/2020 Applications of queue ADT’S 13/10/2020

10 13/10/2020 Implementation of binary trees and its operations 20/10/2020

11 13/10/2020 Implementation of binary tree search 20/10/2020

12 13/10/2020 Implementation of AVL tree 20/10/2020

13 13/10/2020 Implementation of heap using priority queues 20/10/2020

14 20/10/2020 Graph representations and traversal algorithm 27/10/2020


CS8881-Data Structures Lab Department of CSE 2020-2021

15 20/10/2020 Application of graphs 27/10/2020

16 20/10/2020 Implementation of linear searching 27/10/2020

17 20/10/2020 Implementation of binary searching 27/10/2020

18 20/10/2020 Implementation of bubble sorting algorithm 27/10/2020

19 20/10/2020 Implementation of selection sorting algorithm 27/10/2020

20 27/10/2020 Implementation of insertion sorting algorithm 28/10/2020

21 27/10/2020 Implementation of hashing using linear quadratic 28/10/2020


probing

St. Joseph’s Institute of Technology 2


Ex No: 1 ARRAY IMPLEMENTATION OF STACK ADT’ S
Aim:
To write a C program to implement Stack using Array.
Algorithm:
Step 1:- Include all the header files which are used in the program and define a constant 'SIZE'
with specific value.
Step 2:- Declare all the functions used in stack implementation.
Step 3:- Create a one dimensional array with fixed size (int stack[SIZE])
Step 4:- Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5:- In main method display menu with list of operations and make suitable function calls
to perform operation selected by the user on the stack.
Step 6:- For Push operation, Check whether stack is FULL. (top == n-1)
Step 7:- If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate
the function.
Step 8:- If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
Step 9:- For Pop operation, Check whether stack is EMPTY. (top == -1)
Step 10:- If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Step 11:- If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).
Step 12:- Finally display the result generated after the operations.

Program:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
printf("*****312419104152*****");
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
Sample Output:
*****312419104152*****
Enter the size of STACK[MAX=100]:10
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice: 1
Enter a value to be pushed: 12
Enter the Choice: 1
Enter a value to be pushed: 24
Enter the Choice: 1
Enter a value to be pushed: 98
Enter the Choice: 3
The elements in STACK 98 24 12
Press Next Choice
Enter the Choice: 2
The popped elements is 98
Enter the Choice: 3
The elements in STACK 4 12
Press Next Choice
Enter the Choice: 4
EXIT POINT
Output:

Result:
Thus the above c program to implement stack using array is executed successfully
Ex No: 2 ARRAY IMPLEMENTATION OF QUEUE ADT’ S
Aim:
To write a C program to implement Stack using Queue.
Algorithm:
Step 1: Include all the header files which are used in the program and define a constant 'SIZE'
with specific value.
Step 2: Declare all the user defined functions which are used in queue implementation.
Step 3: Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4: Define two integer 'front' and 'rear' and initialize both with '-1'. (int front = -1, rear = -
1)
Step 5: Then implement main method by displaying menu of operations list and make suitable
function calls to perform operation selected by the user on queue.
Step 6: Check whether queue is FULL. (rear == SIZE-1)
Step 7: If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and terminate
the function.
Step 8: If it is NOT FULL, then increment rear value by one (rear++) and set queue[rear] =
value.
Step 9: Check whether queue is EMPTY. (front == rear)
Step 10: If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Step 11: If it is NOT EMPTY, then increment the front value by one (front ++). Then display
queue[front] as deleted element. Then check whether both front and rear are equal (front ==
rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
Step 12: Check whether queue is EMPTY. (front == rear)
Step 13: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 14: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 15: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i'
value is equal to rear (i <= rear).
Program
#include <stdio.h>
#define MAX 50
void display();
void insert();
void delete();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void main()
{
printf(“*****312419104152*****”);
int choice;
while (choice<3)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /*End of switch*/
} /*End of while*/
} /*End of main()*/
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
Sample Output:
*****312419104152*****
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
15
Output:

Result:
Thus the above c program to implement queue using array is executed successfully
Ex No: 3 ARRAY IMPLEMENTATION OF LIST ADT’ S
Aim:
To write a C program to implement List using Array.
Algorithm:
Step 1:- Include all the header files which are used in the program and define a constant 'MAX'
with specific value.
Step 2:- Declare all the functions used in List implementation.
Step 3:- Create a one dimensional array with fixed size
Step 4:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 5:- Finally display the result generated after the operations.
Program:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
printf("*****312419104152*****");
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n); for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n) {
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{ printf("\t%d", b[i]);
}
}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list::", e);
continue;
}
}
}
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p; n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}
}
Sample Output:
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice 1
Enter the number of nodes 5
Enter the Element: 10
Enter the Element: 20
Enter the Element: 30
Enter the Element: 40
Enter the Element: 50
Do u want to continue::: y
Enter your Choice 2
Enter the position u want to delete:: 3
The Elements after deletion 10 20 30 50
Do u want to continue::: y
Enter your Choice 3
Enter the Element to be searched: 20
Value is in the 1 Position
Do u want to continue::: y
main Menu
Enter your Choice 5
The Elements of The list ADT are: 10 20 30 50
Output:
Result:
Thus the above c program to implement queue using array is executed successfully.
Ex No: 4 LINKED LIST IMPLEMENTATION OF LIST ADT’ S
Aim:
To write a C program to implement List using Linked List.
Algorithm:
Step 1:- Include all the header files which are used in the
Step 2:- Declare all the functions used in List implementation.
Step 3:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 4:- Finally display the result generated after the operations.
Program
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
{
printf("*****312419104152*****\n");
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
while (choice)
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data item\n");
scanf("%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
15
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);
}
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
}
Sample output:
*****312419104152*****
Enter the data item 5
Do you want to continue(Type 0 or 1)? 1
Enter the data item 9
Do you want to continue(Type 0 or 1)? 1
Enter the data item 3
Do you want to continue(Type 0 or 1)? 0
status of the linked list is
5=>9=>3=>NULL
No. of nodes in the list = 3
Output:
Result:
Thus the above c program to implement queue using array is executed successfully .
Ex No: 5 LINKED LIST IMPLEMENTATION OF STACK ADT’ S
Aim
To write a ‘c’ program to implement stack ADT using Linked List.
Algorithm
Step 1:- Define a list as a node of a structure with one DATA and one LINK pointing to the next
element in the structure.
Step 2:- Declare the function prototypes push(), pop(), display(), Create() to perform the list
functions.
Step 3:- Declare necessary pointer variables as struct node data type and initialize first and
last as NULL..
Step 4:- Get a choice from the user. If the choice is create, get first data from the user by calling
the function create( ), assign the new node as fist and last, and display contents of the list.
Step 5:- If the choice is insert, get the data by calling the function push( ), the LINK field of the
new node points to the fist node and reassign the new node as the first node. Display the
contents of the queue.
Step 6:- If the choice is delete, remove the data from the first of the queue.
Step 7:- Repeat the steps 4, 5 & 6 until the choice is exit.
Step 8:- Terminate the execution.
Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *cur,*first;
void create();
void push();
void pop();
void display();
void create()
{
printf("\nENTER THE FIRST ELEMENT: ");
cur=(struct node *)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
}
void display()
{
cur=first;
printf("\n");
while(cur!=NULL)
{
printf("%d\n",cur->data);
cur=cur->link;
}
}
void push()
{
printf("\nENTER THE NEXT ELEMENT: ");
cur=(struct node *)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=first;
first=cur;
}
void pop()
{
if(first==NULL)
{
printf("\nSTACK IS EMPTY\n");
}
else
{
cur=first;
printf("\nDELETED ELEMENT IS %d\n",first->data);
first=first->link;
free(cur);
}
}
void main()
{
printf("*****312419104152*****");
int ch;
while(1)
{
printf("\n\n 1.CREATE \n 2.PUSH \n 3.POP \n 4.EXIT \n");
printf("\n ENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
push();
display();
break;
case 3:
pop();
display();
break;
case 4:
exit(0);
}
}
}
Sample Output
1.CREATE
2.PUSH
3.POP
4.EXIT
ENTER YOUR CHOICE : 1
ENTER THE FIRST ELEMENT: 20
20
1.CREATE
2.PUSH
3.POP
4.EXIT
ENTER YOUR CHOICE : 2
ENTER THE NEXT ELEMENT: 40
40
20
1.CREATE
2.PUSH
3.POP
4.EXIT
ENTER YOUR CHOICE : 2
ENTER THE NEXT ELEMENT: 50
50
40
20
1.CREATE
2.PUSH
3.POP
4.EXIT
ENTER YOUR CHOICE : 3
DELETED ELEMENT IS 50
40
20
1.CREATE
2.PUSH
3.POP
4.EXIT
ENTER YOUR CHOICE : 4

Output:
Result:
Thus the above c program to implement stack ADT using Linked List is executed successfully.
Ex No: 6 LINKED LIST IMPLEMENTATION OF QUEUE ADT’ S
Aim
To write a C program to implement queue ADT using Linked List.
Algorithm
Step 1:- Define a list as a node of a structure with one DATA and one LINK pointing to the next
element in the structure.
Step 2:- Declare the function prototypes Create(), enqueue(), dequeue(), display()to perform
the list functions.
Step 3:- Declare necessary pointer variables as struct node data type and initialize first and
last.
Step 4:- Get a choice from the user. If the choice is create, get first data from the user by calling
the function create( ), assign the new node as first and last, and display contents of the list.
Step 5:- If the choice is insert, get the data by calling the function insert( ), assign the LINK
field of the new node as NULL, LINK field of the last node points to the new node and reassign
the new node as last node. Display the contents of the queue.
Step 6:- If the choice is delete, remove the data from the fist of the queue.
Step 7:- Repeat the steps 4, 5 & 6 until the choice is exit.
Step 8:- Terminate the execution.
Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};struct node *cur,*first,*last;
void create();
void enqueue();
void dequeue();
void display();
void create()
{
printf("\nENTER THE FIRST ELEMENT: ");
cur=(struct node *)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
last=cur;
}
void enqueue()
{
printf("\nENTER THE NEXT ELEMENT: ");
cur=(struct node *)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=NULL;
last->link=cur;
last=cur;
}
void dequeue()
{
if(first==NULL)
{
printf("\t\nQUEUE IS EMPTY\n");
}
else
{
cur=first;
first=first->link;
cur->link=NULL;
printf("\n DELETED ELEMENT IS %d\n",cur->data);
free(cur);
}
}
void display()
{
cur=first;
printf("\n");
while(cur!=NULL)
{
printf("\t%d",cur->data);
cur=cur->link;
}
}
void main()
{
printf("*****312419104152*****");
int ch;
while(1)
{
printf("\n\n 1.CREATE \n 2.ENQUEUE \n 3.DEQUEUE \n 4.EXIT \n");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
enqueue();
display();
break;
case 3:
dequeue();
display();
break;
case 4:
exit(0);
}
}
}
Sample Output
*****312419104152*****
1.CREATE
2.ENQUEUE
3.DEQUEUE
4.EXIT
ENTER YOUR CHOICE : 1
ENTER THE FIRST ELEMENT: 5
5
1.CREATE
2.ENQUEUE
3.DEQUEUE
4.EXIT
ENTER YOUR CHOICE : 2
ENTER THE NEXT ELEMENT: 4
54
1.CREATE
2.ENQUEUE
3.DEQUEUE
4.EXIT
ENTER YOUR CHOICE : 2
ENTER THE NEXT ELEMENT: 3
542
1.CREATE
2.ENQUEUE
3.DEQUEUE
4.EXIT
ENTER YOUR CHOICE : 3
DELETED ELEMENT IS 5
43
1.CREATE
2.ENQUEUE
3.DEQUEUE
4.EXIT
ENTER YOUR CHOICE : 4
OUTPUT:
Result:
Thus the above c program to implement queue ADT using Linked List is executed successfully .
Ex No: 7 APPLICATIONS OF LIST ADT’ S
Aim:
To write a C program to implement Polynomial ADT.
Algorithm:
Step 1:- Initialize the Structure with coefficient and power also pointer to next position.
Step 2:- Write a Function create_node to create new node.
Step 3:- Create a node to store power and coeff of a variable.
Step 4:- Write a Function polyadd to add two polynomial numbers.
Step 5:- If power of 1st polynomial is greater then 2nd, then store 1st as it is and move its
pointer
Step 6:- If power of both polynomial numbers is same then add their coefficients
Step 7:- Finally display resultant Linked List.
Program:
struct Node
{
int coeff;
int pow;
struct Node *next;
};
// Function to create new node
void create_node(int x, int y, struct Node **temp)
{
struct Node *r, *z;
z = *temp;
if(z == NULL)
{
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
else
{
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
// Function Adding two polynomial numbers
void polyadd(struct Node *poly1, struct Node *poly2, struct Node *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;
}
// Dynamically create new node
poly->next = (struct Node *)malloc(sizeof(struct Node));
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 Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
// Display Linked list
void show(struct Node *node)
{
while(node->next != NULL)
{
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}
// Driver program
int main()
{
printf("*****312419104152*****");
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
// Create first list of 5x^2 + 4x^1 + 2x^0
create_node(5,2,&poly1);
create_node(4,1,&poly1);
create_node(2,0,&poly1);
// Create second list of 5x^1 + 5x^0
create_node(5,1,&poly2);
create_node(5,0,&poly2);
printf("1st Polynomial Equation: ");
show(poly1);
printf("\n2nd Polynomial Equation: ");
show(poly2);
poly = (struct Node *)malloc(sizeof(struct Node));
// Function add two polynomial numbers
polyadd(poly1, poly2, poly);
// Display resultant List
printf("\nAdded polynomial: ");
show(poly);
return 0;
}
Sample Output:
*****312419104152*****
1st Polynomial Equation: 5x^2 + 4x^1 + 2x^0
2nd Polynomial Equation: 5x^1 + 5x^0
Added polynomial: 5x^2 + 9x^1 + 7x^0
Output:
Result:
Thus the above c program to implement Polynomial ADT is executed successfully.
Ex.No: 8 APPLICATION OF STACK ADT’ S
Aim
To write a C program to implement stack and use it to convert infix to postfix expression.
Algorithm
Step 1:- Initialize the Stack array with maximum size.
Step 2:- Read an expression.
Step 3:- Scan the expression from left to right and repeat steps 4 to 7 for each character in the
expression till the delimiter.
Step 4:- If the character is an operand, place it on to the output.
Step 5:- If the character is an operator, then check the Stack operator has a higher or equal
priority than the input operator then pop that operator from the stack and place it onto the
output. Repeat this till there is no higher priority operator on the top of the Stack and push the
input operator onto the Stack.
Step 6:- If the character is a left parenthesis, push it onto the Stack.
Step 7:- If the character is a right parenthesis, pop all the operators from the stack till it
encounters left parenthesis. Discard both the parenthesis in the output.
Step 8:- Terminate the execution.
Program:
#include<stdio.h>
//#include<process.h>
#include<stdlib.h>
#define SIZE 20
char Expr[SIZE];
char Stack[SIZE];
int Top=-1;
void push(char ch);
void pop();
void infix_to_postfix();
int m,l;
void main()
{
printf("*****312419104152*****");
char ch;
printf("Program to covert infix expression into postfix expression:\n");
printf("Enter your expression & to quit enter fullstop(.)\n");
while((ch=getc(stdin))!='\n')
{
Expr[m]=ch;
m++;
}
l=m;
infix_to_postfix();
}
void push(char ch)
{
if(Top+1 >= SIZE)
{
printf("\nStack is full");
}
else
{
Top=Top+1;
Stack[Top] = ch;
}
}
void pop()
{
if (Top < 0)
{
printf("\n Stack is empty");
}
else
{
if(Top >=0)
{
if(Stack[Top]!='(')
printf("%c",Stack[Top]);
Top=Top-1;
}
}
}
void infix_to_postfix()
{
m=0;
while(m<l)
{
switch(Expr[m])
{
case '+' :
case '-' :
while(Stack[Top] =='-' || Stack[Top] =='+' ||Stack[Top] =='*' ||Stack[Top] =='/' ||
Stack[Top] =='^' && Stack[Top] !='(')
pop();
push(Expr[m]);
++m;
break;
case '/' :
case '*' :
while(Stack[Top] =='*' ||Stack[Top] =='/' ||Stack[Top] =='^' && Stack[Top] !='(')
pop();
push(Expr[m]);
++m;
break;
case '^':
push(Expr[m]);
++m;
break;
case '(':
push(Expr[m]);
++m;
break;
case ')':
while(Stack[Top]!='(')
pop();
pop();
++m;
break;
case '.' :
while (Top >= 0)
pop();
exit(0);
default :
if(isalpha(Expr[m]))
{
printf("%c",Expr[m]);
++m;
break;
}
else
{
printf("\n Some error");
exit(0);
}
}
}
}
Sample Output
*****312419104152*****
Program to covert infix expression into postfix expression:
Enter your expression & to quit enter fullstop(.)
A+B
AB+
Output:
Result:
Thus the above c program to implement stack and use it to convert infix to postfix expression
is executed successfully.
Ex No: 9 APPLICATION OF QUEUE ADT’ S
Aim:
To write a C program to implement List using Linked List.
Algorithm:
Step 1:- Include all the header files which are used in the
Step 2:- Declare all the functions used in List implementation.
Step 3:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 4:- Finally display the result generated after the operations.
Program
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int player_id;
struct node *next;
};
struct node *start, *ptr, *new_node;
int main()
{
printf("*****312419104152*****");
int n, k, i, count;
clrscr();
printf("\n Enter the number of players : ");
scanf("%d", &n);
printf("\n Enter the value of k (every kth player gets eliminated): ");
scanf("%d", &k);
start = malloc(sizeof(struct node));
start–>player_id = 1;
ptr = start;
for (i = 2; i <= n; i++)
{
new_node = malloc(sizeof(struct node));
ptr–>next = new_node;
new_node–>player_id = i;
new_node–>next=start;
ptr=new_node;
}
for (count = n; count > 1; count––)
{
for (i = 0; i < k – 1; ++i)
ptr = ptr–>next;
ptr–>next = ptr–>next–>next;
}
printf("\n The Winner is Player %d", ptr–>player_id); getche();
return 0;
}
Sample Output:
*****312419104152*****
Enter the number of players : 5
Enter the value of k (every kth player gets eliminated) : 2
The winner is player 3
Output:

Result:
Thus, the above c program to implement Applications of Queue is executed successfully.
Ex No: 10 IMPLEMENTATION OF BINARY TREES AND ITS OPERATIONS
Aim:
To write a C program to implement List using Linked List.
Algorithm:
Step 1:- Include all the header files which are used in the
Step 2:- Declare all the functions used in List implementation.
Step 3:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 4:- Finally display the result generated after the operations.
Program
#include<stdio.h>
#include<stdlib.h>
struct tree {
int info;
struct tree *left;
struct tree *right;
};
struct tree *insert(struct tree *,int);
void inorder(struct tree *);
void postorder(struct tree *);
void preorder(struct tree *);
struct tree *delet(struct tree *,int);
struct tree *search(struct tree *);
int main(void)
{
printf("*****312419104152*****");
struct tree *root;
int choice, item,item_no;
root = NULL;
do {
do {
printf("\n \t 1. Insert in Binary Tree ");
printf("\n\t 2. Delete from Binary Tree ");
printf("\n\t 3. Inorder traversal of Binary tree");
printf("\n\t 4. Postorder traversal of Binary tree");
printf("\n\t 5. Preorder traversal of Binary tree");
printf("\n\t 6. Search and replace ");
printf("\n\t 7. Exit ");
printf("\n\t Enter choice : ");
scanf(" %d",&choice);
if(choice<1 || choice>6)
printf("\n Invalid choice - try again");
}
while (choice<1 || choice>6);
switch(choice) {
case 1:
printf("\n Enter new element: ");
scanf("%d", &item);
root= insert(root,item);
printf("\n root is %d",root->info);
printf("\n Inorder traversal of binary tree is : ");
inorder(root);
break;
case 2:
printf("\n Enter the element to be deleted : ");
scanf(" %d",&item_no);
root=delet(root,item_no);
inorder(root);
break;
case 3:
printf("\n Inorder traversal of binary tree is : ");
inorder(root);
break;
case 4:
printf("\n Postorder traversal of binary tree is : ");
postorder(root);
break;
case 5:
printf("\n Preorder traversal of binary tree is : ");
preorder(root);
break;
/*case 6:
printf("\n Search and replace operation in binary tree ");
root=search(root);
break;*/
default:
printf("\n End of program ");
}
/* end of switch */
}
while(choice !=6);
return(0);
}
struct tree *insert(struct tree *root, int x) {
if(!root) {
root=(struct tree*)malloc(sizeof(struct tree));
root->info = x;
root->left = NULL;
root->right = NULL;
return(root);
}
if(root->info > x)
root->left = insert(root->left,x); else {
if(root->info < x)
root->right = insert(root->right,x);
}
return(root);
}
void inorder(struct tree *root) {
if(root != NULL) {
inorder(root->left);
printf(" %d",root->info);
inorder(root->right);
}
return;
}
void postorder(struct tree *root) {
if(root != NULL) {
postorder(root->left);
postorder(root->right);
printf(" %d",root->info);
}
return;
}
void preorder(struct tree *root) {
if(root != NULL) {
printf(" %d",root->info);
preorder(root->left);
preorder(root->right);
}
return;
}
/* FUNCTION TO DELETE A NODE FROM A BINARY TREE */
struct tree *delet(struct tree *ptr,int x) {
struct tree *p1,*p2;
if(!ptr) {
printf("\n Node not found ");
return(ptr);
} else {
if(ptr->info < x) {
ptr->right = delet(ptr->right,x);
/*return(ptr);*/
} else if (ptr->info >x) {
ptr->left=delet(ptr->left,x);
return ptr;
} else
/* no. 2 else */ {
if(ptr->info == x)
/* no. 2 if */ {
if(ptr->left == ptr->right)
/*i.e., a leaf node*/ {
free(ptr);
return(NULL);
} else if(ptr->left==NULL)
/* a right subtree */ {
p1=ptr->right;
free(ptr);
return p1;
} else if(ptr->right==NULL)
/* a left subtree */ {
p1=ptr->left;
free(ptr);
return p1;
} else {
p1=ptr->right;
p2=ptr->right;
while(p1->left != NULL)
p1=p1->left;
p1->left=ptr->left;
free(ptr);
return p2;
}
}
/*end of no. 2 if */
}
/* end of no. 2 else */
/* check which path to search for a given no. */
}
return(ptr);
}
Sample Output:
*****312419104152*****
1. Insert in Binary Tree
2. Delete from Binary Tree
3. Inorder traversal of Binary tree
4. Postorder traversal of Binary tree
5. Preorder traversal of Binary tree
6. Search and replace
7. Exit
Enter choice : 1
Enter new element: 10
root is 10
Inorder traversal of binary tree is : 10
1. Insert in Binary Tree
2. Delete from Binary Tree
3. Inorder traversal of Binary tree
4. Postorder traversal of Binary tree
5. Preorder traversal of Binary tree
6. Search and replace
7. Exit
Enter choice : 1
Enter new element: 20
root is 10
Inorder traversal of binary tree is : 10 20
1. Insert in Binary Tree
2. Delete from Binary Tree
3. Inorder traversal of Binary tree
4. Postorder traversal of Binary tree
5. Preorder traversal of Binary tree
6. Search and replace
7. Exit
Enter choice : 1
Enter new element: 30
root is 10
Inorder traversal of binary tree is : 10 20 30
1. Insert in Binary Tree
2. Delete from Binary Tree
3. Inorder traversal of Binary tree
4. Postorder traversal of Binary tree
5. Preorder traversal of Binary tree
6. Search and replace
7. Exit
Enter choice : 1
Enter new element: 40
root is 10
Inorder traversal of binary tree is : 10 20 30 40
1. Insert in Binary Tree
2. Delete from Binary Tree
3. Inorder traversal of Binary tree
4. Postorder traversal of Binary tree
5. Preorder traversal of Binary tree
6. Search and replace
7. Exit
Enter choice : 1
Enter new element: 50
root is 10
Inorder traversal of binary tree is : 10 20 30 40 50
1. Insert in Binary Tree
2. Delete from Binary Tree
3. Inorder traversal of Binary tree
4. Postorder traversal of Binary tree
5. Preorder traversal of Binary tree
6. Search and replace
7. Exit
Enter choice : 1
Enter new element: 60
root is 10
Inorder traversal of binary tree is : 10 20 30 40 50 60
1. Insert in Binary Tree
2. 2. Delete from Binary Tree
3. 3. Inorder traversal of Binary tree
4. 4. Postorder traversal of Binary tree
5. 5. Preorder traversal of Binary tree
6. 6. Search and replace
7. 7. Exit
8. Enter choice : 2
9. Enter the element to be deleted : 40
10. 10 20 30 50 60
11. 1. Insert in Binary Tree
12. 2. Delete from Binary Tree
13. 3. Inorder traversal of Binary tree
14. 4. Postorder traversal of Binary tree
15. 5. Preorder traversal of Binary tree
16. 6. Search and replace
17. 7. Exit
18. Enter choice : 3
19. Inorder traversal of binary tree is : 10 20 30 50 60
20. 1. Insert in Binary Tree
21. 2. Delete from Binary Tree
22. 3. Inorder traversal of Binary tree
23. 4. Postorder traversal of Binary tree
24. 5. Preorder traversal of Binary tree
25. 6. Search and replace
26. 7. Exit
27. Enter choice : 4
28. Postorder traversal of binary tree is : 60 50 30 20 10
29. 1. Insert in Binary Tree
30. 2. Delete from Binary Tree
31. 3. Inorder traversal of Binary tree
32. 4. Postorder traversal of Binary tree
33. 5. Preorder traversal of Binary tree
34. 6. Search and replace
35. 7. Exit
36. Enter choice : 5
37. Preorder traversal of binary tree is : 10 20 30 50 60
38. 1. Insert in Binary Tree
39. 2. Delete from Binary Tree
40. 3. Inorder traversal of Binary tree
41. 4. Postorder traversal of Binary tree
5. Preorder traversal of Binary tree
6. Search and replace
7. Exit
Enter choice : 6
End of program
Output:

Result:
1. Thus the above c program to implement Binary Tree is executed successfully.
Ex.No: 11 IMPLEMENTATION OF BINARY SEARCH TREE
Aim
To write a C program to implement Binary Search Tree using C.
Algorithm:
Step 1:- Initialize the Structure with value and pointers for left- right node.
Step 2:- Three (preorder, postorder and inorder) operations for depth-first traversal of a tree.
Step 2.1 :- Preorder: Visit a node before traversing it’s sub-trees.
Step 2.2:- Inorder: Visit a node after traversing it’s left subtree but before traversing it’s right
sub-tree.
Step 2.3:- Postorder: Visit a node after traversing all of it’s subtrees.
Step 3:- To find search smallest and largest element compare and go to the left sub tree to find
the min element .
Step 4:- Write a Function to search the appropriate position to insert the new node .
Step 5:- Write a Function to delete a node given by user.
Step 6:- Terminate the execution.
Program:
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
printf("*****312419104152*****");
int ch;
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
/* To insert a node in the tree */
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
/* To create a node */
void create()
{
int data;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;
temp->l = temp->r = NULL;
}
/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t)
{
if ((temp->value > t->value) && (t->r != NULL))
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL))
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
/* recursive function to perform inorder traversal of tree */
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}
/* To check for the deleted node */
void delete()
{
int data;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
/* To find the preorder traversal */
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
/* To find the postorder traversal */
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}
/* Search for the appropriate position to insert the new node */
void search1(struct btnode *t, int data)
{
if ((data>t->value))
{
t1 = t;
search1(t->r, data);
}
else if ((data < t->value))
{
t1 = t;
search1(t->l, data);
}
else if ((data==t->value))
{
delete1(t);
}
}
/* To delete a node */
void delete1(struct btnode *t)
{
int k;
/* To delete leaf node */
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
/* To delete node having one left hand child */
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;
}
else if (t1->l == t)
{
t1->l = t->l;
}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
/* To delete node having right hand child */
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}
/* To delete node having two child */
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
}
}
/* To find the smallest element in the right sub tree */
int smallest(struct btnode *t)
{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value);
}
/* To find the largest element in the left sub tree */
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value);
}
Sample Output:
OPERATIONS ---
1 - Insert an element into tree
2 - Delete an element from the tree
3 - Inorder Traversal
4 - Preorder Traversal
5 - Postorder Traversal
6 - Exit
Enter your choice :1
Enter data of node to be inserted : 10
Enter your choice : 1
Enter data of node to be inserted : 20
Enter your choice :1
Enter data of node to be inserted : 30
Enter your choice : 1
Enter data of node to be inserted : 40
Enter your choice : 1
Enter data of node to be inserted : 50
Enter your choice : 2
Enter the data to be deleted : 20
Enter your choice : 3
10 30 40 50
Enter your choice : 4
10 30 40 50
Enter your choice : 5
50 40 30 10
Enter your choice : 6
Output:
Result:
Thus the above c program to implement Binary Search Tree is executed successfully.
Ex.No:12 IMPLEMENTATION OF AVL TREE
Aim:
To write a C program to implement AVL Tree.
Algorithm:
Step 1:- Initialize the Structure with balance and pointer for left, right node .
Step 2:- Write a Function for insertion operation.
Step 3:- Read the insert element from the user
Step 4:- Insert the new element into the tree using Binary Search Tree insertion logic.
Step 5:- After insertion, check the Balance Factor of every node.
Step 6:- If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
Step 7:- If the Balance Factor of any node is other than 0 or 1 or -1 then tree is said to be
imbalanced. Step 8:- Then perform the suitable single(LL or RR) or double(LR or RL) Rotation
to make it balanced. And then terminate the program.
Program:
#include<stdio.h>
#include<malloc.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};
struct node *insert (int , struct node *, int *);
struct node* search(struct node *,int);
void display();
void insert();
void main()
{
printf("*****312419104152*****");
bool ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
while(1)
{
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
if( search(root,info) == NULL )
root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
struct node* search(struct node *ptr,int info)
{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}/*End of search()*/
struct node *insert (int info, struct node *pptr, int *ht_inc)
{
struct node *aptr;
struct node *bptr;
if(pptr==NULL)
{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}
if(info < pptr->info)
{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1: /* Right heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = 1;
break;
case 1: /* Left heavy */
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf("Left to Left Rotation\n");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;
if(bptr->balance == 1 )
pptr->balance = -1;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}/*End of switch */
}/*End of if */
}/*End of if*/
if(info > pptr->info)
{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1: /* Left heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = -1;
break;
case -1: /* Right heavy */
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf("Right to Right Rotation\n");
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Right to Left Rotation\n");
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;
if(bptr->balance == -1)
pptr->balance = 1;
else
pptr->balance = 0;
if(bptr->balance == 1)
aptr->balance = -1;
else
aptr->balance = 0;
bptr->balance=0;
pptr = bptr;
}/*End of else*/
*ht_inc = FALSE;
}/*End of switch */
}/*End of if*/
}/*End of if*/
return(pptr);
}/*End of insert()*/
void display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
void inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
Sample Output:
1.Insert
2.Display
3.Quit
Enter your choice :1
Enter the value to be inserted : 10
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 20
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 30
Right to Right Rotation
1.Insert
2.Display
3.Quit
Enter your choice :1
Enter the value to be inserted : 40
1.Insert
2.Display
3.Quit
Enter your choice :1
Enter the value to be inserted : 50
Right to Right Rotation
1.Insert
2.Display
3.Quit
Enter your choice :1
Enter the value to be inserted :60
Right to Right Rotation
1.Insert
2.Display
3.Quit
Enter your choice : Tree is :
60
50
40
30
20
10
Inorder Traversal is: 10 20 30 40 50 60

Result:
Thus the above c program to implement AVL Tree is executed successfully.
Ex.No:13 IMPLEMENTATION OF HEAP USING PRIORITY QUEUE
Aim:
To write a C program to implement heap using priority Queue
Algorithm:
Step 1:- Initialize the Structure with priority, information and pointer to next node .
Step 2:- Write a Function for insertion, deletion and display operation.
Step 3:- Read the insert element and its priority from the user.
Step 4:- Insert the element according to the priority in appropriate node.
Step 5:- Delete the queue using del function, display “QUEUE UNDERFLOW” if null.
Step 6:- Display the queue with separate function and terminate the execution.
Program:
#include<stdio.h>
#include<malloc.h>
void insert();
void del();
void display();
struct node
{
int priority;
int info;
struct node *next;
}*start=NULL,*q,*temp,*new;
typedef struct node N;
int main()
{
printf("*****312419104152*****");
int ch;
do
{
printf("\n[1] INSERTION\t[2] DELETION\t[3] DISPLAY [4] EXIT\t:");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:
break;
}
}
while(ch<4);
}
void insert()
{
int item,itprio;
new=(N*)malloc(sizeof(N));
printf("ENTER THE ELT.TO BE INSERTED :\t");
scanf("%d",&item);
printf("ENTER ITS PRIORITY :\t");
scanf("%d",&itprio);
new->info=item;
new->priority=itprio;
new->next=NULL;
if(start==NULL )
{
//new->next=start;
start=new;
}
else if(start!=NULL&&itprio<=start->priority)
{
new->next=start;
start=new;
}
else
{
q=start;
while(q->next != NULL && q->next->priority<=itprio)
{
q=q->next;
}
new->next=q->next;
q->next=new;
}
}
void del()
{
if(start==NULL)
{
printf("\nQUEUE UNDERFLOW\n");
}
else
{
new=start;
printf("\nDELETED ITEM IS %d\n",new->info);
start=start->next;
//free(start);
}
}
void display()
{
temp=start;
if(start==NULL)
printf("QUEUE IS EMPTY\n");
else
{
printf("QUEUE IS:\n");
if(temp!=NULL)
for(temp=start;temp!=NULL;temp=temp->next)
{
printf("\n%d priority =%d\n",temp->info,temp->priority);
//temp=temp->next;
}
}
}
Sample Output:
*****312419104152*****
[1] INSERTION [2] DELETION [3] DISPLAY [4] EXIT
ENTER THE ELT.TO BE INSERTED :10
ENTER ITS PRIORITY : 1
[1] INSERTION [2] DELETION [3] DISPLAY [4] EXIT
ENTER THE ELT.TO BE INSERTED :20
ENTER ITS PRIORITY :3
[1] INSERTION [2] DELETION [3] DISPLAY [4] EXIT
ENTER THE ELT.TO BE INSERTED :30
ENTER ITS PRIORITY : 2
[1] INSERTION [2] DELETION [3] DISPLAY [4] EXIT
ENTER THE ELT.TO BE INSERTED :40
ENTER ITS PRIORITY :4
[1] INSERTION [2] DELETION [3] DISPLAY [4] EXIT
DELETED ITEM IS 10
[1] INSERTION [2] DELETION [3] DISPLAY [4] EXIT
QUEUE IS: 20 30 40
Output:
Result:
Thus the above c program to implement heap using priority Queue is executed successfully.
Ex.No: 14 GRAPH REPRESENTATION AND TRAVERSAL ALGORITHMS
Aim:
To write the C program to implement BFS and DFS on a graph represented using adjacency list
Algorithm:
Step 1:- Initialize the Structure with data, vertex and pointer to next node .
Step 2:- Write a readgraph function to create adjacency list.
Step 3:- In BFS, insert all unvisited, adjacent vertices of v into queue.
Step 4:-In readgraph, display the graph with search result with enqueuer and dequeue.
Step 5:- Create DFS function with backtracking property.
Step 6:- Terminate the execution.
Program:
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef struct Q
{
int data[MAX];
int R,F;
}Q;
typedef struct node
{
struct node *next;
int vertex;
}node;
void enqueue(Q *,int);
int dequque(Q *);
int empty(Q *);
int full(Q *);
void BFS(int);
void readgraph();
void insert(int vi,int vj); //insert an edge (vi,vj)in adj.list
void DFS(int i);
int visited[MAX];
node *G[20]; //heads of the linked list
int n; // no of nodes
void main()
{
printf("*****312419104152*****");
int i,op;
do
{
printf("\n\n1)Create\n2)BFS\n3)DFS\n4)Quit");
printf("\nEnter Your Choice: ");
scanf("%d",&op);
switch(op)
{
case 1:readgraph();break;
case 2:printf("\nStarting Node No. : ");
scanf("%d",&i);
BFS(i);break;
case 3:for(i=0;i<n;i++)
visited[i]=0;
printf("\nStarting Node No. : ");
scanf("%d",&i);
DFS(i);break;
}
}while(op!=4);
}
void BFS(int v)
{
int w,i,visited[MAX];
Q q;
node *p;
q.R=q.F=-1; //initialize
for(i=0;i<n;i++)
visited[i]=0;
enqueue(&q,v);
printf("\nVisit\t%d",v);
visited[v]=1;
while(!empty(&q))
{
v=dequeue(&q);
for(p=G[v];p!=NULL;p=p->next)
{
w=p->vertex;
if(visited[w]==0)
{
enqueue(&q,w);
visited[w]=1;
printf("\nvisit\t%d",w);
}
}
}
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
}
int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}
int full(Q *P)
{
if(P->R==MAX-1)
return(1);
return(0);
}
void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=P->R+1;
P->data[P->R]=x;
}
}
int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=P->F+1;
return(x);
}
void readgraph()
{
int i,vi,vj,no_of_edges;
printf("\nEnter no. of vertices :");
scanf("%d",&n);
//initialise G[] with NULL
for(i=0;i<n;i++)
G[i]=NULL;
//read edges and insert them in G[]
printf("\nEnter no of edges :");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("\nEnter an edge (u,v) :");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
insert(vj,vi);
}
}
void insert(int vi,int vj)
{
node *p,*q;
//acquire memory for the new node
q=(node *)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(G[vi]==NULL)
G[vi]=q;
else {
// go to the end of linked list
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
Sample Output:
*****312419104152*****
1)Create
2)BFS
3)DFS
4)Quit
Enter Your Choice: 1
Enter no. of vertices :4
Enter no of edges :5
Enter an edge (u,v) :1 2
Enter an edge (u,v) :2 3
Enter an edge (u,v) :3 4
Enter an edge (u,v) :4 1
Enter an edge (u,v) :1 3
1)Create
2)BFS
3)DFS
4)Quit
Enter Your Choice: 2
Starting Node No. : 1
Visit 1
visit 2
visit 3
visit 4
1)Create
2)BFS
3)DFS
4)Quit
Enter Your Choice: 3
Starting Node No. :2
2
1
4
3
1)Create
2)BFS
3)DFS
4)Quit
Enter Your Choice: 2
Starting Node No. : 4
Visit 4
visit 3
visit 1
visit 2
1)Create
2)BFS
3)DFS
4)Quit
Enter Your Choice: 4
Output:
Result:
Thus the above c program to implement BFS and DFS on a graph represented using adjacency
list is executed successfully.
Ex No: 15 APPLICATIONS OF GRAPHS
Aim:
To write a C program to implement minimum spanning tree using Prim’s algorithm
Algorithm:
Step 1:- Include all the header files which are used in the
Step 2:- Declare all the functions used in List implementation.
Step 3:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 4:- Finally display the result generated after the operations.
Program:
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]= {0},min,mincost=0,cost[10][10];
void main() {
clrscr();
printf("*****312419104152*****");
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n) {
for (i=1,min=999;i<=n;i++)
for (j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0) {
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0) {
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
Sample Output:
Enter the number of nodes: 5
Enter the adjacency matrix:
10111
01010
01101
11010
Edge 1:(1 2) cost:1
Edge 2:(1 5) cost:1
Edge 3:(2 3) cost:1
Edge 4:(2 4) cost:1
Minimun cost=4
Output:

Result:
Thus the above c program to implement Applications on Graph is executed successfully.
Ex.No: 16 IMPLEMENTATION OF LINEAR SEARCHING
Aim
To write a C program to implement linear search.
Algorithm
Step1:- Get the total number of elements and the elements.
Step2:- Get the key to be searched.
Step3:- Each element in the array is checked with the key.
Step4:- If the element is equal to key, the element is found and displays the location of the key.
Step5:-Otherwise, the element is not found.
Program
#include<stdio.h>
void main()
{
printf("*****312419104152*****");
int array[100],search,c,n;
printf("Enter the Number of elements\n");
scanf("%d",&n);
printf("Enter the Elements\n");
for (c=0; c<n;c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for(c=0;c<n;c++)
{
if(array[c] == search)
{
printf("%d is present at location %d.\n",search,c+1);
break;
}
}
if(c==n)
{
printf("%d is not present in array.\n",search);
}
//return 0;
}
Sample Output
Enter the Number of elements 4
Enter the Elements 4 3 2 1
Enter the number to search 2
2 is present at location 3.
Output:
Result:
Thus the above c program to implement linear search is executed successful.
Ex.No: 17 IMPLEMENTATION OF BINARY SEARCHING
Aim
To write a C program to implement binary search.
Algorithm
Step1:-Get the number of elements, elements and the key to be searched.
Step2:-Sort the list in non-decreasing order.
Step 3:-Initialize low = 0, high = number_of_elements and mid = floor((low+high)/2).
(i) If the middle element (mid) is less than key then key has to present in range [mid+1 ,
high], so low=mid+1, high remains unchanged and mid is adjusted accordingly
(ii) If middle element is the key, then the element is found.
(iii) If the middle element is greater than key then key has to be present in the range [low,mid-
1], so high=mid-1, low remains unchanged and mid is adjusted accordingly.
Step 4:-Repeat the step until low is less than or equal to high or the position of the ‘input key’
has been found.
Program
#include<stdio.h>
int BinarySearch(int *array, int n, int key)
{
int low = 0, high = n-1, mid;
while(low <= high)
{
mid = (low + high)/2;
if(array[mid] < key)
{
low = mid + 1;
}
else if(array[mid] == key)
{
return mid;
}
else if(array[mid] > key)
{
high = mid-1;
}
}
return -1;
}
int main()
{
printf("*****312419104152*****");
int n=5;
int array[n];
int i,key,index;
printf("\nEnter the 5 elements");
//scanf("%d",&n);
printf("\nEnter the elements");
for(i = 0;i < n;i++)
{
scanf("%d",&array[i]);
}
for(i = 1;i < n;i++)
{
if(array[i] < array[i - 1])
{
printf("Given input is not sorted\n");
return 0;
}
}
printf("Enter the key to be searched");
scanf("%d",&key);
index = BinarySearch(array,n,key);
if(index==-1)
{
printf("Element not found\n");
}
else
{
printf("Element is at index %d\n",index);
}
return 0;
}
Sample Output
Enter the 5 elements
Enter the elements10
102
104
108
101
Enter the key to be searched10
Element is at index 0
Output:
Result:
Thus the above c program to implement binary search is executed successfully.
Ex.No: 18 IMPLEMENTATION OF BUBBLE SORTING ALGORITHM
Aim
To write a C program to implement Bubble Sorting Algorithm.
Algorithm
Algorithm:
Step 1:- Include all the header files which are used in the
Step 2:- Declare all the functions used in List implementation.
Step 3:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 4:- Finally display the result generated after the operations.
Program
#include <stdio.h>
int main()
{
printf("*****312419104152*****");
int data[100],i,n,step,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1]) /* To sort in descending order, change > to < in this line. */
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
SAMPLE OUTPUT
Enter the number of elements to be sorted: 6
1. Enter element: 12
2. Enter element: 3
3. Enter element: 0
4. Enter element: -3
5. Enter element: 1
6. Enter element: -9
In ascending order: -9 -3 0 1 3 13
Output:

Result:
Thus the above c program to implement Bubble Sort is executed successfully.
Ex.No: 19 IMPLEMENTATION OF SELECTION SORTING ALGORITHM
Aim
To write a C program to implement Selection Sorting Algorithm.
Algorithm:
Step 1:- Include all the header files which are used in the
Step 2:- Declare all the functions used in List implementation.
Step 3:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 4:- Finally display the result generated after the operations.
Program
#include <stdio.h>
int main()
{
printf("*****312419104152*****");
int data[100],i,n,steps,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)
{
if(data[steps]>data[i])
/* To sort in descending order, change > to <. */
{
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
Sample Output
Enter the number of elements to be sorted: 5
1. Enter element: 12
2. Enter element: 1
3. Enter element: 23
4. Enter element: 2
5. Enter element: 0
In ascending order: 0 1 2 12 23

Output:

Result:
Thus the above c program to implement Selection Sort is executed successfully.
Ex.No: 20 IMPLEMENTATION OF INSERTION SORTING ALGORITHM
Aim
To write a C program to implement Insertion Sorting Algorithm.
Algorithm:
Step 1:- Include all the header files which are used in the
Step 2:- Declare all the functions used in List implementation.
Step 3:- In main method display menu with list of operations and make suitable function calls
to
perform operation selected by the user on the stack.
Step 4:- Finally display the result generated after the operations.
Program
#include<stdio.h>
int main()
{
printf("*****312419104152*****");
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
// This loop would store the input numbers in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);
// Implementation of insertion sort algorithm
for(i=1;i<count;i++)
{
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0))
{
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Sample Output:
How many numbers u are going to enter?: 5
Enter 5 elements: 50 30 20 10 40
Order of Sorted elements: 10 20 30 40 50
Output:
Result:
Thus the above c program to implement Insertion Sort is executed successfully.
Ex.No: 21 IMPLEMENTATION OF HASHING USING LINEAR AND QUATRATIC PROBING
Aim:
To write a C program to implement Hashing using Linear and Quatratic Probing
Algorithm:
Step1:- For linear probing, calculate a hash code from the key
Step 2:- Access that hash element

• If the hash element is empty, add straight away

• If not, probe through subsequent elements (looping back if necessary), trying to find a
free place

• If not, probe through subsequent elements (looping back if necessary), trying to find a
free place

• If no free place is found, the add will fail

Step 3:- Quadratic probing operates by taking the original hash value
Step 4:- Then add successive values of an arbitrary quadratic polynomial to the starting value.
Step 5:- Terminate the execution.
Program:
#include <stdio.h>
int tsize;
int hasht(int key)
{
int i ;
i = key%tsize ;
return i;
}
//-------LINEAR PROBING-------
int rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}
//-------QUADRATIC PROBING-------
int rehashq(int key, int j)
{
int i ;
i = (key+(j*j))%tsize ;
return i ;
}
void main()
{
printf("*****312419104152*****");
int key,arr[20],hash[20],i,n,s,op,j,k ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);
printf ("\nEnter the number of elements: ");
scanf ("%d",&n);
for (i=0;i<tsize;i++)
hash[i]=-1 ;
printf ("Enter Elements: ");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1:
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
{
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashl(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
case 2:
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
{
j=1;
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashq(i,j);
j++ ;
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
}
}while(op!=3);
}
Sample Output:
Enter the size of the hash table: 10
Enter the number of elements: 8
Enter Elements: 72 27 36 24 63 81 92 101
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 1
The elements in the array are:
Element at position 0: -1
Element at position 1: 81
Element at position 2: 72
Element at position 3: 63
Element at position 4: 24
Element at position 5: 92
Element at position 6: 36
Element at position 7: 27
Element at position 8: 101
Element at position 9: -1
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 2
The elements in the array are:
Element at position 0: -1
Element at position 1: 81
Element at position 2: 72
Element at position 3: 63
Element at position 4: 24
Element at position 5: 101
Element at position 6: 36
Element at position 7: 27
Element at position 8: 92
Element at position 9: -1
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 3
Output:
Result:
Thus the above c program to implement Hashing using Linear and Quatratic Probing is
executed successfully.

You might also like