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

Problem statement 

: 1.
Design, Develop and Implement a menu driven Program in C for the following Array
Operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Exit.
 Support the program with functions for each of the above operations.

Array:
The simplest type of data structure is a linear (or one dimensional) array. A list of a finite
number n of similar data referenced respectively by a set of n consecutive numbers, usually 1,
2, 3 . . . . . . . n.
if A is chosen the name for the array, then the elements of A are denoted bysubscript notation
a1, a2, a3….. an or by the parenthesis notation A (1), A (2), A (3) . . . . . . A (n) or by the
bracket notation A [1], A [2], A [3] . . . . . . A [n]

Program implementation:

#include<stdio.h>
#include<stdlib.h>
int a[10],n;               //initialization of variables
void create();       //function declaration
void display();
int main()              
{
 int ch,a;
do
 {
                  printf("\n\n______MENU_____\n");
                  printf("1.Create\n 2.Display\n 3..Exit\n");
                  printf("Enter Your Choice:");
scanf("%d",&ch);
                  switch(ch)
                  {
                           case 1:create();
                                                  break;
                             case 2:display();
                                                  break;
                                  case 3:exit(0);
                                                   break;
                           default :printf("INVALID CHOICE\n");
                  }
                printf("\n do you like to continue  (1/0) ? \n");
                scanf("%d",&a);
 }while (a==1);
return 0;
}
void create()
{
         int i;
         printf("Enter the size of array\n");
         scanf("%d",&n);
         printf("Enter the elements of array\n");
         for(i=0; i<n; i++)
                  scanf("%d",&a[i]);
}
void display()
{
 int i;
 printf("The array elements are:\n");
 for(i=0; i<n; i++)
                  printf("%d\t",a[i]);
}

Output:

inux:~/dslab # gedit array.c


linux:~/dslab # cc array.c
linux:~/dslab # ./a.out
--------MENU-----------
1.CREATE
2.DISPLAY
3.EXIT
-----------------------
ENTER YOUR CHOICE: 1
Enter the size of the array elements: 3
Enter the elements for the array
10 25 30
ENTER YOUR CHOICE: 2
The array elements are:
10 25 30
ENTER YOUR CHOICE: 4
INVALID CHOICE
ENTER YOUR CHOICE: 3
Problem Statement -02
Design, Develop and Implement a menu driven Program in C for the following Array
operations

1.  Inserting an Element (ELEM) at a given valid Position (POS)


2. Deleting an Element at a given valid Position POS)
3. Display of Array Elements
4.   Exit.   Support the program with functions for each of the above operations.

Array operations:
Traversing, Insertion, Deletion and searching are the basic operation on the data
structure .This program is the implementation of insertion and deletion. The algorithm for
both the operations on array is as follows:
Insertion:
Inserting an element at the “end” of the linear array can be easily done provided the memory
space allocated for the array is large enough to accommodate the additional element.
 Inserting an element in the middle of the array, then on average, half of the elements must be
moved downwards to new locations to accommodate the new element and keep the order of
the other elements.

Algorithm: INSERT (LA, N, K, ITEM)  


Here LA is a linear array with N elements and K is a positive integer such that K ≤ N. This
algorithm inserts an element ITEM into the Kth position in LA.
1. [Initialize counter] set J:= N
2. Repeat step 3 and 4 while J ≥ K
3. [Move Jth element downward] Set LA [J+1] := LA[J]
4. [Decrease counter] set J:= J – 1
 
[End of step 2 loop]
5. [Insert element] set LA[K]:= ITEM
6. [Reset N] set N:= N+1
7. Exit

Deleting
Deleting an element at the “end” of the linear array can be easily done with difficulties.
If element at the middle of the array needs to be deleted, then each subsequent elements be
moved one location upward to fill up the array.

Algorithm: DELETE (LA, N, K, ITEM)


Here LA is a linear array with N elements and K is a positive integer such that K ≤ N. this
algorithm deletes the Kth element from LA
1. Set ITEM:= LA[K]
2. Repeat for J = K to N – 1
[Move J + 1 element upward] set LA[J]:= LA[J+1]
[End of loop]
3. [Reset the number N of elements in LA] set N:= N – 1
4. Exit
 
