Judging the Relevance and worth of ideas part 2.pptx
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);
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)
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)
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;
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;
}
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()