You are on page 1of 77

EX.

NO:1a ARRAY IMPLEMENTATION OF LIST


DATE:

AIM
To write a C program to implement the concept of list using array.

ALGORITHM

Step-1: Read the array size.


Step-2: Get the values for the array.
Step-3: To insert an element at the given index position, move the elements after the given
position one position right.
Step-4: To delete an element at the given index position, move the elements before the given
position one position left.
Step-5: Display the array.
PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int ch,a[10],x,pos,n,i,flag=0,k;
clrscr();
printf("Enter the no of elements");
scanf("%d",&n);
do
{
printf("\n\n 1.Creation 2.Insert First 3.Insert Middle ");
printf("\n 4.Insert Last 5.Delete First 6.Delete Middle");
printf("\n 7.Delete Last 8.Search 9.Traverse ");
printf("\n\n\n Enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
break;
case 2:
printf("Enter the element");
scanf("%d",&x);
for(i=n-1; i>=0; i--)
{
a[i+1]=a[i];
}
a[0]=x;
n=n+1;
break;
case 3:
printf("Enter the element");
scanf("%d",&x);
printf("Enter the position value");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=x;
n=n+1;
break;
case 4:
printf("Enter the element");
scanf("%d",&x);
a[n]=x;
n=n+1;
break;
case 5:
for(i=0;i<n-1;i++)
{
a[i]=a[i+1];
}
n--;
break;
case 6:
printf("Enter the pos value");
scanf("%d",&pos);
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n--;
break;
case 7:
n=n-1;
break;
case 8:
printf("Enter the element to be searched");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag==1)
printf("Element is found");
else
printf("Element is not found");
break;
case 9:
for(i=0;i<n;i++)
printf("%d",a[i]);
break;
}// END OF SWITCH
printf("\ndo you want to continue press1 otherwise 0\n");
scanf("\n%d",&k);
}while(k!=0); // END OF DO-WHILE
getch();
}

OUTPUT

Enter the no of elements5


1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse

Enter your choice: 1


Enter the elements
2
3
4
5
6
do you want to continue press1 otherwise 0
1

1.Creation 2.Insert First 3.Insert Middle


4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse

Enter your choice: 2


Enter the element7
do you want to continue press1 otherwise 0
1

1.Creation 2.Insert First 3.Insert Middle


4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 9
723456
do you want to continue press1 otherwise 0
3
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 3
Enter the element9
Enter the position value3
do you want to continue press1 otherwise 0
1

1.Creation 2.Insert First 3.Insert Middle


4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 9
7239456
do you want to continue press1 otherwise 0
4
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 4
Enter the element7
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 9
72394567
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 5
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 9
2394567
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 6
Enter the pos value3
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 9
239567
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 7
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 9
23956
do you want to continue press1 otherwise 0
1
1.Creation 2.Insert First 3.Insert Middle
4.Insert Last 5.Delete First 6.Delete Middle
7.Delete Last 8.Search 9.Traverse
Enter your choice: 8
Enter the element to be searched5
Element is found
do you want to continue press1 otherwise 0
0

RESULT
Thus the C Program for the Array Implementation of List was executed.
EX. NO:1b IMPLEMENTATION OF SINGLY LINKED LIST
DATE:

AIM
To write a C program to implement the operations of singly linked list.

ALGORITHM
Step-1: Create a structure called node with data and next fields.
Creation
Step-2: Allocate memory dynamically for a node and declare it as a header head.
Step-3: Allocate memory for a node temp and set. temp-
>data=num, temp->next=NULL
Step-4: Move a pointer p to the newly created node.
Step-5: Repeat the steps(3) & (4) for all the elements.

Insertion
Step-6: To insert an element x at the front
temp->next=head; head=temp;
Step-7: To insert an element x at the end traverse a pointer p till the end of the list and assign
the last node‟s next value to temp.
Step-8: To insert an element x at a position, traverse a pointer p at position-1.
temp->next=a->next; a->next=temp;

Deletion
Step-9: To delete an element x at a given position, move the pointer p to the previous node and
make a link from the node p to the node next to next of p.
Step-10: To delete an element x, traverse from head node to find the previous node of element x,
make a link from the node p to the node next to next of p.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define null 0
struct node
{
int data;
struct node *next;
}*head=null;
int num,q,i=2;
void create()
{
struct node *temp,*temp1;
head=malloc(sizeof(struct node));
printf("Node:1\n");
scanf("%d",&num);
head->data=num;
head->next=null;
temp=head;
do
{
printf("Node:%d",i);
scanf("%d",&num);
temp->next=malloc(sizeof(struct node));
temp=temp->next;
temp->data=num;

printf("Continue?\n");
scanf("%d",&q);
i++;
}
while(q!=0);
temp->next=NULL;
}
void insert_at_first()
{
struct node *temp;
temp=malloc(sizeof(struct node));
printf("Enter the data to be inserted in that node\n");
scanf("%d",&num);
temp->data=num;
temp->next=head;
head=temp;
}
void insert_at_last()
{
struct node *temp;
temp=head;
printf("Enter the data to be inserted in that node\n");
scanf("%d",&num);
while(temp->next!=null)
temp=temp->next;
temp->next=malloc(sizeof(struct node));
temp->data=num;
temp->next=null;
}
void insert_at_middle()
{
struct node *a,*temp;
a=head;
printf("Enter the position\n");
scanf("%d",&q);
printf("Enter the number to be inserted in that node\n");
scanf("%d",&num);
while(a->data!=q)
a=a->next;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->next=a->next;
a->next=temp;
}
void delete_at_first()
{
struct node *temp;
temp=head;
head=head->next;
free(temp);
}
void delete_at_last()
{
struct node *temp,*a;
temp=head;
while(temp->next!=null)
{
a=temp;
temp=temp->next; }
a->next=null;
free(temp);
}
void delete_at_middle()
{
struct node *temp,*a;
temp=head;
printf("Enter the position\n");
scanf("%d",&q);
while(temp->data!=q)
{
a=temp;
temp=temp->next;
}
a->next=temp->next;
free(temp);
}
void display()
{
struct node *temp;
temp=head;
printf("head->");
while(temp!=null)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("null");
}
void main()
{
int ch,c;
clrscr();
create();
do
{
printf("What operation do you want to do? \n");
printf("1.Insert at first\n2.Insert at last\n3.Insert at middle\n4.delete at first\n5.delete at
last\n6.delete at middle\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_at_first();
break;
case 2:
insert_at_last();
break;
case 3:
insert_at_middle();
break;
case 4:
delete_at_first();
break;
case 5:
delete_at_last();
break;
case 6:
delete_at_middle();
break;
default:
printf("Wrong choice");
}
printf("The modified list is\n");
display();
printf("\ndo you want to continue?\n1.yes\n2.no\n");
scanf("%d",&c);
}
while(c!=0);
getch();
}

