DSA Labmanual PDF
DSA Labmanual PDF
DSA Labmanual PDF
LAB MANUAL
18CSL38
List of Experiments
SL Experiment/Program
No.
1 Array Operations
2 Operations on Strings
3 Stacks
4 Conversion from infix expression to postfix expression
5 a. Evaluation of suffix expression
b. Tower of Hanoi
6 Circular Queue
7 Operation on SLL for student data
8 Operation on DLL for employee data
9 Operations on CLL
a) Evaluation of a polynomial
b) Sum of two polynomial
10 Operation on Binary Search Tree
11 Operation on Graphs (G) of cities
12 C program on Hashing technique & resolving collision using linear probing
Program-1
/*****************************************************************************
1. Design, Develop and Implement a menu driven Program in C for the following Array
operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position(POS)
e. Exit.
Support the program with functions for each of the above operations.
*****************************************************************************/
#include<stdio.h>
void create()
{
printf("\nEnter the size of the array elements: ");
scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
}
void display()
{
int i;
printf("\nThe array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t", a[i]);
}
}
void insert()
{
printf("\nEnter the position for the new element: ");
scanf("%d", &pos);
printf("\nEnter the element to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos; i--)
{
a[i+1] = a[i];
}
a[pos] = elem;
n = n+1;
}
void main()
{
int ch;
clrscr();
do{
printf("\n\n--------Menu-----------\n");
printf("1.Create\n 2.Display\n 3.Insert\n 4.Delete\n
5.Exit\n");
printf("-------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: del();
break;
case 5: exit(0);
break;
default: printf("\nInvalid choice:\n");
break;
}
}while(ch!=5);
getch();
}
OUTPUT:
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 1
Enter the size of the array elements: 5
Enter the elements for the array:
10 20 30 40 50
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 2
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 2
The array elements are:
10 20 90 30 40
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter your choice: 5
Program-2
/*****************************************************************************
2. Design, Develop and Implement a Program in C for the following
operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace
String (REP)
b. Perform Pattern Matching Operation: Find and Replace all
occurrences of PAT in STR
with REP if PAT exists in STR. Report suitable messages in case PAT
does not exist in
STR
Support the program with functions for each of the above operations.
Don't use Built-in
functions.
**********************************************************
*******************/
#include<stdio.h>
#include<stdlib.h>
void stringmatch()
{
i = m = c = j = 0;
while(str[c] ! = '\0')
{
if(str[m] = = pat[i]) // ...... matching
{
i++; m++;
if(pat[i] = = '\0') //.....found occurrences.
{
flag = 1;
//.... copy replace string in ans string.
for(k = 0; rep[k] != '\0'; k++, j++)
ans[j] = rep[k];
i = 0;
c = m;
}
}
else //... mismatch
{
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
ans[j] = '\0';
}
void main()
{
printf("\nEnter a main string \n");
gets(str);
printf("\nEnter a pattern string \n");
gets(pat);
printf("\nEnter a replace string \n");
gets(rep);
stringmatch();
if(flag = = 1)
printf("\nThe resultant string is\n %s" , ans);
else
printf("\nPattern string NOT found\n");
}
OUTPUT:
Enter a main string
Test
Enter a pattern string
Te
Enter a replace string
Re
Program-3
/*****************************************************************************
3. Design, Develop and Implement a menu driven program in C for the following
operations on
STACK of integers (Array implementation of stack with maximum size MAX)
a. Push an element on to stack
b. Pop an element from stack.
c. Demonstrate how stack can be used to check palindrome.
d. Demonstrate Overflow and Underflow situations on stack.
e. Display the status of stack.
f. Exit.
Support the program with appropriate functions for each of the above operations.
******************************************************************
***********/
#include<stdio.h>
#include<conio.h>
#define MAX 4
if (top == (MAX-1))
printf("\n\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
void main()
{
do{
printf("\n\n----MAIN MENU----\n");
printf("\n1. PUSH (Insert) in the Stack");
printf("\n2. POP (Delete) from the Stack");
printf("\n3. PALINDROME check using Stack");
printf("\n4. Exit (End the Execution)");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter a element to be pushed: ");
scanf("%d", &item);
push(stack, item);
display(stack);
break;
case 2: pop(stack);
display(stack);
break;
case 3:palindrome(stack);
break;
case 4: exit(0);
break;
default: printf("\nEnter a Valid Choice");
}
OUTPUT:
----MAIN MENU-----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
-----
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 2
The stack contents are:
------
|2|
------
|1|
(-------- AFTER THE 4 TIMES PUSH OPERATION -------)
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 9
Stack is Overflow
The stack contents are:
------
|1|
|2|
------
|2|
------
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter a element to be pushed: 1
The stack contents are:
------
|1|
------
|2|
------
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 3
Stack contents are Palindrome
(-------- CHECKING FOR PALINDROME OR NOT USING STACK-------)
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
-----
|1|
(AFTER 3 TIMES PUSH)
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 1
Enter an element to be pushed: 3
The stack contents are:
------
|3|
------
|2|
------
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution)
Enter Your Choice: 3
Stack contents are not Palindrome
Program-4
/*****************************************************************************
4. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix
Expression. Program should support for both parenthesized and free parenthesized expressions
with the operators: +, -, *, /, %( Remainder), ^ (Power) and alphanumeric operands.
*****************************************************************************/
#include<stdio.h>
#include<string.h>
#include<conio.h>
{
switch(symbol)
{
case '+' :
case '-': return 2;
case '*':
case '/': return 4;
case '^':
case '$': return 5;
case '(': return 0;
case '#': return -1;
default: return 8;
}
}
{
postfix[j] = s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++] = s[top--];
}
postfix[j] = '\0';
}
void main()
{
char infix[20], postfix[20];
clrscr();
printf("\nEnter a valid infix expression\n");
flushall();
gets(infix);
infix_postfix(infix,postfix);
printf("\nThe infix expression is:\n");
printf ("%s",infix);
printf("\nThe postfix expression is:\n");
printf ("%s",postfix);
getch();
}
OUTPUT:
Enter a valid infix expression
(a+(b-c)*d)
The infix expression is:
(a+(b-c)*d)
The postfix expression is: abc-d*+
Program-5
/*****************************************************************************
5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
b. Solving Tower of Hanoi problem with n disks.
*****************************************************************************/
/*Evaluation of Suffix Expression*/
#include<stdio.h>
#include<ctype.h>
#include<math.h>
float s[25];
int top=-1;
float pop()
{
return(s[top--]);
}
void main()
{
char postfix[25],symbol;
float op1,op2,res;
int i;
printf("Enter the Postfix Expression\n");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
symbol=postfix[i];
if(isdigit(symbol))
push(symbol-'0');
else
{
op2=pop();
op1=pop();
res=operation(symbol,op1,op2);
push(res);
}
}
res=pop();
printf("Result=%.2f",res);
}
OUTPUT:
Enter the Postfix Expression
12+
Result=3.00
#include<stdio.h>
#include<conio.h>
#inlcude<math.h>
void main()
{
int n;
clrscr();
printf("\nEnter the number of discs: \n");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-1);
getch();
}
OUTPUT:
Enter the number of discs:
3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
Total Number of moves are: 7
Program-6
/*****************************************************************************
6.Design, Develop and Implement a menu driven Program in C for the following operations on
Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
*****************************************************************************/
#include <stdio.h>
#include<stdlib.h>
#define MAX 5
int q[MAX],f=0,r=-1,count=0;
int delet()
{
int itemdel;
if (count==0)
return 0;
else
{
itemdel=q[f];
f=(f+1)%MAX;
count=count-1;
return (itemdel);
}
}
void display()
{
int i,j;
if (count==0)
printf("Q empty\n");
else
{
i=f;
for (j=1;j<=count;j++)
{
printf("%d\t",q[i]);
i=(i+1)%MAX;
}
}
}
void main()
{
int ch,item,itemdel;
while (1)
{
printf("\nEnter the choice\n");
printf("1.Insert\t2.Delete\t3.Display\t4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the item\n");
scanf("%d",&item);
insert(item);
break;
case 2: itemdel=delet();
if (itemdel)
printf("The deleted item is %d\n",itemdel);
else
printf("Queue underflow\n");
break;
case 3: display();
break;
case 4: exit(0);
default: pritnf("Enter a valid choice\n");
}
}
getch();
}
OUTPUT:
Enter the Choice
1. Insert 2. Delete 3. Display 4. Exit
1
Enter the item
10
Enter the Choice
1. Insert 2. Delete 3. Display 4. Exit
1
Enter the item
20
(-----After inserting 5 elements-------)
Enter the Choice
1. Insert 2. Delete 3. Display 4. Exit
1
Enter the item
60
Queue Overflow
Program-7
/*****************************************************************************
7. Design, Develop and Implement a menu driven Program in C for the following
operations on Singly Linked List (SLL) of Student Data with the fields: USN,
Name, Branch, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
*****************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct student
{
char usn[10];
char name[30];
char branch[5];
int sem;
char phno[15];
struct student *link; //Self referential pointer.
};
typedef struct student NODE;
printf("\nUSN\tNAME\t\tBRANCH\tSEM\tPh.NO.");
while(cur!=NULL)
{
printf("\n%s\t%s\t\t%s\t%d\t%s", cur->usn, cur->name, cur->branch,
cur->sem, cur->phno);
cur = cur->link;
count++;
}
printf("\nThe no. of nodes in list is: %d",count);
}
return first;
}
else
{
temp=getnode(first);
temp->link=first;
first=temp;
}
return first;
}
{
temp=getnode(first);
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
}
return first;
}
if(first->link==NULL)
{
printf("\nLast(end)entry is deleted");
free(first);
return NULL;
}
prev=NULL;
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
free(cur);//Delete last NODE...
printf("\nLast(end) entry is deleted");
return first;
}
void main()
{
int ch, i, n;
NODE* first;
first=NULL;
clrscr();
printf("\n*----------Student Database-----------*");
while(1)
{
printf("\n 1.Create\t 2.Display\t 3.Insert_front\t 4.Insert_rear\t 5.Delete_front\t
6.Delete_rear\t 7.Stack\t 8.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many student data you want to create: ");
scanf("%d", &n);
for(i=0;i<n;i++)
first=create(first);//Call to Create NODE...
break;
case 2: first=display(first); //Call to Display...
break;
case 3: first=insert_front(first); //Call to Insert...
break;
case 4: first=insert_rear(first);
break;
case 5: first=del_front(first); //Call to delete
break;
case 6: first=del_rear(first);
break;
case 7: first=stack(first);
break;
case 8: exit(0); //Exit...
default:printf("Enter valid choice\n");
}
}
}
OUTPUT:
1. Create 2.Display 3.Insert_front 4.Insert_rear 5.Delete_front 6.Delete_rear 7.Stack 8.Exit
Enter your choice: 1
How many student data you want to create: 2
Enter USN, Name, Branch, Sem, Ph.No
4vv14cs001 raju cs 3 9900099000
Enter USN, Name, Branch, Sem, Ph.No
4vv14is002 arun is 3 9900099111
1. Create 2.Display 3.Insert_front 4.Insert_rear 5.Delete_front 6.Delete_rear 7.Stack 8.Exit
Enter your choice: 2
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
4vv14is002 arun is 3 9900099111
4vv14cs001 raju cs 3 9900099000
The no. of nodes in list is: 2
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO
4vv14cs001 raju cs 3 9900099000
The no. of nodes in list is: 1
Enter your choice: 2
Front (first) node is deleted
No Student Data
1.Insert at Front(First) 2.Delete at front(First) 3.Exit
Enter your choice: 3
1. Create 2.Display 3.Insert_front 4.Insert_rear 5.Delete_front 6.Delete_rear 7.Stack 8.Exit
Enter your choice: 8
Program-8
/*****************************************************************************
8. Design, Develop and Implement a menu driven Program in C for the following
operations on Doubly Linked List (DLL) of Employee Data with the fields: SSN,
Name, Dept, Designation, Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
NODE* getnode()
{
NODE *x;
x=(NODE*)malloc(sizeof(NODE));
x->right=x->left=NULL;
printf("\nEnter SSN, Name, Dept, Designation, Sal, Ph.No\n");
scanf("%d",&x->ssn);
flushall();
gets(x->name);
flushall();
gets(x->dept);
flushall();
gets(x->desig);
scanf("%d",&x->sal);
flushall();
gets(x->phno);
return x;
}
NODE* display(NODE *first)
{
NODE *cur;
int count=0;
if(first==NULL)
printf("\nNo Employee data\n");
else
{
cur=first;
printf("\n----EMPLOYEE DATA----\n");
printf("\nSSN\tNAME\tDEPT\tDESINGATION\tSAL\t\tPh.NO.");
while(cur!=NULL)
{
printf("\n%d\t%s\t%s\t%s\t\t%d\t\t%s", cur->ssn, cur->name, cur->dept,
cur->desig, cur->sal, cur->phno);
cur = cur->right;
count++;
}
printf("\nThe no. of nodes in list is: %d",count);
}
return first;
}
{
temp=getnode();
first=temp;
}
else
{
temp=getnode();
temp->right=first;
first->left=temp;
first=temp;
}
return first;
}
{
printf("No employee data\n");
return NULL;
}
if(first->right==NULL)
{
free(first);
printf("\nFront end entry is deleted");
return NULL;
}
temp=first->right;
temp->left=NULL;
free(first);
printf("\nFront end entry is deleted");
return temp;
}
void main()
{
int ch, i, n;
NODE* first;
first=NULL;
clrscr();
printf("\n*----------Employee Database-----------*");
while(1)
{
printf("\n 1.Create\t 2.Display\t 3.Insert_front\t 4.Insert_rear\t 5.Delete_front\t
6.Delete_rear\t 7.Dequeue\t 8.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many employee data you want to create: ");
scanf("%d", &n);
for(i=0;i<n;i++)
first=create(first);
break;
case 2: first=display(first);
break;
case 3: first=insert_front(first);
break;
case 4: first=insert_end(first);
break;
case 5: first=delete_front(first);
break;
case 6: first=delete_end(first);
break;
case 7: first=dequeue(first);
break;
case 8: exit(0);
default:printf("Enter valid choice\n");
}
}
}
OUTPUT:
1. Create 2.Display 3.Insert_front 4.Insert_rear 5.Delete_front 6.Delete_rear 7.Dequeue 8.Exit
Enter your choice: 1
How many employee data you want to create: 2
Enter SSN NAME DEPT DESIGNATION SAL Ph. No.
1 KUMAR CSE INSTRUCTOR 8000 900099000
Enter SSN, Name, Dept, Designation, Sal, Ph.No
2 RAVI ISE INSTRUCTOR 9000 900099111
Program-9
/*****************************************************************************
9.Design, Develop and Implement a Program in C for the following operationsonSingly
Circular Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) andstore the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
*****************************************************************************/
9a. Program
struct node
{
int cf;
int px,py,pz;
struct node* link;
};
typedef struct node* NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc( sizeof(struct node));
if(x==NULL)
{
printf("Out of memeory");
exit(0);
}
return x;
}
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
int i,n;
int cf,px,py,pz;
printf("Enter the no. of terms in the polynomial");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("Enter term:%d\n",i);
printf("Cf Px, py, pz=");
scanf("%d %d %d %d",&cf,&px,&py,&pz);
head= insert_rear(cf,px,py,pz,head);
}
return head;
}
scanf("%d %d %d",&x,&y,&z);
p=head->link;
while(p!=head)
{
sum+=p->cf*pow(x,p->px)* pow(y,p->py)* pow(z,p->pz);
p=p->link;
}
return sum;
}
void main()
{
NODE head;
float res;
head=getnode();
head->link=head;
printf("Enter the polynomial\n");
head=read_poly(head);
res=evaluate(head);
printf("The given polynomial is\n");
display(head);
printf("The result=%f\n",res);
}
OUTPUT:
Enter the number of terms in the polynomial: 5
Enter term: 1
Cf px py pz = 6 2 2 1
Enter term: 2
Cf px py pz = -4 0 1 5
Enter term: 3
Cf px py pz = 3 3 1 1
Enter term: 4
Cf px py pz = 2 1 5 1
Enter term: 5
Cf px py pz = -2 1 1 3
Enter the value of x, y and z
123
The given polynomial is
+6x^2y^2z^1-4y^1z^5+3x^3y^1z^1+2x^1y^5z^1-2x^1y^1z^3
The result = -1770
9b. Program
/* Program to find the sum of two Polynomial */
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct node
{
int cf;
int px,py,pz;
NODE getnode()
{
NODE x;
x=(NODE)malloc( sizeof(struct node));
if(x==NULL)
{
printf("Out of memeory");
exit(0);
}
return x;
}
scanf("%d %d %d %d",&cf,&px,&py,&pz);
head= insert_rear(cf,px,py,pz,head);
}
return head;
}
px2=p2->px;
py2=p2->py;
pz2=p2->pz;
if( px1==px2 && py1==py2 && pz1==pz2 ) break;
p2=p2->link; //Obtain the next term of poly2
}
return p2;
}
h3=insert_rear(sum,px1,py1,pz1,h3);
p2->cf=-999; //Delete the term of poly2
}
else //If not found, insert term of poly1 to poly 3
h3= insert_rear(cf1,px1,py1,pz1,h3);
p1=p1->link; //Get the next term of polynomial1
}
h3=copy_poly(h2,h3); //Copy remaining terms of poly2 into poly3
return h3; //return total terms in poly3
}
void main()
{
NODE h1,h2,h3;
clrscr();
h1= getnode();
h2=getnode();
h3= getnode();
h1->link=h1; h2->link=h2; h3->link=h3;
printf("Enter the first polynomial\n");
h1=read_poly(h1);
printf("Enter the second polynomial\n");
h2=read_poly(h2);
printf("Poly 1:");
display(h1);
printf("Poly2:");
display(h2);
printf("----------------------------------------------------------------\n");
h3= add_poly(h1,h2,h3);
printf("Poly 3:");
display(h3);
printf("----------------------------------------------------------------\n");
getch();
}
OUTPUT:
Enter the first polynomial
Enter the number of terms in the polynomial: 2
Enter term: 1
Cf px py pz = 5 2 2 0
Enter term: 2
Cf px py pz = 4 0 0 1
Enter the second polynomial
Program-10
/*****************************************************************************
10.Design, Develop and Implement a menu driven Program in C for the following
operations on Binary Search Tree (BST) of Integers
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
*****************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("Insufficient memory");
exit(0);
}
return x;
}
if(root==NULL) return;
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
else
cur=cur->rlink;
}
return NULL;
}
void main()
{
int item,ch,n,i;
NODE root,cur;
root=NULL;
while (1)
{
printf("\nEnter thechoice\n1.Insert\n2.Preorder\n3.Inorder\n4.Postorder\n5.Search
an Element\n6:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number of items\n");
scanf("%d",&n);
printf("Enter the item to be inserted\n");
for(i=0;i<n; i++)
{
scanf("%d",&item);
root=insert(item,root);
}
break;
case 2: if(root==NULL)
{
printf("Tree is empty\n");
break;
}
printf("The given tree in tree form is\n");
display(root,1);
printf("Preorder traversal is \n");
preorder(root);
printf("\n");
break;
case 3: if(root==NULL)
{
printf("Tree is empty\n");
break;
}
printf("Inorder traversal is \n");
inorder(root);
printf("\n");
break;
case 4: if(root==NULL)
{
printf("Tree is empty\n");
break;
}
printf("Postorder traversal is \n");
postorder(root);
printf("\n");
break;
case 5: printf("Enter the item to be searched\n");
scanf("%d",&item);
cur=search(item,root);
if(cur==NULL)
printf("Item not found\n");
else
printf("Item found\n");
break;
case 6: exit(0);
default: printf("Enter valid choice\n");
}
}
}
Output:
Preorder traversal is
6 5 2 2 5 9 8 7 8 15 14 24
Enter the choice
1. Insert
2. Preorder
3. Inorder
4. Postorder
5. Search an element
6. Exit
3
Inorder traversal is
2 2 5 5 6 7 8 8 9 14 15 24
Enter the choice
1. Insert
2. Preorder
3. Inorder
4. Postorder
5. Search an element
6. Exit
4
Postorder traversal is
2 2 5 5 7 8 8 14 24 15 9 6
Enter the choice
1. Insert
2. Preorder
3. Inorder
4. Postorder
5. Search an element
6. Exit
5
Enter the item to be searched
12
Item not found
Enter the choice
1. Insert
2. Preorder
3. Inorder
4. Postorder
5. Search an element
6. Exit
Enter the item to be searched
6
Item found
Enter the choice
1. Insert
2. Preorder
3. Inorder
4. Postorder
5. Search an element
6. Exit
6
Program-11
/*****************************************************************************
11Design, Develop and Implement a Program in C for the following operations on
Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using
DFS/BFS method
*****************************************************************************/
/* Traversal using BFS method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10][10], source, i,j;
clrscr();
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}
for(source=0; source<n; source++)
{
bfs(a,n,source);
}
getch();
}
OUTPUT:
Enter the number of nodes: 4
Enter the adjacency matrix:
0110
0011
0001
0000
The nodes visited from 0: 0 1 2 3
The nodes visited from 1: 1 2 3
The nodes visited from 2: 2 3
The nodes visited from 3: 3
Void dfs(int u)
{
int v;
s[u]=1;
printf(“%d”,u);
for(v=0; v<n; v++)
{
if(a[u][v]==1 && s[v]==0)
dfs(v);
}
}
Void main()
{
int i, source;
printf(“Enter the number of nodes in the graph:”);
scanf(“%d”,&n);
printf(Enter the adjacency matrix:”);
read_AM(a,n);
for(source=0; source<n; source++)
{
for(i=0; i<n; i++)
s[i]=0;
printf(“The nodes reachable from %d”, source);
dfs(source);
}
}
OUTPUT:
Program-12
/*****************************************************************************
12. Given a File of N employee records with a set K of Keys(4-digit) which uniquelydetermine the
records in file F. Assume that file F is maintained in memory by aHash Table(HT) of m memory
locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and
addresses in L are Integers. Designand develop a Program in C that uses Hash function H: K L as
H(K)=K modm (remainder method), and implement hashing technique to map a given key K
to the address space L. Resolve the collision (if any) using linear probing.
*****************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HASH_SIZE 100
int H(int k)
{
return k% HAHS_SIZE;
}
{
a[index].id=id; //insert employee id
strcpy(a[index].name,name); //insert employee name
break;
}
}
if(i==HASH_SIZE) printf("Hash table full\n"); // NO empty slot
}
void main()
{
EMPLOYEE a[HASH_SIZE];
char name[20];
int key,id,choice,flag;
initialize_hash_table(a); //Create intial hash table
for(;;)
{
printf("1:Insert 2:Display 4:Exit\n");
printf("Enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter emp id:");
scanf("%d",&id);
printf("Enter the name:");
scanf("%s",name);
insert_hash_table(id,name,a);
break;
case 2: printf("Contents of hash table\n");
display_hash_table(a,HASH_SIZE);
printf("\n");
break;
case 3: exit(0);
deafult: printf(Enter valid choice\n”);
}
}
}
OUTPUT:
1: Insert 2: Display 3: Exit
Enter the choice: 1
Enter the empid: 1234
Enter the name: Anu
1: Insert 2: Display 3: Exit
Enter the choice: 1
Enter the empid: 1236
Enter the name: Anil
1: Insert 2: Display 3: Exit
Enter the choice: 1
Enter the empid: 1334
Enter the name: Charan
1: Insert 2: Display 3: Exit
Enter the choice: 2
Contents of hash table
H_value Emp_id Emp_name
34 1234 Anu
35 1334 Charan
36 1236 Anil
1: Insert 2: Display 3: Exit
Enter the choice: 3
Basically, the key difference is the memory area that is being accessed. When dealing with the
structure that resides the main memory of the computer system, this is referred to as storage
structure. When dealing with an auxiliary structure, we refer to it as file structures.
7) What is LIFO?
LIFO is short for Last In First Out, and refers to how data is accessed, stored and retrieved.
Using this scheme, data that was stored last , should be the one to be extracted first. This also
means that in order to gain access to the first data, all the other data that was stored before this
first data must first be retrieved and extracted.
8 ) What is a queue?
A queue is a data structure that can simulates a list or stream of data. In this structure, new
elements are inserted at one end and existing elements are removed from the other end.
10) Which data structures is applied when dealing with a recursive function?
Recursion, which is basically a function that calls itself based on a terminating condition, makes
use of the stack. Using LIFO, a call to a recursive function saves the return address so that it
knows how to return to the calling function after the call terminates.
Pushing and popping applies to the way data is stored and retrieved in a stack. A push denotes
data being added to it, meaning data is being “pushed” into the stack. On the other hand, a pop
denotes data retrieval, and in particular refers to the topmost data being accessed.
25) What is the difference between the HEAP and the STACK?
(Solution: HEAP is used to store dynamically allocated memory (malloc). STACK stores static
data (int, const).)
27) How do you insert a record between two nodes in double-linked list?
(Solution: Previous R; Data R; Next R; To insert a record (B) between two others (A and C):
Previous.B = A; Next.B = C; Next.A = B; Previous.C = B;)
28) In which data structure, elements can be added or removed at either end, but not in
the middle?
(Solution: queue)
29) Which one is faster? A binary search of an orderd set of elements in an array or a
sequential search of the elements.
(Solution: binary search)
(Solution: A binary tree is balanced if the depth of two subtrees of every node never differ by
more than one)
31) Which data structure is needed to convert infix notations to post fix notations?
(Solution: stack)
32) What is data structure or how would you define data structure?
(Solution: In programming the term data structure refers to a scheme for organizing related
piece of information. Data Structure = Organized Data + Allowed Operations.)
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int t1,t2,n,i;
longdouble t[20];
scanf("%d%d%d",&t1,&t2,&n);
if(n>20){return 0;}
t[0]=t1;t[1]=t2;
for(i=0;i<=n-1;i++)
{
t[i+2]=t[i]+t[i+1]*t[i+1];
}
printf("%Lf",t[n-1]);
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 100
char s1[MAX]="amobile",*s=s1;
char s2[MAX]="uto",*t=s2;
void main()
{
int i=1;
strnins(s1,s1,i);
printf("The concat string is:%s",s1);
getch();
}
#include<stdio.h>
#include<conio.h>
#define MAX 101
typedefstruct
{
int row,col,val;
}TERM;
void main()
{
int m,n,item,v;
TERM a[MAX];
clrscr();
printf("Enter the number of rows");
scanf("%d",&m);
#include<stdio.h>
#include<conio.h>
void main()
{
int *p;
int n,i;
clrscr();
printf("Enter the number of elements\n");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
if(p==NULL)
{
printf("Enough memory not available\n");
exit(0);
}
printf("Enter array elements\n");
for(i=0; i<n; i++)
{
scanf("%d",p+i);
}
printf("Array elements are\n");
for(i=0; i<n; i++)
{
printf("%d\t",*(p+i));
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int *p;
int n,i;
clrscr();
printf("Enter the number of elements\n");
scanf("%d",&n);
p=(int *)calloc(n,sizeof(int));
if(p==NULL)
{
printf("Enough memory not available\n");
exit(0);
}
printf("Enter array elements\n");
for(i=0; i<n; i++)
{
scanf("%d",p+i);
}
printf("Array elements are\n");
for(i=0; i<n; i++)
{
printf("%d\t",*(p+i));
}
getch();
}
//Program on UNIONS
#include<stdio.h>
void main()
{
typedefunion
{
int marks;
char grade;
}student;
student x;
clrscr();
x.marks=100;
x.grade='A';
printf("marks=%d\n",x.marks);
printf("Grade=%c\n",x.grade);
getch();
}
//Insert operation
#include <stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
n = n + 1;
while( j >= k) {
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
#include<stdio.h>
main(){
int LA[]={1,3,5,7,8};
int k =3, n =5;
int i, j;
j = k;
n = n -1;
//Search operation
#include<stdio.h>
main(){
int LA[]={1,3,5,7,8};
int item =5, n =5;
int i =0, j =0;
j = j +1;
}
#include<stdio.h>
main()
{
int LA[]={1,3,5,7,8};
int k =3, n =5, item =10;
int i, j;
LA[k-1]= item;