You are on page 1of 14

KL University, AP :Department of BES: FED

AY 2016-2017(Even Semester)

C& DS-II (15CS1201)

Test-2(15.02.2017) Answer Key


1. (a) Write a recursive function to convert decimal integer to its equivalent Binary
number by successive divisions. 5 Marks
#include <stdio.h> 
int binary_conversion(int);
int main()
{
int num;
  printf("Enter a decimal number: ");
scanf("%d", &num);
binary_conversion(num);
}
 
int binary_conversion(int num)
{
if (num == 0)
return 0;
else
{
digit=num%2;
printf(“%d”,digit);
binary_conversion(num / 2);
}
}

1) (b) Implement stacks using Linked List. 5 Marks


#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;

}*top = NULL;
void push(int);
void pop();
void display();

void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
2. (a) 5 Marks
Consider the following algorithm:
Algorithm fun1(x)
if(x<5)
return (3*x)
else
return (2* fun1(x-5)+7)
end if
end fun1

what would be returned if fun1 is called as


i) fun1 (4) ?
ii) fun1(10)?
iii) Fun1 (12)?

i) 12
ii) 21
iii) 45
Need Explanation for this result to award full marks

2.(b). Write a function that converts the given Infix expression to postfix notation. The
input to the function is an infix string consisting of a single alphabetic characters of
variables. parentheses, and the +, - ,*, and / operators. The function should print the
resulting postfix expression. 5 Marks

char * infix_to_postfix(char * infix, int size )


{
int i=0;
char postfix[size];
while((token=infix[i])!='\n')
{
if(isalnum(token))
printf(“%c”,token);
else
if(token=='(')
push(&s,'(');
else
{
if(token==')')
while((x=pop(&s))!='(')
printf("%c",x);
else
{
while(priority(token)<=priority(top(&s))&&!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
push(&s,token);
}
}
}
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
}
int priority(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
else
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
void push(stack *s,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}

char pop(stack *s)


{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
char top(stack * s)
{
return(s->data[s->top]);
}
3. a. Write a program to implement the following operations of Circular Queue

i) ENQUEUE

ii) DEQUEUE

int cqueue[SIZE], rear=-1, front=-1;


void enqueue()
{
int item;
if(front==(rear+1)%SIZE)
printf("\nCircular Queue is overflown.");
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d", &item);
if(rear == -1) //can also use if(front==-1)
{
rear = 0;
front = 0;
}
else
rear=(rear+1)%SIZE;
cqueue[rear] = item;
}
}

void dequeue()
{
int item;
if(front == -1)
printf("\nCircular Queue is underflown.Cannot delete anything.");
else
{
item = cqueue[front];
if(front == rear)
{
front = -1;
rear = -1;
}
else
front=(front+1)%SIZE;
printf("\nThe deleted element is: %d", item);
}
}
3.b Evaluate the following postfix expression with single digit operands using a stack
and also implement a C-function for evaluation.
823/23*+51*-^
Note that ^ is the exponentiation operator.

SYMBOL STACK SYMBOL STACK SYMBOL


STACK
8 2 2 3
8 3
8
Step 1 Step 2 2
Step 3 8

Step 4
/ y=pop();
X=pop(); 0
Z=x/y=2/3=0 8
Push(z)
SYMBOL STACK SYMBOL STACK
2 2 3 3
0 2
8 0
Step 5 8 Step 6
--------------------------------------------------------------------------------------------------
Step 7
* y=pop();
X=pop(); 6
Z=x*y=2*3=6 0
Push(z) 8
------------------------------------------------------------------------------------------------------
Step 8
+ y=pop();
X=pop();
Z=x+y=0+6=6 6
Push(z) 8
-----------------------------------------------------------------------------------------------------
SYMBOL STACK SYMBOL STACK
5 5 1 1
6 5
8 6
Step 9 8 Step 10
-------------------------------------------------------------------------------------------
Step 11
* y=pop();
X=pop(); 5
Z=x*y=5*1=5 6
Push(z) 8
Step 12
- y=pop();
X=pop();
1
8
Z=x-y=6-5=1
Push(z)
----------------------------------------------------------------------------------------------------

Step 13
^ y=pop();
X=pop();
Z=x^y=8^1=8
Push(z) 8

FINALLY pop() yields 8 after entire expression is evaluated.


/*Function to evaluate a postfix expression*/
int stack[20];
int top = -1;

void evalpfix(char exp[100])