OUTPUT
Node:1
1
Node:22
Continue?
1
Node:33
Continue?
0
What operation do you want to do?
1.Insert at first
2.Insert at last
3.Insert at middle
4.delete at first
5.delete at last
6.delete at middle
1
Enter the data to be inserted in that node
4
The modified list is
head->4->1->2->3->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first
2.Insert at last
3.Insert at middle
4.delete at first
5.delete at last
6.delete at middle
2
Enter the data to be inserted in that node
5
The modified list is
head->4->1->2->3->5->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first
2.Insert at last
3.Insert at middle
4.delete at first
5.delete at last
6.delete at middle
4
The modified list is
head->1->2->3->5->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first
2.Insert at last
3.Insert at middle
4.delete at first
5.delete at last
6.delete at middle
5
The modified list is
head->1->2->3->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first
2.Insert at last
3.Insert at middle
4.delete at first
5.delete at last
6.delete at middle
6
Enter the position
2
The modified list is
head->1->3->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first
2.Insert at last
3.Insert at middle
4.delete at first
5.delete at last
6.delete at middle
3
Enter the position
2
Enter the number to be inserted in that node
6
head->1->6->3->null
do you want to continue?
1.yes
2.no
2

RESULT
Thus a single linked list has been created and executed successfully.
EX NO:1c LIST USING DOUBLY LINKED LISTS

DATE:

AIM
To write a c program to execute the doubly linked lists

ALGORITHM

Step 1: Create a structure LINKED_LIST with a data element and two pointers previous and
next of type LINKED_LIST and redefine the structure LINKED_LIST with NODE for easy
usage.

Step 2: Create a header node and last node.

Step 3: Accept the user choice Insert, Delete, Reverse and Display.

3.1 If the Choice is Insert


3.1.1 Read the data.
3.1.2 Find the position in the list for the new data.
3.1.3 Allocate space for new node and store new data in it.
3.1.4 Insert the new node in the correct position by updating pointers.
3.2 If the Choice is Delete
3.2.1 Read the data to be deleted from the list.
3.2.2 Remove the node containing data by updating the pointers.
3.3 If the Choice is Reverse
Reverse the list using last node and display the elements.
3.4 If the choice is Display
Traverse the list and display the elements.

Step 4: Stop the program


