DS Unit 1 Lab Programs
DS Unit 1 Lab Programs
Write a menu driven program that implement following operations (using separate
b) Delete an element from a given whose value is given or whose position is given.
Program in C
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int n = 0;
void create();
void display();
void insert();
void delete();
void findlocation();
void main()
int choice;
while(1)
printf("\n\n~~~~MENU~~~~");
printf("\n=>6. Exit");
scanf("%d", &choice);
switch(choice)
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: delete();
break;
case 5: findlocation();
break;
case 6: exit(1);
break;
default: printf("\nPlease enter a valid choice:");
void create()
int i;
scanf("%d", &n);
scanf("%d", &a[i]);
void display()
int i;
if(n == 0)
return;
}
printf("\nArray elements are: ");
for(i=0; i<n;i++)
void insert()
int i;
if(n == MAX)
return;
do
scanf("%d", &pos);
scanf("%d", &elem);
a[i+1] = a[i];
a[pos] = elem;
n = n+1;
display();
void delete()
int i;
if(n == 0)
return;
do
scanf("%d", &pos);
}while(pos>=n);
elem = a[pos];
printf("\nDeleted element is : %d \n", elem);
a[i] = a[i+1];
n = n-1;
display();
void findlocation()
scanf("%d", &element);
if(a[i] == element)
position = i+1;
break;
} //display();
~~~~MENU~~~~
=>6. Exit
~~~~MENU~~~~
=>6. Exit
Enter your choice: 2
~~~~MENU~~~~
=>6. Exit
~~~~MENU~~~~
~~~~MENU~~~~
=>6. Exit
Deleted element is : 3
~~~~MENU~~~~
=>6. Exit
~~~~MENU~~~~
=>6. Exit
Write a program to demonstrate the use of linear and binary search to find a given element
in an array.
To search an element in a given array, there are two popular algorithms available:
1. Linear Search
2. Binary Search
Linear Search
#include<stdio.h>
#include<conio.h>
void main()
int arr[30];
scanf("%d",&size);
scanf("%d",&arr[i]);}
scanf("%d",&search);
{
if(search==arr[i]) // if both search element value is matched with the
// array value
break;
}getch();
Output:
-- Linear Search --
Enter 1 element : 6
Enter 2 element : 8
Enter 3 element : 2
Enter 4 element : 5
Binary Search
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements");
scanf("%d",&n);
printf("Enter %d integers", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at the position %d ", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list", key);
return 0;
}
Output:
Enter 3 integers 2 4 7
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at the desired
location\n4.Delete from Beginning\n5.Delete from last\n6.Delete node after
specified location\n7.Search for an element\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("\n Please enter valid choice..\n");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted\n");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted\n");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert\n");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform
deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete\n");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("\nNothing to print\n");
}
else
{
printf("\n The Linked List is . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
} }
Output
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value?
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
3
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
C++ Program
/*
#include<iostream>
#include<cstdio>
#include<cstdlib>
/*
* Node Declaration
*/
struct node
int info;
}*start;
/*
* Class Declaration
*/
class single_llist
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void search();
void update();
void reverse();
void display();
single_llist()
start = NULL;
};
/*
*/
main()
{
single_llist sl;
start = NULL;
while (1)
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"7.Search Element"<<endl;
cout<<"10.Exit "<<endl;
switch(choice)
case 1:
sl.insert_begin();
cout<<endl;
break;
case 2:
sl.insert_last();
cout<<endl;
break;
case 3:
sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<endl;
break;
case 5:
sl.delete_pos();
break;
case 6:
sl.update();
cout<<endl;
break;
case 7:
sl.search();
cout<<endl;
break;
case 8:
sl.display();
cout<<endl;
break;
case 9:
sl.reverse();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
/*
* Creating Node
*/
node *single_llist::create_node(int value)
if (temp == NULL)
return 0;
else
temp->info = value;
temp->next = NULL;
return temp;
/*
*/
void single_llist::insert_begin()
int value;
cin>>value;
temp = create_node(value);
if (start == NULL)
start = temp;
start->next = NULL;
else
p = start;
start = temp;
start->next = p;
}
/*
*/
void single_llist::insert_last()
int value;
cin>>value;
temp = create_node(value);
s = start;
s = s->next;
temp->next = NULL;
s->next = temp;
}
/*
*/
void single_llist::insert_pos()
cin>>value;
temp = create_node(value);
cin>>pos;
int i;
s = start;
while (s != NULL)
s = s->next;
counter++;
}
if (pos == 1)
if (start == NULL)
start = temp;
start->next = NULL;
else
ptr = start;
start = temp;
start->next = ptr;
s = start;
ptr = s;
s = s->next;
ptr->next = temp;
temp->next = s;
else
/*
*/
void single_llist::sort()
int value;
if (start == NULL)
{
cout<<"The List is empty"<<endl;
return;
ptr = start;
value = ptr->info;
ptr->info = s->info;
s->info = value;
ptr = ptr->next;
/*
* Delete element at a given position
*/
void single_llist::delete_pos()
if (start == NULL)
cout<<"List is empty"<<endl;
return;
cin>>pos;
s = start;
if (pos == 1)
start = s->next;
else
{
while (s != NULL)
s = s->next;
counter++;
s = start;
ptr = s;
s = s->next;
ptr->next = s->next;
else
free(s);
cout<<"Element Deleted"<<endl;
/*
*/
void single_llist::update()
if (start == NULL)
cout<<"List is empty"<<endl;
return;
cin>>pos;
cin>>value;
if (pos == 1)
start->info = value;
else
if (s == NULL)
return;
s = s->next;
s->info = value;
cout<<"Node Updated"<<endl;
}
/*
* Searching an element
*/
void single_llist::search()
if (start == NULL)
cout<<"List is empty"<<endl;
return;
cin>>value;
s = start;
while (s != NULL)
pos++;
if (s->info == value)
flag = true;
s = s->next;
if (!flag)
/*
*/
void single_llist::reverse()
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
if (start->next == NULL)
return;
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
start = ptr2;
}
/*
*/
void single_llist::display()
if (start == NULL)
return;
temp = start;
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
Experiment-4
a) Bubble Sort.
b) Quick Sort
Bubble Sort:
#include <stdio.h>
int main()
{
int arr[100], i, n, step, temp;
// ask user for number of elements to be sorted
printf("Enter the number of elements to be sorted: ");
scanf("%d", &n);
// input elements if the array
for(i = 0; i < n; i++)
{
printf("Enter element no. %d: ", i+1);
scanf("%d", &arr[i]);
}
// call the function bubbleSort
bubbleSort(arr, n);
return 0; }
Output:
Quick Sort
#include <stdio.h>
int partition(int a[], int beg, int end);
void quickSort(int a[], int beg, int end);
void main()
{
int i;
int arr[11]={91,24,102,46,66,27,68,88,38,27,30};
quickSort(arr, 0, 10);
printf("\n The sorted array is: \n");
for(i=0;i<11;i++)
printf(" %d\t", arr[i]);
}
int partition(int a[], int beg, int end)
{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quickSort(a, beg, loc-1);
quickSort(a, loc+1, end);
}
}
Output:
The sorted array is:
24 27 27 30 38 46 66 68 88 91 102
Program2
#include <stdio.h>
int main()
int list[60];
int size, i;
scanf("%d", &size);
scanf("%d", &list[i]);
printf("\n");
return 0;
pivot = low;
i = low;
j = high;
while (i < j)
i++;
j--;
}
if (i < j)
temp = list[i];
list[i] = list[j];
list[j] = temp;
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, j + 1, high);
Output:
Enter the number of elements: 5
92
1 2 3 7 92