Solutions for Questions in Practical Slips from 1 to 5

                                            Slip 1

A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes

- Create a Binary Search Tree

- Insert element in a Binary Search Tree

- Display 


#include <stdlib.h>

#include<stdio.h>

#include <string.h>

typedef struct node
{
struct node *leftchild;
int info;
struct node *rightchild;
}
NODE;

NODE * get_node(int val)
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
Name:
Class:
Roll No:
p->info=val;
p->leftchild = p->rightchild = NULL;
return p;
}

NODE* insert(NODE *h, int key)
{
NODE *p,*q;
q = get_node(key);
if(h==NULL)
h = q;
else
{
p = h;
while(1)
{
if(p->info>q->info)
{
if(p->leftchild==0)
{
p->leftchild = q;
break;
}
else
p=p->leftchild;
}
else
{
if(p->rightchild==0)
{
p->rightchild = q;
break;
}
else
p=p->rightchild;
}
}
}
return h;
}

NODE * create()
{
int i,n,key;
NODE *h=NULL;
printf("Enter no.of keys:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter key:");
scanf("%d",&key);
h = insert(h,key);
}
return h;
}
Name:
Class:
Roll No:
void inorder(NODE *h)
{
if(h!=NULL)
{
inorder(h->leftchild);
printf("%d\t",h->info);
inorder(h->rightchild);
}
}

int main()
{
NODE *root;
int ch,key;
while(1)
{
printf("1.Create\n");
printf("2.Insert\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("Enter your choice (1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
root = create();
break;
case 2:
printf("Enter key to insert:");
scanf("%d",&key);
root=insert(root,key);
break;
case 3:
inorder(root);
printf("\n");
break;
case 4:
exit(0);
}
getch();
}
}



Output-
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):1
Enter no.of keys:2
Enter key:3
Enter key:4
1.Create
Name:
Class:
Roll No:
2.Insert
3.Display
4.Exit
Enter your choice (1-4):2
Enter key to insert:5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):3
3 4 5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):4


B) Write a ‘C’ program to evaluate a given polynomial using function. (Use array).


#include <stdio.h>
#include <math.h>

int evaluate (int arr[],int limit,int x)
{
    int sum =0, count;
    
    for (count=limit; count>=0; count--)
    {
        sum=sum+arr[count]*pow (x, count);
    }
    return sum;
}

int main ()
{
    int array[30], degree, x, count, result, i;
    
    printf("\n Enter the Degree of polynomial :");
    scanf("%d",&degree);
    
    printf("\n Enter the Co-efficient of polynomial:");
    for(count=degree;count>=0;count--)
    {
        printf("\n Co-efficient of count :" );
        scanf("%d",&array[count]);
    }
    
    printf("\n The Polynomial :");
    for(i=degree ;i>0; i--)
    {
        if (array[i] !=0) 
        {
            printf("%d^%d+",array[i],i);
        }
    }
    printf("%d",array[count]);
    
    printf("\n Enter the value of x :");
    scanf("%d",&x);
    
    result = evaluate (array, degree, x);
    printf("\n Evaluate of polynomial :%d",result);
    return 0;
}


                          Slip 2


A) Write a ‘C’ program to accept a string from user and reverse it using Static

implementation of Stack.


#include<stdio.h>
#include<conio.h>
#define MAX 50
char a[MAX];
int top=-1;
void push(char c)
{
top++;
a[top]=c;
}
char pop()
{
char c;
c=a[top];
top–;
return c;
}
void main()
{
int i;
clrscr();
printf(“\n\nENTER THE STRING”);
gets(a);
for(i=0;a[i]!=”; i++)
{
if(a[i]==’ ‘)
{
while(top!=-1)
{
printf(“%c”,pop());
}
printf(” “);
}
else
{
if(top==MAX-1)
{
printf(“\n\nSTACK IS FULL”);
}
else
{
push(a[i]);
}
}
}
while(top!=-1)
{
printf(“%c”,pop());
}
getch();
}

B) Write a ‘C’ program to create Circularly Doubly Linked list and display it.


#include <stdio.h>
#include <stdlib.h>

struct node {
    int num;
    struct node * nextptr;
}*stnode;
 

void ClListcreation(int n);
void displayClList();

int main()
{
    int n;
    stnode = NULL;
printf("\n\n Circular Linked List : Create and display a circular linked list :\n");
printf("-----------------------------------------------------------------------\n");    

    printf(" Input the number of nodes : ");
    scanf("%d", &n);
 
    ClListcreation(n); 
    displayClList();
    return 0;
}