PROGRAM

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define null 0
struct dnode
{
int data;
struct dnode *prev;
struct dnode *next;
}*head=null;
int num,q,i=2;
void create()
{
struct dnode *temp,*temp1;
head=malloc(sizeof(struct dnode));
printf("Node:1\n");
scanf("%d",&num);
head->data=num;
head->prev=null;
head->next=null;
temp1=head;
do
{
printf("Node:%d",i);
scanf("%d",&num);
temp1->next=malloc(sizeof(struct dnode));
temp=temp1->next;
temp->data=num;
temp->prev=temp1;
temp1=temp1->next;
printf("Continue?\n");
scanf("%d",&q);
i++;
}
while(q!=0);
temp->next=null;}
void insert_at_first()
{
struct dnode *temp;
temp=malloc(sizeof(struct dnode));
printf("Enter the data to be inserted in that node\n");
scanf("%d",&num);
temp->data=num;
head->prev=temp;
temp->next=head;
temp->prev=null;
head=temp;
}
void insert_at_last()
{
struct dnode *temp;
temp=head;
printf("Enter the data to be inserted in that node\n");scanf("%d",&num);
while(temp->next!=null)
temp=temp->next;
temp->next=malloc(sizeof(struct dnode));
temp->next->data=num;
temp->next->prev=temp;
temp->next->next=null;
}
void insert_at_middle()
{
struct dnode *temp,*temp1;
temp=head;
printf("Enter the position\n");scanf("%d",&q);
printf("Enter the number to be inserted in that node\n");scanf("%d",&num);
while(temp->data!=q)
temp=temp->next;
temp1=malloc(sizeof(struct dnode));
temp1->data=num;
temp1->next=temp->next;
temp1->prev=temp;
temp->next=temp1;
temp1->next->prev=temp1;
}
void delete_at_first()
{
struct dnode *temp;
temp=head;
head=head->next;
free(temp);
head->prev=null;
}
void delete_at_last()
{
struct dnode *temp;
temp=head;
while(temp->next!=null)
temp=temp->next;
temp->prev->next=null;
free(temp);
}
void delete_at_middle()
{
struct dnode *temp,*temp1;
temp=head;
printf("Enter the position\n");scanf("%d",&q);
while(temp->data!=q)
temp=temp->next;
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
void display()
{
struct dnode *temp;
temp=head;
printf("head->");
while(temp!=null)
{
printf("%d->",temp->data); temp=temp->next;
}
printf("null");
}
void main()
{
int ch,c;
clrscr();
create();
do
{
printf("What operation do you want to do? \n");
printf("1.Insert at first\n2.Insert at last\n3.Insert at middle\n4.delete at first\n5.delete at
last\n6.delete at middle\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_at_first();
break;
case 2:
insert_at_last();
break;
case 3:
insert_at_middle();
break;
case 4:
delete_at_first();
break;
case 5:
delete_at_last();
break;
case 6:
delete_at_middle();
break;
default:
printf("Wrong choice");
}
printf("The modified list is\n");
display();
printf("\ndo you want to continue?\n1.yes\n2.no\n");
scanf("%d",&c);
}
while(c!=2);
getch();
}

OUTPUT

Node:1 1
Node:2 2
Continue?
1
Node:3 3
Continue?
1
Node:4 4
Continue?
1
Node:5 5
Continue?
0
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle
1
Enter the data to be inserted in that node
10
The modified list is
head->10->1->2->3->4->5->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle
2
Enter the data to be inserted in that node
19
The modified list is
head->10->1->2->3->4->5->19->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middl 4.delete at first 5.delete at last 6.delete at middle
3
Enter the position
3
Enter the number to be inserted in that node
30
The modified list is
head->10->1->2->3->30->4->5->19->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle
4
The modified list is
head->1->2->3->30->4->5->19->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle
5
The modified list is
head->1->2->3->30->4->5->null
do you want to continue?
1.yes
2.no
1
What operation do you want to do?
1.Insert at first 2.Insert at last 3.Insert at middle 4.delete at first 5.delete at last 6.delete at middle
6
Enter the position
30
The modified list is
head->1->2->3->4->5->null
do you want to continue?
1.yes
2.no

RESULT
Thus a doubly linked list has been created and executed successfully.
EX NO:2 APPLICATION OF LINKED LIST - POLYNOMIAL ADDITION

DATE :

AIM

To write a c program to execute polynomial addition.

ALGORITHM
Step 1: Define the node structure and create a header node for the first polynomial.
Step 2: Get first polynomial term’s coefficients and powers from the user and create a node and
store the coefficient and power.
Step 3: Link the above nodes to the linked list, for first polynomial and repeat the steps for the
second polynomial.
3.1 If the exponents of both the terms are equal then add the coefficients.
3.1.1 Create a node of resultant polynomial
3.1.2 Store the added coefficient and exponent in the node and link it to the resultant
linked list.
3.1.3 Update the pointers to point to next node in each polynomial.
3.2 If the exponent of first polynomial is greater than the second
3.2.1 Then copy the node to the resultant polynomial
3.2.2 Update the pointer of the first polynomial
3.3 If the exponent of second polynomial is greater than the first
3.3.1 Then copy the node to the resultant polynomial
3.3.2 Update the pointer of the second polynomial
Step 4: Repeat the steps 8-10 until end of first polynomial or second polynomial is reached.
Step 5: If end of first polynomial is not reached then copy the addition of remaining terms to the
resultant polynomial.
Step 6: Print the resultant polynomial.
PROGRAM

#include<stdio.h>
#include<conio.H>
#include<alloc.h>
#define null 0
struct polynode
{
int coeff;
int exp;
struct polynode *next;
}*poly1,*poly2,*poly3;
void createpoly1();
void createpoly2();
void polyadd();
void disp();
void main()
{
clrscr();
printf("\nto create poly1........");
createpoly1();
printf("\nto create poly2.........");
createpoly2();
printf("\npolyadd.......");
polyadd();
getch();
}
void createpoly1()
{
struct polynode *temp,*a;
int i,n,c,e;
printf("\nenter no of terms of 1st polynomial:");
scanf("%d",&n);
printf("\nenter the coeff:");
scanf("%d",&c);
printf("\nenter the expo:");
scanf("%d",&e);
poly1=malloc(sizeof(struct polynode));
poly1->coeff=c;
poly1->exp=e;
poly1->next=null;
temp=poly1;
for(i=1;i<n;i++)
{
printf("\nenter the coeff for next term:");
scanf("%d",&c);
printf("\nenter the expo for next term:");
scanf("%d",&e);
temp->next=malloc(sizeof(struct polynode));
temp=temp->next;
temp->coeff=c;
temp->exp=e;
}
temp->next=null;
a=poly1;
printf("\n1st polynomial:");
do
{
printf("%d(x^%d)+",a->coeff,a->exp);
a=a->next;
}while(a->next!=null);
printf("%d",a->coeff);
}
void createpoly2()
{
struct polynode *temp,*a;
int i,n,c,e;
printf("\nenter no of terms of 2nd polynomial:");
scanf("%d",&n);
printf("\nenter the coeff:");
scanf("%d",&c);
printf("\nenter the expo:");
scanf("%d",&e);
poly2=malloc(sizeof(struct polynode));
poly2->coeff=c;
poly2->exp=e;
poly2->next=null;
temp=poly2;
for(i=1;i<n;i++)
{
printf("\nenter the coeff for next term:");
scanf("%d",&c);
printf("\nenter the expo for next term:");
scanf("%d",&e);
temp->next=malloc(sizeof(struct polynode));
temp=temp->next;
temp->coeff=c;
temp->exp=e;
}
temp->next=null;
a=poly2;
printf("\n2nd polynomial:");
do
{
printf("%d(x^%d)+",a->coeff,a->exp);
a=a->next;
}while(a->next!=null);
printf("%d",a->coeff);
}
void polyadd()
{
struct polynode *x,*y,*z;
x=poly1;
y=poly2;
if(x==null && y==null)
printf("\nlist is empty");
while(x!=null && y!=null)
{
if(poly3==null)
{
poly3=malloc(sizeof(struct polynode));
z=poly3;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
if(x->exp < y->exp)
{
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next;
}
else
{
if(x->exp > y->exp)
{
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next;
}
else
{
if(x->exp == y->exp)
{
z->coeff=x->coeff+y->coeff;
z->exp=x->exp;
x=x->next;
y=y->next;
}}}}
while(x!=null)
{
if(poly3==null)
{
poly3=malloc(sizeof(struct polynode));
z=poly3;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next;
}
while(y!=null)
{
if(poly3==null)
{
poly3=malloc(sizeof(struct polynode));
z=poly3;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next;
}
z->next=null;
disp();
}
void disp()
{
struct polynode *a;
a=poly3;
printf("\nresultant polynomial:");
do
{
printf("%d(x^%d)+",a->coeff,a->exp);
a=a->next;
}while(a->next!=null);
printf("%d",a->coeff);
}

OUTPUT

to create poly1........
enter no of terms of 1st polynomial:2
enter the coeff:2
enter the expo:1
enter the coeff for next term:1
enter the expo for next term:0
1st polynomial:2(x^1)+1

to create poly2.........
enter no of terms of 2nd polynomial:2
enter the coeff:1
enter the expo:1
enter the coeff for next term:1
enter the expo for next term:0
2nd polynomial:1(x^1)+1

polyadd.......
resultant polynomial : 3(x^1)+2

RESULT

Thus the c program for polynomial addition was executed successfully.


EX NO:3a ARRAY IMPLEMENTATION OF STACK

DATE :

AIM

To write a c program to execute stack using array.


ALGORITHM

Step-1: Start the Program


Step-2: Declare Stack as an array.
Step-3: Accept the user choice [PUSH, POP, Display or Quit]
Step-4: If the choice is PUSH, accept the value only if stack is not Full; Else display the
message “STACK OVERFLOW”
Step-5: Store the value entered in the next location and increment the Pointer by one
Step-6: If the choice is POP, if the stack is empty, display the message “STACK
UNDERFLOW”; else delete the last element inserted and decrement the pointer by one.
Step-7: If the choice is Display, if the stack is not empty display the elements of the stack; else
display “STACK EMPTY” message.
Step-8: If the choice is Quit, Quit the Program.
Step-9: Stop the Execution
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 10
void push();
void pop();
oid traverse();
int stack[MAXSIZE];
int top=-1;
void main()
{
int choice;
char ch;
while(1)
{
clrscr();
printf("\n1. PUSH ");
printf("\n2. POP ");
printf("\n3. TRAVERSE ");
printf("\n4. Exit ");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
case 4:
exit(0);
default:
printf("\nYou Entered Wrong Choice");
}
getch();
}
}
void push()
{
int item;
if(top == MAXSIZE - 1)
{
printf("\nThe Stack Is Full");
}
else
{
printf("Enter the element to be inserted");
scanf("%d",&item);
top=top+1;
stack[top] = item;
}
}
void pop()
{
int item;
if(top == -1)
printf("The stack is Empty");
else
{
item = stack[top];
top = top-1;
printf("\nDeleted item is %d",item);
}
}
void traverse()
{
int i;
if(top == -1)
{
printf("The Stack is Empty");
}
else
{
printf("\n Traverse the element");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
}
}

OUTPUT
1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice1
Enter the element to be inserted10
1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice1
Enter the element to be inserted20

1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice1
Enter the element to be inserted30

1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice3

Traverse the element 30


20
10

1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice 2

Deleted item is 30
1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice3

Traverse the element 20


10
1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice1
Enter the element to be inserted 40
1. PUSH
2. POP
3. TRAVERSE
4. Exit
Enter your choice3

Traverse the element


40
20
10

RESULT

Thus the C program for the Array Implementation of Stack was executed.
EX NO:3b LINKED LIST IMPLEMENTATION OF STACK

DATE :

AIM
To write a c program to execute stack using linked list.

ALGORITHM
Step 1: Each element in the stack is described in the form of a structure having an integer value
to get the data and a pointer those points to the next structure of the stack
Step 2: HEAD pointer is the pointer that points to the top most structure (node) of the stack,
which is initialized to NULL.
Step 3: Accept the user choice [ PUSH, POP, Display or Quit ]
3.1 If the choice is PUSH
3.1.1 Receive the data from the user
3.1.2 Check for stack overflow.
3.1.3 If the stack is full display “stack overflow”
3.1.4 Else
3.1.4.1 Allocate new memory space of the type structure
3.1.4.2 The value is placed in the structure
3.1.4.3 Link between the new structure and stack was made.
3.2. If the choice is POP
3.2.1 Check for stack underflow
3.2.2 If the stack is empty display “stack underflow error”
3.2.3 Else
3.2.3.1 The node pointer by the HEAD pointer is removed using free()
3.2.3.2 The HEAD pointer is made to point to the next node in the stack.
3.3 If the choice is Display, check if the stack is empty or not.
3.4 If the stack is not empty, display the elements of the stack.
3.5 If the choice is Quit, Quit the Program.
Step 4: Stop the Execution
PROGRAM
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int data;
struct stack *link;
};
typedef struct stack node;
node *top=NULL,*t;
void push();
void pop();
void disp();
void main()
{
int c;
char ch;
while(1)
{
clrscr();
printf("\n1. Push ");
printf("\n2. POP");
printf("\n3. Display ");
printf("\n4. Exit ");
printf("\nEnter Your Choice ");
scanf("%d",&c);
switch(c)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
exit(0);
default:
printf("\nInvalid Choice ");
}
getch();
}
}
void push()
{
t=(node*)malloc(sizeof(node*));
printf("\nEnter the Data value ");
scanf("%d",&t->data);
t->link=NULL;
if(top==NULL)
top=t;
else
{
t->link=top;
top=t;
}
}
void pop()
{
if(top==NULL)
printf("\nStack Empty");
else
{
t=top;
top=top->link;
printf("\nNode Deleted is %d",t->data);
free(t);
}
}
void disp()
{
if(top==NULL)
printf("\nStack Empty");
else
{
t=top;
printf("\n");
while(t!=NULL)
{
printf("%d->",t->data);
t=t->link;
}
}
}
OUTPUT

to create press 1
push operation press 2
pop operation press 3
display top press 4
search press 5
quit press 0
enter your choice 1
Enter the data 1
Enter the data 2
times to be looped : 8
Enter the data 3
times to be looped : 8
Enter the data 4
times to be looped : 0
1234

enter no of times to be looped 5


to create press 1
push operation press 2
pop operation press 3
display top press 4
search press 5
quit press 0
enter your choice 2
Enter the data : 9
91234

enter no of times to be looped 8


to create press 1
push operation press 2
pop operation press 3
display top press 4
search press 5
quit press 0
enter your choice 2
1234

enter no of times to be looped 5


to create press 1
push operation press 2
pop operation press 3
display top press 4
search press 5

quit press 0
enter your choice 4
top=3

enter no of times to be looped 8


to create press 1
push operation press 2
pop operation press 3
display top press 4
search press 5
quit press 0
enter your choice 5
Enter the data to be searched 9
Element is not in the stack
enter no of times to be looped 0

RESULT
Thus the C program to execute the stack using linked list was executed successfully.
EX NO:4a ARRAY IMPLEMENTATION OF QUEUE

DATE :

AIM
To write a c program to execute queue using array.

ALGORITHM
Step 1: Start the Program
Step 2: Declare queue as an array.
Step 3: Accept the user choice [PUSH, POP, Display, Isfull, Isempty or exit]
Step 4: If the choice is ENQUEUE, accept the value only if queue is not Full; Else display the
message “QUEUE OVERFLOW”
Step 5: Store the value entered in the next location and increment the Pointer by one.
Step 6: If the choice is POP, if the stack is empty, display the message “QUEUE
UNDERFLOW”; else delete the last element inserted and decrement the pointer by one.
Step 7: If the choice is Display, if the stack is not empty display the elements of the queue; else
display “QUEUE EMPTY” message.
Step 8: If the choice is Isfull(),check whether the queue is full or not.If it is full display the
message “QUEUE IS FULL” else “QUEUE IS NOT FULL”.
Step 9: If the choice is Isfull(),check whether the stack is full or not.If it is full display the
message “QUEUE IS FULL” else “QUEUE IS NOT FULL”.
Step 10: If the choice is exit, exit the Program.
Step 11: Stop the Execution
PROGRAM

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 5
int front=-1, rear=-1,choice;
int q[10];
void del();
void display();
void ins();
void main()
{
clrscr();
while(1)
{
clrscr();
printf("\n1-->insert\n");
printf("2-->delete\n");
printf("3-->display\n");
printf("4-->exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
return;
}
getch();
}
}
void ins()
{
int num;
if(rear==(MAXSIZE-1))
printf("queue is full\n");
else
{
printf("enter no\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
front++;
}
}
void del()
{
if(front==-1)
printf("queue empty\n");
else
{
printf("deleted item=%d",q[front]);
if(front==rear)
front=rear=-1;
else
front++;
}
}
void display()
{
int i;
if(front==-1)
printf("queue empty\n");
else
{
printf("\nThe status of the queu\n");
for(i=front;i<=rear;i++)
printf("%d\n",q[i]);
}
}

OUTPUT

1.creation
2.insertion
3.deletion
4.display front element
5.display rear element
Enter ur choice 1
enter the number of elements 4
enter the elements
2
3
4
5
2345
Enter no of times to be looped 7
1.creation
2.insertion
3.deletion
4.display front element
5.display rear element
Enter ur choice 2
Enter the number to be inserted :8
23458
Enter no of times to be looped 6
1.creation
2.insertion
3.deletion
4.display front element
5.display rear element
Enter ur choice 3
3458
Enter no of times to be looped 6
1.creation
2.insertion
3.deletion
4.display front element
5.display rear element
6.display
Enter ur choice 4
3
Enter no of times to be looped 5
1.creation
2.insertion
3.deletion
4.display front element
5.display rear element
Enter ur choice 5
8
Enter no of times to be looped
0

RESULT
Thus the C program to execute the queue using array was executed successfully.
EX NO:4b LINKED LIST IMPLEMENTATION OF QUEUE

DATE :

AIM
To write a c program to execute queue using linked list.

ALGORITHM
Step 1: Each element in the stack is described in the form of a structure having an integer value
to get the data and a pointer those points to the next structure of the stack
Step 2: REAR h is initialized to NULL.
Step 3: Accept the user choice [ ENQUEUE,DEQUEUE, Display or Exit ]
Step 4: If the choice is ENQUEUE, Receive the data from the user. Check for queue overflow.
If the stack is full display “queue overflow” Else Allocate new memory space of the type
structure. The value is placed in the structure. Link between the new structure and queue was
made.
Step 5: If the choice is DEQUEUE, Check for queue underflow .If the stack is empty display
“queue underflow error” Else The node pointer by the FRONT pointer is removed using free()
The FRONT pointer is made to point to the next node in the queue
Step 6: If the choice is Display, check if the queue is empty or not.
Step 7: If the queue is not empty, display the elements of the stack.
Step 8: If the choice is Exit, Exit the Program.
Step 9: Stop the Execution.
PROGRAM

#include<stdio.h>
#include<conio.h>
struct queue
{
int data;
struct queue *next;
};
typedef struct queue queue;
queue *front=NULL,*rear=NULL,*t;
void ins();
void del();
void traverse();
void main()
{
int ch;
while(1)
{
clrscr();
printf("----1. insert\n");
printf("----2. delete\n");
printf("----3. traverse\n");
printf("----4. exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: ins();
break;
case 2: del();
break;
case 3: traverse();
break;
case 4: return;
default : printf("wrong choice\n");
}
getch();
}
}
void ins()
{
t=(queue*)malloc(sizeof(queue));
printf("\nEnter the data");
scanf("%d",&t->data);
t->next=NULL;
if(front==NULL&&rear==NULL)
{
rear=front=t;
}
else
{
rear->next=t;
rear=t;
}
}
void del()
{
int value;
if(front==NULL)
{
printf("queue is empty");
}
else
{
value=front->data;
t=front;
front=front->next;
printf("\nDeleted item is %d",value);
free(t);
}
}
void traverse()
{
t=front;
if(t==NULL)
printf("\nQueue is Empty");
else
{
printf("\n");
while(t!=NULL)
{
printf("%d->",t->data);
t=t->next;
}
}
}
OUTPUT

Enter the number :2


Enter the number :3
Enter the number 0 to stop2
Enter the number :4
Enter the number 0 to stop6
Enter the number :5
Enter the number 0 to stop
0
2345
1.Enqueue
2.Dequeue
3.Display
4.Display Front
5.Display rear
6.Search
7.exit
Enter ur choice 1
Enter the number :8
23458
Enter no of times to be looped 7
1.Enqueue
2.Dequeue
3.Display
4.Display Front
5.Display rear
6.Search
7.exit
Enter ur choice 2
3458
Enter no of times to be looped 6
1.Enqueue
2.Dequeue
3.Display
4.Display Front
5.Display rear
6.Search
7.exit
Enter ur choice 3
3458
Enter no of times to be looped 6
1.Enqueue
2.Dequeue
3.Display
4.Display Front
5.Display rear
6.Search
7.exit
Enter ur choice 4
3
Enter no of times to be looped 5
1.Enqueue
2.Dequeue
3.Display
4.Display Front
5.Display rear
6.Search
7.exit
Enter ur choice 5
8
Enter no of times to be looped 5
1.Enqueue
2.Dequeue
3.Display
4.Display Front
5.Display rear
6.Search
7.exit
Enter ur choice 6
Enter the number to searched 4
The element is found
Enter no of times to be looped
0

RESULT
Thus the C program to execute the queue using linked list was executed successfully.
EX NO:5 IMPLEMENTATION OF BINARY SEARCH TREE

DATE :

AIM
To implement binary search tree using linked list and possible operations on binary search
trees.

ALGORITHM
1. Declare a node structure with three fields: left pointer, data and right pointer.
Insertion:
2. Create the memory space for the root node .
3. Read the value.
4. If the value is less than the root value, it is assigned as the left child of the root.
5. Else if new value is greater than the root value, it is assigned as the right child of the root.
6. The step (2) and (3) is repeated to insert the ‘n’ number of values.

Find operation

7. Read the value to be searched.

8. Check whether the root is not null.

9. If the value to be searched is less than the root, consider the left sub-tree for searching the
particular element else if the value is greater than the root consider the right sub - tree to search
the particular element else if the value is equal then return the value that is the value which was
searched.

Deletion
10. Read the value to be deleted.

11. If the element to be deleted is a leaf node, delete it.

12. If the element to be deleted has one child, delete the parent node and assign the child a
parent.
13.If the element to be deleted has two children, find the minimum element among the right
subtree and assign if as the parent, then apply deletion procedure for the right child.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedefstruct node *position;
typedefstruct node *tree;
struct node
{
int element;
tree left;
tree right;
};
tree insert (int,tree);
position findmin(tree);
position findmax(tree);
tree deletion(int,tree);
void inorder(tree);
void main()
{
tree t=NULL,s;
intch,n,num;
char c='y';
clrscr();
do
{
printf("\n 1.Insertion");
printf("\n2.Find Minimum");
printf("\n3.Find Maximum");
printf("\n4.Deletion");
printf("\n 5.Display");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the data:\n");
scanf("%d",&num);
t=insert(num,t);
break;
case 2:
s=findmin(t);
printf("\nThe minimum data %d",s->element);
break;
case 3:
s=findmax(t);
printf("\nThe maximum data is %d",s->element);
break;
case 4:
printf("\nEnter the data to be deleted\n");
scanf("%d",&num);
t=deletion(num,t);
break;
case 5:
printf("Tree:");
inorder(t);
}
printf("\nDo you want to continue:");
c=getche();
}while(c=='y');
getch();
}
tree insert(intx,tree t)
{
if(t==NULL)
{
t=(struct node*)malloc(sizeof(struct node));
t->element=x;
t->left=t->right=NULL;
}
else
{
if(x<t->element)
t->left=insert(x,t->left);
else
t->right=insert(x,t->right);
}
return t;
}
position findmin(tree t)
{
if(t!=NULL)
{
while(t->left!=NULL)
t=t->left;
}
return t;
}
position findmax(tree t)
{
if(t!=NULL)
{
while(t->right!=NULL)
t=t->right;
}
return t;
}
tree deletion(intx,tree t)
{
position tmpcell;
if(x<t->element)
t->left=deletion(x,t->left);
else if(x>t->element)
t->right=deletion(x,t->right);
else if(t->left!=NULL&&t->right!=NULL)
{
tmpcell=findmin(t->right);
t->element=tmpcell->element;
t->right=deletion(t->element,t->right);
}
else
{
tmpcell=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
free(tmpcell);
}
return t;
}
void inorder(tree t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d\t",t->element);
inorder(t->right);
}
}

