SlideShare a Scribd company logo
DS LAB PROGRAMS(18CSL38)
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>
#include<stdlib.h>
int a[6], n, elem, i, pos;
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("%dt", 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 del()
{
printf("nEnter the position of the element to be deleted: ");
scanf("%d", &pos);
elem = a[pos];
for(i=pos; i<n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
printf("nThe deleted element is = %d", elem);
}
int main()
{
int ch;
while(ch!=5)
{
printf("n 1.Createt 2.Displayt 3.Insertt 4.Deletet 5.Exitn");
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;
}
}
return 0;
}
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>
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag=0;
void stringmatch()
{
i = m = c = j = 0;
while(str[c] != '0')
{
if(str[m] == pat[i])
{
i++; m++;
if(pat[i] == '0')
{
flag = 1;
for(k = 0; rep[k] != '0'; k++, j++)
ans[j] = rep[k];
i = 0;
c = m;
}
}
else
{
ans[j] = str[c];
j++;
c++;
m=c;
}
}
ans[j] = '0';
}
void main()
{
printf("Enter a main string n");
gets(str);
printf("Enter a pattern string n");
gets(pat);
printf("Enter a replace string n");
gets(rep);
stringmatch();
if(flag == 1)
printf("The resultant string isn %s" , ans);
else
printf("Pattern string NOT foundn");
}
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<stdlib.h>
#define MAX 5
int stack[MAX], item, ch, top=-1, status=0;
void push(int stack[],int item)
{
if(top==(MAX-1))
printf("nOverflown");
else
stack[++top]=item;
status++;
}
int pop()
{
if(top==-1)
printf("nUnderflown");
else
return stack[top--];
status--;
}
void palindrome(int stack[])
{
int temp, count=0;
temp=status;
for(int i=0;i<temp;i++)
{
if(stack[i]==pop())
count++;
}
if(count==temp)
printf("Palindromen");
else
printf("not palindromen");
}
void display(int stack[])
{
if(top==-1)
printf("nstack is emptyn");
else
for(int i=top;i>=0;i--)
{
printf("|%d|n",stack[i]);
}
}
int main()
{
int ch;
while(ch<6)
{
printf("n 1.pusht 2.popt 3.palindromet 4.displayt 5.exitn");
printf("Enter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the item: ");
scanf("%d",&item);
push(stack,item);
break;
case 2: printf("popped value = %d",pop());
break;
case 3: palindrome(stack);
break;
case 4: display(stack);
break;
case 5: exit(0);
default: printf("Invalid Choicen");
break;
}
}
return 0;
}
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 <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 == '/' || x=='%')
return 2;
if(x=='^')
return 3;
return 0;
}
void main()
{
char exp[20];
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());
}
}
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>
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 main()
{
char postfix[20], *p;
int n1,n2,n3,num;
printf("Enter the postfix expression: ");
scanf("%s",postfix);
p = postfix;
while(*p != '0')
{
if(isdigit(*p))
{
num=(*p-48);
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*p)
{
case '+': n3 = n2 + n1;
break;
case '-': n3 = n2 - n1;
break;
case '*': n3 = n2 * n1;
break;
case '/': n3 = n2 / n1;
break;
case '^': n3 = pow(n2,n1);
break;
case '%': n3 = n2%n1;
break;
}
push(n3);
}
p++;
}
printf("nThe result = %d",pop());
}
/* Tower of Hanoi */
#include<stdio.h>
#include<math.h>
void tower(int n, char S, char T, char D)
{
if(n==0)
return;
tower(n-1,S,D,T);
printf("n Move disc %d from %c to %c",n,S,D);
tower(n-1,T,S,D);
}
void main()
{
int n;
printf("Enter the number of Discs: ");
scanf("%d",&n);
tower(n,'A','B','C');
printf("n Total number of Moves=%d",(int)pow(2,n)-1);
}
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;
void insert (int item)
{
if (count==MAX)
printf("Queue Overflown");
else
{
r=(r+1)%MAX;
q[r]=item;
count=count+1;
}
}
int delete()
{
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 emptyn");
else
{
i=f;
for (j=1;j<=count;j++)
{
printf("%dt",q[i]);
i=(i+1)%MAX;
}
}
}
void main()
{
int ch,item,itemdel;
while (1)
{
printf("nEnter the choicen");
printf("1.Insertt2.Deletet3.Displayt4.Exitn");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the itemn");
scanf("%d",&item);
insert(item);
break;
case 2: itemdel=delete();
if (itemdel)
printf("The deleted item is %dn",itemdel);
else
printf("Queue underflown");
break;
case 3: display();
break;
case 4: exit(0);
default: pritnf("Enter a valid choicen");
}
}
}
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<stdlib.h>
typedef struct student
{
char usn[10];
char name[30];
char branch[5];
int sem;
char phno[15];
struct student *link;
}NODE;
NODE* getnode()
{
NODE *x;
x=(NODE*)malloc(sizeof(NODE));
printf("n Enter Student Details: ");
scanf("%s %s %s %d %s",x->usn,x->name,x->branch,&x->sem,x->phno);
x->link=NULL;
return x;
}
NODE* insert_front(NODE* first)
{
NODE *temp;
temp=getnode();
if(first==NULL)
{
first=temp;
}
else
{
temp->link=first;
first=temp;
}
return first;
}
NODE* insert_rear(NODE* first)
{
NODE *temp,*cur;
temp=getnode();
if(first==NULL)
{
first=temp;
}
else
{
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
}
return first;
}
NODE* delete_front(NODE* first)
{
NODE *cur;
if(first==NULL)
{
printf("n List is emptyn");
}
else
{
cur=first;
first=first->link;
free(cur);
}
return first;
}
NODE* delete_rear(NODE *first)
{
NODE *prev, *cur;
if(first==NULL)
{
printf("n List is emptyn");
}
if(first->link==NULL)
{
free(first);
return NULL;
}
prev=NULL;
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
free(cur);
return first;
}
void display(NODE *first)
{
NODE *cur;
int count=0;
if(first == NULL)
printf("nNo student datan");
else
{
cur=first;
printf("nUSNt NAMEt Brancht Semt Phnon");
while(cur!=NULL)
{
printf("n %st %st %st %dt %sn", cur->usn, cur->name,cur->branch,cur->sem,cur->phno);
cur = cur->link;
count++;
}
printf("nThe no. of nodes in list is: %d",count);
}
}
NODE* stack(NODE *first)
{
int ch;
while(1)
{
printf("nSLL used as Stack...");
printf("n 1.PUSH t 2.POPt3.Displayt 4.Exitt");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: first=insert_front(first); break;
case 2: first=delete_front(first); break;
case 3: display(first); break;
case 4: exit(0);
}
}
return first;
}
int main()
{
NODE *first=NULL;
int ch;
while(1)
{
printf("n1.InsertFrontt 2. InsertReart 3.DeleteFrontt 4.DeleteReart 5.Displayt 6.Stackt
7.exitn");
printf("Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:first=insert_front(first);
break;
case 2:first=insert_rear(first);
break;
case 3:first=delete_front(first);
break;
case 4:first=delete_rear(first);
break;
case 5:display(first);
break;
case 6:first=stack(first);
break;
case 7:exit(0);
break;
default: printf("n Invalid choicen");
break;
}
}
return 0;
}
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
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit
#include <stdio.h>
#include<stdlib.h>
typedef struct student
{
int ssn;
char name[30];
char dept[5];
int sal;
char des[15];
struct student *next,*prev;
}NODE;
NODE* getnode()
{
NODE *x;
x=(NODE*)malloc(sizeof(NODE));
printf("n Enter Employee Details: ");
scanf("%d %s %s %d %s",&x->ssn,x->name,x->dept,&x->sal,x->des);
x->next=x->prev=NULL;
return x;
}
NODE* insert_front(NODE* first)
{
NODE *temp;
temp=getnode(first);
if(first==NULL)
{
first=temp;
}
else
{
temp->next=first;
first->prev=temp;
first=temp;
}
return first;
}
NODE* insert_rear(NODE* first)
{
NODE *temp,*cur;
if(first==NULL)
{
temp=getnode(first);
first=temp;
}
else
{
temp=getnode(first);
cur=first;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=temp;
temp->prev=cur;
}
return first;
}
NODE* delete_front(NODE* first)
{
NODE *cur;
if(first==NULL)
{
printf("n List is emptyn");
}
else if(first->next==NULL)
{
free(first);
return NULL;
}
else
{
cur=first;
first=first->next;
first->prev=NULL;
free(cur);
first->prev=NULL;
}
return first;
}
NODE* delete_rear(NODE* first)
{
NODE *prev, *cur;
if(first==NULL)
{
printf("n List is emptyn");
}
else if(first->next==NULL)
{
free(first);
return NULL;
}
else
{
prev=NULL;
cur=first;
while(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
prev->next=NULL;
free(cur);
}
return first;
}
void display(NODE *first)
{
NODE *cur;
int count=0;
if(first == NULL)
printf("nNo Employee datan");
else
{
cur=first;
printf("nSSNt NAMEt Deptt Salt Designtnn");
while(cur!=NULL)
{
printf("n %dt %st %st %dt %sn", cur->ssn, cur->name,cur->dept,cur->sal,cur->des);
cur = cur->next;
count++;
}
printf("nThe no. of nodes in list is: %d",count);
}
}
NODE* dequeue(NODE *first)
{
int ch;
while(ch<6)
{
printf("nDLL used as DEQUEUE...");
printf("n 1.Insert_Front t
2.Delete_Reart3.Insert_reart4.Delete_Frontt5.Displayt 6.Exitn");
printf("nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: first=insert_front(first); break;
case 2: first=delete_rear(first); break;
case 3: first=insert_rear(first); break;
case 4: first=delete_front(first);break;
case 5: display(first); break;
case 6: exit(0);
}
}
return first;
}
int main( )
{
NODE *first=NULL;
int ch;
while(1)
{
printf("n1.InsertFrontt 2. InsertReart 3.DeleteFrontt 4.DeleteReart 5.Displayt 6.Dequeuet
7.exitn");
printf("Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:first=insert_front(first);
break;
case 2:first=insert_rear(first);
break;
case 3:first=delete_front(first);
break;
case 4:first=delete_rear(first);
break;
case 5:display(first);
break;
case 6:first=dequeue(first);
break;
case 7:exit(0);
break;
default: printf("n Invalid choicen");
break;
}
}
return 0;
}
9. Design, Develop and Implement a Program in C for the following operations on Singly
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) and store the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
9a. Program
/* Program to Represent and Evaluate a Polynomial */
#include<stdio.h>
#include<conio.h>
#include<math.h>
typedef struct node
{
int cf;
int px,py,pz;
struct node* link;
}NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc( sizeof(struct node));
if(x==NULL)
{
printf("Out of memeory");
exit(0);
}
return x;
}
NODE insert_rear(int cf,int px,int py,int pz, NODE head)
{
NODE temp,cur;
temp=getnode();
temp->cf=cf;
temp->px=px;
temp->py=py;
temp->pz=pz;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
NODE read_poly(NODE 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:%dn",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;
}
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("Polynomail does not exist");
return;
}
temp=head->link;
while(temp!=head)
{
if(temp->cf<0)
printf("%d",temp->cf);
else
printf("+%d",temp->cf);
if(temp->px!=0)
printf("x^%d",temp->px);
if(temp->py!=0)
printf("y^%d",temp->py);
if(temp->pz!=0)
printf("z^%d",temp->pz);
temp=temp->link;
}
printf("n");
}
float evaluate(NODE head)
{
int x,y,z;
float sum=0;
NODE p;
printf("Enter the value of x,y,z");
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 polynomialn");
head=read_poly(head);
res=evaluate(head);
printf("The given polynomial isn");
display(head);
printf("The result=%fn",res);
}
9b. Program
/* Program to find the sum of two Polynomial */
#include<stdio.h>
#include<conio.h>
#include<math.h>
typedef struct node
{
int cf;
int px,py,pz;
struct node* link;
}NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc( sizeof(struct node));
if(x==NULL)
{
printf("Out of memeory");
exit(0);
}
return x;
}
NODE insert_rear(int cf,int px,int py,int pz, NODE head)
{
NODE temp,cur;
temp=getnode();
temp->cf=cf;
temp->px=px;
temp->py=py;
temp->pz=pz;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
NODE read_poly(NODE 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:%dn",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;
}
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("Polynomail does not exist");
return;
}
temp=head->link;
while(temp!=head)
{
if(temp->cf<0)
printf("%d",temp->cf);
else
printf("+%d",temp->cf);
if(temp->px!=0)
printf("x^%d",temp->px);
if(temp->py!=0)
printf("y^%d",temp->py);
if(temp->pz!=0)
printf("z^%d",temp->pz);
temp=temp->link;
}
printf("n");
}
NODE search(NODE p1,NODE h2)
{
NODE p2;
int cf1,px1,py1,pz1, cf2,px2,py2,pz2;
px1=p1->px;
py1=p1->py;
pz1=p1->pz;
p2=h2->link;
while ( p2 != h2)
{
px2=p2->px;
py2=p2->py;
pz2=p2->pz;
if( px1==px2 && py1==py2 && pz1==pz2 ) break;
p2=p2->link;
}
return p2;
}
NODE copy_poly (NODE h2, NODE h3)
{
NODE p2;
int cf2,px2,py2,pz2;
p2=h2->link;
while( p2!=h2)
{
if( p2->cf != -999)
{
cf2=p2->cf;
px2=p2->px;
py2=p2->py;
pz2=p2->pz;
h3=insert_rear(cf2, px2, py2, pz2, h3);
}
p2 = p2->link;
}
return h3;
}
NODE add_poly(NODE h1, NODE h2, NODE h3)
{
NODE p1,p2;
int cf1,px1,py1,pz1, sum;
p1 = h1->link;
while(p1!=h1)
{
cf1=p1->cf;
px1=p1->px;
py1=p1->py;
pz1=p1->pz;
p2=search(p1,h2);
if(p2!=h2)
{
sum=cf1+p2->cf;
h3=insert_rear(sum,px1,py1,pz1,h3);
p2->cf=-999;
}
else
h3= insert_rear(cf1,px1,py1,pz1,h3);
p1=p1->link;
}
h3=copy_poly(h2,h3);
return h3;
}
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 polynomialn");
h1=read_poly(h1);
printf("Enter the second polynomialn");
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();
}
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<stdlib.h>
typedef struct node
{
int item;
struct node *llink, *rlink;
}NODE;
NODE* getnode()
{
NODE* x;
x=(NODE*)malloc(sizeof(NODE));
scanf("%d",&x->item);
x->llink=x->rlink=NULL;
return x;
}
NODE* insert(NODE* root)
{
NODE *temp,*cur,*prev;
temp=getnode();
if(root==NULL)
{
root=temp;
}
else
{
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(temp->item<cur->item)
cur=cur->llink;
else
cur=cur->rlink;
}
if(temp->item<prev->item)
prev->llink=temp;
else
prev->rlink=temp;
}
return root;
}
void search(NODE *root)
{
int item;
NODE *cur;
cur=root;
if(root==NULL)
{
printf("Tree is emptyn");
}
else
{
printf("Enter the item to be searched: ");
scanf("%d",&item);
while(cur!=NULL)
{
if(cur->item==item)
break;
if(cur->item<item)
cur=cur->rlink;
else
cur=cur->llink;
}
if(cur!=NULL)
{
printf("Item foundn");
}
else
{
printf("Item Not found");
}
}
}
void preorder(NODE *root)
{
if(root==NULL) return;
printf("%dt",root->item);
preorder(root->llink);
preorder(root->rlink);
}
void postorder(NODE *root)
{
if(root==NULL) return;
postorder(root->llink);
postorder(root->rlink);
printf("%dt",root->item);
}
void inorder(NODE *root)
{
if(root==NULL) return;
inorder(root->llink);
printf("%dt",root->item);
inorder(root->rlink);
}
int main()
{
int ch,i,n,ht;
NODE *root=NULL;
while(1)
{
printf("n 1.Createt 2.Traverset 3.Searcht 4.Heightt 5.Exitn");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the number of nodes to be inserted: ");
scanf("%d",&n);
printf("Enter the tree nodesn");
for(i=0;i<n;i++)
{
root=insert(root);
}
break;
case 2:printf("n Preorder Traversal: ");
preorder(root);
printf("n Inorder Traversal: ");
inorder(root);
printf("n Postorder Traversal: ");
postorder(root);
break;
case 3:search(root);
break;
case 4:exit(0);
default:printf("n Invalid Choicen");
break;
}
}
return 0;
}
11. Design, 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 DFS method*/
#include<stdio.h>
int stack[10];
int top=-1;
int adj[10][10];
int vis[10]={0};
void main()
{
int n, s, u, v, i, j;
int found=0;
printf("n Enter the number of vertex:");
scanf("%d",&n);
printf("n Enter the adj matrix:n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&adj[i][j]);
}
}
printf("n Enter the source vertex:");
scanf("%d",&s);
stack[++top]=s;
vis[s]=1;
printf("source %d:",s);
while(top!=-1)
{
found=0;
u=stack[top];
for(v=0;v<n && found==0;v++)
{
if(adj[u][v]==1 && vis[v]==0)
{
printf("->%d",v);
stack[++top]=v;
vis[v]=1;
found=1;
}
}
if(found==0)
{
top--;
}
}
}
/* Traversal using BFS method*/
#include<stdio.h>
int q[10];
int r=-1, f=0;
int adj[10][10];
int vis[10]={0};
void main()
{
int n, i, j, s, v, u;
printf("n Enter the number of vertex:");
scanf("%d",&n);
printf("n Enter the Adj matrix:n ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&adj[i][j]);
}
}
printf("n Enter the source vertex:");
scanf("%d",&s);
q[++r]=s;
vis[s]=1;
printf("%d: ",s);
while(f<=r)
{
u=q[f++];
for(v=0;v<n;v++)
{
if(adj[u][v]==1 && vis[v]==0)
{
printf("->%d",v);
vis[v]=1;
q[++r]=v;
}
}
}
}
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. Design and 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.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10
typedef struct
{
int id;
char name[20];
}EMPLOYEE;
EMPLOYEE e[SIZE];
void initialize_table()
{
for(int i=0; i<SIZE; i++)
{
e[i].id=0;
}
}
void insert_table()
{
int i,id,index,hvalue;
char name[26];
printf("Enter the employee id and name: ");
scanf("%d %s",&id,name);
hvalue= id % SIZE;
for(i=0; i<SIZE; i++)
{
index=(hvalue+i) % SIZE;
if(e[index].id==0)
{
e[index].id=id;
strcpy(e[index].name,name);
break;
}
}
if(i==SIZE)
{
printf("Hash table fulln");
}
}
void display_table()
{
printf("Ht Idt Namen");
for(int i=0; i<SIZE; i++)
{
printf("%dt %dt %sn",i,e[i].id,e[i].name);
}
}
void main()
{
int ch, id;
char name[26];
initialize_table();
while(ch<4)
{
printf("1:Insertt 2:Displayt 3:Exitn");
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:insert_table( );
break;
case 2:display_table();
break;
case 3: exit(0);
break;
deafult: printf("Enter valid choicen");
break;
}
}
}
Ad