Program Implementation:

#include<stdio.h>                                //Header files.


#include<stdlib.h>
int a[10],n,elem,i,pos;               //Global declaration of variables
void create();                            //function declaration
void insert();
void delete();
void display();
int main()                                    
{
                int ch,a;
do
 {
                  printf("\n\n______MENU_____\n");
                      printf(" 1.INSERT\n 2.DELETE\n 3.DISPLAY\n 4.EXIT\n");
                  printf("Enter Your Choice:");
                  scanf("%d",&ch);
                      switch(ch)
                      {
                               case 1:insert();
                                break;
 
                                 case 2:delete();
                                  break;
                                case 3:display();
                                  break;
 
                                case 4:exit(0);
                                   break;
                               default :printf("INVALID CHOICE\n");
                      }
                    printf("\n Do you like to continue  (1/0) ? \n");
                scanf("%d",&a);
 }while (a==1);
return 0;
}
void create()
{
             int i;
             printf("Enter the size of array\n");
         scanf("%d",&n);
             printf("Enter the elements of array\n");
             for(i=0; i<n; i++)
                  scanf("%d",&a[i]);
}
 
void display()
{
                int i;
                printf("The array elements are:\n");
                for(i=0; i<n; i++)
                                printf("%d\t",a[i]);
}
void insert()
{
                 create();
 int i;
 printf("Enter the position for new element:\n");
                scanf("%d",&pos);
 printf("Enter the element to be inserted:\n");
 scanf("%d",&elem);
 for (i = n-1; i >=pos; i--)
                            a[i+1]=a[i];
                a[pos]=elem;
 n=n+1;
}
void delete()
{
                printf("Enter  position of element to be deleted:\n");
 scanf("%d",&pos);
                elem=a[pos];
                for (int i = pos; i < n-1; i++)
                            a[i]=a[i+1];
                n=n-1;
                printf("Deleted element is %d\n",elem );
}

Output:

______MENU_____
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:1
Enter the size of array  3
Enter the elements of array
23             42           62
Enter the position for new element:0
Enter the element to be inserted:0
do you like to continue  (1/0) ? 1
______MENU_____
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:3
The array elements are:
0              23            42            62            
 do you like to continue  (1/0) ?1
______MENU_____
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:2
Enter  position of element to be deleted:0
Deleted element is 0
  do you like to continue  (1/0) ?1
______MENU_____
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:3
The array elements are:
23            42            62            
 do you like to continue  (1/0) ? 1
______MENU_____
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice:5
INVALID CHOICE
Problem Statement -03
Design, Develop and Implement a menu driven Program in C for the following
operations on STACK of Integers (Array Implementation of Stack with maximum size
MAX)
a)    Push an Element on to Stack
b)    Pop an Element from Stack
c)     Demonstrate Overflow and Underflow situations on Stack
d)    Display the status of Stack
e)     Exit
Support the program with appropriate functions for each of the above operation

Stack:
“A stack is an ordered list of elements in which insertions (pushes) and deletions (pops) are
made at one end called the top.”
The last element inserted into a stack is the first element removed, a stack is also known as a
Last-In-First-Out (LIFO) list.
Stack operations:
PUSH:
This operation is used to insert the elements onto the stack. The first inserted element will be
at the bottom of the stack and the last element inserted will be at the top most element of
stack.
procedure:PUSH(STACK,TOP,MAXSTK,ITEM)
This procedure pushes an item onto a stack.
1.If TOP=MAXSTK,then:Wrtie:OVERFLOW and Return.
2.Set TOP=TOP+1
3.Set STACK[TOP]=ITEM.
4.Return.
POP:
This operation is used to remove the elements from the stack. The first inserted element will
be at the bottom of the stack and the last element inserted will be at the top most element of
stack. The first element will be removed at last and the last element inserted will be removed
first respectively.
procedure:POP(STACK,TOP,ITEM)
This procedure deletes the top element of STACK and assigns it to the variable ITEM.
1.If TOP=0 then:Write:UNDERFLOW and Return.
2.Set ITEM =STACK[TOP].
3.Set TOP=TOP-1.
4.Return.

Program implementation:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top = -1;
void push();
void pop();
void display();
int main()
{
        int choice;
                 while(choice)
        {
                        printf("\n\n--------STACK OPERATIONS-----------\n");
                        printf("1.Push\n");
                        printf("2.Pop\n");
                        printf("3.Display\n");
                        printf("4.Exit\n");
                        printf("-----------------------");
                        printf("\nEnter your choice:\t");
                        scanf("%d",&choice);
                        switch(choice)
                        {
                            case 1:push();
                                        break;
                                case 2:pop();
                                        break;
                            case 3:display();
                                               break;
                                  case 4:exit(0);
                                               break;
                                 default:printf("\nInvalid choice:\n");
                                        break;
    }
}
return 0;
}
void push() //Inserting element into the stack
{
        int item,n;
            if(top==(max_size-1))
        {
                        printf("\nStack Overflow:");
        }
        else
        {
                        printf("Enter the element to be inserted:\t");
                        scanf("%d",&item);
                        top=top+1;
                        stack[top]=item;
        }
}
void pop() //deleting an element from the stack
{
        int item;
            if(top==-1)
        {
                        printf("Stack Underflow:");
        }
        else
        {
                        item=stack[top];
                        top=top-1;
                        printf("\nThe poped element: %d\t",item);
        }
}
void display()
{
        int i;
            if(top==-1)
        {
                        printf("\nStack is Empty:");
        }
        else
        {
                        printf("\nThe stack elements are:\n" );
                        for(i=top;i>=0;i--)
                        {
                                   printf("%d\n",stack[i]);
                        }
        }
}

OUTPUT:

--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Display
4.Exit
-----------------------
Enter your choice:                1
Enter the element to be inserted:          12
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Display
4.Exit
-----------------------
Enter your choice:                1
Enter the element to be inserted:          13
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Display
4.Exit
-----------------------
Enter your choice:                2
The poped element: 13
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Display
4.Exit
-----------------------
Enter your choice:                3
The stack elements are:  12
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Display
4.Exit
-----------------------
Enter your choice:                5
Invalid choice
Problem Statement -04
Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,
 b. Solving Tower of Hanoi problem with n disks
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,

Postfix expression:
It refers to the analogous notation in which the operator symbol is placed after its two
operands.
Eg: 23+

Evaluation of postfix expression:

Algorithm: This algorithm finds the value of an arithmetic expression P written in postfix
notation.
1.scan P from left to right and repeat steps 3 and 4 for each element of P until the last sentinel
is encountered.
2. If an operand is encountered,put it on STACK.
3.If an operator is encountered, then:
a.Remove the 2 top elements of STACK,where OP2 is the top element and OP1 is the
next-to-top element.
b.Evaluate OP1 operator OP2.
c.Place the result of (b) back on STACK.
[End of If]
[End of step 2 Loop]
4.Set VALUE equal to the top element on STACK.
5.Exit.

Program Implementation:

#include<stdio.h>
#include<math.h>
#include<string.h>
float compute(char symbol, float op1, float op2)
{
              switch (symbol)
              {
                 case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '%':return (int)op1 %(int)op2;
case '^': return pow(op1,op2);
default : return 0;
         }
}
void main()
{
float s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;
printf("\nEnter the postfix expression:\n");
scanf ("%s", postfix);
top=-1;
for (i=0; i<strlen(postfix) ;i++)
{
symbol = postfix[i];
if(isdigit(symbol))
 s[++top]=symbol - '0';
else
 {
 op2 = s[top--];
 op1 = s[top--];
 res = compute(symbol, op1, op2);
 s[++top] = res;
 }
}
res = s[top--];
printf("\nThe result is : %f\n", res);

OUTPUT:

Enter postfix expression:


23+
The result is : 5.000

 b. Solving Tower of Hanoi problem with n disks.

Procedure:TOWER(N,BEG,AUXEND)
This procedure gives a recursive solution to the tower of hanoi problem for N disks.
1.If N=1 then:

1. write:BEG →END.
2. Return.

[END of IF]
2.[Move N-1 disks from peg BEG to peg AUX]
        Call TOWER(N-1,BEG,END,AUX)
3.write:BEG →END.
4.[Move N-1 disks from peg AUX to peg END]
        Call TOWER(N-1,AUX,BEG,END)
5.Return.

Program Implementation:

#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("enter the number of disks:");
scanf("%d", &num);
towers(num,'A', 'B', 'C');
return 0;
}
void towers(int num, char beg, char aux, char end)
{
if (num == 1)
{
printf("%c to %c\n",beg,end);
Return;
}
towers(num - 1, beg, end, aux);
printf("%c to %c\n",beg,end);
towers(num - 1, aux,beg,end);
}