OUTPUT
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice1
Enter the data:
10
Do you want to continue:y
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice1
Enter the data:
5
Do you want to continue:y
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice1
Enter the data:
15
Do you want to continue:y
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice5
Tree:5 10 15
Do you want to continue:y
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice2
The minimum data 5
Do you want to continue:y
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice3
The maximum data is 15
Do you want to continue:y
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice4
Enter the data to be deleted
10
Do you want to continue:y
1.Insertion
2.Find Minimum
3.Find Maximum
4.Deletion
5.Display
Enter your choice5
Tree:5 15
Do you want to continue:

RESULT
Thus a C program to implement binary search tree was executed successfully.
EX.NO:6 EXPRESSION TREE AND TRAVERSALS

DATE:

AIM
To write a C program to construct an expression tree and perform tree traversals.

ALGORITHM
1. Read one symbol at a time from postfix expression.
2. Check whether the symbol is an operand or operator.
3. If a symbol is an operand, create a one-node tree and push a pointer onto the stack.
4. If the symbol is an operator, pop two pointers from the stack namely T1 and T2 and form a
new tree with root as the operator and T2 as a lefty child and T1 as a right child .A pointer to
this new tree is then pushed onto the stack.
Inorder traversal:
5. Traverse left subtree .
6. Visit root.
7. Traverse right subtree.
Preorder traversal:
8. Visit root.
9. Traverse left subtree .
10. Traverse right subtree.
Postorder traversal:
11. Traverse left subtree .
12. Traverse right subtree.
13. Visit root.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define operand(x) (x>='a'&&x<='z'||x>='A'&&x<='Z')
typedefstruct node *tree;
void push(tree);
tree pop();
void conversion();
void postfix(tree);
void infix(tree);
void prefix(tree);
struct node
{
char data;
tree left,right;
}*T=NULL;
char post[20];
tree stack[20];
int top=-1;
void main()
{
clrscr();
printf("enter postfix expression:");
scanf("%s",post);
conversion();
printf("\npostfix expression is ");
postfix(T);
printf("\ninfix expression is ");
infix(T);
printf("\nprefix expression is ");
prefix(T);
getch();
}
void conversion()
{
inti;
tree a,b,c;
char x;
for(i=0;(x=post[i])!='\0';i++)
{
if(operand(x))
{
a=(tree)malloc(sizeof(struct node));
a->data=x;
a->left=a->right=NULL;
push(a);
}
else
{
a=pop();
b=pop();
c=(tree)malloc(sizeof(struct node));
c->data=x;
c->right=a;
c->left=b;
push(c);
}
}
T=stack[top];
}
void push(tree a)
{
stack[++top]=a;
}
tree pop()
{
tree a;
a=stack[top];
top--;
return(a);
}
void postfix(tree a)
{
if(a!=NULL)
{
postfix(a->left);
postfix(a->right);
printf("%c",a->data);
}
}
void infix(tree a)
{
if(a!=NULL)
{
infix(a->left);
printf("%c",a->data);
infix(a->right);
}
}
void prefix(tree a)
{
if(a!=NULL)
{
printf("%c",a->data);
prefix(a->left);
prefix(a->right);
}
}
OUTPUT
enter postfix expression:abc*+