More Related Content

What's hot (20)

Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
Kandarp Tiwari
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
AkhilaaReddy
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
Piyush rai
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
Dabbal Singh Mahara
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
Sayantan Sur
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
kalyanineve
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
Mukesh Tekwani
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
Emertxe Information Technologies Pvt Ltd
 
Python lab manual all the experiments are available
Python lab manual all the experiments are availablePython lab manual all the experiments are available
Python lab manual all the experiments are available
Nitesh Dubey
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
Bhavik Vashi
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
Adnan Khan
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
Tony Kurishingal
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
Kandarp Tiwari
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
AkhilaaReddy
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
Piyush rai
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
kalyanineve
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
Mukesh Tekwani
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Python lab manual all the experiments are available
Python lab manual all the experiments are availablePython lab manual all the experiments are available
Python lab manual all the experiments are available
Nitesh Dubey
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
Bhavik Vashi
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
Adnan Khan
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey
 

Similar to VTU Data Structures Lab Manual (20)

ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
week-17x
week-17xweek-17x
week-17x
KITE www.kitecolleges.com
 
week-18x
week-18xweek-18x
week-18x
KITE www.kitecolleges.com
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
Asrinath1
 
Struct examples
Struct examplesStruct examples
Struct examples
mondalakash2012
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
Arkadeep Dey
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
manoj11manu
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
Cpds lab
Cpds labCpds lab
Cpds lab
praveennallavelly08
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
happycocoman
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
sreekanth3dce
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1
Balaji Thala
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
KavyaSharma65
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
Pnno
PnnoPnno
Pnno
shristichaudhary4
 
