Q) Session10 a)Execute program to remove negative values from list of values by using
queues
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int queue[MAX],f=-1,r=-1;
void enq();
void deq();
void display();
void deletenegative();
main()
{
int n,i;
printf("enter the number of elements");
scanf("%d",&n);
for(i=1;i<=n;i++)
enq();
display();
printf("nAfter deleting negative valuesn");
deletenegative();
display();
}
void enq()
{
int val;
if(r==MAX-1) {
printf("Queue full");
return;
}
else if(f == -1 && r == -1)
f=r=0;
else
r=r+1;
printf("enter the data to be enteredn");
scanf("%d",&val);
queue[r]=val;
}
void deq()
{
if(f==-1)
{
move(i);
r=r-1;
i=i-1;
}
}
else
i=i+1;
}
}
}
To see output... See my blog
http://enthusiaststudent.blogspot.in/2017/01/removing-negative-values-in-queues-c.html
Q) 10 bEnqueue, Dequeue and Display operations in Circular Queue using Arrays
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int cqueue[MAX], front=-1, rear=-1;
void deq();
void enq();
void display();
main()
{
int n,i;
printf("enter the number of elements");
scanf("%d",&n);
for(i=1;i<=n;i++)
enq();
display();
deq();
display();
}
void enq()
{
int val;
if((front == 0 && rear == MAX-1)||(front == rear+1))
{
printf("Queue full");
return;
}
if(front == -1 && rear == -1)
front = rear = 0;
else if(rear == MAX-1)
rear== 0;
else
rear=rear+1;
printf("enter the data to be enteredn");
scanf("%d",&val);
cqueue[rear]=val;
}
void deq()
{
if(front == -1 && rear == -1)
{
printf("queue is empty");
return;
}
printf("ndeleted element=%dn ",cqueue[front]);
if(front == rear)
front = rear = -1;
else if(front == MAX-1)
front = 0;
else
front = front+1;
}
void display()
{
int i;
if(front <= rear)
{
for(i=front;i<=rear;i++)
printf("%dt",cqueue[i]);
}
else
{
for(i=front;i<MAX;i++)
printf("%dt",cqueue[i]);
for(i=0;i<=rear;i++)
printf("%dt",cqueue[i]);
}
}
Q) Program to implement Ascending Priority queue
#include<stdio.h>
#include <stdlib.h>
struct node
{
int data,pri;
struct node *next;
};
struct node *head=NULL,*c,*p;
void create();
void display();
void deleteMin();
main()
{
int n,i;
printf("enter the number of elements");
scanf("%d",&n);
for(i=1;i<=n;i++)
create();
display();
deleteMin();
printf("n after deleting one element, the contents of apq are :n");
display();
}
void create()
{
int v,priority;
printf("enter value and priorityn");
scanf("%d%d",&v,&priority);
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data =v;
newnode->pri=priority;
newnode->next = NULL;
if(head == NULL)
head = newnode;
else if( newnode->pri < head->pri)
{
newnode->next=head;
head=newnode;
}
else
{
c=head;
while(newnode->pri >= c->pri && c->next != NULL)
{
p=c;
c=c->next;
}
if(c->next == NULL && newnode->pri >= c->pri)
c->next=newnode;
else
{
p->next = newnode;
newnode->next=c;
}
}
}
void display()
{
if(head == NULL)
printf("list is empty");
else
{
c=head;
while(c != NULL)
{
printf("%d %d->",c->data,c->pri);
c=c->next;
}
}
}
void deleteMin()
{
/* delete the first node as the first node is minimum in ascending priority queue*/
c=head;
head=head->next;
free(c);
}
Session-13 A) What is a recursive solution to summing up a list of numbers? First you
should note that the sum of [2 13 4 25 66 71 82 91]) is equal to 2 + sum of [ 13 4 25 66 71
82 91]
#include <stdio.h>
int sum(int *a,int index,int n)
{
if(index == n)
return 0;
return(a[index]+sum(a,index+1,n));
}
main()
{
int n,i,a[100];
printf("enter size of arrayn");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array sum =%d",sum(a,0,n));
}
Session-13b) . Write a recursive function called elfish? that, given a word, tells us if that
word is elfish or not.
#include <stdio.h>
int elfish(char *s,char *b, int i)
{
if(b[i]=='0')
return 1;
int k,flag=0;
for(k=0;s[k]!='0';k++)
{
if(s[k]==b[i])
{
flag=1;
break;
}
}
if(flag == 1)
return elfish(s,b,i+1);
else
return 0;
}
main()
{
char s[100],b[4]="elf";
int result;
printf("enter the stringn");
gets(s);
result=elfish(s,b,0);
if(result ==0)
printf("given string is not elfish");
else
printf("given string is elfish");
}
Session-14a)Write a recursive function to Find the total number of sequences of length
n (using H and T) such that no two Hs are next to each other.
#include <stdio.h>
int a[20];
int total_seq(int n)
{
if(n == 1)
return 2;
if(n ==2)
return 3;
if(a[n]!=-1 )
return a[n];
a[n]=total_seq(n-1)+total_seq(n-2);
return a[n];
}
main()
{
int n,i;
printf("enter n value");
scanf("%d",&n);
for(i=0;i<20;i++)
a[i]=-1;
printf("n Total number of sequences = %d",total_seq(n));
}
Session14 b) Given an array of positive numbers, find the maximum sum of a
subsequence with the constraint that no 2 numbers in the sequence should be adjacent
in the array.
#include<stdio.h>
int excl,incl;
/*Function to return max sum such that no two elements are adjacent */
int FindMaxSum(int *a, int index, int n)
{
int temp;
if(index == n)
return incl;
else
{
temp=incl;
if(incl<(excl+a[index]))
incl=excl+a[index];
excl = temp;
return FindMaxSum(a,index+1,n);
}
}
main()
{
int n, a[100],i;
printf(“enter the number of elementsn”);
scanf(“%d”, &n);
printf(“enter array elementsn”);
for(i=0;i<n;i++)
scanf(“%d”, &a[i]);
incl = a[0];
excl = 0;
printf("nMaximum sum of non- adjacent elements= %d n", FindMaxSum(a,1, n));
}