postfix expression is abc*+


infix expression is a+b*c
prefix expression is +a*bc

RESULT
Thus a C program to construct an expression tree and perform tree traversals was
executed successfully.
EX: NO:7 IMPLEMENTATION OF AVL TREE

DATE:

AIM
To write a C program to implement AVL tree insertion using linked list .

ALGORITHM
1. Declare a node structure with three fields: left pointer, data ,height and right pointer .
Insertion:
2. Create the memory space for the root node
3. Read the value.
4. If the value is less than the root value, it is assigned as the left child of the root.
5. Else if new value is greater than the root value, it is assigned as the right child of the root.
6. Check if the tree is balanced. If the tree violates AVL property, then perform single or double
rotation.
Single rotation:
7. If the element is inserted at left subtree of left child or right subtree of right child, then
perform single rotation.
Double rotation:
8. If the element is inserted at left subtree of right child or right subtree of left child, then
perform single rotation.
PROGRAM
#include<stdio.h>
typedef struct node
{
int data;
struct node *left,*right;
int ht;
}node;
node *insert(node *,int);
node *Delete(node *,int);
void preorder(node *);
void inorder(node *);
int height( node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);
int main()
{
node *root=NULL;
int x,n,i,op;
do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
break;
case 2: printf("\nEnter a data:");
scanf("%d",&x);
root=insert(root,x);
break;
case 3: printf("\nEnter a data:");
scanf("%d",&x);
root=Delete(root,x);
break;
case 4: printf("\nPreorder sequence:\n");
preorder(root);
printf("\n\nInorder sequence:\n");
inorder(root);
printf("\n");
break;
}
}while(op!=5);
return 0;
}
node * insert(node *T,int x)
{
if(T==NULL)
{
T=(node*)malloc(sizeof(node));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2)
if(x>T->right->data)
T=RR(T);
else
T=RL(T);
}
else
if(x<T->data)
{
T->left=insert(T->left,x);
if(BF(T)==2)
if(x < T->left->data)
T=LL(T);
else
T=LR(T);
}
T->ht=height(T);
return(T);
}

