0% found this document useful (0 votes)
10 views91 pages

Slip Solution

Ds slips solution

Uploaded by

Vaibhav Bhor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views91 pages

Slip Solution

Ds slips solution

Uploaded by

Vaibhav Bhor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 91

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>

typedef struct node

struct node *l,*r;

int data;

}NODE;

NODE *insert(NODE *h,int v)

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;

printf("Enter How many nodes\t");

scanf("%d",&n);

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

printf("Enter value :");

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];

int i, num, power;

float x, polySum;

printf("Enter the order of the polynomial \n");

scanf("%d", &num);

printf("Enter the value of x \n");

scanf("%f", &x);
/* Read the coefficients into an array */

printf("Enter %d coefficients \n", num + 1);

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

scanf("%d", &array[i]);

polySum = array[0];

for (i = 1; i <= num; i++)

polySum = polySum * x + array[i];

power = num;

printf("Given polynomial is: \n");

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

if (power < 0)

break;

if (array[i] > 0)

printf(" + ");

else if (array[i] < 0)

printf(" - ");

else

printf(" ");

printf("%dx^%d ", abs(array[i]), power--);

printf("\n Sum of the polynomial = %6.2f \n", polySum);

}
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>

#define MAX 100

int top=-1;

int item;

char stack_string[MAX];

void pushChar(char item);

char popChar(void);

int isEmpty(void);

int isFull(void);

int main()

char str[MAX];

int i;

printf("Input a string: ");

scanf("%[^\n]s",str);

for(i=0;i<strlen(str);i++)

pushChar(str[i]);

for(i=0;i<strlen(str);i++)

str[i]=popChar();

printf("Reversed String is: %s\n",str);

return 0;

void pushChar(char item)

if(isFull())

printf("\nStack is FULL !!!\n");


return;

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;

struct node * nextptr;

}*stnode;

void ClListcreation(int n);

void displayClList();

int main()

int n;

stnode = NULL;

printf("\n\n Circular Linked List : Create and display a circular linked list :\n");

printf("-----------------------------------------------------------------------\n");

printf(" Input the number of nodes : ");

scanf("%d", &n);

ClListcreation(n);

displayClList();

return 0;

void ClListcreation(int n)

int i, num;

struct node *preptr, *newnode;

if(n >= 1)

stnode = (struct node *)malloc(sizeof(struct node));

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;
stnode->nextptr = NULL;

preptr = stnode;

for(i=2; i<=n; i++)

newnode = (struct node *)malloc(sizeof(struct node));

printf(" Input data for node %d : ", i);

scanf("%d", &num);

newnode->num = num;

newnode->nextptr = NULL;

preptr->nextptr = newnode;

preptr = newnode;

preptr->nextptr = stnode

} }

void displayClList()

struct node *tmp;

int n = 1;

if(stnode == NULL)

printf(" No data found in the List yet.");

else

tmp = stnode;

printf("\n\n Data entered in the list are :\n");

do {

printf(" Data %d = %d\n", n, tmp->num);

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;

struct node *next;

} *LLOne, *LLTwo, *unionLL, *intersectionLL;

void initialize(){

LLOne = LLTwo = NULL;

void insert(struct node **head, int num)

struct node* newNode = (struct node*) malloc(sizeof(struct node));

newNode->data = num;

newNode->next = *head;

*head = newNode;

int search(struct node *head, int num)

while (head != NULL)

if (head->data == num)

return 1;

head = head->next;

return 0;

}
struct node* findunion(struct node *LLOne, struct node *LLTwo)

unionLL = NULL;

struct node *temp = LLOne;

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;

struct node* intersection(struct node *LLOne, struct node *LLTwo)

intersectionLL = NULL;

while(LLOne != NULL)

if(search(LLTwo, LLOne->data))

insert(&intersectionLL, LLOne->data);

LLOne = LLOne->next;

return intersectionLL;
}