void ClListcreation(int n)
{
    int i, num;
    struct node *preptr, *newnode;

    if(n >= 1)
    {
        stnode = (struct node *)malloc(sizeof(struct node));

        printf(" Input data for node 1 : ");
        scanf("%d", &num);
        stnode->num = num;
        stnode->nextptr = NULL;
        preptr = stnode;
        for(i=2; i<=n; i++)
        {
            newnode = (struct node *)malloc(sizeof(struct node));
            printf(" Input data for node %d : ", i);
            scanf("%d", &num);
            newnode->num = num;
            newnode->nextptr = NULL; // next address of new node set as NULL
            preptr->nextptr = newnode; // previous node is linking with new node
            preptr = newnode;    // previous node is advanced
        }
        preptr->nextptr = stnode; //last node is linking with first node
    }
}

void displayClList()
{
    struct node *tmp;
    int n = 1;

    if(stnode == NULL)
    {
        printf(" No data found in the List yet.");
    }
    else
    {
        tmp = stnode;
        printf("\n\n Data entered in the list are :\n");

        do {
            printf(" Data %d = %d\n", n, tmp->num);

            tmp = tmp->nextptr;
            n++;
        }while(tmp != stnode);
    }
}



                                            Slip 3


A) Write a program to create two singly linked list of elements of type integer and find

the union of the linked lists. (Accept elements in the sorted order)


#include<stdio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *alloc(int);
NODE *create();
NODE *findlast(NODE *);
NODE *unionlist(NODE *,NODE *);
NODE *search(NODE *,int);
void display(NODE *);
NODE *getnode();
void main()
{
NODE *list1=NULL,*list2=NULL,*list3=NULL;
clrscr();
printf(“\nCreate first list.”);
list1=create();
printf(“\nCreate second list.”);
list2=create();
printf(“\n Data in first list: “);
display(list1);
printf(“\n Data in second list: “);
display(list2);
printf(“\n\n Union of two list is: “);
list3=unionlist(list1,list2);
if(list3==NULL)
printf(“\nList of union is empty”);
else
display(list3);
getch();
}
NODE *alloc(int val)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=NULL;
return temp;
}
NODE *getnode()
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
printf(“\nEnter the data: “);
scanf(“%d”,&temp->data);
temp->next=NULL;
return temp;
}
NODE *search(NODE *list,int val)
{
NODE *ptr;
for(ptr=list;ptr!=NULL && ptr->data!=val;ptr=ptr->next);
return(ptr);
}
NODE *create()
{
NODE *ptr,*temp,*list=NULL;
int n,i;
printf(“\n Enter how many nodes : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
temp=getnode();
if(list==NULL)
{
list=temp;
}
else
{
ptr=findlast(list);
ptr->next=temp;
}
}
return(list);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}
NODE *unionlist(NODE *list1,NODE *list2)
{
NODE *temp,*ptr1,*ptr2,*list3=NULL;
int i,val;
for(ptr1=list1;ptr1!=NULL;ptr1=ptr1->next)
{
temp=alloc(ptr1->data);
if(list3==NULL)
list3=temp;
else
{
ptr2=findlast(list3);
ptr2->next=temp;
}
}
for(ptr1=list2;ptr1!=NULL;ptr1=ptr1->next)
{
ptr2=search(list1,ptr1->data);
if(ptr2==NULL)
{
temp=alloc(ptr1->data);

ptr2=findlast(list3);

ptr2->next=temp;
}
}
return(list3);
}
void display(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next)
printf(“%d->”,ptr->data);
printf(“NULL”);
}


B) Write a ‘C’ program to read the adjacency matrix of directed graph and convert it

into adjacency list.

#include<stdio.h>