node * Delete(node *T,int x)


{
node *p;
if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found
if(T->right!=NULL)
{ //delete its inorder succesor
p=T->right;
while(p->left!= NULL)
p=p->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
if(BF(T)==2)//Rebalance during windup
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);\
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}

int height(node *T)


{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}

node * rotateright(node *x)


{
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node * rotateleft(node *x)
{
node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}

node * RR(node *T)


{
T=rotateleft(T);
return(T);
}

node * LL(node *T)


{
T=rotateright(T);
return(T);
}

node * LR(node *T)


{
T->left=rotateleft(T->left);
T=rotateright(T);
return(T);
}
node * RL(node *T)
{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}
int BF(node *T)
{
int lh,rh;
if(T==NULL)
return(0);

if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;

if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
void preorder(node *T)
{
if(T!=NULL)
{
printf("%d(Bf=%d)",T->data,BF(T));
preorder(T->left);
preorder(T->right);
}
}
void inorder(node *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d(Bf=%d)",T->data,BF(T));
inorder(T->right);
}
}

OUTPUT
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:1
Enter no. of elements:4
Enter tree data:7 12 4 9
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
Preorder sequence:
7(Bf=-1)4(Bf=0)12(Bf=1)9(Bf=0)
Inorder sequence:
4(Bf=0)7(Bf=-1)9(Bf=0)12(Bf=1)

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:3
Enter a data:7

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
Preorder sequence:
9(Bf=0)4(Bf=0)12(Bf=0)
Inorder sequence:
4(Bf=0)9(Bf=0)12(Bf=0)

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:5

RESULT
Thus a C program to implement AVL tree insertion was executed successfully.
EX.NO:8 DIJKSTRA’S ALGORITHM

DATE:

AIM
To write a C program to find the shortest path using Dijkstra’s algorithm.

ALGORITHM
1.Start the program.
2.Declare the adjacency matrix for the graph.
3.For each vertex, initialize known=0, distance=∞ and path=0.
4.Set the distance of start node as 0.
5.Declare the known value of start vertex V as known
6.Find the adjacent vertex W for v
7.If known value of adjacent vertex is not equal to 1
Distance[W]=distance[V]+CVW
8.Check if new distance [W] < old distance [W], if so update the distance and path.
9.. Next, declare the vertex with minimum distance as known and continue step (5) until all
vertices are declared as known.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define max 4
#define INFINITE 100
int allselected( int *selected)
{
int i;
for(i=0;i<max;i++)
if(selected[i]==0)
return 0;
return 1;
}
void shortpath(int cost[][max],int *preceed,int *distance)
{
int selected[max]={0};
int current=0,i,k,dc,smalldist,newdist;
for(i=0;i<max;i++)
distance[i]=INFINITE;
selected[current]=1;
distance[0]=0;
current=0;
while(!allselected(selected))
{
smalldist=INFINITE;
dc=distance[current];
for(i=0;i<max;i++)
{
if(selected[i]==0)
{
newdist=dc+cost[current][i];
if(newdist<distance[i])
{
distance[i]=newdist;
preceed[i]=current;
}
if(distance[i]<smalldist)
{
smalldist=distance[i];
k=i;
}
}
}
current=k;
selected[current]=1;
}
}
int main()
{
int cost[max][max]={{INFINITE,2,INFINITE,1},{INFINITE,INFINITE,INFINITE,2},
{3,INFINITE,INFINITE,INFINITE},{INFINITE,INFINITE,1,INFINITE}};
intpreceed[max]={0},i,distance[max];
clrscr();
shortpath(cost,preceed,distance);
for(i=0;i<max;i++)
{
printf("The shortest path from 0 to %d is ",i);
printf("%d\n",distance[i]);
}
return 0;
getch();
}

OUTPUT
The shortest path from 0 to 0 is 0
The shortest path from 0 to 1 is 2
The shortest path from 0 to 2 is 2
The shortest path from 0 to 3 is 1

RESULT
Thus a C program to find the shortest path using Dijkstra’s algorithm was executed successfully.
EX.NO:9 PRIM’S ALGORITHM

DATE:

AIM
To write a C program to find the minimum spanning tree using Prim’s algorithm.

ALGORITHM
1.Start the program.
2.Declare the adjacency matrix for the graph.
3.For each vertex, initialize known=0, distance=∞ and path=0.
4.Set the distance of start node as 0.
5.Declare the known value of start vertex V as known
6.Find the adjacent vertex W for v
7.If known value of adjacent vertex is not equal to 1
Distance[W]= CVW
8.Check if new distance [W] < old distance [W],if so update the distance and path.
9. Next, declare the vertex with minimum distance as known and continue step (5) until all
vertices are declared as known.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 10
#define infinity 9999
void create_graph();
intmaketree(struct edge*,int*);
intall_perm(struct node*);
struct node
{
int path;
intdist;
int known;
};
struct edge
{
int u;
int v;
};
intadj[MAX][MAX];
int n;
void main()
{
inti,j;
intmincost,count;
struct edge tree[MAX];
clrscr();
create_graph();
count=maketree(tree,&mincost);
printf("\nEdges to be included in spanning tree are: \n");
for(i= 1 ;i<=count;i++)
{
printf("%d->",tree[i].u);
printf("%d\n",tree[i].v);
}
printf("\n Weight of spanning tree is: %d\n", mincost);
}
void create_graph()
{
inti,max_edges,source,destin,wt;
printf("Enter number of vertices:");
scanf("%d",&n);
max_edges=n*(n-1 )/2;
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d(0 0 to quit): ",i);
scanf("%d %d",&source,&destin);
if((source==0) && (destin==0))
break;
printf("Enter weight for this edge:");
scanf("%d",&wt);
if( source>n || destin>n || source<=0|| destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
{
adj[source][destin]=wt;
adj[destin][source]=wt;

}
}
if(i<n-1)
{
printf("Spanning tree is not possible\n");
}
}
intmaketree(struct edge tree[MAX],int *weight)
{
struct node state[MAX];
inti,k,min,count,current,newdist;
int m;
int u1,v1;
*weight=0;
/*Make all nodes temporary*/
for(i=1;i<=n;i++)
{
state[i].path=0;
state[i].dist=infinity;
state[i].known= 0;
}
state[1].path=0;
state[1].dist = 0;
state[1].known =1;
/*Start from first node*/
current=1;
count=0; /*count represents number of nodes in tree */
while(all_perm(state)!= 1)
{
for(i=1;i<=n;i++)
{
if(adj[current][i]>0&&state[i].known == 0)
{

if(adj[current][i]< state[i].dist)
{
state[i].path = current;
state[i].dist=adj[current][i];
}
}
}
/* Search for temporary node with minimum distance and make it current node*/
min=infinity;
for(i=1;i<=n;i++)
{
if(state[i].known==0 && state[i].dist<min)
{
min=state[i].dist; current=i;
}
}
state[current].known= 1;
//Insert the edge(u 1 ,v 1) into the tree */
u1=state[current].path;
v1=current;
count++;
tree[count].u=u1;
tree[count].v=v1;
//Add wt on this edge to weight of tree
*weight=*weight+adj[u1][v1];
}
return (count);
}
intall_perm(struct node state[MAX])
{
inti;
for(i=1;i<=n;i++)
if(state[i].known== 0)
return 0;
return 1;
}

OUTPUT
Enter edge 2(0 0 to quit): 1 4
Enter weight for this edge:1
Enter edge 3(0 0 to quit): 1 3
Enter weight for this edge:4
Enter edge 4(0 0 to quit): 2 5
Enter weight for this edge:10
Enter edge 5(0 0 to quit): 2 4
Enter weight for this edge:3
Enter edge 6(0 0 to quit): 3 4
Enter weight for this edge:2
Enter edge 7(0 0 to quit): 3 6
Enter weight for this edge:5
Enter edge 8(0 0 to quit): 4 5
Enter weight for this edge:7
Enter edge 9(0 0 to quit): 4 6
Enter weight for this edge:8
Enter edge 10(0 0 to quit): 4 7
Enter weight for this edge:4
Enter edge 11(0 0 to quit): 5 7
Enter weight for this edge:6
Enter edge 12(0 0 to quit): 6 7
Enter weight for this edge:1
Enter edge 13(0 0 to quit): 0 0

Edges to be included in spanning tree are:


1->4
1->2
4->3
4->7
7->6
7->5
Weight of spanning tree is: 16

RESULT
Thus a C program to find the minimum spanning tree using Prim’s algorithm was executed
successfully.
EX.NO:10 HASHING WITH OPEN ADDRESSING

DATE:

AIM
To write a C program to implement hashing concept using open addressing (linear
probing) techniques.

ALGORITHM
1. Start the program.
2. Hash the given data by applying the hash function H(X) =X mod Tablesize.
3. If collision arises, search the next empty cell and hash the value there.
4. End the program
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={0,0,0,0,0,0,0,0,0,0};
intn,value,temp,hashvalue;
clrscr();
printf("\n enter value of n(table size):");
scanf("%d",&n);
do
{
printf("\n enter the hash value");
scanf("%d",&value);
hashvalue=value%n;
if(a[hashvalue]==0)
{
a[hashvalue]=value;
printf("\n a[%d] the value %d is stored",hashvalue,value);
}
else
{
for(hashvalue++;hashvalue<n;hashvalue++)
{
if(a[hashvalue]==0)
{
printf("space is allocated give other value");
a[hashvalue]=value;
printf("\n a[%d] the value %d is stored",hashvalue,value);
goto l1;
}
}
}
l1:printf("\ndo u want to enter more-enter 1:");
scanf("%d",&temp);
}
while(temp==1);
getch();
}

OUTPUT
enter value of n(table size):10

enter the hash value12

a[2] the value 12 is stored


do u want to enter more-enter 1:1

enter the hash value45


a[5] the value 45 is stored
do u want to enter more-enter 1:1

enter the hash value67

a[7] the value 67 is stored


do u want to enter more-enter 1:1

enter the hash value55


space is allocated give other value
a[6] the value 55 is stored

RESULT
Thus a C program to implement open addressing (linear probing) techniques was
executed successfully.
EX.NO:11a LINEAR SEARCH

DATE:

AIM
To write a C program to sort the given numbers using Linear Search.

ALGORITHM
Step-1: Start the Program
Step-2: Define the size od the array. Step-3:
Get the Array elements.
Step-4: Get the element to search.
Step-5: Using for loop, compare each element the element to find.
Step-6: If the element is found, Display the element is found else Display not found.
Step-7: Stop the Program.
PROGRAM
#include<stdio.h> int
main()
{
int a[10],i,n,m,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
if(a[i]==m)
{ c=1;
break;
}
}
if(c==0)
printf("The number is not in the list"); else
printf("The number is found"); return
0;
}

OUTPUT
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3 Enter
the number to be search: 0
The number is found

RESULT
Thus the C program to sort the given numbers using Linear Search was executed.
EX.NO:11b BINARY SEARCH

DATE:

AIM
To write a C program to sort the given numbers using Binary Search.

ALGORITHM
Step-1: Start the Program
Step-2: Read sorted N number of integers in an array „a‟ and element to be searched as „m‟.
Step-3: Initialize low=0 and high=n-1.
Step-4: Check low value is less than or equal to high value.
Step-4.1: If yes, compute the mid value by adding the value of high and low and divide by 2.
Step-5: If the search element „m‟ is equal to mid value break.
Step-6: Else the search element „m‟ is less than mid element in the array set the value of high
as mid-1 and low as mid+1.
Step-7: Display the element if is found. Step-8:
Stop the Program.
PROGRAM
#include<stdio.h>
#include<conio.h> int
main()
{
int a[10],i,n,m,c=0,low,high,mid;
clrscr();
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
low=0,high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(m==a[mid])
{ c=1;
break;
}
else if(m<a[mid])
{
high=mid-1;
}
else
low=mid+1;
}
if(c==0)
printf("The number is not found."); else
printf("The number is found.");
getch();
return 0;
}

OUTPUT
Enter the size of an array: 5
Enter the elements in ascending order: 1 3 5 6 7 Enter the
number to be search: 1
The number is found.

RESULT
Thus the C program to sort the given numbers using Binary Search was executed.
EX.NO:11c SELECTION SORT

DATE:

AIM
To write a C program to sort the given numbers using selection sort.

ALGORITHM
Step-1: Start the program
Step-2: Declare the variables
Step-3: Get the total number of elements.
Step-4: Get the array elements
Step-5: Compare every element with their next element to get the sorted array.
Step-6: Display the sorted array.
PROGRAM
#include<stdio.h>
#include<conio.h> int
main()
{
int s,i,j,temp,a[20]; printf("Enter total
elements: "); scanf("%d",&s);
printf("Enter the elements: “);
for(i=0;i<s;i++)
{scanf("%d",&a[i]);
}
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
return 0;
}

OUTPUT
Enter total elements: 5
Enter the elements: 4 5 2 1 7
After sorting is: 1 2 4 5 7

RESULT
Thus the C program to sort the given numbers using Selection Sort was executed.

You might also like