DSC Manual 2020

You might also like

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

1 Design develop and implement menu driven C program to perform following set of

operations on Stack of integers (using array of maximum size MAX)


. i)) Push ii) Pop iii) Display iv) Exit
The program should print appropriate messages for stack overflow, stack underflow, and
stack empty.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
typedef enum {false,true} bool;
int top= -1, s[SIZE], item;
bool s_empty()
{
if(top<0)
return true;
else
return false;
}
bool s_full()
{
if(top==SIZE-1)
return false;
else
return true;
}
void push()
{
if(!s_full())
{ top++;
s[top]=item;
}
else
printf("Stack is full..\n");
}
int pop()
{
if(!s_empty())
return s[top--];
else
return -99;
}
void display()
{
int i;
printf("Stack contains:\n");
for(i=top; i>=0;i--)

1
printf("%d\t",s[i]);
printf("\n");
}
int main()
{
int ch=1;
while(1)
{
printf("\nEnter a choice:\n1. Push\t2. Pop\t3. Display\t4. Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the no. to insert: ");
scanf("%d",&item);
push();
break;
case 2: item=pop();
if (item!=-99)
printf("\nelement %d deleted.. \n",item);
else
printf("\nStack empty");
break;
case 3: display();
break;
default: exit(0);
}
}
return 0;
}

2
2 Design , develop and implement a program in C to convert and print a given valid
parenthesized or parenthesize free infix expression to postfix expression. The expression
consists of single character operands and the binary operators + (plus), - (minus), *
(multiply) , / (divide), % (mod) and ^ ( power) .
#include<stdio.h>
#include<string.h>
#include<conio.h>
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case '#':return -1;
default:return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default:return 7;
}
}
void infix_postfix(char infix[],char postfix[])
{
int i,j,top;
char s[30],symbol;
top=-1;
top=top+1;
s[top]='#';
j=0;
for(i=0;i<strlen(infix);i++)
3
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j]=s[top--];
j=j+1;
}
if(F(s[top])!=G(symbol))
{
top=top+1;
s[top]=symbol;
}
else
top=top-1;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
}
int main()
{
char infix[20],postfix[20];
printf("enter a valid infix expression\n");
gets(infix);
infix_postfix(infix,postfix);
printf("postfix expression is \n");
printf("%s\n",postfix);
return 0;
}

4
3 Design, develop and implement a program in C to evaluate a valid suffix/postfix expression
using stack. Assume that the suffix/postfix expression is read as a single line consisting of
positive single digit operands and binary arithmetic operators. The arithmetic operators are +
(add), - (subtract), * (multiply), % (mod) and ^ (power).
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <process.h>

int top = -1;


float s[50];

float operate (float opr1, float opr2, char symbol)


{
switch(symbol)
{
case '+': return(opr1 + opr2);
case '-': return(opr1 - opr2);
case '*': return(opr1 * opr2);
case '/': return(opr1 / opr2);
case '^': return( pow(opr1,opr2));
}
}
void push(float symbol)
{
s[++top] = symbol;
}
float pop()
{
return s[top--];
}
float eval(char postfix[50])
{
int i;
char symbol;
float opr1, opr2, value, res;
for (i = 0; postfix[i] != '\0'; i++ )
{
symbol = postfix[i];
if(isdigit(symbol))
push(symbol - 48);
else if(isalpha(symbol))
{
printf("\nEnter the value for %c ", symbol);
scanf("%f", &value);
push(value);
5
}
else
{
opr2 = pop();
opr1 = pop();
res = operate(opr1, opr2, symbol);
push(res);
}
}
return pop();
}

int main()
{
char postfix[80];
float res;
printf("\nEnter the Postfix Expression: ");
gets(postfix);
printf("\nThe given expression is: ");
puts(postfix);
res = eval(postfix);
printf("\nThe value of the expression is %f\n", res);
return 0;
}

