DS LAB Manual
DS LAB Manual
INDEX
EX. NO. NAME OF THE EXPERIMENT
9. 9.A.Graph representation
Sort.
AIM:
To write a program in C to implement the stack ADT using array concept that
performs all the operations of stack.
ALGORITHM:
STEP 1: Define an array to store the element.
STEP 2: Get the users’ choice.
STEP 3: If the option is 1 perform creation operation and goto step4.
If the option is 2 perform insertion operation and goto step5.
If the option is 3 perform deletion operation and goto step6.
If the option is 4 perform display operation and goto step7.
STEP 4: Create the stack. Initially get the limit of stack and the get the items. If the limit of
stack is exceeds print the message unable to create the stack.
STEP 5: Get the element to be pushed. If top pointer exceeds stack capacity. Print Error
message that the stack overflow. If not, increment the top pointer by one and store the
element in the position which is denoted by top pointer.
STEP 6: If the stack is empty, then print error message that stack is empty. If not fetch the
element from the position which is denoted by top pointer and decrement the top pointer by
one
STEP 7: If the top value is not less than the 0 the stack is display otherwise print the message
“stack is empty”.
STEP 8: Stop the execution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 20
int opt, a[20],i,top=0,n;
void main()
{
void create(),push(),pop(),disp();
int wish;
do
{
clrscr();
printf("\nMENU");
printf("\n1.Create\n2.Push\n3.pop\n4.Display\n5.Exit\n");
printf("\nEnter your option");
scanf("%d",&opt);
switch(opt)
{
case 1:create();
break;
case 2:push();
break;
case 3:pop();
break;
case 4:disp();
break;
case 5:exit(0);
}
void create()
{
printf("\n Enter the limit of stack");
scanf("%d",&n);
if(n<max)
{
printf("\nEnter the items");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
top=n-1;
}
else
printf("\nUnable to create the stack");
}
void push()
{
int x;
if(top<max)
{
printf("\nEnter the element to be pushed:");
scanf("%d",&x);
top=top+1;
a[top]=x;
n=top;
}
else
printf("\n Stack is full");
}
void pop()
{
if(top<0)
printf("\n Stack is empty");
else
{
printf("\nThe element popped is %d",a[top]);
top=top-1;
n=top;
}}
void disp()
{
if(top<0)
printf("\n Stack is empty"); else
{
printf("\n The elements in the stack are:"); for(i=top;i>=0;i--)
printf("\n%d",a[i]);
}
}
OUTPUT:
RESULT:
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int front = - 1;
int rear = - 1;
int q[SIZE];
void insert( );
void del( );
void display( );
void main( )
{
int choice;
do
{
printf("\t Menu");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display ");
printf("\n 4. Exit");
printf("\n Enter Your Choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert( );
display( );
break;
case 2:
del( );
display( );
break;
case 3:
display( );
break;
case 4:
printf("End of Program. !!!!");
exit(0);
}}
while(choice != 4);
}
void insert( )
{
int no;
printf("\n Enter No.:");
scanf("%d", &no);
if(rear < SIZE - 1)
{
q[++rear]=no;
if(front == -1)
front=0;
// front=front+1;
}
else
{
printf("\n Queue overflow");
}}
void del( )
{
if(front == - 1)
{
printf("\n Queue Underflow");
return;
}
else
{
printf("\n Deleted Item:-->%d\n", q[front]);
}
if(front == rear)
{
front = - 1;
rear = - 1;
}
else{
front = front + 1;
}}
void display( )
{
int i;
if( front == - 1)
{
printf("\nQueue is empty. ");
return;
}
for(i = front; i<=rear; i++)
printf("\t%d",q[i]);
}
OUTPUT
RESULT:
AIM:
ALGORITHM:
Step1: Create nodes first, last; next, prev and cur then set the value as NULL.
Step 2: Read the list operation type.
Step 3: If operation type is create then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Assign cur node as NULL.
4. Assign first=last=cur.
Step 4: If operation type is Insert then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Read the position the Data to be insert.
4. Availability of the position is true then assing cur's node as first and first=cur.
5. If availability of position is false then do following steps.
1. Assign next as cur and count as zero.
2. Repeat the following steps until count less than postion.
1 .Assign prev as next
2. Next as prev of node.
3. Add count by one.
4.If prev as NULL then display the message INVALID POSITION.
5.If prev not qual to NULL then do the following steps.
1.Assign cur's node as prev's node.
2.Assign prev's node as cur.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
clrscr();
int ch;
char g='y';
do
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y'); getch();
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}
}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}}}
void insert()
{
printf("\n Enter the position u need to insert::"); scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p; n++;
}
RESULT:
Thus the C program for array implementation of List ADT was created, executed and output
was verified successfully
EX. NO: 3A STACK ADT USING LINKED LIST
AIM:
ALGORITHM:
1. Define a struct for each node in the stack. Each node in the stack contains data and
link to the next node. TOP pointer points to last node inserted in the stack.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*top,*new1,*first;
void main()
{
int wish,opt;
void create(),push(),pop(),view();
do
{
clrscr();
printf("Stack using linked list menu");
printf("\n1.Create\n2.Push\n3.Pop\n4.View\n5.Exit\n");
printf("\nEnter your option(1,2,3,4,5):");
scanf("%d",&wish);
switch(wish)
{
case 1: create();
break;
case 2: push();
break;
case 3: pop();
break;
case 4: view();
break;
case 5: exit(0);
}
printf("\nDo you wnat to continue(0/1):");
scanf("%d",&opt);
}while(opt==1);
}
void create()
{
int ch;
top=(struct node*)malloc(sizeof(struct node));
top->next=NULL;
do
{
clrscr();
printf("Enter the data:\n");
scanf("%d",&top->data);
printf("Do you want to insert another(1/0)\n");
scanf("%d",&ch);
{
new1=(struct node*)malloc(sizeof(struct node));
new1->next=top;
top=new1;
first=top;
break;
}
else
}while(ch==1);
}
void push()
{
top=first;
new1=(struct node*)malloc(sizeof(struct node));
printf("Enter the element to be pushed:");
scanf("%d",&new1->data); new1->next=top; top=new1;
first=top;
}
void pop()
{
clrscr();
top=first;
if(top==NULL)
printf("\n Stack is empty");
else
{
printf("\nThe element popped out from stack is %d",top->data);
top=top->next;
first=top;
}}
void view()
{
printf("\nStack contents\n");
while(top->next!=NULL)
{
printf("%d->",top->data);
top=top->next;
}
printf("%d\n",top->data);
getch();
}
OUTPUT
RESULT:
Thus the C program for array implementation of Stack ADT was created, executed and
output was verified successfully
EX. NO :3B QUEUE ADT USING LINKED LIST
AIM:
To write a C program for Queue using Linked implementation.
ALGORITHM:
1. Define a struct for each node in the queue. Each node in the queue contains data and
link to the next node. Front and rear pointer points to first and last node inserted in the queue.
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
void delet();
void display();
int item;
void main()
{
int ch;
do
{
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Please try again...\n");
}
} while(1);
getch();
}
void insert()
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else
{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}}
void delet()
{
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
while(ptr != NULL)
{
printf("%d\t",ptr->info);
ptr = ptr->link;
}}}
OUTPUT
RESULT:
Thus the C program for array implementation of Queue ADT was created, executed and
output was verified successfully.
EXNO:4A REPRESENT A POLYNOMIAL AS A LINKED LIST
AIM:
To write program in C to convert given infix expression in to postfix notation
ALGORITHM:
2: For addition of two polynomials if exponents of both the polynomials are same then we ad
the coefficients. For storing the result we will create the third linked lists say P3.
6: Continue the above step from 3 to 5 until end o the two polynomials.
#include<stdio.h>
#include<conio.h>
main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();
printf("\n\tPolynomial Addition\n");
printf("\t===================\n");
printf("\n\tEnter the no. of terms of the polynomial:");
scanf("%d", &m);
printf("\n\tEnter the degrees and coefficients:");
for (i=0;i<2*m;i++)
scanf("%d", &a[i]);
printf("\n\tFirst polynomial is:");
k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]);
else
printf("%dx^%d", a[k1+1],a[k1]);
k1+=2;
while (k1<i)
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}
printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);
printf("\n\tEnter the degrees and co-efficients:");
for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");
k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);
k1+=2;
}
i=0; j=0; k=0;
while (m>0 && n>0)
{
if (a[i]==b[j])
{
c[k+1]=a[i+1]+b[j+1];
c[k]=a[i];
m--;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{
c[k+1]=a[i+1];
c[k]=a[i];
m--;
i+=2;
}
else
{
c[k+1]=b[j+1];
c[k]=b[j];
n--;
j+=2;
} k+=2;
}
while (m>0)
{
c[k+1]=a[i+1];
c[k]=a[i];
k+=2;
i+=2;
m--;
}
while (n>0)
{
c[k+1]=b[j+1];
c[k]=b[j];
k+=2;
j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:");
k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]);
else
printf("%dx^%d", c[k1+1],c[k1]);
k1+=2;
while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]);
else
printf("+%dx^%d", c[k1+1], c[k1]);
k1+=2;
}
getch();
return 0;
}
OUTPUT
RESULT:
Thus the program in C to convert given infix expression in to postfix notation
EX.NO:4B CONVERSION OF INFIX EXPRESSION TO POSTFIX NOTATION
AIM:
To write program in C to convert given infix expression to postfix notation
ALGORITHM:
5: If the incoming operator has less priority than the stack symbol then copy the symbol at the
top of the stack and then print until the condition becomes false and push the following
operator on the stack.
6: If the symbol is ‘)’ then copy operators from top of the stack. Deletion opening parenthesis
is from top of the stack.
#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}
main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}
OUTPUT
RESULT:
AIM:
ALGORITHM:
2. Compare the inserting element with root, if less than root, then recurse for left, else
recurse for right.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
} *root = NULL, *node = NULL, *temp = NULL;
int main()
{
int key, choice;
while(choice != 7)
{
printf("1. Insert\n2. Search\n3. Delete\n4. Display\n5. Min Value\n6.
Max Value\n7. Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter the value to insert:\n");
scanf("%d", &key);
root = insert(key, root);
break;
case 2:
printf("\nEnter the value to search:\n");
scanf("%d", &key);
search(key,root);
break;
case 3:
printf("\nEnter the value to delete:\n");
scanf("%d", &key);
delete(root,key);
break;
case 4:
printf("Preorder:\n");
preorder(root);
printf("Inorder:\n");
inorder(root);
printf("Postorder:\n");
postorder(root);
break;
case 5:
if(minvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Minimum value is %d\n", minvalue(root)- >data);
break;
case 6:
if(maxvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Maximum value is %d\n", maxvalue(root)- >data);
break;
case 7:
printf("Bye Bye!\n");
exit(0);
break;
default:
printf("Invalid choice!\n");
}
}
return 0;
OUTPUT
RESULT:
Thus the program in C is implemented Binary Tree and Operations of Binary Trees.
EX. NO: 6 IMPLEMENTATION OF BINARY SEARCH TREE
AIM:
To write a C program to implementation of binary search tree.
ALGORITHM:
1. Declare function create (), search (), delete (), Display ().
2. Create a structure for a tree contains left pointer and right pointer.
3. Insert an element is by checking the top node and the leaf node and the operation will
be performed.
4. Deleting an element contains searching the tree and deleting the item.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}*t,*temp;
int element;
void inorder(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);
struct tree * create(struct tree *, int);
struct tree * find(struct tree *, int);
struct tree * insert(struct tree *, int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
struct tree * findmax(struct tree *);
void main()
{
int ch;
do
{
printf("\n\t\t\tBINARY SEARCH TREE");
printf("\n\t\t\t****** ****** ****");
printf("\nMain Menu\n");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");
printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the data:");
scanf("%d",&element);
t=create(t,element);
inorder(t);
break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=insert(t,element);
inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=del(t,element);
inorder(t);
break;
case 4:
printf("\nEnter the data:");
scanf("%d",&element);
temp=find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=findmin(t);
printf("\nMax element=%d",temp->data);
break;
case 6:
temp=findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
inorder(t);
break;
case 8:
preorder(t);
break;
case 9:
postorder(t);
break;
case 10:
exit(0);
}
}while(ch<=10);
}
return t;
}
else if(t->rchild==NULL)
t=t->lchild;
free(temp);
}
RESULT:
Thus the C program for binary search tree was created, executed and output was verified
successfully.
EX NO:7 IMPLEMENTATION OF AVL TREE
AIM:-
To write a C program to implement insertion in AVL trees.
ALGORITHM:-
PROGRAM
#include<stdio.h>
#include<malloc.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};
main()
{
bool ht_inc; int info ; int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
while(1)
{
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
if( search(root,info) == NULL )
root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2: if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
struct node *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;
}/*End of display()*/
RESULT
Thus the ‘C’ program to implement an AVL trees . Produce its pre-Sequence, In-
Sequence, and Post- Sequence traversals
EXNO:8 IMPLEMENTATION OF PRIORITY QUEUE USING HEAPS
AIM:
ALGORITHM:
6. Else if element value is less than the root value, insert element at the left of the root.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
enum {FALSE=0,TRUE=-1};
struct Node
{
struct Node *Previous;
int Data;
struct Node *Next;
}Current;
struct Node *head;
struct Node *ptr;
static int NumOfNodes;
int PriorityQueue(void);
int Maximum(void);
int Minimum(void);
void Insert(int);
int Delete(int);
void Display(void);
int Search (int);
void main()
{
int choice;
int DT;
PriorityQueue();
while(1)
{
printf("\nEnter ur Choice:");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Search\n5.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter a data to enter Queue");
scanf("%d",&DT);
Insert(DT);
break;
case 2:
Display();
break;
case 3:
{
int choice,DataDel;
printf("\nEnter ur choice:");
printf("\n1.Maximum Priority queue\n2.Minimum priority Queue\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
DataDel=Maximum();
Delete(DataDel);
printf("\n%d is deleted\n",DataDel);
break;
case 2:
DataDel=Minimum();
Delete(DataDel);
printf("\n%d is deleted\n",DataDel);
break;
default:
printf("\nSorry Not a correct Choice\n");
}
}
break;
case 4:
printf("\nEnter a data to Search in Queue:");
scanf("%d",&DT);
if(Search(DT)!=FALSE)
printf("\n %d is present in queue",DT);
else
printf("\n%d is not present in queue",DT);
break;
case 5:
exit(0);
default:
printf("\nCannot process ur choice\n");
} }}
int PriorityQueue(void)
{
Current.Previous=NULL;
printf("\nEnter first element of Queue:");
scanf("%d",&Current.Data);
Current.Next=NULL;
head=&Current; ptr=head;
NumOfNodes++; return;
}
int Maximum(void)
{
int Temp; ptr=head;
Temp=ptr->Data;
while(ptr->Next!=NULL)
{
if(ptr->Data>Temp)
Temp=ptr->Data;
ptr=ptr->Next;
}
if(ptr->Next==NULL && ptr->Data>Temp)
Temp=ptr->Data;
return(Temp);
}
int Minimum(void)
{
int Temp;
ptr=head;
Temp=ptr->Data;
while(ptr->Next!=NULL)
{
if(ptr->Data<Temp)
Temp=ptr->Data;
ptr=ptr->Next;
}
if(ptr->Next==NULL && ptr->Data<Temp)
Temp=ptr->Data;
return(Temp);
}
temp=ptr->Next;
free(temp);
ptr->Next=NULL;
NumOfNodes--;
return(TRUE);
}
}
return(FALSE);
}
void Display(void)
{
ptr=head;
printf("\nPriority Queue is as Follows:-\n");
while(ptr!=NULL)
{
printf("\t\t%d",ptr->Data); ptr=ptr->Next;
}
}
OUTPUT
RESULT:
Thus the Priority Queue using Binary Heap is implemented and the result is verified
successfully.
Ex:10.A GRAPH REPRESENTATIONS
AIM:
ALGORITHM:
PROGRAM
#include <stdio.h>
#include <stdlib.h>
void main()
{
int option;
do
{
printf("\n A Program to represent a Graph by using an ");
printf("Adjacency Matrix method \n ");
printf("\n 1. Directed Graph ");
printf("\n 2. Un-Directed Graph ");
printf("\n 3. Exit ");
printf("\n\n Select a proper option : ");
scanf("%d", &option);
switch(option)
{
case 1 : dir_graph();
break;
case 2 : undir_graph(); break;
case 3 : exit(0);
} // switch
}while(1);
}
int dir_graph()
{
int adj_mat[50][50];
int n;
int in_deg, out_deg, i, j;
printf("\n How Many Vertices ? : ");
scanf("%d", &n);
read_graph(adj_mat, n);
printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
for (i = 1; i <= n ; i++ )
{
in_deg = out_deg = 0;
for ( j = 1 ; j <= n ; j++ )
{
if ( adj_mat[j][i] == 1 )
in_deg++;
}
for ( j = 1 ; j <= n ; j++ )
if (adj_mat[i][j] == 1 )
out_deg++;
printf("\n\n%5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg);
}return;
}
int undir_graph()
{
int adj_mat[50][50];
int deg, i, j, n;
printf("\n How Many Vertices ? : ");
scanf("%d", &n);
read_graph(adj_mat, n);
printf("\n Vertex \t Degree ");
for ( i = 1 ; i <= n ; i++ )
{
deg = 0;
for ( j = 1 ; j <= n ; j++ )
if ( adj_mat[i][j] == 1)
deg++;
printf("\n\n %5d \t\t %d\n\n", i, deg);
}
return;
}
return;
}
OUTPUT
RESULT:
AIM:
ALGORITHM:
DFS
BFS
1. Define a Queue of size total number of vertices in the graph.
2. Select any vertex as starting point for traversal. Visit that vertex and insert it into the
Queue.
3. Visit all the adjacent vertices of the verex which is at front of the Queue which is not
visited and insert them into the Queue.
4. When there is no new vertex to be visit from the vertex at front of the Queue then
delete that vertex from the Queue.
5. Repeat step 3 and 4 until queue becomes empty.
6. When queue becomes Empty, then produce final spanning tree by removing unused
edges from the graph
PROGRAM
include<stdio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
do
{
for(i=1;i<=n;i++)
vis[i]=0;
printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);
switch(ch)
{
case 1:bfs(s,n); break;
case 2:dfs(s,n); break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
}
int delete()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}}
OUTPUT
RESULT:
AIM:
ALGORITHM:
Linear Search:
1. Read the search element from the user
2. Compare, the search element with the first element in the list.
3. If both are matching, then display "Given element found!!!" and terminate the
function
4. If both are not matching, then compare search element with the next element in the
list.
5. Repeat steps 3 and 4 until the search element is compared with the last element in the
list.
6. If the last element in the list is also doesn't match, then display "Element not found!!!"
and terminate the function.
Binary search :
1. Read the search element from the user
2. Find the middle element in the sorted list
3. Compare, the search element with the middle element in the sorted list.
4. If both are matching, then display "Given element found!!!" and terminate the
function
5. If both are not matching, then check whether the search element is smaller or larger
than middle element.
6. If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5
for the left sublist of the middle element.
7. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for
the right sublist of the middle element.
8. Repeat the same process until we find the search element in the list or until sublist
contains only one element.
9. If that element also doesn't match with the search element, then display "Element not
found in the list!!!" and terminate the function.
PROGRAM
#include <stdio.h>
void sequential_search(int array[], int size, int n)
{
int i;
for (i = 0; i < size; i++)
{
if (array[i] == n)
{
printf("%d found at location %d.\n", n, i+1);
break;
}
}
if (i == size)
printf("Not found! %d is not present in the list.\n", n);
}
int main()
{
int a[200], i, j, n, size;
printf("Enter the size of the list:");
scanf("%d", &size);
printf("Enter %d Integers in ascending order\n", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
printf("Enter value to find\n");
scanf("%d", &n);
printf("Sequential search\n");
sequential_search(a, size, n);
printf("Binary search\n");
binary_search(a, size, n); return 0;
}
OUTPUT
RESULT
Thus the C Program to implement different searching techniques – Linear and Binary
search
ALGORITHM:
1: Start.
2: Repeat Steps 3 and 4 for i=1 to 10
3: Set j=1
4: Repeat while j<=n
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, temp , a[100];
printf("Enter the total integers you want to enter (make it less than 100):\n");
scanf("%d",&n);
printf("Enter the %d integer array elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j+1]<a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}}}
OUTPUT
RESULT:
Thus a C program for the concept of bubble sort was implemented successfully
ALGORITHM:
1: Start.
2: First you divide the number of elements by 2 and seperate them as two. 3: Divide those
two which are divided by 2.
4: Divide them until you get a single element.
5: Start comparing the starting two pair of elements with each other and place them in
ascending order.
6: When you combine them compare them so that you make sure they are sorted.
7: When all the elements are compared the array will be surely sorted in an ascending order.
8: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void merge(int [],int ,int ,int );
void part(int [],int ,int );
void main()
{
int arr[30];
int i,size;
printf("\n\t------- Merge sorting method \n\n");
printf("Enter total no. of elements : ");
scanf("%d",&size);
for(i=0; i<size; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&arr[i]);
}
part(arr,0,size-1);
printf("\n\t------- Merge sorted elements \n\n");
for(i=0; i<size; i++)
printf("%d ",arr[i]); getch();
}
RESULT:
Thus a C program for the concept of merge sort was implemented successfully.
Ex. No:11.B.3 QUICK SORT
AIM:
ALGORITHM:
1: Start.
3: Divide all other elements (except the pivot) into two partitions.
All elements less than the pivot must be in the first partition.
All elements greater than the pivot must be in the second partition.
5: Join the first sorted partition, the pivot, and the second sorted partition. 6: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void qsort(int arr[20], int fst, int last);
void main()
{
int arr[30];
int i,size;
printf("Enter total no. of the elements : ");
scanf("%d",&size);
printf("Enter total %d elements : \n",size);
for(i=0; i<size; i++)
scanf("%d",&arr[i]);
qsort(arr,0,size-1);
printf("Quick sorted elements are as : \n");
for(i=0; i<size; i++)
printf("%d\t",arr[i]);
getch();
}
OUTPUT
RESULT:
AIM:
ALGORITHM:
1. Create a structure, data (hash table item) with key and value as data.
2. Now create an array of structure, data of some certain size (10, in this case). But, the
size of array must be immediately updated to a prime number just greater than initial array
capacity (i.e 10, in this case).
4. User must choose one option from four choices given in the menu
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct data
{
int key;
int value;
};
if ( n == 1||n == 0)
{
return 0;
}
void init_array()
{
int i;
capacity = get_prime(capacity);
array = (struct data*) malloc(capacity * sizeof(struct data));
for (i = 0; i < capacity; i++)
{
array[i].key = 0;
array[i].value = 0;
}}
int size_of_hashtable()
{
return size;
}
void main()
{
case 2:
case 3:
n = size_of_hashtable();
printf("Size of Hash Table is-:%d\n", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input\n");
}
getch();
}
OUTPUT
RESULT: