DS Lab Manual-Final
DS Lab Manual-Final
DS Lab Manual-Final
1. Design, Develop and Implement a menu driven Program in C for the following
a) To create an Array of N Integer Elements and store n values.
• Inserting an Element (ELEM) at a given valid Position (POS)
• Deleting an Element at a given valid Position POS)
• Display of Array Elements
• Exit.
Support the program with functions for each of the above operations.
#include<stdio.h>
#include<stdlib.h>
int a[10], pos, elem;
int n = 0;
void create();
void display();
void insert();
void del();
void main()
{
int choice;
while(1)
{
printf("\n\n~~~~MENU~~~~");
printf("\n=>1. Create an array of N integers");
printf("\n=>2. Display of array elements");
printf("\n=>3. Insert ELEM at a given POS");
printf("\n=>4. Delete an element at a given POS");
printf("\n=>5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4:del();
break;
case 5:exit(1);
break;
default:printf("\nPlease enter a valid choice:");
}
}
}
void create()
{
int i;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
}
void display()
{
int i;
if(n == 0)
{
printf("\nNo elements to display");
return;
}
printf("\nArray elements are: ");
for(i=0; i<n;i++)
printf("%d\t ", a[i]);
}
void insert()
{
int i;
if(n == 5)
{
printf("\nArray is full. Insertion is not possible");
return;
}
do
{
printf("\nEnter a valid position where element to be inserted: ");
scanf("%d", &pos);
}while(pos > n);
printf("\nEnter the value to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos ; i--)
{
a[i+1] = a[i];
}
a[pos] = elem;
n = n+1;
display();
}
void del()
{
int i;
if(n == 0)
{
printf("\nArray is empty and no elements to delete");
return;
}
do
{
printf("\nEnter a valid position from where element to be deleted: ");
scanf("%d", &pos);
}while(pos>=n);
elem = a[pos];
printf("\nDeleted element is : %d \n", elem);
for( i = pos; i< n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
display();
}
b] To create student structure with fields Roll No, Name, Semester, marks in 3 subjects.
And Write functions to
• Enter 5 students’ details and display the same using pointer to structure
• Find Student wise and subject wise total marks and display the same.
ptr=&s;
for(i=0;i<2;i++)
{
printf("\nEnter semester:");
scanf("%d",&ptr->sem);
printf("\nEnter mark of subject1:");
scanf("%f",&ptr->mark1);
printf("Enter mark of subject2:");
scanf("%f",&ptr->mark2);
printf("Enter mark of subject3:");
scanf("%f",&ptr->mark3);
ptr++;
}
ptr=s;
for(i=0;i<2;i++)
{
printf("\nEnter student%d details",i+1);
printf("\nName=%s\n,RollNo=%d\n,Semester=%d\n,Mark1=%f,\nmark2=%f,\nMark3=%f",ptr-
>name,ptr->rollno,ptr->sem,ptr->mark1,ptr->mark2,ptr->mark3);
ptr++;
}
ptr=s;
for(i=0;i<2;i++)
{
printf("\nTotalmarksofstudent %d is:",i+1);
total=ptr->mark1+ptr->mark2+ptr->mark3;
printf("%f",total);
ptr++;
}
ptr=s;
for(i=0;i<2;i++)
{
s1total=s1total+ptr->mark1;
s2total=s2total+ptr->mark2;
s3total=s3total+ptr->mark3;
ptr++;
}
printf("\nSubject 1 total marks %f:",s1total);
printf("\nSubject 2 total marks%f",s2total);
printf("\nSubject 3 total marks%f",s3total);
return 0;
}
2. Design, Develop and Implement a menu driven Program in C for the following
operations on
a) STACK of Integers (Array with structure Implementation of Stack with size
MAX)
• Push an Element on to Stack.
• Pop an Element from Stack.
• Demonstrate Overflow and Underflow situations on Stack.
• Display the status of Stack .
• Exit
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct stack
{
int top;
int items[MAX];
};
void push(int,struct stack *);
void pop(struct stack *);
void display(struct stack *);
int main()
{
struct stack s;
s.top=-1;
int choice,item;
for(;;)
{
printf("Enter your choice\n");
printf("1 Push\n2 Pop\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n");
scanf("%d",&item);
push(item,&s);
break;
case 2: pop(&s);
break;
case 3: display(&s);
break;
case 4: exit(0);
}
}
return 0;
}
void push(int item,struct stack *s)
{
if(s->top==MAX-1)
printf("The stack is full\n");
else
{
(s->top)++;
s->items[s->top]=item;
}
}
void pop(struct stack *s)
{
int item;
if(s->top==-1)
printf("The stack is empty\n");
else
{
item=s->items[s->top];
(s->top)--;
printf("%d deleted\n",item);
}
}
void display(struct stack *s)
{
int t=s->top;
if(s->top==-1)
printf("The stack is empty\n");
else
{
printf("Elements in the stack are\n");
while(t>-1)
{
printf("%d ",s->items[t--]);
}
printf("\n");
}
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
3. Queue of Integers (Array with structure Implementation of Queue with size MAX)
• Insert an Element in to Queue.
• Delete an Element from Queue.
• Demonstrate Overflow and Underflow situations on Queue.
• Display the status of Queue .
• Exit
Support the program with appropriate functions for each of the above
operations
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
struct queue{
int rear,front;
int q[MAX];
};
void INSERT(int,struct queue *);
if(s->front>s->rear)
printf("Queue is empty\n");
else
{
item=s->q[s->front];
printf("%d deleted\n",item);
(s->front)++;
}
}
void DISPLAY(struct queue *s)
{
int i;
if(s->front>s->rear)
printf("Queue is empty\n");
else
{
printf("Elements in the queue are\n");
for(i=s->front;i<=s->rear;i++)
printf("%d ",s->q[i]);
printf("\n");
}
}
4. Design, Develop and Implement a Program in C for the following Stack Applications
• Evaluation of Suffix expression with single digit operands and operators: +, -
, *, /, %, ^ b
• Solving Tower of Hanoi problem with n disks
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
#define size 10
struct stack
{
int top;
double item[size];
};
double op(char,double,double);
void push(char, struct stack *);
s->top--;
return(ele);
}
double op(char sym, double op1,double op2)
{
switch(sym)
{
case '+':return(op1+op2);
case '-':return(op1-op2);
case '*':return(op1*op2);
case '/':return(op1/op2);
case '&':
case '^':return(pow(op1,op2));
default:return -9999;
}
exit(0);
}
}
int G(char symbol)
{
switch(symbol)
{
case'+':
case'-':return 1;
case'*':
case'/':return 3;
case'^':
case'$':return 6;
case'(':return 9;
case')':return 0;
default:return 7;
}
void infix_postfix(char infix[],char postfix[])
{
int top,j,i;
char s[30],symbol;
top=-1;
s[++top]='#';
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j]=s[top--];
j++;
}
if(F(s[top])!=G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
puts(postfix);
}
void main()
{
char infix[25],postfix[30];
printf("Enter the expression");
gets(infix);
infix_postfix(infix,postfix);
}
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)
• Insert an Element on to Circular QUEUE.
• Delete an Element from Circular QUEUE.
• Demonstrate Overflow and Underflow situations on Circular QUEUE d.
Display the status of Circular QUEUE.
• Exit
Support the program with appropriate functions for each of the above
operations
#include<stdio.h>
#include<stdlib.h>
#define SIZE 3
int items[SIZE];
int front=-1,rear=-1;
int isFull()
{
if((front==rear+1)||(front==0 && rear==SIZE-1))
return 1;
return 0;
}
int isEmpty()
{
if(front==-1)
return 1;
return 0;
}
void insert()
{
int element;
if(isFull())
printf("Queue is full\n");
else
{
printf("Enter the element\n");
scanf("%d",&element);
if(front==-1)
front=0;
rear=(rear+1)%SIZE;
items[rear]=element;
}
}
void delete()
{
int element;
if(isEmpty())
printf("Queue is empty\n");
else
{
element=items[front];
if(front==rear) {front=-1; rear=-1;}
else
front=(front+1)%SIZE;
printf("Deleted element %d \n",element);
}
}
void display()
{
int i;
if(isEmpty())
printf("Queue is empty\n");
else
{
printf("Elements in the queue\n");
for(i=front; i!=rear; i=(i+1)%SIZE)
printf("%d ",items[i]);
printf("%d \n",items[i]);
}
}
int main()
{
int choice;
for(;;)
{
printf("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
}
}
}
7. Design, Develop and Implement a menu driven Program in C for the following
operations on Singly Linked List (SLL) for data of integers.
• Create a SLL of Data using front insertion.
• Display the status of SLL and count the number of nodes in it.
• Demonstration of stack
• Demonstration of Queue.
• Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node * NODE;
NODE getnode()
{
NODE X;
X=(NODE)malloc(sizeof(struct node));
return(X);
}
void freenode(NODE X)
{
free(X);
}
NODE insert_front(NODE first,int item)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return(temp);
}
NODE insert_rear(int item,NODE first)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(first==NULL)
return (temp);
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return (first);
}
NODE delete_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("List is empty\n");
return first;
}
temp=first;
temp=temp->link;
printf("Deleted data is %d\n",first->info);
freenode(first);
return(temp);
}
void display(NODE first)
{
NODE temp;
Int count=0;
if(first==NULL)
{
printf("List is empty\n");
return;
}
printf("Contents of the Linked list are\n");
temp=first;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->link;
count++;
}
printf("\n");
printf(“Number of nodes in the list are:%d”,count);
}
int main()
{
int choice,item;
NODE first=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert rear\n2 Delete rear\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the item\n");
scanf("%d",&item);
first=insert_front(first,item);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
case 4:first=insert_rear(first,item);
break;
case 5:exit(0);
}
}
}
8. Design, Develop and Implement a menu driven Program in C for the following
operations on Circular Singly Linked List (CSLL) for data of integers.
• Create a CSLL of data using front insertion.
• Perform Insertion and Deletion to the right of the given node.
• Perform Insertion and Deletion at end of CSLL.
• Display the status of CSLL and count the number of nodes in it.
• Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
typedef struct node * NODE;
NODE insert_front(int item, NODE head)
{
NODE temp;
temp = getnode(); /* Create a node, insert the item */
temp->info = item;
first = head->link; /* Obtain the address of the first node */
head->link = temp; /* Insert at the front end */
temp->link = first;
return head; /* Return the header node */
}
NODE insert_rear(NODE first,int item)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->data=item;
if(first==NULL)
{
first=newnode;
first->link=newnode;
return first;
}
else
{
NODE temp=first;
while(temp->link!=first)
temp=temp->link;
temp->link=newnode;
newnode->link=first;
return first;
}
}
NODE delete_rear(NODE first)
{
if(first==NULL)
{
printf("List is empty\n");
return first;
}
else
{
int itm;
NODE temp=first;
if(first->link==first)
{
itm=first->data;
free(temp);
first=NULL;
}
else
{
NODE prev=NULL;
while(temp->link!=first)
{
prev=temp;
temp=temp->link;
}
prev->link=first;
itm=temp->data;
free(temp);
}
printf("Deleted data is %d\n",itm);
return first;
}
}
void display(NODE first)
{
Int count=0;
if(first==NULL)
{
printf("List is empty");
return;
}
else
{
NODE temp;
temp=first;
printf("Contents of the Circular Linked list are\n");
while(temp->link!=first)
{
printf("%d ",temp->data);
temp=temp->link;
count++;
}
printf("%d",temp->data);
printf("Number of nodes in the list are %d:",count);
}
}
int main()
{
int choice,val;
NODE first=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert rear\n2 Delete rear\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&val);
first=insert_rear(first,val);
break;
case 2: first=delete_rear(first);
break;
case 3:display(first);
printf("\n");
break;
case 4: printf("Enter the data\n");
scanf("%d",&val);
first=insert_front(first,val);
break;
case 5:exit(0);
}
}
return 0;
}
9. Design, Develop and Implement a menu driven Program in C for the following
operations on Doubly Linked List (DLL) for data of integers.
• Create a DLL of data .
• Perform Insertion and Deletion at End of DLL.
• Perform Insertion and Deletion at Front of DLL.
• Display the status of DLL and count the number of nodes in it.
• Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink, *rlink;
};
typedef struct node * NODE;
NODE insert_front(NODE first,int val)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=val;
newnode->llink=NULL;
if(first==NULL)
{
first=newnode;
newnode->rlink=NULL;
return first;
}
else
{
newnode->rlink=first;
first->llink=newnode;
return newnode;
}
}
NODE insert_rear(int item, NODE first)
{
NODE temp, cur;
temp = getnode(); /*obtain a node from OS */
temp->info = item; /* Insert an item into new node */
temp->link = temp->rlink = NULL;
if (first == NULL) return temp; /* Insert a node for the first time */
/* Get the address of the first node */
cur = first;
/* Find the address of the last node */
while (cur->rlink != NULL)
{
cur = cur->rlink;
}
/* Insert the node at the end */
cur->rlink = temp;
temp->llink = cur;
/* return address of the first node */
return first;
}
NODE second;
if ( first == NULL ) /* Check for empty list */
{
printf("List is empty cannot delete\n");
return NULL; // We can replace NULL with first also
}
if (first ->rlink == NULL) /* Delete if there is only one node */
{
printf(“Item deleted = %d\n”, first->info);
free(first);
return NULL;
}
second = first->rlink; /* Get the address of second node */
second->llink = NULL; /* Make second node as the first node */
printf(“Item deleted = %d\n”, first->info);
free(first); /* Delete the first node */
return second;
}
}
printf("Deleted data is %d\n",del);
return first;
}
void display(NODE first)
{
Int count=0;
if(first==NULL)
{
printf("List is Empty\n");
return;
}
else
{
NODE temp=first;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->rlink;
count++;
}
printf(“No of nodes in the list%d”,count);
printf("\n");
}
}
int main()
{
NODE first=NULL;
int choice,val;
while(1)
{
printf("Enter your choice\n1 Insert front\n2 Delete rear\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&val);
first=insert_front(first,val);
break;
case 2: first=delete_rear(first);
break;
case 3: display(first);
break;
case 4: printf("Enter the data\n");
scanf("%d",&val);
first=insert_rear(first,val);
break;
case 5: first=delete_front(first);
break;
case 6: exit(0);
}
}
return 0;
}
10. Design, Develop and Implement a menu driven Program in C for the following
operations on Binary Search Tree (BST) of integers .
• Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2.
• Traverse the BST in Inorder, Preorder and Post Order.
• Search the BST for a given element (KEY) and report the appropriate
message.
• Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
typedef struct node * NODE;
NODE create_B_Tree()
{
NODE newnode;
int data=0;
newnode=NULL;
printf("Enter data ('0' if no data)");
scanf("%d",&data);
if(data)
{
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=data;
printf("\nLeft child of %d\n",newnode->info);
newnode->lchild=create_B_Tree();
printf("\nRight child of %d\n",newnode->info);
newnode->rchild=create_B_Tree();
}
return newnode;
}
NODE search(int item, NODE root)
{
NODE cur;
if (root == NULL) return NULL; /* empty tree */
cur = root;
while ( cur != NULL ) /* search for the item */
{
if (item == cur->info) return cur; /* If found return the node */
if ( item < cur->info )
cur = cur->llink; /* Search towards left */
else
cur = cur->rlink; /* Search towards right */
}
return NULL; /* Key not found */
}
void pre_order(NODE root)
{
if(root!=NULL)
{
printf("%d\n",root->info);
pre_order(root->lchild);
pre_order(root->rchild);
}
}
void post_order(NODE root)
{
if(root!=NULL)
{
post_order(root->lchild);
post_order(root->rchild);
printf("%d\n",root->info);
}
}
void in_order(NODE root)
{
if(root!=NULL)
{
in_order(root->lchild);
printf("%d\n",root->info);
in_order(root->rchild);
}
}
int main()
{
printf("Create binary tree, start from root\n");
NODE root;
root=create_B_Tree();
while(1)
{
printf("Select mode of traversal for displaying the binary tree\n");
printf("1 Pre-order\n2 Post-order\n3 In-order\n4 Exit\nChoice \n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1:pre_order(root);
printf("\n");
break;
case 2:post_order(root);
printf("\n");
break;
case 3:in_order(root);
printf("\n");
break;
case 4: printf("Enter the item to be searched\n");
scanf("%d",&item);
cur = search(item, root);
if (cur == NULL)
printf(“Item not found\n”);
else
printf(“Item found\n”);
break;
case 5:exit(0);
}
}
return 0;
}