6
4 Design develop and implement menu driven C program to perform following set of
operations on queue of integers using an array.
i) Insert ii) Delete iii) Display iv) Exit The program should print appropriate
messages for queue overflow, queue underflow, and queue empty.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
typedef enum {false,true} bool;
int f= -1,r=-1,q[SIZE],item; // fifo list
bool q_empty()
{
if(f<0)
return true;
else
return false;
}
bool q_full()
{
if(r<(SIZE))
return false;
else
return true;
}
void qinsert()
{
if(!q_full()) // overflow of queue
{
if(r==-1) // insert into empty queue
f=0;
r++;
q[r]=item; // insert into partially filled queue
}
else
printf("\nQueue is full cannot insert");
}
int qdelete()
{
int d;
if(!q_empty()) // not empty queue
{
if(f==r) //
{
d= q[f];
f=-1;
r=-1;

7
}
else
d=q[f++]; // partially filled queue
return d; //
}
else
return -99;
}
void display()
{
int i;
if(!q_empty())
{
printf("\nQueue contains:\n");
for(i=f; i<=r;i++)
printf("%d\t",q[i]);
printf("\n");
}
else
printf("\nqueue is empty");
}
int main()
{
int ch;
while(1)
{
printf("\nEnter a choice:\n1. Insert\t2. Delete\t3. Display\t 4. Q empty 5. Q is full 6. Exit\
n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the no. to insert");
scanf("%d",&item);
qinsert( ); //function
break;
case 2: item=qdelete();
if (item!=-99)
printf("\nelement deleted..%d",item);
else
printf("\nQueue empty");
break;
case 3: display();
break;
case 4: if(q_empty())
printf("\nQueue is empty");
else

8
printf("\nQueue is not empty");
break;
case 5: if(q_full())
printf("\nQueue is full");
else
printf("\nQueue is not full");
break;
default: exit(0);
}
}
return 0;
}

9
5 Design develop and implement menu driven C program to perform following set of
operations on circular queue of integers using an array.
i) Insert ii) Delete iii) Display iv) Exit The program should print appropriate
messages for circular queue overflow, circular queue underflow, and circular queue empty.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
int f= -1,r=-1,cq[SIZE],item; // fifo list
void cqinsert()
{
if(f==(r+1)%SIZE)
printf("\nCircular queue is full");
else
{
printf("\nEnter the item to be inserted: ");
scanf("%d", &item);
r=(r+1)%SIZE;
cq[r]=item;
if(f==-1)
f=f+1;
printf("\nItem %d is inserted successfully", item);
}
}
void cqdelete()
{
if(f==-1)
printf("\nCircular queue is empty");
else
{
printf("\nElement deleted is %d", cq[f]);
if(f==r)
f=r-1;
else
f=(f+1)%SIZE;
}
}
void cqdisplay()
{
int i;
if(f==-1)
printf("\nCircular queue is empty");
else
{

10
printf("\nThe content of circular queue is: \n");
for(i=f; i!=r; i=(i+1)%SIZE)
printf("%d\t", cq[i]);
printf("%d\t", cq[r]);
}
}
int main()
{
int choice;
while(1)
{
printf("\nEnter a choice:\n1. Insert\t2. Delete\t3. Display\t 4. Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1: cqinsert();
break;
case 2: cqdelete();
break;
case 3: cqdisplay();
break;
default: exit(0);
}
}
return 0;
}

11
6 Design, Develop and Implement a menu driven program in C for the following operations
on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem,
PhNo .Create a SLL of N Students Data by using front insertion.
i. Display the status of SLL and count the number of nodes in it
ii. Perform Insertion at End of SLL
iii. Perform Deletion at End of SLL
iv. Exit The program should print appropriate messages for dynamic stack overflow,
underflow and empty.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define size 3
struct node
{
char usn[10], branch[20], name[30], sem[3], phno[10];
struct node* link;
};
struct node* top=NULL;
int count=0;
struct node* createnode()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter Name\n");
fflush(stdin);
gets(temp->name);
printf("Enter USN\n");
gets(temp->usn);
printf("Enter Branch\n");
gets(temp->branch);
printf("Enter sem\n");
gets(temp->sem);
printf("Enter phone number\n");
gets(temp->phno);
temp->link=NULL;
return temp;
}
void push()
{
struct node* temp;
if(count>=size)
{
printf("stack overflow");
return;
}
12
temp=createnode();
if(top!=NULL)
temp->link=top;
top=temp;
count++;
}
void pop()
{
struct node *temp=top;
if (temp==NULL)
printf("stack IS EMPTY\n");
else
{
top=top->link;
printf("THE DELETED NODE IS\n");
printf("\nNAME =");
puts(temp->name);
printf("\nUSN =");
puts(temp->usn);
printf("\nBRANCH =");
puts(temp->branch);
printf("\nSEM =");
puts(temp->sem);
printf("\nphone no. =");
puts(temp->phno);
free(temp);
count--;
}
}
void display()
{
struct node* temp=top;
printf("The elements in the stack are\n");
while (temp!=NULL)
{
printf("\nNAME =");
puts(temp->name);
printf("\nUSN =");
puts(temp->usn);
printf("\nBRANCH =");
puts(temp->branch);
printf("\nSEM =");
puts(temp->sem);
printf("\nphone no. =");
puts(temp->phno);
temp=temp->link;

13
}
printf("THE NO. OF NODES ARE %d",count);
}

void main()
{
int ch;
while(1)
{
printf("\nEnter your choice\n1-push\t2-pop\t3-display\t4-Exit: ");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
default: exit(0);
}
}
}

14
7. Design, Develop and Implement a menu driven program in C for the following operations
on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem,
PhNo
i. Create a SLL of N Students Data by using front insertion.
ii. Display the status of SLL and count the number of nodes in it
iii. Perform Insertion at End of SLL
iv. Perform Deletion at front end of SLL
v. Exit The program should print appropriate messages for dynamic queue overflow,
underflow and empty
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define SIZE 3
struct node
{
char usn[11],name[15],branch[4],phno[11],sem[5];
struct node *link;
}*f=NULL,*r=NULL,*t=NULL;
int count=0;
void ins()
{
if(count==SIZE)
{
printf("queue overflow\n");
return;
}
t=(struct node*)malloc(sizeof(struct node));
printf("\nEnter USN:");
scanf("%s",t->usn);
printf("Enter Name:");
scanf("%s",t->name);
printf("Enter Branch:");
scanf("%s",t->branch);
printf("Enter Sem:");
scanf("%s",t->sem);
printf("Enter Phno:");
scanf("%s",t->phno);
t->link=NULL;
if(r==NULL)
f=r=t;
else
{
r->link=t;
r=t;
}
count++;

15
}
void del()
{
if(f==NULL)
{
printf("\nqueue Empty");
return;
}
else
{
struct node *t1;
t1=f;
if(f==r)
{
f=r=NULL;
}
else
{
f= f->link;
}
count--;
printf("\nElement deleted is:\n");
printf("USN:%s\nName:%s\nBranch:%s\nSem:%s\nPhno:%s\n",t1->usn,t1->name,t1-
>branch,t1->sem,t1->phno);
free(t1);
}
}
void display()
{
struct node *temp=f;
printf("The elements in the queue are\n");
while (temp!=NULL)
{
printf("\nNAME =");
puts(temp->name);
printf("\nUSN =");
puts(temp->usn);
printf("\nBRANCH =");
puts(temp->branch);
printf("\nSEM =");
puts(temp->sem);
printf("\nphone no. =");
puts(temp->phno);
temp=temp->link;
}
printf("THE NO. OF NODES ARE %d",count);

16
}

int main()
{
int ch;
while(1)
{
printf("\nEnter your choice\n1-Insert\t2-Delete\t3-Display\t4-Exit: ");
scanf("%d",&ch);
switch(ch)
{
case 1: ins();
break;
case 2: del();
break;
case 3: display();
break;
default: exit(0);
}
}
return 0;
}

8. Design, Develop and Implement a menu driven Program in C for the following operations
17
onDoubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept,
Designation,Sal, PhNo
i. Create a DLL of N Employees Data by using end insertion.
ii. Display the status of DLL and count the number of nodes in it
iii. Perform Insertion and Deletion at End of DLL
iv. Perform Insertion and Deletion at Front of DLL
v. Exit
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
char ssn[25],name[25],dept[10],designation[25];
int sal;
long int phone;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
NODE first = NULL;
int count=0;
NODE create()
{
NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode== NULL)
{
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the ssn,Name,Department,Designation,Salary,PhoneNo of the
employee: \n");
scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode->dept, enode-
>designation, &enode->sal, &enode->phone);
enode->llink=NULL;
enode->rlink=NULL;
count++;
return enode;
}

NODE insertfront()
{
NODE temp;
temp = create();
if(first == NULL)
{

18
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}

void display()
{
NODE cur;
cur = first;
if(cur == NULL)
{
printf("\nNo Contents to display in DLL");
return;
}

count=;
while(cur!=NULL)
{
printf("\nSSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone no:
%ld", cur->ssn, cur->name,cur->dept, cur->designation, cur->sal, cur->phone);
cur = cur->rlink;
count++;
}
printf("\nNo of employee nodes is %d",count);
}

NODE deletefront()
{
NODE temp;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink== NULL)
{
printf("\nThe employee node with the ssn:%s is deleted", first->ssn);
free(first);
count--;
return NULL;
}
temp = first;
first = first->rlink;
temp->rlink = NULL;

19
first->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",temp->ssn);
free(temp);
count--;
return first;
}

NODE insertend()
{
NODE cur, temp;
temp = create();
if(first == NULL)
{
return temp;
}
cur= first;
while(cur->rlink!=NULL)
{
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
}

NODE deleteend()
{
NODE prev,cur;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink == NULL)
{
printf("\nThe employee node with the ssn:%s is deleted",first->ssn);
free(first);
count--;
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
prev=cur;
cur = cur->rlink;

20
}
cur->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",cur->ssn);
free(cur);
prev->rlink = NULL;
return first;
}
int main()
{
int ch,i,n;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Employee Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\nothers:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the no of Employees: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertend();
break;
case 2: display();
break;
case 3: first = insertend();
break;
case 4: first = deleteend();
break;
case 5: first = insertfront();
break;
case 6: first = deletefront();
break;
default: exit(0);
}
}
return 0;
}

21
22
9. Design, Develop and Implement a menu driven Program in C for the following operations
onBinary Search Tree (BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
int count=0;
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;
}

NODE* search(NODE *node, int data)


{
if(node == NULL)
printf("\nElement not found");

23
else if(data < node->data)
{
node->left=search(node->left, data);
}
else if(data > node->data)
{
node->right=search(node->right, data);
}
else
printf("\nElement found is: %d", node->data);
return node;
}

void countnode(NODE *node)


{
if(node != NULL)
{
countnode(node->left);
count++;
countnode(node->right);
}
}
void inorder(NODE *node)
{
if(node != NULL)
{
inorder(node->left);
printf("%d\t", node->data);
count++;
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)
{

24
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
}
}

int findMin(NODE *node)


{
if(node==NULL)
{
return NULL;
}
if(node->left)
return findMin(node->left);
else
return node->data;
}

int main()
{
int data, ch, i, n;
NODE *root=NULL;
for(;;)
{
printf("\n1.Insertion in Binary Search Tree");
printf("\n2.Search Element in Binary Search Tree");
printf("\n4.Inorder\n5.Preorder\n6.Postorder\n7.Count of tree\n8.Min of tree\n9.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\nEnter N value: " );
scanf("%d", &n);
printf("\n Enter the values to create BST like(6,7,8,5,2)\n");
for(i=0; i<n; i++)
{
scanf("%d", &data);
root=createtree(root, data);
}
break;
case 2: printf("\nEnter the element to search: ");
scanf("%d", &data);
root=search(root, data);
break;
case 4:printf("\nInorder Traversal: \n");
inorder(root);

25
break;
case 5:printf("\nPreorder Traversal: \n");
preorder(root);
break;
case 6:printf("\nPostorder Traversal: \n");
postorder(root);
break;
case 7:countnode(root);
printf(" total count=%d",count);
break;
case 8: printf("min data =%d",findMin(root));
break;
case 9: exit(0);
default:printf("\nWrong option");exit(0);
break;
}
}
return 0;
}

26
10. Design, Develop and Implement a Program in C for the following operations on Singly
Circular Linked List (SCLL) with header nodes.
Represent and Evaluate a Polynomial:
P(x,y,z) = 6 x 2 y 2 z - 4 y z 5 + 3 x 3 y z + 2 x y 5 z - 2 x y z 3

#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define null 0
struct node
{
int cf, px, py, pz;
struct node *link;
} *head=null;

void display()
{
struct node *temp;
temp=head;
if(temp==null)
{
printf("Polynomial does not exist\n");
return;
}
printf("polynomial-");
while(temp!=null)
{
printf("%d x^%d y^%d z^%d",temp->cf,temp->px,temp->py,temp->pz);
temp=temp->link;
}
printf("\n");
}

void read_poly()
{ struct node *nn;
int px, py, pz, cf, ch=1;
while(ch == 1)
{
printf("\n Enter coeff: ");
scanf("%d",&cf);
printf("\n Enter x, y, z powers-");
scanf("%d%d%d",&px, &py, &pz);
nn=(struct node*)malloc(sizeof(struct node));

27
nn->cf=cf;
nn->px=px;
nn->py=py;
nn->pz=pz;
nn->link=null;
if(head==null)
head=nn;
else
{
nn->link=head;
head=nn;
}
printf("\n to add more terms enter 1- ");
scanf("%d", &ch);
}
}
void evaluate()
{
struct node *temp;
int x,y,z;
float result=0.0;
temp=head;
if(temp==null) return;
printf("\nEnter x, y, z values ");
scanf("%d%d%d",&x, &y, &z);
while(temp != null)
{
result = result + (temp->cf * pow(x,temp->px) * pow(y,temp->py) * pow(z,temp->pz));
temp=temp->link;
}
printf("\nPolynomial result is: %f", result);
}
void main()
{
int ch=1;
while(ch==1)
{
head=null;
read_poly();
display();
evaluate();
printf("\n Enter 1 to continue polynomial evaluation-\n");

28
scanf("%d",&ch);
}
}

29
30

You might also like