Ada file
Ada fileAda file
Ada file
Kumar Gaurav
 
Ad

More from Nithin Kumar,VVCE, Mysuru (13)

VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
Nithin Kumar,VVCE, Mysuru
 
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...
Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...
Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...
Nithin Kumar,VVCE, Mysuru
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruJava programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU C-sharp & .net programming notes by Nithin, VVCE, Mysuru
VTU C-sharp & .net programming notes by Nithin, VVCE, MysuruVTU C-sharp & .net programming notes by Nithin, VVCE, Mysuru
VTU C-sharp & .net programming notes by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU internet of things(IOT) notes by Nithin,VVCE, Mysuru
VTU internet of things(IOT) notes by Nithin,VVCE, MysuruVTU internet of things(IOT) notes by Nithin,VVCE, Mysuru
VTU internet of things(IOT) notes by Nithin,VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru
 VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru
VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru
Nithin Kumar,VVCE, Mysuru
 
Vtu Data Structures Notes CBCS by Nithin, VVCE
Vtu Data Structures Notes CBCS by Nithin, VVCEVtu Data Structures Notes CBCS by Nithin, VVCE
Vtu Data Structures Notes CBCS by Nithin, VVCE
Nithin Kumar,VVCE, Mysuru
 
VTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCE
VTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCEVTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCE
VTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCE
Nithin Kumar,VVCE, Mysuru
 
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuruVtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU Advanced Algorithms Notes by Nithin, VVCE Mysuru
VTU Advanced Algorithms Notes by Nithin, VVCE MysuruVTU Advanced Algorithms Notes by Nithin, VVCE Mysuru
VTU Advanced Algorithms Notes by Nithin, VVCE Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU Network management -15cs833 Notes by Nithin, VVCE, Mysuru
VTU Network management -15cs833 Notes by Nithin, VVCE, MysuruVTU Network management -15cs833 Notes by Nithin, VVCE, Mysuru
VTU Network management -15cs833 Notes by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuruVtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
Nithin Kumar,VVCE, Mysuru
 
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
18CSMP68 VTU Mobile Application Develeopment Lab Manual by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...
Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...
Vtu research methodology handwritten notes(16phdrm) for PG and PhD by Nithin ...
Nithin Kumar,VVCE, Mysuru
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruJava programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU C-sharp & .net programming notes by Nithin, VVCE, Mysuru
VTU C-sharp & .net programming notes by Nithin, VVCE, MysuruVTU C-sharp & .net programming notes by Nithin, VVCE, Mysuru
VTU C-sharp & .net programming notes by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU internet of things(IOT) notes by Nithin,VVCE, Mysuru
VTU internet of things(IOT) notes by Nithin,VVCE, MysuruVTU internet of things(IOT) notes by Nithin,VVCE, Mysuru
VTU internet of things(IOT) notes by Nithin,VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru
 VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru
VTU C programming(CPS) 18CPS13/23 notes by Nithin,VVCE,Mysuru
Nithin Kumar,VVCE, Mysuru
 
Vtu Data Structures Notes CBCS by Nithin, VVCE
Vtu Data Structures Notes CBCS by Nithin, VVCEVtu Data Structures Notes CBCS by Nithin, VVCE
Vtu Data Structures Notes CBCS by Nithin, VVCE
Nithin Kumar,VVCE, Mysuru
 
VTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCE
VTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCEVTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCE
VTU Algorithms Notes CBCS (DAA Notes) by Nithin, VVCE
Nithin Kumar,VVCE, Mysuru
 
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuruVtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU Advanced Algorithms Notes by Nithin, VVCE Mysuru
VTU Advanced Algorithms Notes by Nithin, VVCE MysuruVTU Advanced Algorithms Notes by Nithin, VVCE Mysuru
VTU Advanced Algorithms Notes by Nithin, VVCE Mysuru
Nithin Kumar,VVCE, Mysuru
 
VTU Network management -15cs833 Notes by Nithin, VVCE, Mysuru
VTU Network management -15cs833 Notes by Nithin, VVCE, MysuruVTU Network management -15cs833 Notes by Nithin, VVCE, Mysuru
VTU Network management -15cs833 Notes by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuruVtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Vtu Data Mining-15CS651 notes by Nithin vvce,mysuru
Nithin Kumar,VVCE, Mysuru
 