void printLinkedList(struct node *nodePtr)

while (nodePtr != NULL) {

printf("%d", nodePtr->data);

nodePtr = nodePtr->next;

if(nodePtr != NULL)

printf("-->");

int main()

int i, LLOneCount, LLTwoCount, temp;

initialize();

printf("Enter number of nodes in first Linked List\n");

scanf("%d", &LLOneCount);

printf("Enter %d integers\n", LLOneCount);

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

scanf("%d", &temp);

insert(&LLOne, temp);

printLinkedList(LLOne);

printf("\nEnter number of nodes in second Linked List\n");

scanf("%d", &LLTwoCount);

printf("Enter %d integers\n", LLTwoCount);

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

scanf("%d", &temp);

insert(&LLTwo, temp);

}
printLinkedList(LLTwo);

findunion(LLOne, LLTwo);

intersection(LLOne, LLTwo);

printf("\nUnion Linked List\n");

printLinkedList(unionLL);

printf("\nIntersection Linked List\n");

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;

};

typedef struct n NODE;

NODE *getnode(int);

NODE *findlast(NODE *);

void display(NODE *[],int);

int main()

NODE *ptr,*temp,*h[10];

int n,a[10][10],i,j;

printf("\n Enter total number of vertices :");

scanf("%d",&n);

printf("\n Enter entries of an adjacency matrix :\n");


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

for(j=0;j<n;j++)

printf("\n Enter a[%d][%d] : ",i+1,j+1);

scanf("%d",&a[i][j]);

printf("\n Entered Adjacency matrix is … \n");

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;

printf("\n The Adjacency list looks like … \n");

display(h,n);

NODE *getnode(int j)

NODE* temp;

temp =(NODE *) malloc(sizeof(NODE));

temp->data=j;

temp->link=NULL;

return(temp);

NODE *findlast(NODE *h)

NODE *ptr;

for(ptr=h;ptr->link!=NULL;ptr=ptr->link);

return(ptr);

void display(NODE *h[10],int n)

NODE *ptr;

int i;

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

printf("\n V%d ",i+1);

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

struct node *left;

int data ;

struct node *right;

};

struct node *insert(struct node *p, int v)

if(p==NULL)

p=(struct node *) malloc(sizeof(struct node));

p -> left = p -> right = 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);

printf("%d ", p->data);

Inorder(p->right);

void postorder (struct node *p)

if (p!=NULL)

postorder (p -> left);

postorder(p -> right);

printf("%d \t", p-> data);

} }

int main()

int n,i,val;

struct node *tree=NULL,*temp=NULL;

printf("Enter How many nodes\t");


scanf("%d",&n);

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

printf("Enter value :");

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;

};

struct poly a[50],b[50],c[50],d[50];

int main()

int i;

int deg1,deg2;

int k=0,l=0,m=0;

printf("Enter the highest degree of poly1:");

scanf("%d",&deg1);

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

printf("\nEnter the coeff of x^%d :",i);


scanf("%f",&a[i].coeff);

a[k++].exp = i;

printf("\nEnter the highest degree of poly2:");

scanf("%d",&deg2);

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

printf("\nEnter the coeff of x^%d :",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].coeff = a[i].coeff + b[i].coeff;

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].coeff = a[i].coeff + b[i].coeff;

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++;

printf("\nExpression after additon = %.1f",c[0].coeff);

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

struct node *left;

int data ;

struct node *right;

};

struct node *insert(struct node *p, int v)

if(p==NULL)

p=(struct node *) malloc(sizeof(struct node));

p -> left = p -> right = 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);

printf("%d ", p->data);

Inorder(p->right);

void postorder (struct node *p)

if (p!=NULL)

postorder (p -> left);

postorder(p -> right);

printf("%d \t", p-> data);

int main()

int n,i,val;

struct node *tree=NULL,*temp=NULL;

printf("Enter How many nodes\t");

scanf("%d",&n);

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