#include<conio.h>
struct n
{
int data;
struct n *link;
};
typedef struct n NODE;
NODE *getnode(int);
NODE *findlast(NODE *);
void display(NODE *[],int);
void main()
{
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
clrscr();
printf(“\n Enter total number of vertices : “);
scanf(“%d”,&n);
printf(“\n Enter entries of an adjacency matrix :\n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(“\n Enter a[%d][%d] : “,i+1,j+1);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n Entered Adjacency matrix is … \n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(“\t %d “,a[i][j]);
}
printf(“\n”);
}
for(i=0;i<n;i++)
h[i]=NULL;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==1)
{
temp=getnode(j+1);
if(h[i]==NULL)
h[i]=temp;
else
{
ptr=findlast(h[i]);
ptr->link=temp;
}
}
}
}
printf(“\n The Adjacency list looks like … \n”);
display(h,n);
getch();
}
NODE *getnode(int j)
{
NODE * temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=j;
temp->link=NULL;
return(temp);
}
NODE *findlast(NODE *h)
{
NODE *ptr;
for(ptr=h;ptr->link!=NULL;ptr=ptr->link);
return(ptr);
}
void display(NODE *h[10],int n)
{
NODE *ptr;
int i;
for(i=0;i<n;i++)
{
printf(“\n V%d “,i+1);
ptr=h[i];
if(ptr==NULL)
printf(” NULL”);
while(ptr!=NULL)
{
printf(” ->V%d”,ptr->data);
ptr=ptr->link;
}
printf(“\n”);
}
}


                                                                Slip 4


A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes

- Create a Binary Search Tree

- Traverse it by using Inorder and Postorder traversing technique


#include <stdlib.h>
#include <string.h>
typedef struct node
{
struct node *leftchild;
int info;
struct node *rightchild;
}
NODE;
NODE * get_node(int val)
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
Name:
Class:
Roll No:
p->info=val;
p->leftchild = p->rightchild = NULL;
return p;
}
NODE* insert(NODE *h, int key)
{
NODE *p,*q;
q = get_node(key);
if(h==NULL)
h = q;
else
{
p = h;
while(1)
{
if(p->info>q->info)
{
if(p->leftchild==0)
{
p->leftchild = q;
break;
}
else
p=p->leftchild;
}
else
{
if(p->rightchild==0)
{
p->rightchild = q;
break;
}
else
p=p->rightchild;
}
}
}
return h;
}
NODE * create()
{
int i,n,key;
NODE *h=NULL;
printf("Enter no.of keys:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter key:");
scanf("%d",&key);
h = insert(h,key);
}
return h;
}
Name:
Class:
Roll No:
void inorder(NODE *h)
{
if(h!=NULL)
{
inorder(h->leftchild);
printf("%d\t",h->info);
inorder(h->rightchild);
}
}
int main()
{
NODE *root;
int ch,key;
while(1)
{
printf("1.Create\n");
printf("2.Insert\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("Enter your choice (1-6):");
scanf("%d",&ch);
switch(ch)
{
case 1:
root = create();
break;
case 2:
printf("Enter key to insert:");
scanf("%d",&key);
root=insert(root,key);
break;
case 3:
inorder(root);
printf("\n");
break;
case 4:
exit(0);
}
getch();
}
}
Output-
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):1
Enter no.of keys:2
Enter key:3
Enter key:4
1.Create
Name:
Class:
Roll No:
2.Insert
3.Display
4.Exit
Enter your choice (1-4):2
Enter key to insert:5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):3
3 4 5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):4


B) Write a ‘C’ program to accept two polynomial and find the addition of accepted

polynomials.(use array)

