Dsc Lab Manual Final
Dsc Lab Manual Final
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash 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. Develop a Program in C that uses Hash function H:
K →L as H(K)=K mod m (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.
Laboratory Outcomes: The student should be able to:
Course Outcomes
1)
read(calendar, size);
display(calendar, size);// Free the dynamically allocated memory
freeMemory(calendar, size);// Free the memory allocated for the calendar array
free(calendar);
return 0;
}
OUTPUT
Enter the number of days in the week: 7Enter details for Day 1:
Enter the day name: Sunday
Enter the date: 1
Enter the activity for the day: Learning Enter details for Day 2:
Enter the day name: Monday
Enter the date: 2
Enter the activity for the day: CodingEnter details for Day 3:
Enter the day name: Tuesday
Enter the date: 3
Enter the activity for the day: TestingEnter details for Day 4:
Enter the day name: Wednesday
Enter the date: 4
Enter the activity for the day: DebuggingEnter details for Day 5:
Enter the day name: Thrusday
Enter the date: 5
Enter the activity for the day: PublishingEnter details for Day 6:
Enter the day name: Friday
Enter the date: 6
Enter the activity for the day: MarketingEnter details for Day 7:
Enter the day name: Saturday
2) Design, Develop and Implement a Program in C for the following operations on Strings
1)Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
2)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
#include<sdio.h>
void main()
{
char STR[100],PAT[100],REP[100],ans[100];
int i,j,c,m,k,flag=0;
printf("\nEnter the MAIN string: \n");
gets(STR);
printf("\nEnter a PATTERN string: \n");
gets(PAT);
printf("\nEnter a REPLACE string: \n");
gets(REP);
i = m = c = j = 0;
while ( STR[c] != '\0')
{
// Checking for Match
if (STR[m] == PAT[i])
{
i++;
m++;
if ( PAT[i] == '\0')
{
//copy replace string in ans string
}
Output
linux:~/dslab # gedit string.c
linux:~/dslab # cc string.c
linux:~/dslab # ./a.out
Enter the MAIN string: good
morning
Enter a PATTERN string:
morning
Enter a REPLACE string:
evening
The RESULTANT string is: good evening
linux:~/dslab # ./a.out Enter
the MAIN string: hi SJCIT
Enter a PATTERN string: NICE
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).
1. Push an Element on to Stack
2. Pop an Element from Stack
3. Demonstrate how Stack can be used to check Palindrome
4. Demonstrate Overflow and Underflow situations on Stack
5. Display the status of Stack
6. Exit
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top = -1;
void push();
void pop();
void display();
void pali();
int main()
{
int choice;
while(choice)
{
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
pali();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}
return 0;
}
void push() //Inserting element into the stack
{
int item,n;
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop() //deleting an element from the stack
{
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void pali()
{
int digit,j,k,len=top+1,flag=0,ind=0 ,length = 0;
int num[len],rev[len],i=0;
while(top!=-1)
{
digit= stack[top];
num[i]=digit;
top--;
i++;
}
for(j=0;j<len;j++)
{
printf("Numbers= %d\n",num[j]);
}
printf("reverse operation : \n");
for(k=len-1;k>=0;k--)
{
rev[k]=num[ind];
ind++;
}
printf("reverse array : ");
for(k=0;k<len;k++)
{
printf("%d\n",rev[k]);
}
printf("check for palindrome :\n");
for(i=0;i<len;i++)
{
if(num[i]==rev[i])
{
length = length+1;
}
}
if(length==len)
{
printf("It is palindrome number\n");
}
else
{
printf("It is not a palindrome number\n");
}
top = len-1;
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}
OUTPUT
linux:~/dslab # gedit stack.c
linux:~/dslab # cc stack.c
linux:~/dslab # ./a.out
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Enter the element to be inserted: 2
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
#include <ctype.h>
#include <stdio.h>
char s[SIZE];
s[++top] = elem;
return (s[top--]);
switch (elem)
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 3;
case '^':
return 4;
int i = 0, k = 0;
scanf("%s", infx);
push('#');
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
pofx[k++] = pop();
OUTPUT
linux:~/dslab # cc intopost.c
linux:~/dslab # ./a.out
(a+b)*c/d^5%1
5) Design, Develop and Implement a Program in C for the following Stack Applications
1.)Evaluation of Suffix expression with single-digit operands and operators:+, -, *, /, %, ^
2.)Solving Tower of Hanoi problem with n disks
/ Evaluation of Suffix Expression
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top= -1;
/*fUNCTION PROTOYPE */
void pushstack(int tmp);
void calculator(char c);
void main()
{
int i;
printf("Insert a postfix notation :: ");
scanf("%s",post);
for(i=0;i<strlen(post);i++)
{
if(post[i]>='0' && post[i]<='9')
{
pushstack(i);
}
if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^')
{
calculator(post[i]);
}
}
printf("\n\nResult :: %d",stack[top]);
}
void pushstack(int tmp)
{
top++;
stack[top]=(int)(post[tmp]-48);
}
void calculator(char c)
{
int a,b,ans;
a=stack[top];
stack[top]='\0';
top--;
b=stack[top];
stack[top]='\0';
top--;
switch(c)
{
case '+':
ans=b+a;
break;
case '-':
ans=b-a;
break;
case '*':
ans=b*a;
break;
case '/':
ans=b/a;
break;
case '^':
ans=pow(b,a);
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
OUTPUT:
Enter a valid postfix expression:
623+-382/+*2$3+
Result = 52
OUTPUT
Insert a postfix notation :: 22^32*+ Result :: 10
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are : Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
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)
1. Insert an Element on to Circular QUEUE
2. Delete an Element from Circular QUEUE
3. Demonstrate Overflow and Underflow situations on Circular QUEUE
4. Display the status of Circular QUEUE
5. Exit
#include<stdio.h>
#include<stdlib.h>
#define max 10
int q[10],front=0,rear= -1;
void main()
{
int ch;
/*FUNCTION PROTOTYPE */
void insert();
void delet();
void display();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
exit(1);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else if(front==max-1)
{
a=q[front];
front=0;
}
else
a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
exit(1);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
OUTPUT
linux:~/dslab #gedit cirQ.c
linux:~/dslab #gcc cirQ.c
linux:~/dslab # ./a.out
Circular Queue
operations 1.insert
2.delete
3.display
4.exit
Enter your choice:1
Enter element to be insert:10
Enter your choice:1
Enter element to be insert:20
Enter your choice:1
Enter element to be insert:30
Enter your
choice:3 10 20 30
rear is at 30
front is at 10
Enter your choice:2
Deleted element is:10
Enter your
choice:3
20 30
rear is at 30
front is at 20
Enter your choice:4
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
1. Create a SLL of N Students Data by using front insertion.
2. Display the status of SLL and count the number of nodes in it
3. Perform Insertion / Deletion at End of SLL
4. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
5. Exit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node
{
int sem,phno;
char name[20],branch[10],usn[20];
struct node *next;
} *first=NULL,*last=NULL,*temp=NULL,*temp1;
void create()
{
int sem,phno;
char name[20],branch[10],usn[20];
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter usn,name, branch, sem, phno of student : ");
scanf("%s %s %s %d %d", usn, name,branch, &sem,&phno);
strcpy(temp->usn,usn);
strcpy(temp->name,name);
strcpy(temp->branch,branch);
temp->sem = sem;
temp->phno = phno;
temp->next=NULL;
count++;
}
void insert_atfirst()
{
if (first == NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
temp->next = first;
first = temp;
}
}
void insert_atlast()
{
if(first==NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
last->next = temp;
last = temp;
}
}
void display()
{
temp1=first;
if(temp1 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp1!= NULL)
{
printf("%s %s %s %d %d\n", temp1->usn, temp1->name,temp1->branch,
temp1->sem,temp1->phno );
temp1 = temp1->next;
}
printf(" No of students = %d ", count);
}
int deleteend()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
}
else
{
while(temp->next!=last)
temp=temp->next;
printf("%s %s %s %d %d\n", last->usn, last->name,last->branch, last->sem,
last->phno );
free(last);
temp->next=NULL;
last=temp;
}
count--;
return 0;
}
int deletefront()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
return 0;
}
else
{
first=temp->next;
printf("%s %s %s %d %d", temp->usn, temp->name,temp->branch,temp->sem,
temp->phno );
free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
first=NULL;
temp = temp1 = NULL;
printf("-----------------MENU----------------------\n");
printf("\n 1- create a SLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("-------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of students : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insert_atfirst();
break;
case 2:
display();
break;
case 3:
insert_atlast();
break;
case 4:
deleteend();
break;
case 5:
insert_atfirst();
break;
case 6:
deletefront();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
}
}
}
OUTPUT
linux:~/dslab #gedit slink.c
linux:~/dslab #gcc slink.c
linux:~/dslab # ./a.out
–
---------------MENU----------------
------
1 – create a SLL of n emp
2 -Display from beginning
3 -Insert at end
4 -delete at end
5 -Insert at beg
6 -delete at beg
7 -exit
------------------------------------------------
Enter choice : 1
Enter no of students : 2
Enter usn,name, branch, sem, phno of student : 007 vijay CSE 3 121
Enter usn,name, branch, sem, phno of student : 100 yashas CSE 3 911
Enter choice : 2
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
1. Create a DLL of N Employees Data by using end insertion.
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstrate how this DLL can be used as Double Ended Queue
6. Exit
#include<string.h>
int count=0;
struct node
{
struct node *prev;
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void create()
{
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
temp =(struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter ssn,name,department, designation, salary and phno of employee : ");
scanf("%d %s %s %s %f %d", &ssn, name,dept,desg,&sal, &phno);
temp->ssn = ssn;
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->sal = sal;
temp->phno = phno;
count++;
}
void insertbeg()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insertend()
{
if(h==NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void displaybeg()
{
temp2 =h;
if(temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp2!= NULL)
{
printf("%d %s %s %s %f %d\n", temp2->ssn, temp2->name,temp2->dept,
temp2->desg,temp2->sal, temp2->phno );
temp2 = temp2->next;
}
printf(" No of employees = %d ", count);
}
int deleteend()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
return 0;
}
else
{
temp2=temp1->prev;
temp2->next=NULL;
printf("%d %s %s %s %f %d\n", temp1->ssn, temp1->name,temp1->dept,
temp1->desg,temp1->sal, temp1->phno );
free(temp1);
}
count--;
return 0;
}
int deletebeg()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
}
else
{
h=h->next;
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept,
temp->desg,temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
h=NULL;
temp = temp1 = NULL;
printf("-----------------MENU--------------------\n");
printf("\n 1 - create a DLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
case 6:
deletebeg();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
}
}
}
OUTPUT
linux:~/dslab #gedit dlink.c
linux:~/dslab #gcc dlink.c
linux:~/dslab # ./a.out
MENU--------------------
–Create a DLL of n emp
- Display from beginning
- Insert at end
- Delete at end
- Insert at beg
- Delete at beg
- exit
------------------------------------------
Enter choice : 1
Enter no of employees : 2
Enter ssn,name,department, designation, salary and phno of employee :1
RAJ SALES MANAGER 15000 911
9) Design, Develop and Implement a Program in C for the following operations on Singly
Circular Linked List (SCLL) with header nodes
1)Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
2)Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result
in POLYSUM(x,y,z)
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<math.h>
typedef struct node
{
int expo,coef;
struct node *next;
}node;
/*FUNCTION PROTOTYPE*/
node * insert(node *,int,int);
node * create();
node * add(node *p1,node *p2);
int eval(node *p1);
void display(node *head);
node *insert(node*head,int expo1,int coef1)
{
node *p,*q;
p=(node *)malloc(sizeof(node));
p->expo=expo1;
p->coef=coef1;
p->next=NULL;
if(head==NULL)
{
head=p;
head->next=head;
return(head);
}
if(expo1>head->expo)
{
p->next=head->next;
head->next=p;
head=p;
return(head);
}
if(expo1==head->expo)
{
head->coef=head->coef+coef1;
return(head);
}
q=head;
while(q->next!=head&&expo1>=q->next->expo)
q=q->next;
if(p->expo==q->expo)
q->coef=q->coef+coef1;
else
{
p->next=q->next;
q->next=p;
}
return(head);
}
node *create()
{
int n,i,expo1,coef1;
node *head=NULL;
printf("\n\nEnter no of terms of polynomial==>");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter coef & expo==>");
scanf("%d%d",&coef1,&expo1);
head=insert(head,expo1,coef1);
}
return(head);
}
node *add(node *p1,node *p2)
{
node *p;
node *head=NULL;
printf("\n\n\nAddition of polynomial==>");
p=p1->next;
do
{
head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p1->next);
p=p2->next;
do
{
head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p2->next);
return(head);
}
int eval(node *head)
{
node *p;
int x,ans=0;
printf("\n\nEnter the value of x=");
scanf("%d",&x);
p=head->next;
do
{
ans=ans+p->coef*pow(x,p->expo);
p=p->next;
}while(p!=head->next);
return(ans);
}
void display(node *head)
{
node *p,*q;
int n=0;
q=head->next;
p=head->next;
do
{
n++;
q=q->next;
}while(q!=head->next);
printf("\n\n\tThe polynomial is==>");
do
{
if(n-1)
{
printf("%dx^(%d) + ",p->coef,p->expo);
p=p->next;
}
else
{
printf(" %dx^(%d)",p->coef,p->expo);
p=p->next;
}
n--;
} while(p!=head->next);
}
void main()
{
int a,x,ch;
node *p1,*p2,*p3;
p1=p2=p3=NULL;
while(1)
{
printf("\n\t----------------<< MENU >>---------------");
printf("\n\tPolynomial Operations :");
printf(" 1.Add");
printf("\n\t\t\t\t2.Evaluate");
printf("\n\t\t\t\t3.Exit");
printf("\n\t------------------------------------------- ");
printf("\n\n\n\tEnter your choice==>");
scanf("%d",&ch);
switch(ch)
{
case 1 :
p1=create();
display(p1);
p2=create();
display(p2);
p3=add(p1,p2);
display(p3);
break;
case 2 :
p1=create();
display(p1);
a=eval(p1);
printf("\n\nValue of polynomial=%d",a);
break;
case 3 :
exit(0);
break;
default :
printf("\n\n\t invalid choice");
break;
}
}
}
OUTPUT
linux:~/dslab #gedit poly.c
linux:~/dslab #gcc poly.c
linux:~/dslab # ./a.out
-----------------<< MENU >>---------------
Polynomial Operations :
1.Add 2.Evaluate
3.Exit
---------------------------------------------------
Enter your choice==>1
Enter no of terms of polynomial==>
3
Enter coef & expo==>
43
Enter coef & expo==>
22
Enter coef & expo==> 5 1
The polynomial is==>5x^(1) + 2x^(2) +
4x^(3)
10) Design, Develop and Implement a menu driven Program in C for the following
operations on Binary Search Tree(BST) of Integers
1.Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
2.Traverse the BST in Inorder, Preorder and Post Order
3.Search the BST for a given element (KEY) and report the appropriate message
4.Exit
Program
#include <stdio.h>
#include <stdlib.h>
int flag=0;
typedef struct BST
{
int data;
struct BST *lchild,*rchild;
} node;
/*FUNCTION PROTOTYPE*/
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
void main()
{
int choice;
int ans =1;
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
scanf("%d", &key);
tmp = search(root, key, &parent);
if(flag==1)
{
printf("\nParent of node %d is %d", tmp->data, parent->data);
}
else
{
printf("\n The %d Element is not Present",key);
}
flag=0;
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display :");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
}
}
}
/*This function is for searching the node from binary Search Tree*/
node *search(node *root, int key, node **parent)
{
node *temp;
temp = root;
while (temp != NULL)
{
if (temp->data == key)
{
printf("\nThe %d Element is Present", temp->data);
flag=1;
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/*This function displays the tree in inorder fashion */
void inorder(node *temp)
{
if (temp != NULL)
{
inorder(temp->lchild);
printf("%d\t", temp->data);
inorder(temp->rchild);
}
}
/*This function displays the tree in preorder fashion */
void preorder(node *temp)
{
if (temp != NULL)
{
printf("%d\t", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*This function displays the tree in postorder fashion */
void postorder(node *temp)
{
if (temp != NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d\t", temp->data);
}
}
OUTPUT
linux:~/dslab #gedit bst.c
inux:~/dslab #gcc bst.c
linux:~/dslab # ./a.out
Program For Binary Search Tree
1.Create
2.Search
3.Recursive
Traversals 4.Exit
Enter your choice :1
Enter The Element 15
Want To enter More Elements?(1/0)1
Enter The Element 25
Want To enter More Elements?(1/0)1
Enter The Element 35
Want To enter More Elements?(1/0)1
Enter The Element 45
Want To enter More Elements?(1/0)1
Enter The Element 5
Want To enter More Elements?(1/0)1
Enter The Element 7
Want To enter More Elements?(1/0)0
Enter your choice :2
Enter Element to be searched :7
The 7 Element is Present
Parent of node 7 is 5
1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :2
Enter Element to be searched :88
The 88 Element is not Present
Enter your choice :3
The Inorder display : 5 7 15 25 35 45
The Preorder display : 15 5 7 25 35 45
The Postorder display : 7 5 45 35 25 15
Enter your choice :4
11) Design, Develop and Implement a Program in C for the following operations on
Graph(G) of Cities
1. Create a Graph of N cities using Adjacency Matrix.
2. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method
#include <stdio.h>
#include <stdlib.h>
void bfs(int v)
for(i=1;i<=n;i++)
q[++r]=i;
if(f<=r)
visited[q[f]]=1;
bfs(q[f++]);
void dfs(int v)
int i;
reach[v]=1;
for(i=1;i<=n;i++)
printf("\n %d->%d",v,i);
count++;
dfs(i);
void main()
int v, choice;
scanf("%d",&n);
for(i=1;i<=n;i++)
q[i]=0;
visited[i]=0;
for(i=1;i<=n-1;i++)
reach[i]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&v);
bfs(v);
if((v<1)||(v>n))
else
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
break;
case 2:
dfs(1);
if(count==n-1)
else
break;
case 3:
exit(0);
OUTPUT
linux:~/dslab # ./a.out
form: 0 1 0 1 0
10101
01010
10100
01000
1.BFS
2.DFS
3.Exit
1->2
2->3
3->4
2->5
Graph is connected
vertices:5
form: 0 1 0 1 0
10100
01010
10100
00000
1.BFS
2.DFS
3.Exit
1->2
2->3
3->4
form: 0 1 1 0 0
00010
00000
00100
00100
1.BFS
2.DFS
3.Exit
1: 2 3 4
form: 0 1 1 0 0
00010
00000
00100
00100
1.BFS
2.DFS
3.Exit
possible
#include <stdio.h>
#include <stdlib.h>
/*FUNCTION PROTOTYPE */
int create(int);
void main()
int a[MAX],num,key,i;
int ans=1;
for (i=0;i<MAX;i++)
a[i] = -1;
do
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
scanf("%d",&ans);
}while(ans);
display(a);
int key;
key=num%100;
return key;
flag=0;
if(a[key]== -1)
a[key] = num;
else
printf("\nCollision Detected...!!!\n");
i=0;
while(i<MAX)
if (a[i]!=-1)
count++;
i++;
if(count == MAX)
display(a);
exit(1);
if(a[i] == -1)
a[i] = num;
flag =1;
break;
//for(i=0;i<key;i++)
i=0;
if(a[i] == -1)
a[i] = num;
flag=1;
break;
i++;
int i,choice;
scanf("%d",&choice);
if(choice==1)
else
if(a[i]!=-1)
continue;
OUTPUT
linux:~/dslab # ./a.out
Collision Detected...!!!
0 1.Display ALL
2.Filtered
Display 2
0 1398
34 1234
48 2548
56 3256
981298
991299
only one end called as “Top” of the Stack. It is also called as LIFO list.(Last In FirstOut)
6) What is Stack Underflow?
Ans) Is Stack is empty and POP operation is performed it is not possible to delete the items.This
situation is called Stack Underflow.
7) What is Stack Overflow?
Ans) If Stack is full and PUSH operation is performed it is not possible to insert or Push thenew
items into the stack. This situation is called Stack Overflow.
8) What are the Applications of Stack?
Ans) i) Stacks are used to convert Infix expression into Postfix.ii) Stacks are used to Evaluate
Postfix Expression.iii) Stacks are used in recursion etc.
9) What is the use of Postfix expressions?
Ans) Postfix Expressions are easy to evaluate as postfix expressions does not make
use ofoperator precedence not does it require the use of parenthesis.
QUEUES: (FIFO DATA STRUCTURE)
10) What is a Queue?
Ans) It is an ordered collection of items into which items can be inserted from one end calledas
REAR end and items are deleted from other end called as FRONT end of the Queue. Itis also
called as FIRST IN FIRST OUT (FIFO) LIST).
11) What are the applications of Queues?
Ans) i) Queues are used in Breadth First Traversal of a Tree.ii) Queues are used in
implementation of Scheduling algorithms of Operating Systems.
12) What is a Circular Queue?
Ans) In Circular Queue, the first position of the array is kept behind the last position of thearray.
13) Differentiate Linear Queue and Circular Queue?
Ans) In Linear Queue once the queue is full and the deletion is performed, even if first positionis
free(vacant) it is not possible to insert the item in that position whereas in CircularQueue it is
possible since the first position is kept behind the last position.
14) What is Dequeue? (Double Ended Queue)
Ans) In Double Ended Queue insertion and deletion are possible from both the ends.
TREES:
Data Structures: The logical or mathematical model of a particular organization of data is called data
structures. Data structures is the study of logical relationship existing between individual data elements,
the way the data is organized in the memory and the efficient way of storing, accessing and manipulating
the data elements. Choice of a particular data model depends on two considerations: it must be rich
enough in structure to mirror the actual relationships of the data in the real world. On the other hand, the
structure should be simple enough that one can effectively process the data when necessary.
Data Structures can be classified as: Primitive data structures Non-Primitive data structures. Primitive
data structures are the basic data structures that can be directly manipulated/operated by machine
instructions. Some of these are character, integer, real, pointers etc. Non-primitive data structures are
derived from primitive data structures, they cannot be directly manipulated/operated by machine
instructions, and these are group of homogeneous or heterogeneous data items. Some of these are Arrays,
stacks, queues, trees, graphs etc.
The following operations play major role in the processing of data. i) Traversing. ii) Searching. iii)
Inserting. iv) Deleting. v) Sorting. vi) Merging STACKS: A stack is an ordered collection of items into
which new items may be inserted and from which items may be deleted at the same end, called the TOP
of the stack. A stack is a non-primitive linear data structure. As all the insertion and deletion are done
from the same end, the first element inserted into the stack is the last element deleted from the stack and
the last element inserted into the stack is the first element to be deleted. Therefore, the stack is called
Last-In First-Out (LIFO) data structure.
QUEUES: A queue is a non-primitive linear data structure. Where the operation on the queue is based on
First-In-FirstOut FIFO process — the first element in the queue will be the first one out. This is
equivalent to the requirement that whenever an element is added, all elements that were added before
have to be removed before the new element can be removed. For inserting elements into the queue are
done from the rear end and deletion is done from the front end, we use external pointers called as rear and
front to keep track of the status of the queue. During insertion, Queue Overflow condition has to be
checked. Likewise during deletion, Queue Underflow condition is checked.
be stored in a list. The second field next/link contains the address of the next node. Since next field
contains the address, It is of type pointer.Here the nodes in the list are logically adjacent to each other.
Nodes that are physically adjacent need not be logically adjacent in the list. The entire linked list is
accessed from an external pointer FIRST that points to (contains the address of) the first node in the list.
(By an “external” pointer, we mean, one that is not included within a node. Rather its value can be
accessed directly by referencing a variable). The list containing 4 items/data 10, 20, 30 and 40 is shown
below. INFO NEXT NODE FIRST 10 INFO NEXT 20 INFO NEXT 30 INFO NEXT TTT 40 INFO
NEXT TT NODE1 NODE2 NODE3 NODE4
TREES: Definition: A data structure which is accessed beginning at the root node. Each node is either a
leaf or an internal node. An internal node has one or more child nodes and is called the parent of its child
nodes. All children of the same node are siblings. Contrary to a physical tree, the root is usually depicted
at the top of the structure, and the leaves are depicted at the bottom. A tree can also be defined as a
connected, acyclic di-graph.