DS Lab Manual
DS Lab Manual
DS Lab Manual
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[20];
int n,val,i,pos;
/*Function Prototype*/
void create();
void display();
void insert();
void delete1();
void main()
{
int choice;
while(choice)
{
printf("\n\n--------MENU-----------\n");
printf("1.CREATE\n");
printf("2.DISPLAY\n");
printf("3.INSERT\n");
printf("4.DELETE\n");
printf("5.EXIT\n");
printf("-----------------------");
printf("\n ENTER YOUR CHOICE:\t");
scanf("%d", &choice);
switch (choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
delete1();
break;
case 5:
exit(0);
break;
default: printf("\nInvalid choice:\n");
break;
}
}
getch( );
}
//creating an array
void create()
{
printf("\nEnter the size of the array elements:\t");
scanf("%d", &n); 4
printf("\n Enter the elements for the array:\n");
for(i=0; i<n; i++)
{
scanf ("%d", &a[i]);
}
}
//displaying an array elements
void display()
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
//inserting an element into an array
void insert()
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=n-1; i>=pos; i--)
{
a[i+1]= a[i];
}
a[pos]= val;
n=n+1;
}
Output
--------MENU-----------
1.CREATE
2.DISPLAY
3.INSERT
4.DELETE
5.EXIT
-----------------------
ENTER YOUR CHOICE: 1
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<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top=-1,length=0;
void push();
void pop();
void display();
void pali();
void 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;
}
}
getch( );
return 0;
}
void pali()
{
int digit, j, k, len=top+1,ind=0;
int num[5],rev[5],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
--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 1
Enter the element to be inserted: 1
4. 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<string.h>
#include<math.h>
double compute(double op1, char symbol, double op2)
{
switch(symbol)
{
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case ‘^’: return pow(op1,op2);
case ‘%’: return (int) op1%(int)op2;
}
return 0;
}
intmain()
{
double res, op1, op2, s[20];
char symbol, postfix[20];
int value, i, top = -1;
printf("\nEnter valid postfix expression");
scanf("%s", postfix);
for(i=0; i<strlen(postfix); i++)
{
symbol=postfix[i];
if(isdigit(symbol))
s[++top] = symbol - '0';
else if(isalpha(symbol))
{
printf("enter the value of %c:",symbol);
scanf("%d",&value);
s[++top]=value;
}
else
{
op2 = s[top--];
op1 = s[top--];
res = compute(op1, symbol, op2);
s[++top] = res;
}
}
printf("the result is %f\n",res);
return 0;
}
// Towers of Hanoi
#include <stdio.h>
void towers(int, char, char, char);
intmain()
{
intnum;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'B', 'C');
return 0;
}
void towers(intnum, char frompeg, char auxpeg, char topeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, topeg, auxpeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, frompeg, topeg);
}
Output
5.Design, Develop and Implement a menu driven Program in C for the following
operations onSingly 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 and Deletion at End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Exit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>int
count=0;
struct node
{
intsem, 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 delete_end()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
return 0;
}
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 delete_front()
{
struct node *temp;
temp=first; i
f(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:
delete_end();
break;
case 5:
insert_atfirst();
break;
case 6:
delete_front();
break;
case 7:
exit(0);
default: printf("wrong choice\n");
}
}
}
Output
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
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 3
Enter usn,name, branch, sem, phno of student : 001 raj CSE 3 111
Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
001 raj CSE 3 111
No of students = 3
Enter choice : 4
001 raj CSE 3 111
Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 5
Enter usn,name, branch, sem, phno of student :
003 harsh cse 3 111
Enter choice : 2
Linked list elements from begining :
003 harsh cse 3 111
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 3
Enter choice : 6
003 harsh cse 3 111
Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 7
6. 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, Ph No.
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>
#include<string.h>
int count=0;
struct node
{
struct node *prev;
intssn,phno;
float sal;
char name[20],dept[10],desg[20];
struct node *next;
}*first,*temp,*last,*temp1;
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 insert_beg()
{
if (first == NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
temp->next = first;
first ->prev =temp;
first = temp;
}
}
void insert_end()
{
if(first ==NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
last ->next = temp;
temp->prev = last;
last = temp;
}
}
void display_beg()
{
temp1 =first;
if(temp1== NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp1!= NULL)
{
printf("%d %s %s %s %f %d\n", temp1->ssn, temp1->name,temp1->dept,temp1->desg,
temp1->sal, temp1->phno );
temp1 = temp1->next;
}
printf(" No of employees = %d ", count);
}
intdeleteend()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
return 0;
}
else
{
temp1=last->prev;
temp1->next=NULL;
printf("%d %s %s %s %f %d\n", last->ssn, last ->name,last ->dept, last ->desg,last ->sal,
last ->phno );
free(last);
}
count--;
return 0;
}
int delete_beg()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
}
else
{
first =first ->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()
{
intch,n,i;
first=NULL;
temp = last = 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");
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 employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insertend();
break;
case 2:
displaybeg();
break; case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default: printf("wrong choice\n");
}
}
}
8. 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.
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. Delete an element(ELEM) from BST
e. Exit
# include <stdio.h>
# include <stdlib.h>
struct BST
{
int item;
struct BST *llink, *rlink;
};
typedefstruct BST* NODE;
NODE insert(NODE);
void inorder(NODE);
void preorder(NODE);
void postorder(NODE);
NODE search(NODE, int);
NODE Delete(NODE, int);
void main()
{
int choice, key;
NODE root = NULL, tmp, parent;
while(1)
{
printf("\n1.Create");
printf("\n2.Traverse the Tree in Preorder, Inorder, Postorder");
printf("\n3.Search");
printf("\n4.Delete an element from the Tree");
printf("\n5.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = insert(root);
break;
case 2:
if (root == NULL)
printf("Tree Is Not Created");
else
{
printf("\nTheInorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThePostorder display : ");
postorder(root);
}
break;
case 3:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key);
if(tmp == NULL)
printf("Element does not exists\n");
else
printf("\nThe element %d found", tmp->item);
break;
case 4: printf("\nEnter Element to be deleted :");
scanf("%d", &key);
root = Delete(root, key);
break;
default: exit(0);
}
}
}
/* This function is for creating a binary search tree */
NODE insert(NODE root)
{
NODE temp, cur, prev;
int item;
printf("\nEnter The Element ");
scanf("%d", &item);
temp = (NODE) malloc(sizeof(struct BST));
temp->llink = NULL;
temp->rlink = NULL;
temp->item = item;
if (root == NULL)
return temp;
prev = NULL;
cur = root;
while(cur != NULL)
{
prev = cur;
if (item < cur-> item)
cur = cur->llink;
else
cur = cur->rlink;
}
if (item <prev->item)
prev->llink = temp;
else
prev->rlink = temp;
return root;
}
/* This function displays the tree in inorder fashion */
void inorder(NODE root)
{
if (root != NULL)
{
inorder(root->llink);
printf("%d\t", root->item);
inorder(root->rlink);
}
}
/* This function displays the tree in preorder fashion */
void preorder(NODE root)
{
if (root != NULL)
{
printf("%d\t", root->item);
preorder(root->llink);
preorder(root->rlink);
}
}
/* This function displays the tree in postorderfashion */
void postorder(NODE root)
{
if (root != NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t", root->item);
}
}
NODE search(NODE root, int key)
{
NODE cur;
if(root == NULL)
return NULL;
cur = root;
while(cur != NULL)
{
if(key == cur->item)
return cur;
if(key<cur->item)
cur = cur->llink;
else
cur = cur->rlink;
}
return NULL;
}
NODE Delete (NODE root, int item)
{
NODE cur, parent= NULL, suc,q;
if(root == NULL)
{
printf("Tree is empty,Element does not exists\n");
return root;
}
cur=root;
while(cur!=NULL)
{
if(item==cur->item) break;
parent=cur;
cur=(item<cur->item)?cur->llink:cur->rlink;
}
if(cur==NULL)
{
printf("Item not found\n");
return root;
}
if(cur->llink ==NULL)
q=cur->rlink;
else if(cur->rlink==NULL)
q=cur->llink;
else
{
suc = cur->rlink;
while(suc->llink != NULL)
suc = suc ->llink;
suc->llink = cur->llink;
q = cur->rlink;
}
if(parent == NULL)
return q;
if(cur == parent->llink)
parent->llink = q;
else
parent->rlink = q;
free(cur);
return root;
}
Output
1.Create
2.Traverse the tree in preorder, inorder, postorder
3.Search
4.Delete an element from the tree
5.Exit
9. 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 BFS / DFS
method.
#include<stdio.h>
#include<stdlib.h>
intn,a[10][10],i,j,source,s[10],reach[10],choice,count=0;
OUTPUT:-
Enter the number of nodes : 5
Enter the adjacency matrix
01010
00010
01000
00001
00100
Enter your choice
1.BFS
2.DFS
3.Exit
1
Enter the source :2
The node 1 is not reachable
The node 2 is reachable
The node 3 is reachable
The node 4 is reachable
The node 5 is reachable
Enter your choice
1.BFS
2.DFS
3.Exit
2
1->2
2->4
4->5
5->3
The graph is connected.
Enter your choice
1.BFS
2.DFS
3.Exit
3