#include <stdio.h>
int main()
{
    int a[27],b[27],c[54],m,n,i,j,z,y=0,t,s=0;
    printf ("How many terms you want to add in the 1st polynomial ?? : ");
    scanf ("%d",&n);
   
    printf ("Enter 1st polynomial : \n");
    for (i=0;i<n*3;i=i+3)
    {
        printf ("Enter coefficient : ");
        scanf("%d",&a[i]);
        printf ("Enter power of x : ");
        scanf("%d",&a[i+1]);
        printf ("Enter power of y : ");
        scanf("%d",&a[i+2]);
    }
    printf ("1st polynomial is : ");
    for (i=0;i<n*3;i=i+3)
    {
        printf ("(%dx^%dy^%d) + ",a[i],a[i+1],a[i+2]);
        
    }
    printf (" 0 \n");
    printf ("How many terms you want to add in the 2nd polynomial ?? : ");
    scanf ("%d",&m);
   
    printf ("Enter 2nd polynomial : \n");
    for (i=0;i<m*3;i=i+3)
    {
        printf ("Enter coefficient : ");
        scanf("%d",&b[i]);
        printf ("Enter power of x : ");
        scanf("%d",&b[i+1]);
        printf ("Enter power of y : ");
        scanf("%d",&b[i+2]);
    }
    printf ("2nd polynomial is : ");
    for (i=0;i<m*3;i=i+3)
    {
        printf ("(%dx^%dy^%d) + ",b[i],b[i+1],b[i+2]);
       
    }
    printf (" 0\n");
   
   
    printf ("Enter 1 to add  : ");
    scanf("%d",&z);
    switch (z)
    {
        case 1:
        for (i=0;i<n*3;i++)
        {c[i]=a[i];}
        for (i=n*3,j=0;i<(n+m)*3,j<m*3;i++,j++)
        {c[i]=b[j];}
        for (i=0;i<(m+n)*3;i=i+3)
    {
        printf ("(%dx^%dy^%d) + ",c[i],c[i+1],c[i+2]);
       
    }
    printf (" 0\n");   
    }
   
    for (i=1;i<(m+n)*3;i=i+3)
    {
        for (j=4;j<(m+n)*3;j=j+3)
        {
            if (c[i]==c[j])
            { if(c[i+1]==c[j+1])
            {  
                c[i-1]=c[i-1]+c[j-1];
                c[j-1]=0;
 
 
            }
            }
        }
    }
    printf ("ADDITION \n");
    for (i=0;i<(m+n)*3;i=i+3)
    if (c[i]!=0)
    {
        printf ("(%dx^%dy^%d) + ",c[i],c[i+1],c[i+2]);
       
    }
    else
    printf (" ");
    printf (" 0 \n");
    }
 

 

 

                                                          Slip 5


A)Write menu driven program using ‘C’ for Binary Search Tree. The menu includes

- Create a Binary Search Tree

- Traverse it by using Inorder and Preorder traversing technique

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *rlink;
struct node *llink;
}*tmp=NULL;
typedef struct node NODE;
NODE *create();
void preorder(NODE *);
void inorder(NODE *);
void postorder(NODE *);
void insert(NODE *);
void main()
{
int n,i,m;
clrscr();
do
{
printf(“\n\n0.create\n\n1.insert \n\n2.preorder\n\n3.postorder\n\n4.inorder\n\n5.exit\n\n”);
printf(“\n\nEnter ur choice”);
scanf(“%d”,&m);
switch(m)
{
case 0:
tmp=create();
break;
case 1:
insert(tmp);
break;
case 2:
printf(“\n\nDisplay tree in Preorder traversal\n\n”);
preorder(tmp);
break;
case 3:
printf(“\n\nDisplay Tree in Postorder\n\n”);
postorder(tmp);
break;
case 4:
printf(“\n\nInorder\n\n”);
inorder(tmp);
break;
case 5:
exit(0);
}
}
while(n!=5);
getch();
}
void insert(NODE *root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE *create()
{
NODE *newnode;
int n;
newnode=(NODE *)malloc(sizeof(NODE));
printf(“\n\nEnter the Data “);
scanf(“%d”,&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}
void postorder(NODE *tmp)
{
if(tmp!=NULL)
{
postorder(tmp->llink);
postorder(tmp->rlink);
printf(“%d->”,tmp->data);
}
}
void inorder(NODE *tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf(“%d->”,tmp->data);
inorder(tmp->rlink);
}
}
void preorder(NODE *tmp)
{
if(tmp!=NULL)
{
printf(“%d->”,tmp->data);
preorder(tmp->llink);
preorder(tmp->rlink);
}
}

B) Write a ‘C’ program to create linked list with given number in which data part of

each node contains individual digit of the number.

(Ex. Suppose the number is 368 then the nodes of linked list should contain 3, 6, 8)


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL,*temp=NULL;
void main()
{
int num,a[10],i,j;
clrscr();
printf("enter the number:-");
scanf("%d",&num);
i=0;
while(num>0)
{
a[i]=num%10;
i++;
num=num/10;
}
i--;
printf("\nthe display of linked list is:-\n");
for(j=i;j>=0;j--)
{
if(start==NULL)
{
start=malloc(sizeof(struct node));
start->data=a[j];
printf("%d",start->data);
start->next=NULL;
temp=start;
}
else
{
temp->next=malloc(sizeof(struct node));
temp->next->data=a[j];
printf(",%d",temp->next->data);
temp->next->next=NULL;
temp=temp->next;
}
}
getch();
}
/*OUTPUT-
enter the number:-584

the display of linked list is:-    
       
                                    5,8,4                                                                             */






































































































































Comments

Popular posts from this blog

Classical Problems of Synchronization in OS

Semester IV : Assignment #1 OPERATING SYSTEM