Slip Solution
Slip Solution
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>
int data;
}NODE;
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=v;
temp->l=NULL;
temp->r=NULL;
if(h==NULL)
h=temp;
else if(v<h->data)
h->l=insert(h->l,v);
else if(v>h->data)
h->r=insert(h->r,v);
}
return h;
int main()
int n,i,val;
NODE *head;
head=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&val);
head=insert(head,val);
printf("\nDisplay :\n",insert);
B) Write a ‘C’ program to evaluate a given polynomial using function. (Use array).
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
int main()
int array[MAXSIZE];
float x, polySum;
scanf("%d", &num);
scanf("%f", &x);
/* Read the coefficients into an array */
scanf("%d", &array[i]);
polySum = array[0];
power = num;
if (power < 0)
break;
if (array[i] > 0)
printf(" + ");
printf(" - ");
else
printf(" ");
}
SLIP NO=2
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>
int top=-1;
int item;
char stack_string[MAX];
char popChar(void);
int isEmpty(void);
int isFull(void);
int main()
char str[MAX];
int i;
scanf("%[^\n]s",str);
for(i=0;i<strlen(str);i++)
pushChar(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=popChar();
return 0;
if(isFull())
top=top+1;
stack_string[top]=item;
char popChar()
if(isEmpty())
printf("\nStack is EMPTY!!!\n");
return 0;
item = stack_string[top];
top=top-1;
return item;
int isEmpty()
if(top==-1)
return 1;
else
return 0;
int isFull()
if(top==MAX-1)
return 1;
else
return 0;
}
B) Write a ‘C’ program to create Circularly Doubly Linked list and display it
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
}*stnode;
void displayClList();
int main()
int n;
stnode = NULL;
printf("\n\n Circular Linked List : Create and display a circular linked list :\n");
printf("-----------------------------------------------------------------------\n");
scanf("%d", &n);
ClListcreation(n);
displayClList();
return 0;
void ClListcreation(int n)
int i, num;
if(n >= 1)
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL;
preptr->nextptr = newnode;
preptr = newnode;
preptr->nextptr = stnode
} }
void displayClList()
int n = 1;
if(stnode == NULL)
else
tmp = stnode;
do {
tmp = tmp->nextptr;
n++;
}while(tmp != stnode); } }
SLIP N0=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>
#include <stdlib.h>
struct node {
int data;
void initialize(){
newNode->data = num;
newNode->next = *head;
*head = newNode;
if (head->data == num)
return 1;
head = head->next;
return 0;
}
struct node* findunion(struct node *LLOne, struct node *LLTwo)
unionLL = NULL;
while(temp != NULL)
insert(&unionLL, temp->data);
temp = temp->next;
while(LLTwo != NULL)
if(!search(LLOne, LLTwo->data))
insert(&unionLL, LLTwo->data);
LLTwo = LLTwo->next;
return unionLL;
intersectionLL = NULL;
while(LLOne != NULL)
if(search(LLTwo, LLOne->data))
insert(&intersectionLL, LLOne->data);
LLOne = LLOne->next;
return intersectionLL;
}
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
int main()
initialize();
scanf("%d", &LLOneCount);
scanf("%d", &temp);
insert(&LLOne, temp);
printLinkedList(LLOne);
scanf("%d", &LLTwoCount);
scanf("%d", &temp);
insert(&LLTwo, temp);
}
printLinkedList(LLTwo);
findunion(LLOne, LLTwo);
intersection(LLOne, LLTwo);
printLinkedList(unionLL);
printLinkedList(intersectionLL);
return 0;
B) Write a ‘C’ program to read the adjacency matrix of directed graph and convert it
into adjacency list.
#include<stdio.h>
#include<malloc.h>
struct n
int data;
struct n *link;
};
NODE *getnode(int);
int main()
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
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;
display(h,n);
NODE *getnode(int j)
NODE* temp;
temp->data=j;
temp->link=NULL;
return(temp);
NODE *ptr;
for(ptr=h;ptr->link!=NULL;ptr=ptr->link);
return(ptr);
NODE *ptr;
int i;
for(i=0;i<n;i++)
ptr=h[i];
if(ptr==NULL)
printf("NULL");
while(ptr!=NULL)
printf(" ->V%d",ptr->data);
ptr=ptr->link;
printf("\n");
SLIP NO-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 Preorder traversing
technique
#include <stdlib.h>
#include<malloc.h>
struct node
int data ;
};
if(p==NULL)
p -> data = v;
}
else if (v<p->data)
p->left=insert(p->left,v);
else if(v>p->data)
p->right=insert(p->right,v);
return (p);
if (p == NULL)
return;
Inorder(p->left);
Inorder(p->right);
if (p!=NULL)
} }
int main()
int n,i,val;
for(i=0;i<n;i++)
scanf("%d",&val);
tree=insert(tree,val);
printf("\nInorder Traversal\n");
Inorder(tree);
printf("\npostorder Traversal\n");
postorder(tree); }
B) Write a ‘C’ program to accept two polynomial and find the addition of accepted
polynomials.(use array)
#include<stdio.h>
#include<math.h>
struct poly
float coeff;
int exp;
};
int main()
int i;
int deg1,deg2;
int k=0,l=0,m=0;
scanf("%d",°1);
for(i=0;i<=deg1;i++)
a[k++].exp = i;
scanf("%d",°2);
for(i=0;i<=deg2;i++)
scanf("%f",&b[i].coeff);
b[l++].exp = i;
printf("\nExpression 1 = %.1f",a[0].coeff);
for(i=1;i<=deg1;i++)
printf("+ %.1fx^%d",a[i].coeff,a[i].exp);
printf("\nExpression 2 = %.1f",b[0].coeff);
for(i=1;i<=deg2;i++)
printf("+ %.1fx^%d",b[i].coeff,b[i].exp);
if(deg1>deg2)
for(i=0;i<=deg2;i++)
c[m].exp = a[i].exp;
m++;
for(i=deg2+1;i<=deg1;i++)
{
c[m].coeff = a[i].coeff;
c[m].exp = a[i].exp;
m++;
else
for(i=0;i<=deg1;i++)
c[m].exp = a[i].exp;
m++;
for(i=deg1+1;i<=deg2;i++)
c[m].coeff = b[i].coeff;
c[m].exp = b[i].exp;
m++;
for(i=1;i<m;i++)
printf("+ %.1fx^%d",c[i].coeff,c[i].exp);
return 0;
}
SLIP N0=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 <stdlib.h>
#include<malloc.h>
struct node
int data ;
};
if(p==NULL)
p -> data = v;
else if (v<p->data)
p->left=insert(p->left,v);
else if(v>p->data)
p->right=insert(p->right,v);
return (p);
}
void Inorder(struct node* p)
if (p == NULL)
return;
Inorder(p->left);
Inorder(p->right);
if (p!=NULL)
int main()
int n,i,val;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&val);
tree=insert(tree,val);
printf("\nInorder Traversal\n");
Inorder(tree);
printf("\npostorder Traversal\n");
postorder(tree);
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<malloc.h>
struct node
int data;
};
int main()
int num,a[10],i,j;
scanf("%d",&num);
i=0;
while(num>0)
a[i]=num%10;
i++;
num=num/10;
i--;
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();
SLIP NO=6
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 <stdlib.h>
#include<malloc.h>
struct node
{
struct node *left;
int data ;
};
if(p==NULL)
p -> data = v;
else if (v<p->data)
p->left=insert(p->left,v);
else if(v>p->data)
p->right=insert(p->right,v);
return (p);
if (p == NULL)
return;
Inorder(p->left);
Inorder(p->right);
if (p!=NULL)
int main()
int n,i,val;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&val);
tree=insert(tree,val);
printf("\nInorder Traversal\n");
Inorder(tree);
printf("\npostorder Traversal\n");
postorder(tree);
B) Write a ‘C’ program to accept and sort n elements in ascending order by using bubble
sort
#include <stdio.h>
long array[100], n, c;
scanf("%ld", &n);
scanf("%ld", &array[c]);
bubble_sort(array, n);
printf("%ld\n", array[c]);
return 0;
long c, d, t;
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
SLIP N0=7
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 <stdlib.h>
struct btnode
int value;
// Function declaration
void create();
void insert();
void delete();
int flag = 1;
void main() {
int ch;
printf("\nOPERATIONS ---");
printf("2 - Display\n");
printf("3 - Delete\n");
printf("4 - Exit\n");
while(1) {
switch (ch) {
case 1:
insert();
break;
case 2:
display(root);
break;
case 3:
delete(root);
break;
case 4:
exit(0);
default :
printf("Invalid Input!");
break;
void create() {
int data;
scanf("%d", &data);
temp->value = data;
void insert() {
create();
if (root == NULL)
root = temp;
else
search(root);
search(t->r);
t->r = temp;
search(t->l);
t->l = temp;
if ((data>t->value)) {
t1 = t;
search1(t->r, data);
t1 = t;
search1(t->l, data);
else if ((data==t->value)) {
delete1(t);
if (root == NULL) {
return;
if (t->l != NULL) {
display(t->l);
if (t->r != NULL) {
display(t->r);
void delete() {
int data;
if (root == NULL) {
return;
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
// To delete a node
if (t1->l == t) {
t1->l = NULL;
else {
t1->r = NULL;
t = NULL;
free(t);
return;
if (t1 == t) {
root = t->l;
t1 = root;
else if (t1->l == t) {
t1->l = t->l;
else {
t1->r = t->l;
t = NULL;
free(t);
return;
root = t->r;
t1 = root;
else if (t1->r == t) {
t1->r = t->r;
else {
t1->l = t->r;
t == NULL;
free(t);
return;
t2 = root;
if (t->r != NULL) {
k = smallest(t->r);
flag = 1;
else {
k =largest(t->l);
flag = 2;
search1(root, k);
t->value = k;
if (t->l != NULL) {
t2 = t;
return(smallest(t->l));
else {
return (t->value);
if (t->r != NULL) {
t2 = t;
return(largest(t->r));
else {
return(t->value);
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 <stdlib.h>
struct node
int num;
}*stnode;
int NodeCount();
void displayList();
int main()
int n,totalNode;
printf("\n\n Linked List : Create a singly linked list and count the number of nodes :\n");
printf("------------------------------------------------------------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
totalNode = NodeCount();
return 0;
void createNodeList(int n)
int num, i;
if(stnode == NULL)
else
scanf("%d", &num);
tmp = stnode;
for(i=2; i<=n; i++)
if(fnNode == NULL)
break;
else
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
} } } }
int NodeCount()
int ctr = 0;
tmp = stnode;
while(tmp != NULL)
ctr++;
tmp = tmp->nextptr;
return ctr;
void displayList()
{
struct node *tmp;
if(stnode == NULL)
else
tmp = stnode;
while(tmp != NULL)
tmp = tmp->nextptr;
} } }
SLIP N0=8
A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes
- Create a Binary Search Tree
- Display
- Search the element in Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
struct btnode
int value;
// Function declaration
void create();
void insert();
void delete();
int flag = 1;
void main() {
int ch;
printf("\nOPERATIONS ---");
printf("2 - Display\n");
printf("3 - Delete\n");
printf("4 - Exit\n");
while(1) {
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
display(root);
break;
case 3:
delete(root);
break;
case 4:
exit(0);
default :
printf("Invalid Input!");
break;
void create() {
int data;
scanf("%d", &data);
temp->value = data;
void insert() {
create();
if (root == NULL)
root = temp;
else
search(root);
search(t->r);
t->r = temp;
search(t->l);
}
t->l = temp;
if ((data>t->value)) {
t1 = t;
search1(t->r, data);
t1 = t;
search1(t->l, data);
else if ((data==t->value)) {
delete1(t);
if (root == NULL) {
return;
if (t->l != NULL) {
display(t->l);
if (t->r != NULL) {
display(t->r);
} }
void delete() {
int data;
if (root == NULL) {
return;
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
// To delete a node
int k;
if (t1->l == t) {
t1->l = NULL;
else {
t1->r = NULL;
t = NULL;
free(t);
return;
if (t1 == t) {
root = t->l;
t1 = root;
else if (t1->l == t) {
t1->l = t->l;
else {
t1->r = t->l;
t = NULL;
free(t);
return;
if (t1 == t) {
root = t->r;
t1 = root;
else if (t1->r == t) {
t1->r = t->r;
else {
t1->l = t->r;
t == NULL;
free(t);
return;
t2 = root;
if (t->r != NULL) {
k = smallest(t->r);
flag = 1;
else {
k =largest(t->l);
flag = 2;
search1(root, k);
t->value = k;
} }
t2 = t;
if (t->l != NULL) {
t2 = t;
return(smallest(t->l));
else {
return (t->value);
} }
if (t->r != NULL) {
t2 = t;
return(largest(t->r));
else {
return(t->value);
}
B) Write a ‘C’ program to accept and sort n elements in ascending order by using insertion
sort.
#include<stdio.h>
#define MAX 20
int i, j, key ;
key =A[i];
A[j+1] = A[j];
A[j+1] =key;
int main()
int A[MAX],i,n;
scanf("%d",&n);
scanf("%d",&A[i]);
insertionsort (A,n);
printf("%d\t",A[i]);
}
SLIP NO=9
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
#include<stdio.h>
int stack[20];
void push(int x)
stack[++top] = x;
int pop()
return stack[top--];
int main()
char exp[20];
char *e;
int n1,n2,n3,num;
scanf("%s",exp);
e = exp;
while(*e != '\0')
if(isdigit(*e))
num = *e - 48;
push(num);
}
else
n1 = pop();
n2 = pop();
switch(*e)
case '+':
n3 = n1 + n2;
break;
case '-':
n3 = n2 - n1;
break;
case '*':
n3 = n1 * n2;
break;
case '/':
n3 = n2 / n1;
break;
} }
push(n3); }
e++; }
return 0; }
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 <malloc.h>
struct list
int data;
}*start;
void createlist(int);
void disp();
void main()
int i,n,m;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&m);
createlist(m);
disp();
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
start=p2;
disp();
void createlist(int m)
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
void disp()
while(q!=NULL)
printf("%d->",q->data);
q=q->link;
printf("NULL");
SLIP N0=10
A) Write a ‘C’ program to read ‘n’ integers and store them in a Binary search tree and
display the nodes level wise.
#include<stdio.h>
#include<stdlib.h>
struct node
int key;
};
temp->key = item;
return temp;
if (root != NULL)
{
inorder(root->left);
inorder(root->right);
}}
return node;
int main()
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
B) Write a ‘C’ program to sort randomly generated array elements using Insertion sort
method. (Use Random Function)
#include<stdio.h>
int main()
{
int i, j, count, temp, number[25];
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
number[j+1]=temp;
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
SLIP NO=11
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 <stdlib.h>
struct node
int num;
}*stnode;
void ser();
void displayList();
int main()
int n,num,m;
printf("\n\n Linked List : Insert a new node at the end of a Singly Linked List :\n");
printf("-------------------------------------------------------------------------\n");
scanf("%d", &n);
createNodeList(n);
displayList();
scanf("%d", &num);
NodeInsertatEnd(num);
printf("\n Data, after inserted in the list are : \n");
ser();
displayList();
scanf("\n%d",&m);
ser(m);
displayList();
return 0;
void createNodeList(int n)
int num, i;
if(stnode == NULL)
else
scanf("%d", &num);
tmp = stnode;
if(fnNode == NULL)
else
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
} } } }
if(fnNode == NULL)
else
fnNode->num = num;
fnNode->nextptr = NULL;
tmp = stnode;
while(tmp->nextptr != NULL)
tmp = tmp->nextptr;
tmp->nextptr = fnNode;
} }
while(q!=NULL)
if(q->num==num)
printf("\nElement Is Found");
break;
else
q=q->nextptr;
} }
if(q==NULL)
} }
void displayList()
if(stnode == NULL)
else
tmp = stnode;
while(tmp != NULL)
tmp = tmp->nextptr;
} } }
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<stdlib.h>
#include<malloc.h>
struct queue
int data;
}*front,*rear;
int insertQ(int n)
temp->data =n;
temp->next =NULL;
if(front == NULL)
rear=front=temp;
else
rear->next=temp;
rear=temp;
} }
int deleteQ()
int x;
if(front==rear)
front=rear=NULL;
else
front=front->next;
free(temp);
return(x);
int display()
while(temp)
printf("%d\t",temp->data);
} }
int main()
int ch,x;
do{
scanf("%d",&ch);
switch(ch)
scanf("%d",&x);
insertQ(x);
display();
break;
case 2 :if(front == NULL)
printf("Queue is Empty\n");
else{
display();
break;
case 3:
display();
break;
case 4 :
exit(1);
break;
} }
return 0;
SLIP NO=12
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>
#include<stdlib.h>
int n,g[10][10];
void inoutdegree()
int i,j,id=0,od=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
od+=g[i][j];
id+=g[j][i];
} }
main()
int i,j,cnt=0;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&g[i][j]);
} }
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(g[i][j]!=g[j][i])
cnt++;
printf("%d\t",g[i][j]);
printf("\n"); } }
SLIP N0=13
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<stdlib.h>
#include<ctype.h>
#include<string.h>
char stack[SIZE];
printf("\nStack Overflow.");
else {
top = top+1;
stack[top] = item;
char pop() {
char item ;
if(top <0) {
getchar();
exit(1);
else {
item = stack[top];
top = top-1;
return(item);
}
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-') {
return 1;
else {
return 0;
if(symbol == '^') {
return(3);
return(2);
return(1);
else {
return(0);
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0') {
if(item == '(') {
push(item);
postfix_exp[j] = item;
j++;
else if(is_operator(item) == 1) {
x=pop();
postfix_exp[j] = x;
j++;
x = pop();
push(x);
push(item);
x = pop();
while(x != '(') {
postfix_exp[j] = x;
j++;
x = pop();
else {
getchar();
exit(1);
i++;
item = infix_exp[i];
if(top>0) {
getchar();
exit(1);
if(top>0) {
getchar();
exit(1);
postfix_exp[j] = '\0';
int main() {
gets(infix);
InfixToPostfix(infix,postfix);
puts(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{
int data;
};
void main() {
int ch;
while(1) {
printf("\nMENU\n");
printf("1. Append.\n");
printf("2. Display.\n");
printf("3. Exit.\n");
scanf("%d", &ch);
switch(ch) {
case 1: append();
break;
case 2: display();
break;
case 3: exit(0);
} } }
// Case 1
void append() {
temp->left = NULL;
temp->right = NULL;
if(root == NULL) {
root = temp;
else {
p = root;
while(p->right != NULL) {
p = p->right;
p->right = temp;
temp->left = p;
// Case 2
void display() {
if(temp == NULL) {
else {
while(temp != NULL) {
if(temp->data % 2 != 0) {
printf("%d\t", temp->data);
temp = temp->right;
printf("\n"); } }
SLIP N0=14
A) Write a ‘C’ program to accept a string from user and reverse it using Dynamic
implementation of Stack.
#include<stdio.h>
#include<string.h>
struct Stack{
char arr[100];
int tos;
};
int main()
struct Stack s;
s.tos=-1;
char str[100];
scanf("%s",&str);
int i;
for(i=0;i<strlen(str);i++)
push(&s,str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop(&s);
return 0;
}
void push(struct Stack *p,char x){
if(p->tos==99){
printf("Stack is OverFolw");
return;
else{
p->tos=p->tos+1;
p->arr[p->tos]=x;
if(p->tos==-1){
return 0;
else{
int x=p->arr[p->tos];
p->tos=p->tos-1;
return x;
}
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>
int main()
char name[25][50],temp[25];
int n,i,j;
printf("-----------------------------------------------------\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
for(i=1;i<=n;i++)
for(j=0;j<=n-i;j++)
if(strcmp(name[j],name[j+1])>0)
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
for(i=0;i<=n;i++)
printf("%s\n",name[i]); }
SLIP N0=15
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<ctype.h>
char stack[100];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
return 0;
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
printf("%c ",pop());
push(*e);
e++;
while(top != -1)
printf("%c ",pop());
}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<conio.h>
#include<malloc.h>
struct Node
int data;
}*top;
newNode->data=value;
newNode->next=NULL;
if(top == NULL)
top=newNode;
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;
top=top->next;
free(temp);
} }
void display()
if(top==NULL)
printf("\nStack is Empty!!!\n");
else
while(temp!=NULL)
printf("%d---->",temp->data);
temp=temp->next;
printf("----->NULL");
} }
int main()
int choice,value;
while(1)
printf("\n#########MENU########\n");
printf("1.Push\n2.Pop\n3.Display\n4.Exit\n");
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);
} } }
SLIP N0=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>
int top,stack[max];
void push(char x) {
if(top == max-1){
printf("stack overflow");
else {
stack[++top]=x;
} }
void pop() {
printf("%c",stack[top--]);
}
main() {
char str[]="Testing";
int i;
for(i=0;i<len;i++) {
push(str[i]);
for(i=0;i<len;i++) {
pop();
} }
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;
};
void main() {
while(1) {
printf("\nMenu\n");
printf("1. Append\n");
printf("2. Search\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1: append();
break;
if(count != 0) {
else {
break;
case 3: exit(1);
} } }
// 1. Append
void append() {
scanf("%d", &temp->data);
temp->link = NULL;
if(root == NULL) {
root = temp;
else {
struct node* p;
p = root;
while(p->link != NULL) {
p = p->link;
}
p->link = temp;
int search() {
scanf("%d", &num);
p = root;
while(p != NULL) {
if(p->data == num) {
return count;
else {
p = p->link;
count++;
} }
return 0;
}
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>
int stack[20];
void push(int x) {
stack[++top] = x;
int pop() {
return stack[top--];
int main() {
char exp[20];
char *e;
int n1,n2,n3,num;
scanf("%s",exp);
e = exp;
while(*e != '\0') {
if(isdigit(*e)) {
num = *e - 48;
push(num);
else {
n1 = pop();
n2 = pop();
switch(*e) {
break;
break;
break;
push(n3);
e++;
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
SLIP N0=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
B) Write a ‘C’ program to accept and sort n elements in ascending order using Merge sort
method.
#include<stdio.h>
#include<conio.h>
int i, j, k;
i = 0;
j = 0;
k = first;
while(i < n1 && j < n2)
//Comparing the elements from temp. array(l & r) and then storing them to the original array
arr[k] = l[i];
i++; }
else
arr[k] = r[j];
j++; }
k++;
arr[k] = l[i];
i++;
k++;
arr[k] = r[j];
j++;
k++;
int mid;
{
mid = (first + last) / 2;
} }
int main()
int arr[20], n, i;
scanf("%d", &n);
scanf("%d", &arr[i]); }
printf("\t%d", arr[i]); }
mergesort(arr, 0, n-1);
printf("\t%d", arr[i]); }
return 0; }
SLIP N0=19
A) Write a ‘C’ program which accept the string and reverse each word of the string using
Dynamic implementation of stack.
Example: Input - This is an input string
Output - sihTsinatupnignirts
#include <stdio.h>
#include <string.h>
int top,stack[max];
void push(char x) {
if(top == max-1){
printf("stack overflow");
else {
stack[++top]=x;
} }
void pop() {
printf("%c",stack[top--]);
main() {
char str[]="Testing";
int i;
for(i=0;i<len;i++) {
push(str[i]);
for(i=0;i<len;i++) {
pop();
}
B) Write a ‘C’ program to create a singly Link list and display its alternative nodes. (start
displaying from first node)
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
};
void main() {
int ch;
while(1) {
printf("\nMenu\n");
printf("1. Append\n");
printf("2. Display\n");
printf("3. Exit\n");
scanf("%d", &ch);
switch(ch) {
case 1: append();
break;
case 2: display();
break;
case 3: exit(1);
} } }
void append() {
scanf("%d", &temp->data);
temp->link = NULL;
if(root == NULL) {
root = temp;
else {
struct node* p;
p = root;
while(p->link != NULL) {
p = p->link;
p->link = temp;
void display() {
int counter = 0;
temp = root;
if(temp == NULL) {
printf("\nLIST IS EMPTY!!\n");
else {
printf("\n");
while(temp != NULL) {
if(counter % 2 == 0) {
printf("%d\t", temp->data);
counter++;
temp = temp->link;
printf("\n"); } }
SLIP N0=20
A) Write a ‘C’ program which accept the string and check whether the string is Palindrome
or not using stack. (Use Static/Dynamic implementation of Stack)
B) Write a ‘C’ program to swap mth and nth element of singly linked list
#include<stdio.h>
#include<conio.h>
struct node{
int data;
struct node *link;
};
struct node *root = NULL;
void main() {
int ch;
while(1) {
printf("\nMENU\n");
printf("1. Append\n");
printf("2. Swap\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1: append();
break;
case 2: swap();
break;
case 3: display();
break;
case 4:exit(1);
default: printf("\nINVALID INPUT!!\n");
} } }
// case 1
void append() {
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d", &temp->data);
temp->link = NULL;
if(root == NULL) {
root = temp;
}
else {
struct node* p;
p = root;
while(p->link != NULL) {
p = p->link; }
p->link = temp; }
printf("\nData entered successfully.\n"); }
// Case 2
void swap() {
struct node *p, *q;
int m, n, i, temp;
printf("Enter the Mth position of the node you want to swap: ");
scanf("%d", &m);
printf("Enter the Nth position of the node you want to swap with: ");
scanf("%d", &n);
p = q = root;
// Travelling till location m
for(i = 1; i < m && p != NULL; i++) {
p = p->link; }
// Travelling till location n
for(i = 1; i < n && q != NULL; i++) {
q = q->link; }
// swaping
if(p != NULL && q != NULL) {
temp = p->data;
p->data = q->data;
q->data = temp;
printf("\nSwaping successfull.\n");
}
else {
printf("\nINVALID INPUT!!\n");
} }
// Case 3
void display() {
struct node* temp;
temp = root;
if(temp == NULL) {
printf("\nLIST IS EMPTY!!\n"); }
else {
printf("\n");
while(temp != NULL) {
printf("%d\t", temp->data);
temp = temp->link; }
printf("\n");
} }
SLIP N0=21
A) Write a ‘C’ program to read an adjacency matrix of a directed graph and traverse using
BFS.
#include<stdio.h>
#include<stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct node {
int vertex;
};
struct Graph {
int numVertices;
int* visited;
};
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
while (temp) {
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
temp = temp->next;
newNode->vertex = v;
newNode->next = NULL;
return newNode;
graph->numVertices = vertices;
int i;
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
q->front = -1;
q->rear = -1;
return q;
if (q->rear == -1)
return 1;
else
return 0;
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
return item;
int i = q->front;
if (isEmpty(q)) {
printf("Queue is empty");
} else {
int main() {
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;
B) Write a ‘C’ program Accept n elements from user store it in an array. Accept a value
from the user and use linear/Sequential search method to check whether the value is
present in array or not. Display proper message.
#include<stdio.h>
#include<conio.h>
void main() {
scanf("%d", &n);
scanf("%d", &arr[i]); }
scanf("%d", &search);
if(arr[i] == search) {
break; } }
if(i == n) {
} }