DS Lab Assignment
DS Lab Assignment
int arr[20];
printf("Enter 6 elements to the array \n");
for (int i = 0; i < 6; i++)
{
scanf("%d", &arr[i]);
}
printf("Array created\n");
scanf("%d", &key);
n = deleteElement(arr, n, key);
return 0;
}
int insertSorted(int arr[], int n,int a,int c)
{
if (n >= c)
return n;
arr[n] = a;
return (n + 1);
}
if (pos == -1)
{
printf("Element not found");
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
return -1;
}
Output of the program:
Ques 2: write a program to implement addition of 2d arrays.
#include<stdio.h>
int main ()
{
int A[3][3],B[3][3],C[3][3],i,j;
printf("Enter the elements of 2D Array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter the elements of 2D Array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&B[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
C[i][j]=A[i][j]+B[i][j];
}
return 0;
}
Output of the program:
Ques 3: Write a program to implement multiplication of 2D arrays.
#include <stdio.h>
int main()
{
int r, c, r2, c2, i, j, k, sum = 0;
int a[10][10], b[10][10], mul[10][10];
if ( c != r2 )
printf("enter only square matrices for multiplication.\n");
else
{
printf("Enter the elements of second matrix\n");
mul[i][j] = sum;
sum = 0;
}
}
return 0;
}
l = 0;
while (s1[l] != '\0')
{
++l;
}
int main()
{
char c[1000];
int i,n,x=0;
for(i=0;i<n/2;i++)
{
if(c[i]==c[n-i-1])
x++;
}
if(x==i)
printf("The given string is palindrome");
else
printf("The given string is not palindrome");
return 0;
}
i)creation
ii)Insertion
iii)Deletion
iv)Traversal
#include <stdlib.h>
#include <stdio.h>
void create();
void display();
void insert_pos();
void delete_pos();
struct node
{
int info;
struct node *next;
};
struct node *start = NULL;
int main()
{
int choice;
while (1)
{
case 4:
delete_pos();
break;
case 9:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}
}
return 0;
}
void create()
{
struct node *temp, *ptr;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\nOut of Memory Space:");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d", &temp->info);
temp->next = NULL;
if (start == NULL)
{
start = temp;
}
else
{
ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
void display()
{
struct node *ptr;
if (start == NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr = start;
printf("\nThe List elements are:\n");
while (ptr != NULL)
{
printf("%d\t", ptr->info);
ptr = ptr->next;
}
}
}
void insert_pos()
{
struct node *ptr, *temp;
int i, pos;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("nEnter the position for the new node to be inserted:\t");
scanf("%d", &pos);
printf("\nEnter the data value of the node:\t");
scanf("%d", &temp->info);
temp->next = NULL;
if (pos == 0)
{
temp->next = start;
start = temp;
}
else
{
for (i = 0, ptr = start; i < pos - 1; i++)
{
ptr = ptr->next;
if (ptr == NULL)
{
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->next = ptr->next;
ptr->next = temp;
}
}
void delete_pos()
{
int i, pos;
struct node *temp, *ptr;
if (start == NULL)
{
printf("nThe List is Empty:n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d", &pos);
if (pos == 0)
{
ptr = start;
start = start->next;
printf("\nThe deleted element is:%d\t", ptr->info);
free(ptr);
}
else
{
ptr = start;
for (i = 0; i < pos; i++)
{
temp = ptr;
ptr = ptr->next;
if (ptr == NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next = ptr->next;
printf("\nThe deleted element is:%d\t", ptr->info);
free(ptr);
}
}
}
Output of the program:
Ques 8: Write a program that uses functions to perform the
following operations on Doubly linked list.
i) Creation
ii) Insertion
iii) Deletion
iv) Traversal
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int n;
struct node *next;
} * h, *temp, *temp1, *temp2, *temp4;
void insert1();
void insert2();
void traversebeg();
void delete ();
int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
delete ();
break;
case 4:
traversebeg();
break;
case 5:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}
/* TO create an empty node */
void create()
{
int data;
if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");
Output of program:
Ques 9: Write a program that uses functions to perform the
following operations on Circular linked list.
i) Creation
ii) Insertion
iii) Deletion
iv) Traversal
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert();
void lastinsert();
void begin_delete();
void last_delete();
void display();
void main()
{
int choice = 0;
while (choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beg
inning\n4.display the list \n5.Exit.\n");
printf("\nEnter your choice?\n");
scanf("\n%d", &choice);
switch (choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
head = ptr;
ptr->next = head;
}
else
{
temp = head;
while (temp->next != head)
temp = temp->next;
ptr->next = head;
temp->next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
getch();
}
}
void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
head = ptr;
ptr->next = head;
}
else
{
temp = head;
while (temp->next != head)
{
temp = temp->next;
}
temp->next = ptr;
ptr->next = head;
}
printf("\nnode inserted\n");
getch();
}
}
void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\nUNDERFLOW");
}
else if (head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
getch();
}
else
{
ptr = head;
while (ptr->next != head)
ptr = ptr->next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
getch();
}
}
void display()
{
struct node *ptr;
ptr = head;
if (head == NULL)
{
printf("\nnothing to print");
getch();
}
else
{
printf("\n printing values ... \n");
printf("%d\n", ptr->data);
ptr = ptr->next;
}
printf("%d\n", ptr->data);
getch();
}
}
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void main()
{
int ch;
while(1)
{
printf("\n---Stack Menu---\n");
printf("\n\n1.Push\n2.Pop\n3.Exit");
printf("\n\tEnter your choice from above :");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: exit(0);
default: printf("\nWrong Choice!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
printf("Node is Inserted\n");
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
int main()
{
int choice, value;
printf("\n---Menu---\n");
while(1){
printf("1. Push\n2. Pop\n3. Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop();
break;
case 3: exit(0);
break;
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char expression[50];
char *e, x;
printf("Enter the expression : ");
scanf("%s",expression);
printf("\n");
e = expression;
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());
}return 0;
}
int main(void)
{
int a;
printf("Enter total no. of terms u want to print: ");
scanf("%d", &a);
for(int i = 0; i < a; i++)
{
printf("%d ", fseries(i));
}
return 0;
}
int fseries(int x)
{
if(x == 0 || x == 1)
{
return x;
}
else
{
return fseries(x-1) + fseries(x-2);
}
getch();
return 0;
}
#include <stdio.h>
#define MAX 10
void insert();
void delete();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int c;
while (1)
{
printf("\n---Queue Menu---\n");
printf("\n\n1.Insert element\n2.delete element\n3.Exit");
printf("\n\tEnter your choice from above :");
scanf("%d", &c);
switch (c)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Insert the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]
);
front = front + 1;
}
}
void create()
{
front = rear = NULL;
}
int empty()
{
if(front == NULL)
{
return 1;
}
else
return 0;
}
int main()
{
int num,choice;
while(1)
{
printf("\n--- Queue Menu ---");
printf("\n1. Enqueue\n2. Dequeue\n3. Exit\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("\n Insert the element in Queue : ");
scanf("%d",&num);
enqueue(num);
break;
case 2:
if(!(empty()))
printf("\nDequeued element : %d",dequeue());
break;
case 3:
exit(1);
break;
default:
printf("\nwrong choice\n");
}}
return 0;
}
Output of the program:
Ques 18: Write a program that implement circular Queue using
arrays.
# include<stdio.h>
# define MAX 10
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
int main()
{
int choice,item;
do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
switch(choice)
{
case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &item);
insert(item);
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=4);
return 0;
}
int main()
{
int a[50],i,x,n;
printf("Enter the no. of elements\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("%d is found at location %d",x,i+1);
else
printf("Element not found");
return 0;
}
#include <stdio.h>
int main()
{
int a[50], n, i, j, pos, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d Numbers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
pos=i;
for(j = i + 1; j < n; j++)
{
if(a[pos] > a[j])
pos=j;
}
if(pos != i)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf("Sorted Array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
if(x<y){
pivot=x;
i=x;
j=y;
while(i<j){
while(a[i]<=a[pivot] && i<y)
i++;
while(a[j]>a[pivot])
j--;
if(i<j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,x,j-1);
quicksort(a,j+1,y);
}
}
int main()
{
int i, n, a[50];
quicksort(a,0,n-1);
int main() {
int i, j, n, temp, arr[50];
return 0;
}
#include<stdio.h>
int main()
{
int a[50],n,i;
printf("Enter no of elements:\n");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
void main()
{
int heap[50],n,i,end,temp;
printf("Enter no. of elements:\n");
scanf("%d",&n);
printf("\nEnter elements:\n");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
heap[0]=n;
create(heap);
while(heap[0] > 1)
{
end=heap[0];
temp=heap[1];
heap[1]=heap[end];
heap[end]=temp;
heap[0]--;
adjust_down(heap,1);
}
Insert an Element
#include<stdio.h>
#include<stdlib.h>
void insert(int);
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
insert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void insert(int item)
{
struct node *ptr, *parentptr , *nodeptr;
ptr = (struct node *) malloc(sizeof (struct node));
if(ptr == NULL)
{
printf("can't insert elements");
}
else
{
ptr -> data = item;
ptr -> left = NULL;
ptr -> right = NULL;
if(root == NULL)
{
root = ptr;
root -> left = NULL;
root -> right = NULL;
}
else
{
parentptr = NULL;
nodeptr = root;
while(nodeptr != NULL)
{
parentptr = nodeptr;
if(item < nodeptr->data)
{
nodeptr = nodeptr -> left;
}
else
{
nodeptr = nodeptr -> right;
}
}
if(item < parentptr -> data)
{
parentptr -> left = ptr;
}
else
{
parentptr -> right = ptr;
}
}
printf("Node Inserted");
}
}
// Main Function
int main()
{
int n;
root = NULL;
printf("\nEnter the number of nodes : ");
scanf("%d", &n);
int i;
int data;
printf("\nInput the nodes of the binary search tree : ");
if(n > 0)
{
scanf("%d", &data);
root = insert(root, data);
}
for(i = 1; i < n; i++)
{
scanf("%d", &data);
insert(root, data);
}
printf("\nInorder traversal of the BST : ");
inorder(root);
printf("\n");
int del_ele;
printf("\nEnter the node to be deleted : ");
scanf("%d", &del_ele);
delete_node(root, del_ele);
printf("\nInorder traversal after deletion : ");
inorder(root);
printf("\n");
return 0;
}
struct TreeNode {
int data;
struct TreeNode *leftChildNode;
struct TreeNode *rightChildNode;
};
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
void printPostorder(struct node* node)
{
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
void printInorder(struct node* node)
{
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
void printPreorder(struct node* node)
{
if (node == NULL)
return;
printf("%d ", node->data);
printPreorder(node->left);
printPreorder(node->right);
}
int main()
{
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
getchar();
return 0;
}