{
int i,x,y,z
for(i=0;exp[i]!= '\0';i++)
{
if(isdigit(exp[i]))
push(exp[i]);
else
{
y = pop();
x = pop();
switch(exp[i])
{
case '+':
{
z = x + y;
break;
}
case '-':
{
z = x - y;
break;
}
case '*':
{
z = x * y;
break;
}
case '/':
{
z = x / y;
break;
}
}
push(z);
}
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
}

4. (a) Write a function to insert an element into Deque using left insertion. 5 marks.

Note: Left side insert means, insert at front end of the circular Queue.

#include<stdio.h>
#define MAX 5

int queue[MAX],front=-1,rear=-1;

void insert_at_front()
{
int ele;
printf("\n Enter the number to be inserted: ");
scanf("%d",&ele);

if((front==0 && rear==MAX-1) || (front == rear+1))


printf("\n Queue OverFlow Occured");
else
{
if(front==-1&&rear==-1)
front=rear=0;
else if(front==0)
front = MAX-1;
else
front=front-1;
queue[front]=ele;
}

4. (b) Write a function to evaluate postfix expression. 5 Marks.

#include<stdio.h>
#include<ctype.h>
#include<string.h>

int stk[20];
int top =-1;
void push(int val)
{
stk[++top] = val;
}

int pop()
{
return(stk[top--]);
}

void main()
{
int op1,op2,len,result,i;
char postfix[30],op;

printf("\nEnter the postfix expresstion");


gets(postfix);

len=strlen(postfix);

for(i=0 ;i<len ; i++)


{
if( isdigit(postfix[i]))
push(postfix[i]-'0');
else
{
op=postfix[i];
op2=pop();
op1=pop();

switch(op)
{
case '+': push(op1+op2);
break;
case '-': push(op1-op2);
break;
case '*': push(op1*op2);
break;
case '/': push(op1/op2);
break;
case '%': push(op1%op2);
break;
}
}
}
result=pop();
printf("The evaluated value is = %d",result);

}
5. (a) Implement Deque by using the following operations: 5 Marks + 5 Marks

(i) Right insert()


Note: Right side insert means, insert at rear end of the circular Queue. This is ordinary
enqueue operation of circular queue.
#include<stdio.h>
#define MAX 5

int queue[MAX],front=-1,rear=-1;
void enqueue_element()
{
int ele;
printf("\n Enter the number to be inserted: ");
scanf("%d",&ele);

if((front==0 && rear==MAX-1) || (front == rear+1))


printf("\n Queue OverFlow Occured");
else
{
if(front==-1&&rear==-1)
front=rear=0;
else if(rear==MAX-1 && front!=0)
rear=0;
else
rear=rear+1;
queue[rear]=ele;
}
}

(ii) Left delete()

Note: Left side delete means, delete at front end of the circular Queue. This is ordinary
dequeue operation of circular queue.

#include<stdio.h>
#define MAX 5

int queue[MAX],front=-1,rear=-1;

void dequeue_element()
{
int ele;
if(front==-1)
{
printf("\n Underflow");
return;
}
else
{
ele=queue[front];

if(front==rear)
front=rear=-1;
else if(front==MAX-1)
front=0;
else
front++;
printf("\n The deleted element is: %d",ele);
}
}

5. (b) Write the following functions to implement stacks using Linked list: 5 marks

(i) Push()

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*top=NULL, *p,*temp;

void push (int ele)


{ temp=(struct node *)malloc(sizeof ( struct node));
temp->data =ele;
temp->next = NULL;
if (top ==NULL)
{ top=temp;
p=temp; }
else
{ temp->next =top;
top=temp;
p=temp;
}
}

(ii) Pop() 5 Marks

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
} *top=NULL, *p,*temp;

int pop ()
{ int ele;
p = top;
ele = p->data;
p->next = top;
free(p);
return (ele);
}

6a) Implement enqueue() and dequeue() operations on priority queue? 5marks


struct node
{
int data;
int priority;
struct node *next;
};
struct node start=NULL;

voidenqueue()
{
struct node *newnode,*temp;
intval,pri;
newnode=(struct node*)malloc(sizeof(struct node));
printf(“enter value and priority”);
scanf(“%d%d”,&val,&pri);
newnode->data=val;
newnode->priority=pri;
newnode->next=NULL;
if(start=NULL)
start=newnode;
else if(newnode->priority<start->priority)
{
newnode->next=start;
start=newnode;
}
else
{
temp=start;
while(temp->new!=NULL &&temp->next->priority<=newnode->priority)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
}
}

void dequeue()
{
struct node *temp;
printf(“%d is deleted”,start->data);
start=start->next;
free(temp);
}

6.b) 5 marks

# define SIZE 100


struct stack
{ charst[SIZE]; int top; };
struct stack s;
s.top=-1;
void push()
char pop();

void main()
{
chatstr[20];
printf(“enter line of text”);
gets(str);
for(i=0;str[i]!=’\0’;i++)
{
if(str[i]==’ ‘)
continue;
push(str[i]);
}
for(i=0;s.top!=-1 ;i++)
{
str[i]=pop();
}
str[i]=’\0’;
printf(“string after removing spaces is %s”,str);
}

void push(char ch)


{
if(s.top==SIZE-1)
printf(“stack over flow”);
else
{
s.top++;
s.st[s.top]=ch;
}
}

char pop()
{ char ch;
if(s.top==-1)
printf(“stack underflow”);
else
{
ch=s.st[s.top];
s.top--;
}
}

You might also like