You are on page 1of 64

CONTENTS

S. DATE NAME OF THE EXPERIMENT PAGE SIGNATURE


NO NO.
1 ARRAY IMPLEMENTATION OF LIST 2
ADT
2 SINGLY LINKED LISTS 6
3 DOUBLY LINKED LISTS 12
4 CIRCULAR SINGLY LINKED LISTS 18
5 ARRAY IMPLEMENTATION OF 23
STACK
6 LINKED LIST IMPLEMENTATION OF 26
STACK
7 ARRAY IMPLEMENTATION OF 29
QUEUE
8 LINKED IMPLEMENTATION OF 32
QUEUE
9 BALANCING SYMBOL 35
10 EXPRESSION TREE 37
11 INFIX TO POSTFIX CONVERSION 40
12 POLYNOMIAL ADDITION 43
13 LINKED LIST IMPLEMENTATION OF 47
DEQUE
14 INSERTION IN AVL TREE 52
15 BINARY SEARCH TREE 58

Ex.No: 1 ARRAY IMPLEMENTATION OF LIST ADT


Date:

Aim:

1
To create list using array and perform various operation.

Algorithm:

1. Start the program

2. Declare all the functions and using the switch case select the operation to be performed

3. Create an array and initialize the values using the for loop

4. Perform insertion, deletion and find operation to insert, delete and find the data in the list.

5. Display the array elements

6. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[100],n,i,t,max=50,p,x;
int ch;
void main()
{
void create();
void insertion();
void deletion();
void find();
void display();
clrscr();
while(1)
{
printf("\n1.creation");
printf("\n2.insertion");
printf("\n3.deletion");
printf("\n4.find");
printf("\n5.display");
printf("\n6.exit");
printf("\nenter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
2
case 2:
insertion();
break;
case 3:
deletion();
break;
case 4:
find();
break;
case 5:
display();
break;
default:
exit(0);
}
getch();
}
}
void create()
{
printf("enter the n value:");
scanf("%d",&n);
printf("enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void insertion()
{
if(n==max)
printf("\narray is full");
else
{
printf("enter the position:");
scanf("%d",&p);
printf("\nenter the element:");
scanf("%d",&x);
for(i=n;i>=p;i--)
{
a[i]=a[i-1];
}
a[i]=x;
n++;
}
}
void deletion()
{
if(n==0)
3
printf("\narray is empty");
else
{
printf("\nenter the position to be deleted:");
scanf("%d",&p);
for(i=p;i<n;i++)
{
a[i-1]=a[i];
}
n--;
}
}
void find()
{
int t=0;
if(n==0)
printf("array is empty:");
else
{
printf("enter the element to find:");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
t=1;
break;
}
}
if(t==1)
printf("\nthe element is present in location %d",i+1);
else
printf("\nthe element is not present");
}
}
void display()
{
if(n==0)
printf("\nthe array is empty");
else
{
printf("\narray elements are");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}}
Output:

4
Result:

Thus the list is created using array and its various operations are performed using C
program.

Ex.No: 2 SINGLY LINKED LISTS


Date:

5
Aim:
To write a C program to perform a singly linked list

Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be performed

3. Declare a structure with all the required variables and allocate the memory using the
malloc function

4. In the insert beginning function set y->link=head. For the insert middle check if c<pos if
so set z=x; and x=x->link;and increment c

5. In the delete beginning function check the above function but perform delete operation

6. In the display function display the array elements

7. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head,*x,*y,*z;
void main()
{
void create();
void insbeg();
void insmid();
void insend();
void delbeg();
void delmid();
void delend();
void display();
int ch;
clrscr();
6
while(1)
{
printf("\n 1.Creation");
printf("\n 2.Insertion at beginning");
printf("\n 3.Insertion at middle");
printf("\n 4.Insertion at end");
printf("\n 5.Deletion at beginning");
printf("\n 6.Deletion at middle");
printf("\n 7.Deletion at end");
printf("\n 8.Display");
printf("\n 9.Exit");
printf("\n Enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insbeg();
break;
case 3:
insmid();
break;
case 4:
insend();
break;
case 5:
delbeg();
break;
case 6:
delmid();
break;
case 7:
delend();
break;
case 8:
display();
break;
default:
exit(0);
}
}
}
void create()
{
int c;
7
head=NULL;
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
head=x;
printf("\n Do u wish to continue press 1 otherwise 0:");
scanf("%d",&c);
while(c!=0)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
y->link=NULL;
x->link=y;
x=y;
printf("\n Do u wish to continue press 1 otherwise 0:");
scanf("%d",&c);
}
}
void insbeg()
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
y->link=head;
head=y;
}
void insmid()
{
int pos,c=1;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
printf("\n Enter the position to be inserted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
y->link=x;
z->link=y;
}
void insend()
8
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
y->link=NULL;
x=head;
while(x->link!=NULL)
{
x=x->link;
}
x->link=y;
}
void delbeg()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
head=x->link;
free(x);
}
}

void delmid()
{
int pos,c=1;
if(head==NULL)
printf("\n List is empty");
else
{
printf("\n Enter the position to be deleted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
z->link=x->link;
free(x);
}
}
void delend()
{
if(head==NULL)
9
printf("\n List is empty");
else
{
x=head;
while(x->link!=NULL)
{
y=x;
x=x->link;
}
y->link=NULL;
free(x);
}
}
void display()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
printf("\n List elements are \n");
while(x->link!=NULL)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}
}

Output:

10
Result:
Thus the list operations are performed using singly linked list in C perform.

11
Ex.No: 3 DOUBLY LINKED LISTS
Date:

Aim:

To a write a C program to implement a doubly linked list

Algorithm:

1. Start the program


2. Declare all the required functions and select the operation to be performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. Perform insertion, deletion and display operation to insert, delete and display the lists.
5. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
struct node *head,*x,*y,*z;
void main()
{
void create();
void insbeg();
void insmid();
void insend();
void delbeg();
void delmid();
void delend();
void display();
int ch;
clrscr();
while(1)
{
printf("\n 1.Creation");
12
printf("\n 2.Insertion at beginning");
printf("\n 3.Insertion at middle");
printf("\n 4.Insertion at end");
printf("\n 5.Deletion at beginning");
printf("\n 6.Deletion at middle");
printf("\n 7.Deletion at end");
printf("\n 8.Display");
printf("\n 9.Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insbeg();
break;
case 3:
insmid();
break;
case 4:
insend();
break;
case 5:
delbeg();
break;
case 6:
delmid();
break;
case 7:
delend();
break;
case 8:
display();
break;
default:
exit(0);
}
}
}
void create()
{
int c;
head=NULL;
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
13
scanf("%d",&x->data);
x->llink=NULL;
x->rlink=NULL;
head=x;
printf("\n Do you wish to continue press 1 or 0:");
scanf("%d",&c);
while(c!=0)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
x->rlink=y;
y->llink=x;
y->rlink=NULL;
x=y;
printf("\n Do you wish to continue press 1 or 0:");
scanf("%d",&c);
}
}
void insbeg()
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
y->llink=NULL;
y->rlink=head;
head->llink=y;
head=y;
}
void insmid()
{
int c=1,pos;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
printf("\n Enter the position to be inserted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->rlink;
c++;
}
y->rlink=x;
y->llink=z;
x->llink=y;
14
z->rlink=y;
}
void insend()
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data: ");
scanf("%d",&y->data);
y->rlink=NULL;
x=head;
while(x->rlink!=NULL)
{
x=x->rlink;
}
x->rlink=y;
y->llink=x;
}
void delbeg()
{
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
head=x->rlink;
head->llink=NULL;
free(x);
}
}
void delmid()
{
int pos,c=1;
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
printf("\n Enter the position to be deleted:");
scanf("%d",&pos);
while(c<pos)
{
z=x;
x=x->rlink;
c++;
}
z->rlink=x->rlink;
x->rlink->llink=z;
free(x);
15
}
}
void delend()
{
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
while(x->rlink!=NULL)
{
y=x;
x =x->rlink;
}
y->rlink=NULL;
free(x);
}
}
void display()
{
if(head==NULL)
printf("\n the list is empty");
else
{
x=head;
printf("\nThe list elements are");
while(x->rlink!=NULL)
{
printf("%d->",x->data);
x=x->rlink;
}
printf("%d",x->data);
}
}

16
Output:

Result:
Thus the list operations are executed using doubly linked list in C program.

17
Ex. No: 4 CIRCULAR SINGLY LINKED LISTS
Date:

Aim:

To construct a circularly linked list using C program

Algorithm:

1. Start the program


2. Declare all the functions and using the switch case select the operation to be performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. Perform insertion, deletion and display operation to insert, delete and display the lists.
5. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head=NULL,*x,*y,*z;
void main()
{
void create();
void insbeg();
void insrem();
void delbeg();
void delrem();
void display();
int ch;
clrscr();
printf("\n 1.Creation \n 2.Insertion at beginning \n 3.Insertion at remaining");
printf("\n4.Deletion at beginning \n5.Deletion at remaining \n6.Display");
printf("\n7.Exit");
while(1)
{
printf("\n Enter your choice:");

18
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insbeg();
break;
case 3:
insrem();
break;
case 4:
delbeg();
break;

case 5:
delrem();
break;
case 6:
display();
break;
default:
exit(0);
}
}
}
void create()
{
int c;
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=x;
head=x;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d",&c);
while(c!=0)
{
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
x->link=y;
y->link=head;
x=y;
printf("\n If you wish to continue press 1 otherwise 0:");
scanf("%d",&c);
19
}
}
void insbeg()
{
x=head;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
while(x->link!=head)
{
x=x->link;
}
x->link=y;
y->link=head;
head=y;
}
void insrem()
{
int c=1,pos;
y=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&y->data);
printf("\n Enter the position to be inserted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
y->link=x;
z->link=y;
}
void delbeg()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
y=head;
while(x->link!=head)
{
x=x->link;
}
head=y->link;
20
x->link=head;
free(y);
}
}
void delrem()
{
if(head==NULL)
printf("\n List is empty");
else
{
int c=1,pos;
printf("\n Enter the position to be deleted:");
scanf("%d",&pos);
x=head;
while(c<pos)
{
y=x;
x=x->link;
c++;
}
y->link=x->link;
free(x);
}
}
void display()
{
if(head==NULL)
printf("\n List is empty");
else
{
x=head;
while(x->link!=head)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}
}

21
Output:

Result:

Thus the program for circular linked list is executed and verified using C program

22
Ex.No:5 ARRAY IMPLEMENTATION OF STACK
Date:

Aim:

To perform the array implementation of stack using C program

Algorithm:

1. Start the program

2. Declare all the required functions and select the operation to be performed

3. Allocate the memory location using malloc function and if the stack is empty then the top
and bottom elements are the same.

4. For deletion operation check if the stack has only one element if so perform deletion. If
the stack is empty then print that the stack is empty.

5. For the display operation use the while loop and check if i<top if so print he elements of
the stack

6. Check if top==0, if so then the stack is empty

7. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int top=0, a[100], max=50;
void main()
{
void push();
void pop();
void display();
int ch;
clrscr();
printf(“\n1.Push \n2.Pop \n3.Display \n4.Exit”);
while(1)
{

23
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
exit();
}
}
}
void push()
{
if(top==max)
printf(“\n Array is full”);
else
{
printf(“\n Enter the data:”);
scanf(“%d”,&x);
st[top++]=x;
}
}
void pop()
{
if (top==0)
printf(“\n Array is empty”);
else
{
top--;
}
}
void display()
{
int i;
if(top==0)
{
printf(“\n Stack is empty”);
else
{
printf(“\n Array elememts are:”);
24
for(i=0;i<top;i++)
printf(“%d \t”,st[i]);
}
}

Output:

25
Result:
Thus the stack is implemented using array and its operations are performed in C program.

Ex. No:6 LINKED LIST IMPLEMENTATION OF STACK


Date:

Aim:

To perform the linked list implementation of stack using C program

Algorithm:

1. Start the program.

2. Declare all the required functions and select the operation to be performed

3. Allocate the memory location using malloc function and if the stack is empty then the top
is the bottom element

4. Insert an element into the stack.

5. For deletion operation check if the stack has only one element if so perform deletion. If
the stack is empty then print that the stack is empty.

6. For the display operation use the while loop and check if x->link! =NULL if so print the
elements.

7. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};

26
struct node *top=NULL,*x;
void main()
{
void push();
void pop();
void display();
int ch;
clrscr();
while(1)
{
printf("\n OPTIONS");
printf("\n 1.Insertion (push)");
printf("\n 2.Deletion (pop)");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void push()
{
if(top==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
top=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
27
scanf("%d",&x->data);
x->link=top;
top=x;
}
}
void pop()
{
if(top==NULL)
printf("\n Stack is empty");
else
{
x=top;
printf("\n Popped Element is %d",x->data);
top=top->link;
free(x);
}
}
void display()
{
x=top;
printf("\n Stack Elements are\n");
while(x->link!=NULL)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}

Output:

28
Result:
Thus the stack is implemented using linked list and its operations are performed in C
program.
Ex. No: 7 ARRAY IMPLEMENTATION OF QUEUE
Date:

Aim:
To write a C program to perform the array implementation of queue

Algorithm:

1. Start the program


2. Declare all the required functions and select the operation to be performed
3. Allocate the memory location using malloc function and if the stack is empty then
the top and bottom elements are the same.
4. For enqueue operation check if rear==front if so then the queue is full. Otherwise
set qu[rear++]=ele
5. For the dequeue operation use the while loop and check if rear==0 if so then print
queue is empty
6. For display operation check if front<rear if so print the values.
7. Stop the program

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int ch,qu[50], max=50,front=0,rear=0;
void main()
{
void enqueue();
void dequeue();
void display();
clrscr();
29
printf(“\n1.Insertion(enqueue) \n2.Deletion(dequeue) \n3.Display \n4.Exit”);
while(1)
{
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void enqueue()
{
int ele;
if(rear==max)
printf(“\n queue is full”);
else
{
printf(“\n Enter the data:”);
scanf(“%d”,&ele);
qu[rear++]=ele;
}
}
void dequeue()
{
int i;
if (rear==0)
printf(“\n queue is empty”);
else
{
printf(“\n Deleted element is %d”,qu[front]);
front++;
}
}
void display()
{
int i;
30
if(rear==0)
printf(“\n Queue is empty”);
else
{
printf(“\n Queue elememts are:”);
for(i=front;i<rear;i++)
printf(“%d \t”,qu[i]);
}
}

Output:

31
Result:
Thus the queue is implemented using array and its operations are performed in C
program.

Ex. No:8 LINKED LIST IMPLEMENTATION OF QUEUE


Date:

Aim:
To write a C program to perform linked list implementation of queue

Algorithm:

1. Start the program


2. Declare all the required functions and select the operation to be performed
3. Allocate the memory location using malloc function and if the queue is empty then the
front and the rear element are the same
4. For deletion operation check if the queue has only one element if so perform deletion. if
the queue contains more elements then perform deletion accordingly.
5. For the display operation use the while loop and check if x->link!=NULL if so print the
elements.
6. For the display operation if rear== null then print that the queue is empty
7. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *front=NULL,*rear=NULL,*x;
void main()
{
32
void enqueue();
void dequeue();
void display();
int ch;
clrscr();
while(1)
{
printf("\n OPTIONS");
printf("\n 1.Insertion (enqueue)");
printf("\n 2.Deletion (dequeue)");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void enqueue()
{
if(rear==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
rear=x;
front=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&x->data);
x->link=NULL;
33
rear->link=x;
rear=x;
}
}
void dequeue()
{
if(front==NULL)
printf("\n Queue is empty");
else
{
x=front;
printf("\n Dequeued Element is %d",x->data);
front=x->link;
free(x);
}
}
void display()
{
x=front;
printf("\n Queue Elements are\n");
while(x->link!=NULL)
{
printf("%d->",x->data);
x=x->link;
}
printf("%d",x->data);
}

Output:

34
Result:
Thus the queue is implemented using linked list and its operations are performed in C program

Ex. No:9 BALANCING SYMBOLS


Date:

Aim:
To write a C program to balance the symbols in an expression.

Algorithm:

1. Start the program


2. Declare the variables
3. Get the string whose symbols are to be balanced from the user
4. Check the string from left to right to check whether the symbols are balanced or not.
5. Print the result accordingly.
6. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
char str1[50],str[50],str2[50];
int i,n,t1=0,t2=0;
void main()
{
clrscr();
printf("\nEnter the string:");
scanf("%s",str);
n=strlen(str);
for(i=0;i<n;i++)
{
printf("%c\t",str[i]);
}
printf("\n");
35
for(i=0;i<n;i++)
{
printf("%d\t",i+1);
}
for(i=0;i<n;i++)
{
if(str[i]=='(')
{
str1[t1++]=str[i];
str2[t2++]=i+1;
}
else
{
if(str[i]==')')
if(str1[--t1]=='(')
printf("\nMatch found for %d\t%d",str2[--t2],i+1);
else
printf("\nNo left match for %d",i+1);
}
}
for(i=0;i<t1;i++)
printf("\nNo right match for %d",str2[--t2]);
getch();
}

Output:

36
Result:

Thus the C program for balancing the symbols is verified

Ex. No:10 EXPRESSION TREE


Date:

Aim:

To construct an expression tree using C program

Algorithm:

1. Start the program


2. Declare all the required functions and select the operation to be performed
3. Allocate the memory location using malloc function
4. In the post function check if p!=NULL if call the recursive function post with parameters
p->rlink and p->llink
5. Using the switch case construct the expression tree
6. Using the push function insert the elements into the tree if topt==max then the tree is full
7. Print the values
8. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char c;
struct node *rlink;
37
struct node *llink;
}*stc[30];
struct node *temp,*root;
char prefix[20],ch;
int topt=-1,max=50,len; //global declaration
void pusht(struct node *p);
struct node *popt();
void tredis(struct node *ptr,int level);
void exptree();
void post(struct node *p);
void main()
{
clrscr();
printf("Enter a prefix expression:");
scanf("%s",prefix);
exptree();
tredis(root,1);
printf("\nThe postfix expression is:");
post(root);
getch();
}
void post(struct node *p)
{
if(p!=NULL)
{
post(p->llink);
post(p->rlink);
printf("%c",p->c);
}
}
void exptree()
{
int i;
len=strlen(prefix);
i=len-1;
while(i>=0)
{
switch(prefix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
temp=(struct node*)malloc(sizeof(struct node));
temp->c=prefix[i];
temp->llink=popt();
38
temp->rlink=popt();
pusht(temp);
break;
default :
temp=(struct node*)malloc(sizeof(struct node));
temp->c=prefix[i];
temp->rlink=NULL;
temp->llink=NULL;
pusht(temp);
}
i--;
}
root=stc[topt];
}

void pusht(struct node * p)


{
if(topt==max)
{
printf("/**Beyond Capacity**/");
}
else
{
stc[++topt]=p;
}
}
struct node *popt()
{
if(topt==-1)
printf("/**No Expression**/");
else
return(stc[topt--]);
}
void tredis(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
tredis(ptr->rlink, level+1);
printf(" ");
for (i = 0; i < level; i++)
printf(" ");
printf("%c", ptr->c);
tredis(ptr->llink, level+1);
}
}

39
Output:

Result:

Thus the C program to construct an expression tree is execute

Ex. No: 11 INFIX TO POSTFIX CONVERSION


Date:

Aim:

To write a C program to convert infix to postfix expression

Algorithm:

1. Start the program


2. Using the strlen function determine the length of the string
3. Declare three arrays; the decremented value of one array is assigned to the incremented
value. This process is repeated until the parenthesis matches.
4. Until t==0 assign the values to sack1
5. Check if (top2==-1) if the condition is satisfied then assign stack2 [++top2]=str[i];
6. Print the result.
7. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[50],stack1[50],stack2[50];
int i=0,j,n,top1=-1,top2=-1;
int prec(char);
void main()
{
int t=0;
clrscr();
printf("\nEnter the infix expression:");
scanf("%s",str);
40
printf("%s",str);
n=strlen(str);
for(i=0;i<n;i++)
{
t=0;
if(str[i]==')')
{
stack1[++top1]=stack2[top2--];
top2--;
t=1;
}
if(str[i]=='(')
{
stack2[++top2]='(';
t=1;
}
if(t==0)
{
if(((str[i]>='a')&&(str[i]<='z'))||(( str[i]>='A')&&(str[i]<='Z')))
stack1[++top1]=str[i];
else
{
if(top2==-1)
stack2[++top2]=str[i];
else
{
if(prec(str[i])>prec(stack2[top2]))
{
stack2[++top2]=str[i];
}
else
{
while(prec(str[i])<=prec(stack2[top2]))
stack1[++top1]=stack2[top2--];
stack2[++top2]=str[i];
}
}
}
}
}
printf("\nANSWER\n");
while(top2>=0)
stack1[++top1]=stack2[top2--];
for(i=0;i<=top1;i++)
printf("%c",stack1[i]);
getch();
}
41
int prec(char ch)
{
int tt;
if((ch=='+')||(ch=='-'))
tt=2;
if((ch=='*')||(ch=='/'))
tt=3;
if(ch=='(')
tt=1;
return tt;
}
Output:

42
Result:

Thus an infix expression is converted to postfix expression using stack.

Ex.No: 12 POLYNOMIAL ADDITION


Date:

Aim:
To write a C program to perform polynomial addition

Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be performed

3. Declare a structure with all the required variables and allocate the memory using the
malloc function

4. Using the while loop check if node->next!=NULL if so print the values of

node->coeff,node->pow

5. Perform the addition according to the conditions of polynomial addition.

6. print the result

7. Stop the program

Program:

#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link
{
int coeff;
43
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n enter coeff:");
scanf("%d",&node->coeff);
printf("\n enter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\n continue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
44
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
main()
{
char ch;
clrscr();
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\nenter 1st number:");
create(poly1);
printf("\nenter 2nd number:");
create(poly2);
printf("\n1st Number:");
show(poly1);
printf("\n2nd Number:");
show(poly2);
polyadd(poly1,poly2,poly);
45
printf("\nAdded polynomial:");
show(poly);
printf("\n add two more numbers:");
ch=getch();
}
while(ch=='y' || ch=='Y');
}

Output:

46
Result:
Thus the program for polynomial addition using linked list is executed.

Ex. No: 13 LINKED LIST IMPLEMENTATION OF DEQUE


Date:

Aim:

To perform the linked list implementation of Deque using C program

Algorithm:

1. Start the program

2. Declare all the required functions and select the operation to be performed

3. Allocate the memory location using malloc function and if the queue is empty then the
front and the rear element are the same

4. For deletion operation check if the queue has only one element if so perform deletion. if
the queue contains more elements then perform deletion accordingly.

5. For the display operation use the while loop and check if x->link!=NULL if so print the
elements.

6. For the display operation if rear== null then print that the queue is empty

47
7. Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *front=NULL,*rear=NULL,*x;
void main()
{
int a;
void enqfro();
void deqrea();
void enqrea();
void deqfro();
void display1();
void display2();
clrscr();
while(1)
{
printf("\n1.insertion at front");
printf("\n2.insertion at rear ");
printf("\nenter choice");
scanf("%d",&a);
while(a==1)
{
int ch;
printf("\n1.insertion at front");
printf("\n2.deletion at rear ");
printf("\n3.display1");
printf("\n4.exit");
printf("\nenter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqfro();
break;
case 2:
deqrea();
break;
case 3:
48
display1();
break;
default:
exit(0);
}
}
while(a==2)
{
int ch;
printf("\n1.insertion at rear");
printf("\n2.deletion at front");
printf("\n3.display2");
printf("\n4.exit");
printf("\nenter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqrea();
break;
case 2:
deqfro();
break;
case 3:
display2();
break;
default:
exit(0);
}
}
}
}
void enqfro()
{
if(front==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
scanf("%d",&x->data);
x->link=NULL;
rear=x;
front=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
49
scanf("%d",&x->data);
x->link=NULL;
front->link=x;
front=x;
}
}
void deqrea()
{
if(rear==NULL)
printf("\nQueue is empty");
else
{
x=rear;
printf("\ndeleted elementis %d",x->data);
rear=x->link;
free(x);
}
}
void enqrea()
{
if(rear==NULL)
{
x=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
scanf("%d",&x->data);
x->link=NULL;
rear=x;
front=x;
}
else
{
x=(struct node*)malloc(sizeof(struct node));
printf("\nenter the data");
scanf("%d",&x->data);
x->link=NULL;
rear->link=x;
rear=x;
}
}
void deqfro()
{
if(front==NULL)
printf("\nQueue is empty");
else
{
x=front;
printf("\ndeleted elementis %d",x->data);
50
front=x->link;
free(x);
}
}
void display2()
{
if(front==NULL)
printf("\nqueue is empty");
else
{
x=front;
printf("\nqueue elements are");
while(x->link!=NULL)
{
printf("%d\t",x->data);
x=x->link;
}
printf("%d",x->data);
}
}
void display1()
{
if(rear==NULL)
printf("\nqueue is empty");
else
{
x=rear;
printf("\nqueue elements are");
while(x->link!=NULL)
{
printf("%d\t",x->data);
x=x->link;
}
printf("%d",x->data);
}
}

Output:

51
Result:

Thus the C program to implement the Deque is verified

Ex. No: 14 INSERTION IN AVL TREE


Date:

Aim:

To write a C program to perform insertion on an AVL tree

Algorithm:

1. Start the program


2. Declare all the functions and using the switch case select the operation to be performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. Check if (search (root, info) == NULL ), if so assign the value to the variable root
5. Insert an element into the tree.
6. After insertion check if the tree is balanced or not. If not balance the tree.
7. Stop the program.

Program:

52
#include<stdio.h>
#include<conio.h>
#include<stdlib.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);
main()
{
bool ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
clrscr();
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);
53
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 */
54
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;
55
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()*/
display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
56
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/

Output:

57
Result:

Thus the C program to perform insertion on an AVL tree is executed and verified

Ex. No: 15 BINARY SEARCH TREE


Date:

Aim:

To construct a binary search tree and perform various operation

Algorithm:

1. Start the program


2. Declare all the functions and using the switch case select the operation to be performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. In the insert function assign the memory and assign n->llink=NULL
n->rlink=NULL
5. In the deletion function perform deletion of node that has no child,having two children
6. In the inorder function perform recursion by calling inorder(head->rlink).

58
7. In the display function display the values
8. Stop the program

Program:

#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>
struct node
{
struct node *llink;
struct node *rlink;
int data;
};
void main()
{
struct node *head,*t;
int s,d;
struct node* finsert();
struct node* delenode(struct node*,int);
void insert(struct node *);
void inorder(struct node *);
clrscr();
head=NULL;
do
{
printf("\n1-Insertion");
printf("\n2-Deletion");
printf("\n3-Inorder");
printf("\n4-Exit");
printf("\nEnter Choice:");
scanf("%d",&s);
switch(s)
{
case 1://insertion
if(head==NULL)
{
head=finsert();
}
else
insert(head);
break;

case 2://Deletion
if(head==NULL)

59
printf("\nBinary Tree Empty.......");
else
{
printf("\nEnter Data to delete:");
scanf("%d",&d);
if(head->llink==NULL && head->rlink==NULL && head->data==d)
{
t=head;
head=NULL;
free(t);
}
else
head = delenode(head,d);
}
break;
case 3://to display
printf("\nIN-ORDER:");
if(head==NULL)
printf("\nBinary Tree Empty....");
else
inorder(head);
break;
case 4://exit
exit(0);
}
}while(s<5 ||s>0);
getch();
}
struct node* finsert()
{
struct node * head;
head=(struct node*)malloc(sizeof(struct node));
printf("\nEnter Data:");
scanf("%d",&head->data);
head->llink=NULL;
head->rlink=NULL;
return head;
}
void insert(struct node *head)
{
struct node *t,*n;
t=head;
n=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Data:");
scanf("%d",&n->data);
n->llink=NULL;
n->rlink=NULL;
60
while(t->llink!=NULL || t->rlink!=NULL)
{
if(t->llink!=NULL)
if(n->data < t->data)
t=t->llink;
if(t->rlink!=NULL)
if(n->data>=t->data)
t=t->rlink;
if((t->llink==NULL) && (n->data < t->data) && (n->data <t->rlink->data))
break;
if((t->rlink==NULL) && (n->data >= t->data) && (n->data >t->llink->data))
break;
}
if((n->data < t->data) && (t->llink==NULL))
t->llink=n;
if((n->data > t->data) && (t->rlink==NULL))
t->rlink=n;
}
void inorder(struct node * head)
{
if(head!=NULL)
{
inorder(head->llink);
printf("%d\t",head->data);
inorder(head->rlink);
}
}
struct node * delenode(struct node *head,int d)
{
int f=0,f1=0;
struct node *p,*t,*t1,*x;
t=head;
//to search found or not
while(t!=NULL)
{
if(t->data==d)
{
f=1;
x=t;
break;
}
if(t->data > d)
{
p=t;
t=t->llink;
}
else if(t->data <= d)
61
{
p=t;
t=t->rlink;
}
}
if(f==0)
{
printf("\nGiven element not found.......");
return head;
}
//Deleted node has no child
if(x->llink==NULL && x->rlink==NULL)
{
if(p->rlink==x)
p->rlink=NULL;
else
p->llink=NULL;
free(x);
return head;
}
//deleted node has 2 children
if(x->llink!=NULL && x->rlink!=NULL)
{
p=x;
t1=x->rlink;
while(t1->llink!=NULL)
{
p=t1; f1=1;
t1=t1->llink;
}
if(t1->llink==NULL && t1->rlink==NULL)
{
x->data=t1->data;
if(f1==1)
p->llink=t1->llink;
if(f1==0)
x->rlink=t1->rlink;
free(t1);
return head;
}
if(t1->rlink!=NULL)
{
x->data=t1->data;
if(f1==1)
p->llink=t1->rlink;
if(f1==0)
p->rlink=t1->rlink;
62
free(t1);
return head;
}
}
//Deleted node has oniy right child
if(x->llink==NULL && x->rlink!=NULL && x->data!=head->data)
{
if(p->llink==x)
p->llink=x->rlink;
else
p->rlink=x->rlink;
free(x);
return head;
}
//Deleted node has oniy left child
if(x->llink!=NULL && x->rlink==NULL && x->data!=head->data)
{
if(p->llink==x)
p->llink=x->llink;
else
p->rlink=x->llink;
free(x);
return head;
}
if(x->llink!=NULL && x->rlink==NULL && x->data==head->data)
{
head=x->llink;
free(p);
return head;
}
if(x->llink==NULL && x->rlink!=NULL && x->data==head->data)
{
head=x->rlink;
free(p);
return head;
}
}

Output:

63
Result:

Thus the binary search tree is constructed and various operation are performed using C
program.

64