OUTPUT:

enter the number of disks:2


A to B
A to C
B to C
Problem Statement -05
 Singly Linked List (SLL) of Integer Data
a. Create a SLL stack of N integers.
b. Display of SLL
c. Linear search.
Create a SLL queue of N Students Data Concatenation of two SLL of integers
 
Singly Linked List (SLL): It is a linear collection of data elements,called nodes,where the
linear order is given by means of pointers. That is,each node is divided into 2 parts:the first
part contains the information of the element and the second part, called the link field or next
pointer feild.  

Traversing (Display)
Let LIST be a linked list in memory.This algorithm traverses LIST,applying an operation
PROCESS to each element of LIST.The variable PTR points to the node currently being
processed.
1.set PTR=START.
2.Repeat steps 3 and 4 while PTR!=NULL.
3.Apply PROCESS to INFO[PTR]
4.set PTR=LINK[PTR].
5.Exit.

Search:SEARCH(INFO,LINK,START,ITEM,LOC)
1.set PTR=START
2.Repeat step 3 while PTR!=NULL:
3.If ITEM==INFO[PTR]
Set LOC=PTR,and exit.
Else:
Set PTR=LINK[PTR].
4.set LOC=NULL
5.Exit.

Concatenate:CONCAT(SLL1,SLL2)
1.if (SLL1==NULL):
2.   Return SLL2.
3.if(SLL2==NULL):
4.return SLL1.
5.Repeat steps 6 while LINK[PTR]!=NULL.
6.               PTR=LINK[PTR]
7.LINK[PTR]=SLL2.
8.return SLL1.

Program Implementation:

#include<stdio.>
#include<stdlib.>
struct node {
int data;
struct node *link;
};
Struct node *start,*nextnode,*node_1,*list,*list1,*list2;
struct node *create();
struct node *display(struct node*);
struct node *push(struct node *);
struct node *concatenate(struct node *,struct node *);
void linearsearch(struct node *);
void unlinearsearch(struct node *);
struct node *pop(struct node *);
struct node *addq(struct node *);
struct node *deleteq(struct node
*);

int main(){
list1=create();
 list2=create();
list = concatenate(list1,list2);
printf("\n");
display(list);
list=push(list);
printf("\nAfter insertion :\n");
display(list);
 
 
list=pop(list);
printf("\nAfter deleting :\n");
display(list);
list=addq(list);
printf("\nAfter insertion in queue:\n");
display(list);
list=deleteq(list);
printf("\nAfter deleting in queue :\n");
display(list);
}
 
struct node *create(){
int n;
printf("Number of nodes in linked list-\t");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
node_1=(struct node *)malloc(sizeof(struct node));
printf("enter value of node--\t");
scanf("%d",&node_1->data);
node_1->link=NULL;
if(start==NULL){
start=nextnode=node_1;
}
else{
nextnode->link=node_1;
nextnode=nextnode->link;
}
}
return list;
}
struct node *display(struct node *list){ struct node *ptr=start;
while(ptr!=NULL){
printf("%d\t",ptr->data); ptr=ptr->link;
}
}
 
struct node *push(struct node *list){
 struct node *new=(struct node *)malloc(sizeof(struct node));
 printf("\n enter the value to be pushed in stack:\t");
scanf("%d",&new->data);
new->link=start;
start=new;
return list;
}
struct node *pop(struct node
*list){
struct node *tmp=start;
start=start->link; free(tmp);
 return list;
}
struct node *addq(struct node *list){
struct node *newnode,*rear=start;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nenter the value to addq\n");
scanf("%d",&newnode->data);
newnode->link=NULL; while(rear->link!=NULL){
rear=rear->link;
}
rear->link=newnode;
 return list;
}
struct node *deleteq(struct node *list){
struct node *tmp=start;
start=start->link; free(tmp); return list;
}
struct node *concatenate(struct node *list1,struct node *list2){
struct node *ptr;
if(list1==NULL)
return list2; if(list2==NULL)
 return list1;
ptr=list1;
while(ptr->link!=NULL){ ptr=ptr->link;
}
ptr->link=list2;
 return list1;
}
void unlinearsearch(struct node *list)
{
struct node
*ptr=start,*loc=NULL;
 int item;
printf("\nenter the value to be searched\n");
scanf("%d",&item);
while(ptr!=NULL)
{
if(item == ptr->data)
{
loc=ptr;
}
ptr=ptr->link;
}
 if(loc==NULL)
{
 printf("item not found");
}
Else{
printf("item found");
}
}
Problem Statement -06
Design, Develop and Implement a menu driven Program in C for the following
operationson Doubly Linked List (DLL) of Professor Data with the fields: ID, Name,
Branch, Area of specialization.
a. Create a DLL stack of N Professor’s Data.
 b. Create a DLL queue of N Professor’s Data
Display the status of DLL and count the number of nodes in it.

Program implementation
#include <stdio.h>
#include<stdlib.h>
struct node{
    int id;
    char pname[18];
    char branch[18];
    char aos[18];
    struct node *prev;
    struct node *next;
};
struct node *start,*new,*temp,*list;
struct node *create();
void display(struct node *);
struct node *push(struct node *);
struct node *pop(struct node *);
struct node *addq(struct node *);
struct node *deleteq(struct node *);
int main()
{
  int ch,a;
  do{
  printf("1.create.\n2.display.\n3.stack_push.\n4.stack_pop.\n5.Add_Queue.\
n6.delete_Queue.\n");
  scanf("%d",&ch);
  switch(ch){
  case 1:list=create();
          break;
case 2:display(list);
        break;
case 3:list=push(list);
        break;
case 4:list=pop(list);
        break;
case 5:list=addq(list);
        break;
case 6:list=deleteq(list);
        break;
default:printf("invalid");
 }
  printf("do continue(1/0)");
  scanf("%d",&a);
  }while(a==1);
}
struct node *create(){
    for(int i=0;i<3;i++){
      new=(struct node *)malloc(sizeof(struct node));
    printf("enter the ID\t");
    scanf("%d",&new->id);
    printf("enter the Name\t");
    scanf("%s",new->pname);
        printf("enter the Branch\t");
    scanf("%s",new->branch);
        printf("enter the Area Of Specification\t");
    scanf("%s",new->aos);
    new->prev=NULL;
    new->next=NULL;
    if (start==NULL){
        start=temp=new;
  }
    else{
        temp->next=new;
        temp=temp->next;
        temp->prev=temp;
  }
  }
    return list;
}
void display(struct node *list){
    struct node *ptr=start;
    while(ptr!=NULL)
    {
        printf("%d \t%s \t%s \t %s \n",ptr->id,ptr->pname,ptr->branch,ptr->aos);
        ptr=ptr->next;
  }
}
struct node *push(struct node *list)
{
    struct node *newnode;
    newnode=(struct node *)malloc(sizeof(struct node));
     printf("enter the ID\t");
    scanf("%d",&new->id);
    printf("enter the Name\t");
    scanf("%s",new->pname);
        printf("enter the Branch\t");
    scanf("%s",new->branch);
        printf("enter the Area Of Specification\t");
    scanf("%s",new->aos);
if(start==NULL)
   {
       newnode->next = NULL;
       newnode->prev=NULL;
       start=newnode;
   }
   else
   {
       newnode->prev=NULL;
       newnode->next = start;
       start->prev=newnode;
       start=newnode;
   }
   return list;
}
struct node *pop(struct node *list)
{
    struct node *ptr=start;
    start=start->next;
    start->prev=NULL;
    free(ptr);
   return list;
}
struct node *addq(struct node *list)
{
    struct node *newnode;
    struct node *ptr=start;
    while(ptr->next!=NULL){
        ptr=ptr->next;
  }
    newnode=(struct node *)malloc(sizeof(struct node));
   printf("enter the ID\t");
    scanf("%d",&newnode->id);
    printf("enter the Name\t");
    scanf("%s",newnode->pname);
    printf("enter the Branch\t");
    scanf("%s",newnode->branch);
    printf("enter the Area Of Specification\t");
    scanf("%s",newnode->aos);
    ptr->next=newnode;
    newnode->prev=ptr;
    newnode->next=NULL;
   return list;
}
struct node *deleteq(struct node *list)
{
  struct node *ptr=start;
    start=start->next;
    start->prev=NULL;
    free(ptr);
   return list;
}