printf("Enter value :");

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;

struct node *next;

};

struct node *start=NULL,*temp=NULL;

int main()

int num,a[10],i,j;

printf("enter the number:-");

scanf("%d",&num);

i=0;

while(num>0)

a[i]=num%10;

i++;

num=num/10;

i--;

printf("\nthe display of linked list is:-\n");

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 ;

struct node *right;

};

struct node *insert(struct node *p, int v)

if(p==NULL)

p=(struct node *) malloc(sizeof(struct node));

p -> left = p -> right = 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);

printf("%d ", p->data);

Inorder(p->right);

void postorder (struct node *p)


{

if (p!=NULL)

postorder (p -> left);

postorder(p -> right);

printf("%d \t", p-> data);

int main()

int n,i,val;

struct node *tree=NULL,*temp=NULL;

printf("Enter How many nodes\t");

scanf("%d",&n);

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

printf("Enter value :");

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>

void bubble_sort(long [], long);


int main()

long array[100], n, c;

printf("Enter number of elements\n");

scanf("%ld", &n);

printf("Enter %ld integers\n", n);

for (c = 0; c < n; c++)

scanf("%ld", &array[c]);

bubble_sort(array, n);

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)

printf("%ld\n", array[c]);

return 0;

void bubble_sort(long list[], long n)

long c, d, t;

for (c = 0 ; c < n - 1; c++) {

for (d = 0 ; d < n - c - 1; d++) {

if (list[d] > list[d+1]) {

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;

struct btnode *l;

struct btnode *r;

}*root = NULL, *temp = NULL, *t2, *t1;

// Function declaration

void create();

void insert();

void search(struct btnode *t);

void search1(struct btnode *t, int data);

void display(struct btnode *t);

void delete();

void delete1(struct btnode *t);

int flag = 1;

void main() {

int ch;

printf("\nOPERATIONS ---");

printf("\n1 - Insert an element into tree\n");

printf("2 - Display\n");

printf("3 - Delete\n");

printf("4 - Exit\n");

while(1) {

printf("\nEnter your choice : ");


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;

printf("Enter data of node to be inserted : ");

scanf("%d", &data);

temp = (struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;

void insert() {

create();

if (root == NULL)

root = temp;
else

search(root);

// find the right position in the tree to insert the data

void search(struct btnode *t) {

if ((temp->value > t->value) && (t->r != NULL)) {

/* value more than root node value insert at right */

search(t->r);

else if ((temp->value > t->value) && (t->r == NULL)) {

t->r = temp;

else if ((temp->value < t->value) && (t->l != NULL)) {

/* value less than root node value insert at left */

search(t->l);

else if ((temp->value < t->value) && (t->l == NULL)) {

t->l = temp;

// Search for the appropriate position to insert the new node

void search1(struct btnode *t, int data) {

if ((data>t->value)) {

t1 = t;

search1(t->r, data);

else if ((data < t->value)) {

t1 = t;

search1(t->l, data);

else if ((data==t->value)) {
delete1(t);

void display(struct btnode *t) {

if (root == NULL) {

printf("No elements in a tree to display");

return;

printf("%d -> ", t->value);

if (t->l != NULL) {

display(t->l);

if (t->r != NULL) {

display(t->r);

void delete() {

int data;

if (root == NULL) {

printf("No elements in a tree to delete");

return;

printf("Enter the data to be deleted : ");

scanf("%d", &data);

t1 = root;

t2 = root;

search1(root, data);

// To delete a node

void delete1(struct btnode *t) {


int k;

// To delete leaf node

if ((t->l == NULL) && (t->r == NULL)) {

if (t1->l == t) {

t1->l = NULL;

else {

t1->r = NULL;

t = NULL;

free(t);

return;

// To delete node having one left hand child

else if ((t->r == NULL)) {

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;

// To delete node having right hand child

else if (t->l == NULL) {


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;

// To delete node having two child

else if ((t->l != NULL) && (t->r != NULL)) {

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;

// To find the smallest element in the right sub tree

int smallest(struct btnode *t) {


t2 = t;

if (t->l != NULL) {

t2 = t;

return(smallest(t->l));

else {

return (t->value);

// To find the largest element in the left sub tree

int largest(struct btnode *t) {

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;

struct node *nextptr;

}*stnode;

void createNodeList(int n);

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");

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list are : \n");

displayList();

totalNode = NodeCount();

printf("\n Total number of nodes = %d\n", totalNode);

return 0;

void createNodeList(int n)

struct node *fnNode, *tmp;

int num, i;

stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL)

printf(" Memory can not be allocated.");

else

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> num = num;

stnode-> nextptr = NULL;

tmp = stnode;
for(i=2; i<=n; i++)

fnNode = (struct node *)malloc(sizeof(struct node));

if(fnNode == NULL)

printf(" Memory can not be allocated.");

break;

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num;

fnNode->nextptr = NULL;

tmp->nextptr = fnNode;

tmp = tmp->nextptr;

} } } }

int NodeCount()

int ctr = 0;

struct node *tmp;

tmp = stnode;

while(tmp != NULL)

ctr++;

tmp = tmp->nextptr;

return ctr;

void displayList()

{
struct node *tmp;

if(stnode == NULL)

printf(" No data found in the list.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num);

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;

struct btnode *l;

struct btnode *r;

}*root = NULL, *temp = NULL, *t2, *t1;

// Function declaration

void create();
void insert();

void search(struct btnode *t);

void search1(struct btnode *t, int data);

void display(struct btnode *t);

void delete();

void delete1(struct btnode *t);

int flag = 1;

void main() {

int ch;

printf("\nOPERATIONS ---");

printf("\n1 - Insert an element into tree\n");

printf("2 - Display\n");

printf("3 - Delete\n");

printf("4 - Exit\n");

while(1) {

printf("\nEnter your choice : ");

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;

printf("Enter data of node to be inserted : ");

scanf("%d", &data);

temp = (struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;

void insert() {

create();

if (root == NULL)

root = temp;

else

search(root);

// find the right position in the tree to insert the data

void search(struct btnode *t) {

if ((temp->value > t->value) && (t->r != NULL)) {

/* value more than root node value insert at right */

search(t->r);

else if ((temp->value > t->value) && (t->r == NULL)) {

t->r = temp;

else if ((temp->value < t->value) && (t->l != NULL)) {

/* value less than root node value insert at left */

search(t->l);
}

else if ((temp->value < t->value) && (t->l == NULL)) {

t->l = temp;

// Search for the appropriate position to insert the new node

void search1(struct btnode *t, int data) {

if ((data>t->value)) {

t1 = t;

search1(t->r, data);

else if ((data < t->value)) {

t1 = t;

search1(t->l, data);

else if ((data==t->value)) {

delete1(t);

void display(struct btnode *t) {

if (root == NULL) {

printf("No elements in a tree to display");

return;

printf("%d -> ", t->value);

if (t->l != NULL) {

display(t->l);

if (t->r != NULL) {

display(t->r);

} }
void delete() {

int data;

if (root == NULL) {

printf("No elements in a tree to delete");

return;

printf("Enter the data to be deleted : ");

scanf("%d", &data);

t1 = root;

t2 = root;

search1(root, data);

// To delete a node

void delete1(struct btnode *t) {

int k;

// To delete leaf node

if ((t->l == NULL) && (t->r == NULL)) {

if (t1->l == t) {

t1->l = NULL;

else {

t1->r = NULL;

t = NULL;

free(t);

return;

// To delete node having one left hand child

else if ((t->r == NULL)) {

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;

// To delete node having right hand child

else if (t->l == NULL) {

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;

// To delete node having two child

else if ((t->l != NULL) && (t->r != NULL)) {

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;

} }

// To find the smallest element in the right sub tree

int smallest(struct btnode *t) {

t2 = t;

if (t->l != NULL) {

t2 = t;

return(smallest(t->l));

else {

return (t->value);

} }

// To find the largest element in the left sub tree

int largest(struct btnode *t) {

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

void insertionsort(int A[MAX], int n)

int i, j, key ;

for(i=1; i<n; i++)

key =A[i];

for(j=i-1;(j>=0)&&(key < A[j]);j--)

A[j+1] = A[j];

A[j+1] =key;

int main()

int A[MAX],i,n;

printf("How many elements you want to sort ?\n");

scanf("%d",&n);

printf("\nEnter the element into an arry:\n");

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

scanf("%d",&A[i]);

insertionsort (A,n);

printf("\nElements is Asending sort:\n");

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

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];

int top = -1;

void push(int x)

stack[++top] = x;

int pop()

return stack[top--];

int main()

char exp[20];

char *e;

int n1,n2,n3,num;

printf("Enter the expression :: ");

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++; }

printf("\nThe result of expression %s = %d\n\n",exp,pop());

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;

struct list *link;

}*start;

void createlist(int);

void disp();

void main()

struct list *p1,*p2,*p3;

int i,n,m;

printf("\nHow many nodes u Want to Created");

scanf("%d",&n);

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

printf("\n\nEnter the Data");

scanf("%d",&m);

createlist(m);

printf("\n\nCreated List Is\n\n");

disp();

printf("\n\nReverses List is\n\n");

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)

struct list *tmp,*q;

tmp=(struct list *) malloc(sizeof(struct list));

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()

struct list *q;


q=start;

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;

struct node *left, *right;

};

struct node *newNode(int item)

struct node *temp = (struct node *)malloc(sizeof(struct node));

temp->key = item;

temp->left = temp->right = NULL;

return temp;

void inorder(struct node *root)

if (root != NULL)

{
inorder(root->left);

printf("%d \n", root->key);

inorder(root->right);

}}

struct node* insert(struct node* node, int key)

if (node == NULL) return newNode(key);

if (key < node->key)

node->left = insert(node->left, key);

else if (key > node->key)

node->right = insert(node->right, key);

return node;

int main()

struct node *root = NULL;

root = insert(root, 50);

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];

printf("How many numbers u are going to enter?: ");

scanf("%d",&count);

printf("Enter %d elements: ", 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;

printf("Order of Sorted elements: ");

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;

struct node *nextptr;

}*stnode;

void createNodeList(int n);

void NodeInsertatEnd(int num);

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");

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list are : \n");

displayList();

printf("\n Input data to insert at the end of the list : ");

scanf("%d", &num);

NodeInsertatEnd(num);
printf("\n Data, after inserted in the list are : \n");

ser();

displayList();

printf("\nENTER THE ELEMENT FOR SEARCH: ");

scanf("\n%d",&m);

ser(m);

displayList();

return 0;

void createNodeList(int n)

struct node *fnNode, *tmp;

int num, i;

stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL)

printf(" Memory can not be allocated.");

else

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> num = num;

stnode-> nextptr = NULL;

tmp = stnode;

for(i=2; i<=n; i++)

fnNode = (struct node *)malloc(sizeof(struct node));

if(fnNode == NULL)

printf(" Memory can not be allocated.");


break;

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num;

fnNode->nextptr = NULL;

tmp->nextptr = fnNode;

tmp = tmp->nextptr;

} } } }

void NodeInsertatEnd(int num)

struct node *fnNode, *tmp;

fnNode = (struct node*)malloc(sizeof(struct node));

if(fnNode == NULL)

printf(" Memory can not be allocated.");

else

fnNode->num = num;

fnNode->nextptr = NULL;

tmp = stnode;

while(tmp->nextptr != NULL)

tmp = tmp->nextptr;

tmp->nextptr = fnNode;

} }

void ser(int num)

struct node *q,*tmp;


q=stnode;

while(q!=NULL)

if(q->num==num)

printf("\nElement Is Found");

break;

else

q=q->nextptr;

} }

if(q==NULL)

printf("\nElement is Not Found");

} }

void displayList()

struct node *tmp;

if(stnode == NULL)

printf(" No data found in the empty list.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num);

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;

struct queue *next;

}*front,*rear;

int insertQ(int n)

struct queue *temp;

temp =(struct queue *)malloc(sizeof(struct queue));

temp->data =n;

temp->next =NULL;

if(front == NULL)

rear=front=temp;

else

rear->next=temp;

rear=temp;

} }

int deleteQ()

int x;

struct queue *temp=front;


x=front->data;

if(front==rear)

front=rear=NULL;

else

front=front->next;

free(temp);

return(x);

int display()

struct queue *temp=front;

printf("\nQuene contents are:\t");

while(temp)

printf("%d\t",temp->data);

temp =temp ->next;

} }

int main()

int ch,x;

do{

printf("\n 1-Insert \n2-Delete\n3-Display\n4-Exit\n");

printf("Enter youre choice\n");

scanf("%d",&ch);

switch(ch)

case 1: printf("Enter element to be insert \n");

scanf("%d",&x);

insertQ(x);

display();

break;
case 2 :if(front == NULL)

printf("Queue is Empty\n");

else{

printf("Deleted element is %d\n",deleteQ());

display();

break;

case 3:

display();

break;

case 4 :

exit(1);

break;

} }

}while(ch>0 && ch<5);

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];

printf("v%d indegree =%d \t Outdegree= %d\n",i,id,od);

} }

main()

int i,j,cnt=0;

printf("How many vertices");

scanf("%d",&n);

printf("Enter matrix elements\n");

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

for(j=0;j<n;j++)

scanf("%d",&g[i][j]);

} }

printf("Adjacency matrix Is\n");

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>

#define SIZE 100

char stack[SIZE];

int top = -1;

void push(char item) {

if(top >= SIZE-1) {

printf("\nStack Overflow.");

else {

top = top+1;

stack[top] = item;

char pop() {

char item ;

if(top <0) {

printf("stack under flow: invalid infix expression");

getchar();

exit(1);

else {

item = stack[top];

top = top-1;

return(item);
}

int is_operator(char symbol) {

if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-') {

return 1;

else {

return 0;

int precedence(char symbol) {

if(symbol == '^') {

return(3);

else if(symbol == '*' || symbol == '/') {

return(2);

else if(symbol == '+' || symbol == '-') {

return(1);

else {

return(0);

void InfixToPostfix(char infix_exp[], char postfix_exp[]) {

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);

else if( isdigit(item) || isalpha(item)) {

postfix_exp[j] = item;

j++;

else if(is_operator(item) == 1) {

x=pop();

while(is_operator(x) == 1 && precedence(x)>= precedence(item)) {

postfix_exp[j] = x;

j++;

x = pop();

push(x);

push(item);

else if(item == ')') {

x = pop();

while(x != '(') {

postfix_exp[j] = x;

j++;

x = pop();

else {

printf("\nInvalid infix Expression.\n");

getchar();
exit(1);

i++;

item = infix_exp[i];

if(top>0) {

printf("\nInvalid infix Expression.\n");

getchar();

exit(1);

if(top>0) {

printf("\nInvalid infix Expression.\n");

getchar();

exit(1);

postfix_exp[j] = '\0';

int main() {

char infix[SIZE], postfix[SIZE];

printf("\nEnter Infix expression : ");

gets(infix);

InfixToPostfix(infix,postfix);

printf("Postfix Expression: ");

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{

struct node *left;

int data;

struct node *right;

};

struct node *root = NULL;

void main() {

int ch;

while(1) {

printf("\nMENU\n");

printf("1. Append.\n");

printf("2. Display.\n");

printf("3. Exit.\n");

printf("\nEnter you choice: ");

scanf("%d", &ch);

switch(ch) {

case 1: append();

break;

case 2: display();

break;

case 3: exit(0);

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->left = NULL;

temp->right = NULL;

if(root == NULL) {

root = temp;

else {

struct node *p;

p = root;

while(p->right != NULL) {

p = p->right;

p->right = temp;

temp->left = p;

printf("\nData entered successfully.\n");

// Case 2

void display() {

struct node *temp = root;

if(temp == NULL) {

printf("\nTHE LIST IS EMPTY!!\n");

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;

};

void push(struct Stack*,char);

char pop(struct Stack*);

int main()

struct Stack s;

s.tos=-1;

char str[100];

printf("Enter string reverse:\n");

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);

printf("After reversing string: %s",str);

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;

char pop(struct Stack* p){

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\nSorts the strings of an array using bubble sort :\n");

printf("-----------------------------------------------------\n");

printf("Input number of strings :");

scanf("%d",&n);

printf("Input string %d :\n",n);

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

fgets(name[i], sizeof name, stdin);

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);

printf("The strings appears after sorting :\n");

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];

int top = -1;

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;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

return 0;

int main()

char exp[100];
char *e, x;

printf("Enter the expression : ");

scanf("%s",exp);

printf("\n");

e = exp;

while(*e != '\0')

if(isalnum(*e))

printf("%c ",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

while((x = pop()) != '(')

printf("%c ", x);

else

while(priority(stack[top]) >= priority(*e))

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;

struct Node *next;

}*top;

void push(int value)

struct Node *newNode;

newNode= (struct Node*)malloc(sizeof(struct Node));

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;

printf("\nDeleted element : %d",temp->data);

free(temp);

} }

void display()

if(top==NULL)

printf("\nStack is Empty!!!\n");

else

struct Node *temp=top;

while(temp!=NULL)

printf("%d---->",temp->data);

temp=temp->next;

printf("----->NULL");

} }

int main()

int choice,value;

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");

} } }

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>

#define max 100

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 len = strlen(str);

int i;

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

push(str[i]);

printf("\nThis the reversed string: ");

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;

struct node *link;

};

struct node *root = NULL;

void main() {

int ch, count;

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;

case 2: count = search();

if(count != 0) {

printf("\nIndex number: %d\n", count);

else {

printf("\nNumber not found.\n");

break;

case 3: exit(1);

default: printf("\nINVALID INPUT!!\n");

} } }

// 1. Append

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");

int search() {

struct node *p;

int num, count = 1;

printf("Enter number you want to 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];

int top = -1;

void push(int x) {

stack[++top] = x;

int pop() {

return stack[top--];

int main() {

char exp[20];

char *e;

int n1,n2,n3,num;

printf("Enter the expression :: ");

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++;

printf("\nThe result of expression %s = %d\n\n",exp,pop());

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>

void merge(int arr[20], int first, int mid, int last)

int i, j, k;

//Storing the number of values from first half of the array in n1

int n1 = (mid - first) + 1;

//Storing the number of values from second half of the array in n2

int n2 = last - mid;

//Declaring to empty arrays

int l[10], r[10];

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

//Transfering values of original array to l(only the values of first half)

l[i] = arr[first + i];

for(j = 0; j < n2; j++)

r[j] = arr[mid + 1 + j];

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

if(l[i] <= r[j])

arr[k] = l[i];

i++; }

else

arr[k] = r[j];

j++; }

k++;

while(i < n1)

arr[k] = l[i];

i++;

k++;

while(j < n2)

arr[k] = r[j];

j++;

k++;

void mergesort(int arr[20], int first, int last)

int mid;

if(first < last)

{
mid = (first + last) / 2;

//Divide the first part of the array

mergesort(arr, first, mid);

//Divide the second part of the array

mergesort(arr, mid + 1, last);

//Merge all the arrays to one

merge(arr, first, mid, last);

} }

int main()

int arr[20], n, i;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

//Accepting rray elements

printf("\nEnter the elements: ");

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

scanf("%d", &arr[i]); }

//Printing the original array

printf("\nThe original array is: \n");

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

printf("\t%d", arr[i]); }

//Calling mergesort function to divide the array into parts

mergesort(arr, 0, n-1);

//Printing the sorted array

printf("\n\nThe sorted array is: \n");

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

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>

#define max 100

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 len = strlen(str);

int i;

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

push(str[i]);

printf("\nThis the reversed string: ");

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;

struct node *link;

};

struct node *root = NULL;

void main() {

int ch;

while(1) {

printf("\nMenu\n");

printf("1. Append\n");

printf("2. Display\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &ch);

switch(ch) {

case 1: append();

break;

case 2: display();

break;

case 3: exit(1);

default: printf("\nINVALID INPUT!!\n");

} } }

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");

void display() {

int counter = 0;

struct node* temp;

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 queue* createQueue();

void enqueue(struct queue* q, int);

int dequeue(struct queue* q);

void display(struct queue* q);

int isEmpty(struct queue* q);

void printQueue(struct queue* q);

struct node {

int vertex;

struct node* next;

};

struct node* createNode(int);

struct Graph {

int numVertices;

struct node** adjLists;

int* visited;

};

void bfs(struct Graph* graph, int startVertex) {

struct queue* q = createQueue();

graph->visited[startVertex] = 1;

enqueue(q, startVertex);
while (!isEmpty(q)) {

printQueue(q);

int currentVertex = dequeue(q);

printf("Visited %d\n", currentVertex);

struct node* temp = graph->adjLists[currentVertex];

while (temp) {

int adjVertex = temp->vertex;

if (graph->visited[adjVertex] == 0) {

graph->visited[adjVertex] = 1;

enqueue(q, adjVertex);

temp = temp->next;

struct node* createNode(int v) {

struct node* newNode = malloc(sizeof(struct node));

newNode->vertex = v;

newNode->next = NULL;

return newNode;

struct Graph* createGraph(int vertices) {

struct Graph* graph = malloc(sizeof(struct Graph));

graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct node*));

graph->visited = malloc(vertices * sizeof(int));

int i;

for (i = 0; i < vertices; i++) {

graph->adjLists[i] = NULL;

graph->visited[i] = 0;

}
return graph;

void addEdge(struct Graph* graph, int src, int dest) {

struct node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

newNode = createNode(src);

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;

struct queue* createQueue() {

struct queue* q = malloc(sizeof(struct queue));

q->front = -1;

q->rear = -1;

return q;

int isEmpty(struct queue* q) {

if (q->rear == -1)

return 1;

else

return 0;

void enqueue(struct queue* q, int value) {

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 dequeue(struct queue* q) {

int item;

if (isEmpty(q)) {

printf("Queue is empty");

item = -1;

} else {

item = q->items[q->front];

q->front++;

if (q->front > q->rear) {

printf("Resetting queue ");

q->front = q->rear = -1;

return item;

void printQueue(struct queue* q) {

int i = q->front;

if (isEmpty(q)) {

printf("Queue is empty");

} else {

printf("\nQueue contains \n");

for (i = q->front; i < q->rear + 1; i++) {

printf("%d ", q->items[i]);

int main() {

struct Graph* graph = createGraph(6);

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() {

int arr[100], n, i, search;

printf("Enter the number of elements you want in the array: ");

scanf("%d", &n);

for(i=0; i<n; i++) {

printf("Enter value for index %d: ", i);

scanf("%d", &arr[i]); }

printf("\nEnter the number you want to search in the array: ");

scanf("%d", &search);

for(i=0; i<n; i++) {

if(arr[i] == search) {

printf("\n%d found at %d position.\n", search, i);

break; } }

if(i == n) {

printf("\n%d not found in the array.\n");

} }

You might also like