Ad

Recently uploaded (20)

E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 

VTU Data Structures Lab Manual

  • 1. DS LAB PROGRAMS(18CSL38) 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> #include<stdlib.h> int a[6], n, elem, i, pos; 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("%dt", 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;
  • 2. n = n+1; } void del() { printf("nEnter the position of the element to be deleted: "); scanf("%d", &pos); elem = a[pos]; for(i=pos; i<n-1; i++) { a[i] = a[i+1]; } n = n-1; printf("nThe deleted element is = %d", elem); } int main() { int ch; while(ch!=5) { printf("n 1.Createt 2.Displayt 3.Insertt 4.Deletet 5.Exitn"); 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; } } return 0; }
  • 3. 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> char str[100], pat[50], rep[50], ans[100]; int i, j, c, m, k, flag=0; void stringmatch() { i = m = c = j = 0; while(str[c] != '0') { if(str[m] == pat[i]) { i++; m++; if(pat[i] == '0') { flag = 1; for(k = 0; rep[k] != '0'; k++, j++) ans[j] = rep[k]; i = 0; c = m; } } else { ans[j] = str[c]; j++; c++; m=c; } } ans[j] = '0'; } void main() { printf("Enter a main string n"); gets(str); printf("Enter a pattern string n"); gets(pat); printf("Enter a replace string n"); gets(rep); stringmatch(); if(flag == 1) printf("The resultant string isn %s" , ans); else printf("Pattern string NOT foundn"); }
  • 4. 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<stdlib.h> #define MAX 5 int stack[MAX], item, ch, top=-1, status=0; void push(int stack[],int item) { if(top==(MAX-1)) printf("nOverflown"); else stack[++top]=item; status++; } int pop() { if(top==-1) printf("nUnderflown"); else return stack[top--]; status--; } void palindrome(int stack[]) { int temp, count=0; temp=status; for(int i=0;i<temp;i++) { if(stack[i]==pop()) count++; } if(count==temp) printf("Palindromen"); else printf("not palindromen"); } void display(int stack[]) {
  • 5. if(top==-1) printf("nstack is emptyn"); else for(int i=top;i>=0;i--) { printf("|%d|n",stack[i]); } } int main() { int ch; while(ch<6) { printf("n 1.pusht 2.popt 3.palindromet 4.displayt 5.exitn"); printf("Enter the choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the item: "); scanf("%d",&item); push(stack,item); break; case 2: printf("popped value = %d",pop()); break; case 3: palindrome(stack); break; case 4: display(stack); break; case 5: exit(0); default: printf("Invalid Choicen"); break; } } return 0; }
  • 6. 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 <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 == '/' || x=='%') return 2; if(x=='^') return 3; return 0; } void main() { char exp[20]; 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);
  • 7. 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()); } }
  • 8. 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> 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 main() { char postfix[20], *p; int n1,n2,n3,num; printf("Enter the postfix expression: "); scanf("%s",postfix); p = postfix; while(*p != '0') { if(isdigit(*p)) { num=(*p-48); push(num); } else { n1 = pop(); n2 = pop(); switch(*p) { case '+': n3 = n2 + n1; break; case '-': n3 = n2 - n1; break; case '*': n3 = n2 * n1; break;
  • 9. case '/': n3 = n2 / n1; break; case '^': n3 = pow(n2,n1); break; case '%': n3 = n2%n1; break; } push(n3); } p++; } printf("nThe result = %d",pop()); } /* Tower of Hanoi */ #include<stdio.h> #include<math.h> void tower(int n, char S, char T, char D) { if(n==0) return; tower(n-1,S,D,T); printf("n Move disc %d from %c to %c",n,S,D); tower(n-1,T,S,D); } void main() { int n; printf("Enter the number of Discs: "); scanf("%d",&n); tower(n,'A','B','C'); printf("n Total number of Moves=%d",(int)pow(2,n)-1); }
  • 10. 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; void insert (int item) { if (count==MAX) printf("Queue Overflown"); else { r=(r+1)%MAX; q[r]=item; count=count+1; } } int delete() { 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 emptyn"); else { i=f; for (j=1;j<=count;j++) { printf("%dt",q[i]);
  • 11. i=(i+1)%MAX; } } } void main() { int ch,item,itemdel; while (1) { printf("nEnter the choicen"); printf("1.Insertt2.Deletet3.Displayt4.Exitn"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the itemn"); scanf("%d",&item); insert(item); break; case 2: itemdel=delete(); if (itemdel) printf("The deleted item is %dn",itemdel); else printf("Queue underflown"); break; case 3: display(); break; case 4: exit(0); default: pritnf("Enter a valid choicen"); } } }
  • 12. 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<stdlib.h> typedef struct student { char usn[10]; char name[30]; char branch[5]; int sem; char phno[15]; struct student *link; }NODE; NODE* getnode() { NODE *x; x=(NODE*)malloc(sizeof(NODE)); printf("n Enter Student Details: "); scanf("%s %s %s %d %s",x->usn,x->name,x->branch,&x->sem,x->phno); x->link=NULL; return x; } NODE* insert_front(NODE* first) { NODE *temp; temp=getnode(); if(first==NULL) { first=temp; } else { temp->link=first; first=temp; } return first; } NODE* insert_rear(NODE* first) { NODE *temp,*cur; temp=getnode(); if(first==NULL)
  • 13. { first=temp; } else { cur=first; while(cur->link!=NULL) { cur=cur->link; } cur->link=temp; } return first; } NODE* delete_front(NODE* first) { NODE *cur; if(first==NULL) { printf("n List is emptyn"); } else { cur=first; first=first->link; free(cur); } return first; } NODE* delete_rear(NODE *first) { NODE *prev, *cur; if(first==NULL) { printf("n List is emptyn"); } if(first->link==NULL) { free(first); return NULL; } prev=NULL; cur=first; while(cur->link!=NULL) { prev=cur; cur=cur->link; } prev->link=NULL;
  • 14. free(cur); return first; } void display(NODE *first) { NODE *cur; int count=0; if(first == NULL) printf("nNo student datan"); else { cur=first; printf("nUSNt NAMEt Brancht Semt Phnon"); while(cur!=NULL) { printf("n %st %st %st %dt %sn", cur->usn, cur->name,cur->branch,cur->sem,cur->phno); cur = cur->link; count++; } printf("nThe no. of nodes in list is: %d",count); } } NODE* stack(NODE *first) { int ch; while(1) { printf("nSLL used as Stack..."); printf("n 1.PUSH t 2.POPt3.Displayt 4.Exitt"); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: first=insert_front(first); break; case 2: first=delete_front(first); break; case 3: display(first); break; case 4: exit(0); } } return first; } int main() { NODE *first=NULL; int ch; while(1) {
  • 15. printf("n1.InsertFrontt 2. InsertReart 3.DeleteFrontt 4.DeleteReart 5.Displayt 6.Stackt 7.exitn"); printf("Enter Your Choice: "); scanf("%d",&ch); switch(ch) { case 1:first=insert_front(first); break; case 2:first=insert_rear(first); break; case 3:first=delete_front(first); break; case 4:first=delete_rear(first); break; case 5:display(first); break; case 6:first=stack(first); break; case 7:exit(0); break; default: printf("n Invalid choicen"); break; } } return 0; }
  • 16. 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 e. Demonstrate how this DLL can be used as Double Ended Queue f. Exit #include <stdio.h> #include<stdlib.h> typedef struct student { int ssn; char name[30]; char dept[5]; int sal; char des[15]; struct student *next,*prev; }NODE; NODE* getnode() { NODE *x; x=(NODE*)malloc(sizeof(NODE)); printf("n Enter Employee Details: "); scanf("%d %s %s %d %s",&x->ssn,x->name,x->dept,&x->sal,x->des); x->next=x->prev=NULL; return x; } NODE* insert_front(NODE* first) { NODE *temp; temp=getnode(first); if(first==NULL) { first=temp; } else { temp->next=first; first->prev=temp; first=temp; } return first; } NODE* insert_rear(NODE* first)
  • 17. { NODE *temp,*cur; if(first==NULL) { temp=getnode(first); first=temp; } else { temp=getnode(first); cur=first; while(cur->next!=NULL) { cur=cur->next; } cur->next=temp; temp->prev=cur; } return first; } NODE* delete_front(NODE* first) { NODE *cur; if(first==NULL) { printf("n List is emptyn"); } else if(first->next==NULL) { free(first); return NULL; } else { cur=first; first=first->next; first->prev=NULL; free(cur); first->prev=NULL; } return first; } NODE* delete_rear(NODE* first) { NODE *prev, *cur; if(first==NULL) { printf("n List is emptyn"); }
  • 18. else if(first->next==NULL) { free(first); return NULL; } else { prev=NULL; cur=first; while(cur->next!=NULL) { prev=cur; cur=cur->next; } prev->next=NULL; free(cur); } return first; } void display(NODE *first) { NODE *cur; int count=0; if(first == NULL) printf("nNo Employee datan"); else { cur=first; printf("nSSNt NAMEt Deptt Salt Designtnn"); while(cur!=NULL) { printf("n %dt %st %st %dt %sn", cur->ssn, cur->name,cur->dept,cur->sal,cur->des); cur = cur->next; count++; } printf("nThe no. of nodes in list is: %d",count); } } NODE* dequeue(NODE *first) { int ch; while(ch<6) { printf("nDLL used as DEQUEUE..."); printf("n 1.Insert_Front t 2.Delete_Reart3.Insert_reart4.Delete_Frontt5.Displayt 6.Exitn"); printf("nEnter your choice: "); scanf("%d", &ch); switch(ch)
  • 19. { case 1: first=insert_front(first); break; case 2: first=delete_rear(first); break; case 3: first=insert_rear(first); break; case 4: first=delete_front(first);break; case 5: display(first); break; case 6: exit(0); } } return first; } int main( ) { NODE *first=NULL; int ch; while(1) { printf("n1.InsertFrontt 2. InsertReart 3.DeleteFrontt 4.DeleteReart 5.Displayt 6.Dequeuet 7.exitn"); printf("Enter Your Choice: "); scanf("%d",&ch); switch(ch) { case 1:first=insert_front(first); break; case 2:first=insert_rear(first); break; case 3:first=delete_front(first); break; case 4:first=delete_rear(first); break; case 5:display(first); break; case 6:first=dequeue(first); break; case 7:exit(0); break; default: printf("n Invalid choicen"); break; } } return 0; }
  • 20. 9. Design, Develop and Implement a Program in C for the following operations on Singly 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) and store the result in POLYSUM(x,y,z) Support the program with appropriate functions for each of the above operations 9a. Program /* Program to Represent and Evaluate a Polynomial */ #include<stdio.h> #include<conio.h> #include<math.h> typedef struct node { int cf; int px,py,pz; struct node* link; }NODE; NODE getnode() { NODE x; x=(NODE)malloc( sizeof(struct node)); if(x==NULL) { printf("Out of memeory"); exit(0); } return x; } NODE insert_rear(int cf,int px,int py,int pz, NODE head) { NODE temp,cur; temp=getnode(); temp->cf=cf; temp->px=px; temp->py=py; temp->pz=pz; cur=head->link; while(cur->link!=head) { cur=cur->link; } cur->link=temp; temp->link=head; return head; }
  • 21. NODE read_poly(NODE 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:%dn",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; } void display(NODE head) { NODE temp; if(head->link==head) { printf("Polynomail does not exist"); return; } temp=head->link; while(temp!=head) { if(temp->cf<0) printf("%d",temp->cf); else printf("+%d",temp->cf); if(temp->px!=0) printf("x^%d",temp->px); if(temp->py!=0) printf("y^%d",temp->py); if(temp->pz!=0) printf("z^%d",temp->pz); temp=temp->link; } printf("n"); } float evaluate(NODE head) { int x,y,z; float sum=0; NODE p; printf("Enter the value of x,y,z"); scanf("%d %d %d",&x,&y,&z); p=head->link; while(p!=head)
  • 22. { 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 polynomialn"); head=read_poly(head); res=evaluate(head); printf("The given polynomial isn"); display(head); printf("The result=%fn",res); } 9b. Program /* Program to find the sum of two Polynomial */ #include<stdio.h> #include<conio.h> #include<math.h> typedef struct node { int cf; int px,py,pz; struct node* link; }NODE; NODE getnode() { NODE x; x=(NODE)malloc( sizeof(struct node)); if(x==NULL) { printf("Out of memeory"); exit(0); } return x; } NODE insert_rear(int cf,int px,int py,int pz, NODE head) { NODE temp,cur; temp=getnode(); temp->cf=cf;
  • 23. temp->px=px; temp->py=py; temp->pz=pz; cur=head->link; while(cur->link!=head) { cur=cur->link; } cur->link=temp; temp->link=head; return head; } NODE read_poly(NODE 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:%dn",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; } void display(NODE head) { NODE temp; if(head->link==head) { printf("Polynomail does not exist"); return; } temp=head->link; while(temp!=head) { if(temp->cf<0) printf("%d",temp->cf); else printf("+%d",temp->cf); if(temp->px!=0) printf("x^%d",temp->px); if(temp->py!=0) printf("y^%d",temp->py); if(temp->pz!=0) printf("z^%d",temp->pz); temp=temp->link;
  • 24. } printf("n"); } NODE search(NODE p1,NODE h2) { NODE p2; int cf1,px1,py1,pz1, cf2,px2,py2,pz2; px1=p1->px; py1=p1->py; pz1=p1->pz; p2=h2->link; while ( p2 != h2) { px2=p2->px; py2=p2->py; pz2=p2->pz; if( px1==px2 && py1==py2 && pz1==pz2 ) break; p2=p2->link; } return p2; } NODE copy_poly (NODE h2, NODE h3) { NODE p2; int cf2,px2,py2,pz2; p2=h2->link; while( p2!=h2) { if( p2->cf != -999) { cf2=p2->cf; px2=p2->px; py2=p2->py; pz2=p2->pz; h3=insert_rear(cf2, px2, py2, pz2, h3); } p2 = p2->link; } return h3; } NODE add_poly(NODE h1, NODE h2, NODE h3) { NODE p1,p2; int cf1,px1,py1,pz1, sum; p1 = h1->link; while(p1!=h1) { cf1=p1->cf;
  • 25. px1=p1->px; py1=p1->py; pz1=p1->pz; p2=search(p1,h2); if(p2!=h2) { sum=cf1+p2->cf; h3=insert_rear(sum,px1,py1,pz1,h3); p2->cf=-999; } else h3= insert_rear(cf1,px1,py1,pz1,h3); p1=p1->link; } h3=copy_poly(h2,h3); return h3; } 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 polynomialn"); h1=read_poly(h1); printf("Enter the second polynomialn"); 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(); }
  • 26. 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<stdlib.h> typedef struct node { int item; struct node *llink, *rlink; }NODE; NODE* getnode() { NODE* x; x=(NODE*)malloc(sizeof(NODE)); scanf("%d",&x->item); x->llink=x->rlink=NULL; return x; } NODE* insert(NODE* root) { NODE *temp,*cur,*prev; temp=getnode(); if(root==NULL) { root=temp; } else { prev=NULL; cur=root; while(cur!=NULL) { prev=cur; if(temp->item<cur->item) cur=cur->llink; else cur=cur->rlink; } if(temp->item<prev->item) prev->llink=temp; else prev->rlink=temp; }
  • 27. return root; } void search(NODE *root) { int item; NODE *cur; cur=root; if(root==NULL) { printf("Tree is emptyn"); } else { printf("Enter the item to be searched: "); scanf("%d",&item); while(cur!=NULL) { if(cur->item==item) break; if(cur->item<item) cur=cur->rlink; else cur=cur->llink; } if(cur!=NULL) { printf("Item foundn"); } else { printf("Item Not found"); } } } void preorder(NODE *root) { if(root==NULL) return; printf("%dt",root->item); preorder(root->llink); preorder(root->rlink); } void postorder(NODE *root) { if(root==NULL) return; postorder(root->llink); postorder(root->rlink);
  • 28. printf("%dt",root->item); } void inorder(NODE *root) { if(root==NULL) return; inorder(root->llink); printf("%dt",root->item); inorder(root->rlink); } int main() { int ch,i,n,ht; NODE *root=NULL; while(1) { printf("n 1.Createt 2.Traverset 3.Searcht 4.Heightt 5.Exitn"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1:printf("Enter the number of nodes to be inserted: "); scanf("%d",&n); printf("Enter the tree nodesn"); for(i=0;i<n;i++) { root=insert(root); } break; case 2:printf("n Preorder Traversal: "); preorder(root); printf("n Inorder Traversal: "); inorder(root); printf("n Postorder Traversal: "); postorder(root); break; case 3:search(root); break; case 4:exit(0); default:printf("n Invalid Choicen"); break; } } return 0; }
  • 29. 11. Design, 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 DFS method*/ #include<stdio.h> int stack[10]; int top=-1; int adj[10][10]; int vis[10]={0}; void main() { int n, s, u, v, i, j; int found=0; printf("n Enter the number of vertex:"); scanf("%d",&n); printf("n Enter the adj matrix:n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&adj[i][j]); } } printf("n Enter the source vertex:"); scanf("%d",&s); stack[++top]=s; vis[s]=1; printf("source %d:",s); while(top!=-1) { found=0; u=stack[top]; for(v=0;v<n && found==0;v++) { if(adj[u][v]==1 && vis[v]==0) { printf("->%d",v); stack[++top]=v; vis[v]=1; found=1; } } if(found==0) { top--; } } }
  • 30. /* Traversal using BFS method*/ #include<stdio.h> int q[10]; int r=-1, f=0; int adj[10][10]; int vis[10]={0}; void main() { int n, i, j, s, v, u; printf("n Enter the number of vertex:"); scanf("%d",&n); printf("n Enter the Adj matrix:n "); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&adj[i][j]); } } printf("n Enter the source vertex:"); scanf("%d",&s); q[++r]=s; vis[s]=1; printf("%d: ",s); while(f<=r) { u=q[f++]; for(v=0;v<n;v++) { if(adj[u][v]==1 && vis[v]==0) { printf("->%d",v); vis[v]=1; q[++r]=v; } } } }
  • 31. 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. Design and 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. #include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 10 typedef struct { int id; char name[20]; }EMPLOYEE; EMPLOYEE e[SIZE]; void initialize_table() { for(int i=0; i<SIZE; i++) { e[i].id=0; } } void insert_table() { int i,id,index,hvalue; char name[26]; printf("Enter the employee id and name: "); scanf("%d %s",&id,name); hvalue= id % SIZE; for(i=0; i<SIZE; i++) { index=(hvalue+i) % SIZE; if(e[index].id==0) { e[index].id=id; strcpy(e[index].name,name); break; } } if(i==SIZE) { printf("Hash table fulln"); } } void display_table()
  • 32. { printf("Ht Idt Namen"); for(int i=0; i<SIZE; i++) { printf("%dt %dt %sn",i,e[i].id,e[i].name); } } void main() { int ch, id; char name[26]; initialize_table(); while(ch<4) { printf("1:Insertt 2:Displayt 3:Exitn"); printf("Enter the choice:"); scanf("%d",&ch); switch(ch) { case 1:insert_table( ); break; case 2:display_table(); break; case 3: exit(0); break; deafult: printf("Enter valid choicen"); break; } } }
  翻译: