Follow my blog for getting updates of blogs.
Q.1. Data Structure Slip No=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 <stdio.h>
#include <stdlib.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
struct node *root, *newnode;
int data;
struct node *createnew()
{
printf("\nEnter data:");
scanf("%d", &data);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct node *display(struct node *root)
{
if(root!=NULL)
{
printf("%d |", root->data);
display(root->left);
display(root->right);
return root;
}
}
struct node *insert(struct node *root)
{
if(root==NULL)
{
root = createnew();
return root;
}
else if(data<=root->data)
{
root->left=insert(root->left);
}
else
{
root->right = insert(root->right);
}
return root;
}
int main()
{
while (1)
{
int choice;
printf("\n1.Create a Binary Search tree \n2.Insert element in binary search tree \n3.Display \n4.Exit");
printf("\nEnter choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = createnew();
printf("Binary Search Tree Created\n");
break;
case 2:
root = insert(root);
printf("Element Inserted\n");
break;
case 3:
display(root);
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
B)Write a ‘C’ program to evaluate a given polynomial using function. (Use array).
#include<stdio.h>
#include<math.h> //It is used to use mathematical functions like pow()
//Function for Evaluate a Polynomial
int eval_poly(int coefficients[], int n, int x) {
int i,result= 0;
for(i=n-1;i>=0;i--){
result+=coefficients[i]*pow(x, i);
}
return result;
}
//Main Function. Every C program starts from Main() Function
int main() {
int n;
printf("Enter the degree of the polynomial: ");
scanf("%d", &n);
int coefficients[n];
printf("Enter the coefficients of the polynomial: ");
for(int i=0;i<n;i++) {
scanf("%d",&coefficients[i]);
}
int x;
printf("Enter the value of x: ");
scanf("%d",&x);
//Calls eval_poly function and assign its returned value to 'value' valriable
int value=eval_poly(coefficients,n,x);
printf("The value of the polynomial at x = %d is %d\n", x, value);
return 0;
}
Slip No=2
Q.1. Data Structure
A) Write a ‘C’ program to accept a string from user and reverse it using Static implementation of Stack.
#include<stdio.h>
#include<string.h>
#define size 10
int top=-1;
int new,i;
char name[50];
char stack[size];
void push()
{
printf("\nEnter String name:");
gets(name);
for(i=0; name[i]!='\0'; i++)
{
if(top==size-1)
{
printf("\nstack is full");
}else{
top=top+1;
stack[top]=name[i];
}}
}
void display()
{
for(i=top; i>=0; i--)
{
printf("%c",name[i]);
}
}
void main()
{
push();
display();
}
B)B) Write a ‘C’ program to create Circularly Doubly Linked list and display it.
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *head,*temp,*newnode;
void create()
{
int i,num;
printf("\nHow many Nodes you want to create:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=temp=newnode;
newnode->next=head;
newnode->prev=temp;
}else{
temp->next=newnode;
newnode->prev=temp;
temp=temp->next;
newnode->next=head;
head->prev=newnode;
}
}
}
void display()
{
struct node *disp=head;
while(disp->next!=head)
{
printf("%d",disp->data);
disp=disp->next;
}
printf("%d",disp->data);
}
void main()
{
create();
display();
}
Slip No=4
Q1. 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<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
struct node *root, *newnode;
int data;
struct node *createnew()
{
printf("\nEnter data:");
scanf("%d", &data);
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
struct node *postorder(struct node *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d |", root->data);
return root;
}
}
struct node *inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d |", root->data);
inorder(root->right);
return root;
}
}
struct node *insert(struct node *root)
{
if(root==NULL)
{
root=createnew();
return root;
}else if(data<=root->data){
root->left=insert(root->left);
} else{
root->right=insert(root->right);
}
return root;
}
int main()
{
while (1)
{
int choice;
printf("\n1.Create a Binary Search tree \n2.Traverse using Inorder \n3.Traverse using Postorder \n4.Exit");
printf("\nEnter choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = createnew();
insert(root);
insert(root);
insert(root);
printf("Binary Search Tree Created\n");
break;
case 2:
inorder(root);
break;
case 3:
postorder(root);
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
B.Write a ‘C’ program to accept two polynomial and find the addition of accepted polynomials.(use array)
#include <stdio.h>
#include<conio.h>
// Function to add two polynomials
void add_polynomials(int p1[],int p2[],int n1,int n2,int p[]){
int i,j,k=0;
for(i=0;i<=n1;i++) {
for(j=0;j<=n2;j++) {
p[k++]=p1[i]+p2[j];
}
}
}
int main() {
int p1[10],p2[10],p[20],n1,n2;
printf("Enter the degree of the first polynomial: ");
scanf("%d",&n1);
printf("Ener the coefficients of the first polynomial: ");
for(int i=0;i<=n1;i++) {
scanf("%d",&p1[i]);
}
printf("Enter the degree of the second polynomial: ");
scanf("%d",&n2);
printf("Enter the coefficients of the second polynomial: ");
for(int i=0;i<=n2;i++) {
scanf("%d",&p2[i]);
}
//Function called
add_polynomials(p1,p2,n1,n2,p);
printf("The sum of the two polynomials is: ");
for(int i=0;i<=n1+n2;i++){
printf("%d",p[i]);
}
return 0;
}
Slip No=5
Q.1. Data Structure
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>
#include <stdlib.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
struct node *root, *newnode;
int data;
struct node *createnew()
{
printf("\nEnter data:");
scanf("%d", &data);
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
struct node *preorder(struct node *root)
{
if (root != NULL)
{
printf("%d |", root->data);
preorder(root->left);
preorder(root->right);
return root;
}
}
struct node *inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d |", root->data);
inorder(root->right);
return root;
}
}
struct node *insert(struct node *root)
{
if(root==NULL)
{
root=createnew();
return root;
}
else if(data<=root->data)
{
root->left=insert(root->left);
}
else
{
root->right=insert(root->right);
}
return root;
}
int main()
{
while (1)
{
int choice;
printf("\n1.Create a Binary Search tree \n2.Traverse using Inorder \n3.Traverse using Preorder \n4.Exit");
printf("\nEnter choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = createnew();
insert(root);
insert(root);
insert(root);
printf("Binary Search Tree Created\n");
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
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<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *create_node(int data){
struct node *new_node=(struct node *)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("Memory allocation failed\n");
exit(1);
}
new_node->data=data;
new_node->next=NULL;
return new_node;
}
struct node *create_linked_list(int number)
{
struct node *head=NULL,*temp;
if(number==0){
return create_node(0);
}
while(number){
int digit=number%10;
temp=create_node(digit);
temp->next=head;
head=temp;
number/=10;
}
return head;
}
void display(struct node *head) {
if(head==NULL) {
printf("The list is empty.\n");
return;
}
printf("Nodes in Newly Created Linked List:\n");
while(head!=NULL) {
printf("%d->",head->data);
head=head->next;
}
}
int main() {
int number;
printf("Enter the number: ");
scanf("%d",&number);
struct node *head=create_linked_list(number);
display(head);
return 0;
}
Slip No=6
Q.1. Data Structure A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes
- Create a Binary Search Tree
- Traverse it by using Preorder and Postorder traversing technique
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
struct node *root, *newnode;
int data;
struct node *createnew()
{
printf("\nEnter data:");
scanf("%d", &data);
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
struct node *preorder(struct node *root)
{
if (root != NULL)
{
printf("%d |", root->data);
preorder(root->left);
preorder(root->right);
return root;
}
}
struct node *postorder(struct node *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d |", root->data);
return root;
}
}
struct node *insert(struct node *root)
{
if (root==NULL)
{
root=createnew();
return root;
}
else if(data<=root->data)
{
root->left=insert(root->left);
}
else
{
root->right=insert(root->right);
}
return root;
}
int main()
{
while (1)
{
int choice;
printf("\n1.Create a Binary Search tree \n2.Traverse using Preorder \n3.Traverse using Postorder \n4.Exit");
printf("\nEnter choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = createnew();
insert(root);
insert(root);
insert(root);
printf("Binary Search Tree Created\n");
break;
case 2:
preorder(root);
break;
case 3:
postorder(root);
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
B) Write a ‘C’ program to accept and sort n elements in ascending order by using bubble sort
#include<stdio.h>
#include<conio.h>
#define size 10;
void main()
{
int array[10];
int i,j,n,k,temp;
printf("\nElements in array:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("\nEnter Element:");
scanf("%d",&array[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
for(k=0; k<n; k++)
{
printf("%d->",array[k]);
}
}
Slip No=7
.1. Data Structure A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes - Create a Binary Search Tree
- Display
- Delete a given element from Binary Search Tree
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *left;
struct Node *right;
};
struct Node *createNode(int value) {
struct Node *newNode=(struct Node *)malloc(sizeof(struct Node));
if(newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node *insert(struct Node *root, int value) {
if(root==NULL) {
return createNode(value);
}
if(value<root->data) {
root->left=insert(root->left,value);
}else if(value>root->data){
root->right=insert(root->right,value);
}
return root;
}
void display(struct Node *root) {
if (root!=NULL) {
display(root->left);
printf("%d |",root->data);
display(root->right);
}
}
struct Node *findMin(struct Node *node) {
while(node->left!=NULL) {
node = node->left;
}
return node;
}
struct Node *deleteNode(struct Node *root, int value) {
if(root==NULL) {
return root;
}
if(value<root->data){
root->left=deleteNode(root->left, value);
}else if(value>root->data){
root->right=deleteNode(root->right, value);
}else{
if(root->left==NULL){
struct Node *temp=root->right;
free(root);
return temp;
}else if(root->right==NULL) {
struct Node *temp=root->left;
free(root);
return temp;
}
struct Node *temp=findMin(root->right);
root->data=temp->data;
root->right=deleteNode(root->right, temp->data);
}
return root;
}
int main(){
struct Node *root = NULL;
int choice, value;
while (1){
printf("\nBinary Search Tree Menu:\n");
printf("1. Insert a value\n");
printf("2. Display: \n");
printf("3. Delete a value\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter a value which you want to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Binary Search Tree elements are: ");
display(root);
printf("\n");
break;
case 3:
printf("Enter a value which you want to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 4:
free(root);
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
B) Write a ‘C’ program to create a singly linked list and count total number of nodes in it and display the list and total number of Nodes.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head,*a,*newnode;
void create()
{
int i,num;
printf("\nHow many Nodes you want to create:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
newnode=(struct node *)malloc(sizeof(struct node ));
printf("\nEnter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=a=newnode;
}else{
a->next=newnode;
a=a->next;
}
}
}
void display()
{
int i=0;
struct node *disp=head;
while(disp!=NULL)
{ i++;
printf("%d->",disp->data);
disp=disp->next;
}
printf("\nTotal Number of Nodes is %d",i);
}
slip no=9
Q.1. Data Structure A) Write a program to accept a postfix expression and evaluate the expression using the stack.
Example: Input: ab+cd-*
Values: a=4, b=2, c=5, d=3
Answer: 12
B) Write a ‘C’ program to create a singly linked list, reverse it and display both the list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head,*newnode,*t;
void create()
{
int i,num;
printf("\nEnter How many Nodes you want to create:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=t=newnode;
}else{
t->next=newnode;
t=t->next;
}}
}
void display()
{
struct node *disp=head;
while(disp!=NULL)
{
printf("%d",disp->data);
disp=disp->next;
}
}
void reverse()
{
struct node *p=head->next, *q=head, *r=NULL;
while(p!=NULL)
{
q->next=r;
r=q;
q=p;
p=p->next;
}
q->next=r;
head=q;
}
void main()
{
create();
display();
printf("\n----------------------------------------------------\n");
reverse();
display();
getch();
}
slip no=11
Q.1. Data Structure
A) Write a menu driven program using ‘C’ for singly linked list-
- To create linked list.
- To display linked list
- To search node in linked list.
- Insert at last position
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head,*newnode,*t;
void create()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d", &newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=t=newnode;
}
else
{
t->next=newnode;
t=t->next;
}
}
void display()
{
struct node *disp=head;
while(disp->next!=NULL)
{
printf("%d->", disp->data);
disp=disp->next;
}
printf("%d->", disp->data);
}
void search()
{
struct node *p=head;
int a,search;
printf("\nEnter Number to be search:");
scanf("%d",&search);
while (p!=NULL)
{
if(p->data==search)
{
a=1;
}
p = p->next;
}
if(a!=1)
{
printf("\nNumber not present in linked list\n");
}else{
printf("\nNumber present in linked list\n");
}
}
void insert_last()
{
struct node *last=head;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
while(last->next!=NULL)
{
last=last->next;
}
last->next=newnode;
}
void main()
{
while(10>1)
{
int ch;
printf("\n1.To create Linked List \n2.To display Linked List \n3.To search Node in Linked List \n4.Insert at Last Position \n5.Exit");
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
insert_last();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
B) Write a menu driven program using ‘C’ for Dynamic implementation of Queue for integers. The menu includes
- Insert
- Delete
- Display
- Exit
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *front,*rear,*newnode;
void insert()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(front==NULL)
{
front=rear=newnode;
}else{
rear->next=newnode;
rear=newnode;
}
}
void display()
{
struct node *disp=front;
while(disp!=NULL)
{
printf("%d->",disp->data);
disp=disp->next;
}
}
void delete()
{
struct node *beg=front;
front=front->next;
beg->next=front;
free(beg);
beg=NULL;
}
void main()
{
while(1)
{
int ch;
printf("\n1. Insert \n2. Delete \n3. Display \n4. Exit");
printf("\nEnter 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("\nInvalid Input");
break;
}
}
}
Slip No=12
Q.1. A) Write a C program that accepts the graph as an adjacency matrix and checks if the
graph is undirected. The matrix for undirected graph is symmetric. Also calculate in
degree of all vertices
- Read a graph as adjacency Matrix
- Check the matrix is symmetric or not
- Calculate indegree of all vertices
#include<stdio.h>
#define max 100
int main(){
int n,adjacency_matrix[max][max],in_degrees[max];
printf("Enter the Number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
scanf("%d",&adjacency_matrix[i][j]);
}
}
int is_symmetric=1;
for(int i=0;i<n && is_symmetric;i++){
for(int j=i+1;j<n;j++){
if(adjacency_matrix[i][j]!=adjacency_matrix[j][i]){
is_symmetric=0;
break;
}
}
}
for(int i=0;i<n;i++){
in_degrees[i]=0;
for(int j=0;j<n;j++){
in_degrees[i]+=adjacency_matrix[j][i];
}
}
if(is_symmetric) {
printf("The graph is undirected or Symmetric.\n");
}else{
printf("The graph is directed or Not Symmetric.\n");
}
printf("The Indegrees of All Vertices are:\n");
for(int i=0;i<n;i++) {
printf("Vertex %d: %d\n",i+1,in_degrees[i]);
}
return 0;
}
B) Write a ‘C’ program to accept and sort n elements in ascending order using
Selection sort method.
#include<stdio.h>
#include<conio.h>
int main(){
int n,arr[100];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for(int i=0;i<n;i++) {
scanf("%d", &arr[i]);
}
//Code for sort elements using selection sort
for(int i=0;i<n-1;i++){
int min_index=i;
for(int j=i+1;j<n;j++) {
if(arr[j]<arr[min_index]) {
min_index=j;
}
}
int temp=arr[i];
arr[i]=arr[min_index];
arr[min_index]=temp;
}
printf("The sorted elements using Selection sort are::\n");
for(int i=0;i<n;i++) {
printf("%d ",arr[i]);
}
return 0;
}
Slip No=13
Q.1. A) Write a C program to accept an infix expression and convert it into postfix
form.(Use Static Implementation of Stack)
Example: - A * B + C as AB*C+
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define max 100
char stack[max];
int top=-1;
void push(char ch){
stack[++top]=ch;
}
char pop(){
return stack[top--];
}
int precedence(char ch){
if(ch=='+'||ch=='-'){
return 1;
}else if(ch=='*'||ch=='/'||ch=='%'||ch=='~'){
return 2;
}else{
return 0;
}
}
int is_operator(char ch){
return (ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='~');
}
void infixToPostfix(char *infix,char *postfix) {
int i,j;
for(i=0,j=0;infix[i]!='\0';i++){
if(!is_operator(infix[i])){
postfix[j++] = infix[i];
}else{
while(top>=0&&precedence(stack[top])>precedence(infix[i])){
postfix[j++] = pop();
}
push(infix[i]);
}
}
while(top>=0) {
postfix[j++]=pop();
}
postfix[j] = '\0';
}
int main(){
char infix[max];
char postfix[max];
printf("Enter Infix Expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix Expression is: %s\n", postfix);
return 0;
}
B) Write a ‘C’ program to create doubly link list and display nodes having odd value.
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *head,*newnode,*temp,*disp;
void create()
{
int i,num;
printf("\nHow many Nodes you want to create:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=temp=newnode;
}else{
temp->next=newnode;
newnode->prev=temp;
newnode->next=NULL;
head->prev=NULL;
temp=temp->next;
}}}
void display()
{
disp=head;
while(disp!=NULL)
{
if(disp->data%2!=0){
printf("%d->",disp->data);
}
disp=disp->next;
}
}
void main()
{
create();
display();
}
Slip No=14
A) Write a ‘C’ program to accept a string from user and reverse it using Dynamic
implementation of Stack.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define max 100
struct Node{
char data;
struct Node* next;
};
struct Node *top = NULL,*newnode;
void push(char ch){
newnode=(struct Node*)malloc(sizeof(struct Node));
newnode->data=ch;
newnode->next=top;
top=newnode;
}
char pop(){
if(top==NULL){
return '\0';
}
char ch=top->data;
struct Node* temp=top;
top=top->next;
free(temp);
return ch;
}
void string_reverse(char *str){
int i,n;
n=strlen(str);
for(i=0;i<n;i++){
push(str[i]);
}
for(i=0;i<n;i++){
str[i]=pop();
}
}
void main(){
char str[max];
printf("Enter String: ");
scanf("%s", str);
string_reverse(str);
printf("Reversed String is: %s\n", str);
}
B)Write a ‘C’ program to accept names from the user and sort in alphabetical order
using bubble sort
- Accept n name
- Bubble sort Function
- Display
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define MAX_SIZE 100
void bubble_sort(char names[][MAX_SIZE], int n) {
int i,j;
for(i=0;i<n-1;i++) {
for(j=0;j<n-i-1;j++) {
if(strcmp(names[j],names[j+1])>0){
char temp[MAX_SIZE];
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
}
}
}
}
int main(){
char names[MAX_SIZE][MAX_SIZE];
int n,i;
printf("Enter the Number of Names you want to Enter: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter %d Name: ",i+1);
scanf("%s",names[i]);
}
bubble_sort(names,n);
printf("Names after Bubble sort are: \n");
for(i=0;i<n;i++) {
printf("%s\n",names[i]);
}
return 0;
}
Slip No=15
Q.1.A) Write a ‘C’ program to accept an infix expression, convert it into its equivalent
postfix expression and display the result.(Use Dynamic Implementation of Stack)
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define max 100
struct Stack{
char data[max];
int top;
};
void push(struct Stack* stack, char ch) {
if(stack->top==max-1) {
printf("Stack is Full\n");
return;
}
stack->data[++stack->top] = ch;
}
char pop(struct Stack* stack) {
if(stack->top == -1) {
printf("Stack is NULL\n");
return '\0';
}
return stack->data[stack->top--];
}
int precedence(char ch) {
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '~':
return 3;
default:
return 0;
}
}
int is_operator(char ch){
return(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='~');
}
void infixToPostfix(char *infix, char *postfix) {
struct Stack stack;
stack.top = -1;
int i, j;
for(i=0,j=0;infix[i]!='\0';i++) {
if(!is_operator(infix[i])) {
postfix[j++]=infix[i];
}else{
while(stack.top>=0 && precedence(stack.data[stack.top]) > precedence(infix[i])) {
postfix[j++] = pop(&stack);
}
push(&stack, infix[i]);
}
}
while (stack.top >= 0) {
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
int main() {
char infix[max];
char postfix[max];
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression is: %s\n", postfix);
return 0;
}
B)Write menu driven program using ‘C’ for Dynamic implementation of Stack. The
menu includes following operations:
- Push
- Pop
- Display
- Exit
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node{
int data;
struct node *next;
};
struct node *top,*newnode;
void push()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(top==NULL)
{
top=newnode;
}else{
newnode->next=top;
top=newnode;
}
}
void pop()
{
struct node *last=top;
top=top->next;
free(last);
last=NULL;
}
void display()
{
struct node *disp=top;
while(disp->next!=NULL)
{
printf("\n%d",disp->data);
disp=disp->next;
}
printf("\n%d",disp->data);
}
void main()
{
while(1)
{
int ch,n;
printf("\n1.Push \n2.Pop \n3.Display \n4.Exit");
printf("\nEnter choice:");
scanf("%d",&ch);
switch (ch)
{
case 1:
printf("How many Elements you want to Push in Stack:");
scanf("%d",&n);
for(int i=0;i<n;i++){
push();
}
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
Slip No=16
A) Write a ‘C’ program which accept the string and reverse each word of the string
using Static implementation of stack.
Example: Input - This is an input string
Output - sihTsinatupnignirts
#include<stdio.h>
#include<string.h>
void reverse(char *str, int start, int end);
void reverseWords(char *str){
int start = 0, end = 0;
while(str[end]){
if(str[end]==' '){
reverse(str, start, end - 1);
start = end + 1;
}
end++;
}
reverse(str, start, end - 1);
}
void reverse(char *str, int start, int end) {
while(start < end){
char temp=str[start];
str[start++]=str[end];
str[end--]=temp;
}
}
int main() {
char input[100];
printf("Enter a string: ");
scanf("%[^\n]", input);
//Function called
reverseWords(input);
printf("Reversed string: %s\n", input);
return 0;
}
B) Write a ‘C’ program to create to a Singly linked list. Accept the number from user,
search the number in the list.If the number is present display the Position of node .If
number not present print the message “Number not Found”.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head,*newnode,*t;
void create()
{
int i,num;
printf("\nEnter How many Nodes you want to create:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=t=newnode;
}else{
t->next=newnode;
t=t->next;
}
}}
void display()
{
struct node *disp=head;
while(disp->next!=NULL)
{
printf("%d->",disp->data);
disp=disp->next;
}
printf("%d->",disp->data);
}
void search()
{
struct node *d=head;
int search,m=0,p=1;
printf("\nEnter Number which you want to search:");
scanf("%d",&search);
while(d!=NULL)
{
if(d->data==search)
{
m=1;
break;
}
d=d->next;
p=p+1;
}
if(m>0)
{
printf("\nNumber Found at %d position",p);
}else{
printf("\nNumber Not found");
}
}
void main()
{
create();
display();
search();
}
Slip No=17
A) Write a ‘C’ program to read a postfix expression, evaluate it and display the result.
(Use Static Implementation of Stack).
#include<stdio.h>
#include<conio.h>
#define size 100
struct Stack {
int top;
int items[size];
};
void push(struct Stack *stack, int item) {
if (stack->top == size) {
printf("Stack is Full\n");
return;
}
stack->items[stack->top++] = item;
}
int pop(struct Stack *stack) {
if (stack->top == -1) {
printf("Stack is Empty!\n");
return -1;
}
return stack->items[--stack->top];
}
int main() {
struct Stack stack;
stack.top = -1;
char expression[100];
printf("Enter a postfix expression: ");
scanf("%s", expression);
int i;
for (i = 0; expression[i] != '\0'; i++) {
if (expression[i] >= '0' && expression[i] <= '9') {
push(&stack, expression[i] - '0');
} else {
int operand1 = pop(&stack);
int operand2 = pop(&stack);
int result = 0;
switch (expression[i]) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
push(&stack, result);
}
}
int result = pop(&stack);
printf("The result is %d\n", result);
return 0;
}
B) Write a ‘C’ program to accept the names of cities and store them in array. Accept
the city name from user and use linear search algorithm to check whether the city is
present in array or not.
#include<stdio.h>
#include<string.h>
#define MAX_SIZE 100
void linear_search(char cities[][MAX_SIZE], int n, char city_name[]) {
int i;
for (i=0;i<n;i++) {
if (strcmp(cities[i], city_name) == 0) {
printf("City is Present.\n");
return;
}
}
printf("City is Not Present\n");
}
int main() {
char cities[MAX_SIZE][MAX_SIZE];
int n;
char city_name[MAX_SIZE];
printf("How many Cities you want to Enter: ");
scanf("%d", &n);
for(int i=0;i<n;i++){
printf("Enter the name of city %d: ", i + 1);
scanf("%s", cities[i]);
}
printf("Enter the name of the city to search: ");
scanf("%s", city_name);
linear_search(cities, n, city_name);
return 0;
}
Slip No=18
A) Write a ‘C’ program to read ‘n’ integers and store them in a binary Search tree
structure and count the following and display it.
- Number of nodes
- Degree of tree
- Leaf nodes
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
struct Node* createNode(int data) {
struct Node* node = malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int countNodes(struct Node* root) {
if(root == NULL){
return 0;
}else{
return 1 + countNodes(root->left) + countNodes(root->right);
}
}
int findMax(int a, int b) {
return a > b ? a : b;
}
int findDegree(struct Node* root) {
if(root == NULL) {
return 0;
}else {
return findMax(findDegree(root->left), findDegree(root->right)) + 1;
}
}
int isLeafNode(struct Node* root) {
return root->left==NULL && root->right == NULL;
}
int countLeafNodes(struct Node* root) {
if(root == NULL){
return 0;
}else{
return isLeafNode(root) ? 1 : countLeafNodes(root->left) + countLeafNodes(root->right);
}
}
struct Node* insertNode(struct Node* root, int data) {
if(root==NULL){
return createNode(data);
}else{
if(data<root->data) {
root->left=insertNode(root->left, data);
}else{
root->right=insertNode(root->right, data);
}
return root;
}
}
int main() {
int n;
printf("Enter How many nodes you want to enter: ");
scanf("%d", &n);
int arr[n];
printf("Enter Elements: ");
for(int i=0;i<n;i++) {
scanf("%d", &arr[i]);
}
struct Node* root=NULL;
for(int i=0;i<n;i++) {
root = insertNode(root, arr[i]);
}
int numNodes = countNodes(root);
int treeDegree = findDegree(root);
int numLeafNodes = countLeafNodes(root);
printf("Number of nodes: %d\n", numNodes);
printf("Degree of tree: %d\n", treeDegree);
printf("Number of leaf nodes: %d\n", numLeafNodes);
return 0;
}
B) Write a ‘C’ program to accept and sort n elements in ascending order using Merge
sort method.
#include <stdio.h>
#include<conio.h>
void merge(int array[], int low, int middle, int high) {
int leftSize = middle - low + 1;
int rightSize = high - middle;
int left[leftSize], right[rightSize];
for(int i=0;i<leftSize;i++) {
left[i] = array[low + i];
}
for(int j=0;j<rightSize;j++) {
right[j] = array[middle + 1 + j];
}
int i = 0, j = 0, k = low;
while (i<leftSize && j<rightSize) {
if(left[i] <= right[j]) {
array[k++] = left[i++];
}else {
array[k++] = right[j++];
}
}
while (i < leftSize) {
array[k++] = left[i++];
}
while (j < rightSize) {
array[k++] = right[j++];
}
}
void mergeSort(int array[], int low, int high) {
if(low<high) {
int middle = low + (high - low) / 2;
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
merge(array, low, middle, high);
}
}
int main() {
int numElements;
printf("Enter the number of elements: ");
scanf("%d",&numElements);
int elements[numElements];
printf("Enter the elements: ");
for(int i=0;i<numElements;i++) {
scanf("%d", &elements[i]);
}
mergeSort(elements, 0, numElements - 1);
printf("Sorted array: ");
for(int i=0;i<numElements;i++) {
printf("%d ", elements[i]);
}
return 0;
}
x
No comments:
Post a Comment