7. Given an array of elements, construct a complete binary tree from this array in level
order fashion. That is, elements from left in the array will be filled in the tree level wise
starting fromlevel 0. Ex: Input :arr[] = {1, 2, 3, 4, 5, 6}
 Output : Root of the following tree
1
/\
 2 3
 / \ /\
456

#include <stdio.h>
#include <stdlib.h>
 struct BST
 { int data;
struct BST* left, * right;
 };
typedef struct BST Node;

 Node* newNode(int data)


{
Node* node = (Node*)malloc(sizeof(Node));
 node->data = data;
node->left = node->right = NULL;
 return (node);
 } // Function to insert nodes in level order
 Node* insertLevelOrder(int arr[], Node* root,int i, int n)
 { // Base case for recursion
 if (i < n)
{
Node* temp = newNode(arr[i]);
 root=temp; // insert left child
root->left = insertLevelOrder(arr,root->left, 2 * i + 1, n); // insert right child
root->right = insertLevelOrder(arr,root->right, 2 * i + 2, n);
}
 return root;
}

// Function to print tree nodes in //


 InOrder fashion void inOrder(Node* root)
{
 if (root != NULL)
 {
inOrder(root->left);
 printf("%d\t",root->data); inOrder(root->right);
}
}

// Driver program to test above function


 int main()
{
 int arr[] = { 1, 2, 3, 4, 5, 6 };
 int n = sizeof(arr)/sizeof(arr[0]);
 Node* root = insertLevelOrder(arr,root, 0, n);
inOrder(root);
}

 OUTPUT: 4 2 5 1 6 3
8. Design, Develop and Implement a menu driven Program in C for the following
operations on
Binary Search Tree (BST) of Integers
a. Create a BST of N Integers
b. Traverse the BST in Inorder, Preorder and Post Order

#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;
NODE *node;
NODE* createtree(NODE *node, int data)
{
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data < (node->data))
{
node->left = createtree(node->left, data);
}
else if (data > node->data)
{
node -> right = createtree(node->right, data);
}
return node;
}
void inorder(NODE *node)
{
if(node != NULL)
{
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
}
}
void preorder(NODE *node)
{
if(node != NULL)
{
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
}
}
void postorder(NODE *node)
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
}
}
NODE* findMin(NODE *node)
{
if(node==NULL)
{
return NULL;
}
if(node->left)
return findMin(node->left);
else
return node;
}
void main()
{
int data, ch, i, n;
NODE *root=NULL;
while (1)
{
printf("\n1.Insertion in Binary Search Tree");
printf("\n2.Inorder\n3.Preorder\n4.Postorder\n5.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\nEnter N value: " );
scanf("%d", &n);
printf("\nEnter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)\n");
for(i=0; i<n; i++)
{
scanf("%d", &data);
root=createtree(root, data);
}
break;
case 2: printf("\nInorder Traversal: \n");
inorder(root);
break;
case 3: printf("\nPreorder Traversal: \n");
preorder(root);
break;
case 4: printf("\nPostorder Traversal: \n");
postorder(root);
break;
case 5: exit(0);
default:printf("\nWrong option");
break;
}
}
}

OUTPUT:

1.Insertion in Binary Search Tree


2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 1
Enter N value: 12
Enter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)
6 9 5 2 8 15 24 14 7 8 5 2
1.Insertion in Binary Search Tree
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 2
Inorder Traversal:
2 5 6 7 8 9 14 15 24
1.Insertion in Binary Search Tree
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 3
Preorder Traversal:
6 5 2 9 8 7 15 14 24
1.Insertion in Binary Search Tree
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 4
Postorder Traversal:
2 5 7 8 14 24 15 9 6
1.Insertion in Binary Search Tree
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your choice: 5
Exit
9. Design, Develop and implement a program in C for the following operations on
Graph (G) of cities
a. Create a Graph of N cities using Adjacency Matrix.
 b. Print all the nodes reachable from a given starting node in a diagraph using
DFS/BFS method.
#include
 #include
 int a[50][50], n, visited[50];
int q[20], front = -1,rear = -1;
 int s[20], top = -1, count=0;
void bfs(int v)
{
 int i, cur;
visited[v] = 1;
 q[++rear] = v;
while(front!=rear)
{
cur = q[++front];
for(i=1;i<=n;i++)
{
 if((a[cur][i]==1)&&(visited[i]==0))
 {
q[++rear] = i;
visited[i] = 1;
 printf("%d ", i);
}
}
 }
}
void dfs(int v)
 {
 int i;
visited[v]=1;
s[++top] = v;
 for(i=1;i<=n;i++)
 {
if(a[v][i] == 1&& visited[i] == 0 )
 {
printf("%d ", i); dfs(i);
}
 }
 }
int main()
 {
 int ch, start, i,j;
 printf("\nEnter the number of vertices in graph: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
 for(i=1; i<=n; i++)
 {
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
 }
 for(i=1;i<=n;i++)
 visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);
printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
 printf("\n==>2. DFS: Print all nodes reachable from a given starting node"); printf("\
n==>3:Exit"); printf("\nEnter your choice: ");
 scanf("%d", &ch);
 switch(ch)
 {
case 1: printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start); for(i=1;i<=n;i++)
{
 if(visited[i]==0)
 printf("\nThe vertex that is not reachable is %d" ,i);
}
break;
case 2: printf("\nNodes reachable from starting vertex %d are:\n",start);
 dfs(start);
break;
case 3: exit(0);
 default: printf("\nPlease enter valid choice:");
}
 }
10. Design and develop a program in C that uses Hash Function H:K->L as H(K)=K
mod
M (remindermethod) and implement hashing technique to map a given key K to the
address space L. Resolve the collision (if any) using linear probing.

#include<stdio.h>
#include<stdlib.h>
int key[20],n,m;
int *ht,index;
int count = 0;
void insert(int key)
{
 index = key % m;
 while(ht[index] != -1)
 {
 index = (index+1)%m;
 }
 ht[index] = key;
 count++;
}
void display()
{
 int i;
 if(count == 0)
 {
 printf("\nHash Table is empty");
 return;
 }
 printf("\nHash Table contents are:\n ");
 for(i=0; i<m; i++)
 printf("\n T[%d] --> %d ", i, ht[i]);
}
void main()
{
 int i;
 printf("\nEnter the number of employee records (N) : ");
 scanf("%d", &n);
 printf("\nEnter the two digit memory locations (m) for hash table: ");
 scanf("%d", &m);
 ht = (int *)malloc(m*sizeof(int));
 for(i=0; i<m; i++)
 ht[i] = -1;
 printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
 for(i=0; i<n; i++)
 scanf("%d", &key[i]);
 for(i=0;i<n;i++)
 {
 if(count == m)
 {
 printf("\n~~~Hash table is full. Cannot insert the record %d key~~~",i+1);
 break;
 }
 insert(key[i]);
 }
 //Displaying Keys inserted into hash table
 display();
}

You might also like