Вы находитесь на странице: 1из 32

Input/Output

Program no.:-1

Write a program for binary search

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
printf("Enter total number of elements :");
scanf("%d",&n);
printf("Enter %d number :", n);
for (i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter a number to find :");
scanf("%d", &search);
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
printf("%d found at location %d\n"search,
middle+1);
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
printf("Not found! %d is not present in the
list.",search);
}
getch(); }
Input/Output
Program no.:-2

Write a program for Insertion Sort

#include<stdio.h>
#include<conio.h>
int main()
{
int arr[50], size, i, j, k, element, index;
printf("Enter Array Size: ");
scanf("%d", &size);
printf("Enter %d Array Elements: ", size);
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
for(i=1; i<size; i++)
{
element = arr[i];
if(element<arr[i-1])
{
for(j=0; j<=i; j++)
{
if(element<arr[j])
{
index = j;
for(k=i; k>j; k--)
arr[k] = arr[k-1];
break;
}
}
}
else
continue;
arr[index] = element;
}
printf("\nSorted Array:\n");
for(i=0; i<size; i++)
printf("%d ", arr[i]);
getch();
return 0;
}
Input/Output
program no.:-3
Write a program for Selection Sort
#include<stdio.h>
#include<conio.h>
int main()
{
int size, arr[50], i, j, temp, small, count=0, index;
printf("Enter size for Array: ");
scanf("%d", &size);
printf("Enter %d array elements: ", size);
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
for(i=0; i<(size-1); i++)
{
small = arr[i];
for(j=(i+1); j<size; j++)
{
if(small>arr[j])
{
small = arr[j];
count++;
index = j;
}
}
if(count!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
count=0;
}
printf("\nNow the Array after sorting is:\n");
for(i=0; i<size; i++)
printf("%d ", arr[i]);
getch();
return 0;
}
Input/Output
Program no.:-4
Write a program for Bubble Sort
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[50], i, j, n, temp;
printf("Enter total number of elements to store: ");
scanf("%d", &n);
printf("Enter %d elements:", n);
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
printf("\nSorting array using bubble sort
technique...\n");
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("All Array elements sorted successfully!\n");
printf("Array elements in ascending order:\n\n");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
getch();
return 0;
}
STACK ELEMENTS :44->33->22->11->

STACK OPTIONS

0: Exit

1: Add item

2: Remove item

Enter choice :::1

Enter an item to insert:55

Input/Output
Program no.:-5
Write a program to implement stack and its operation
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int STACK[MAX],TOP;
void display(int []);
void PUSH(int [],int);
void POP (int []);
void main()
{
int ITEM=0;
int choice=0;
TOP=-1;
while(1)
{
printf("Enter Choice (1: display, 2: insert (PUSH), 3:
remove(POP)), 4:Exit..:");
scanf("%d",&choice);
switch(choice)
{
case 1:
display(STACK);
break;
case 2:
printf("Enter Item to be insert :");
scanf("%d",&ITEM);
PUSH(STACK,ITEM);
break;
case 3:
POP(STACK);
break;
case 4:
exit(0);
default:
printf("\nInvalid choice.");
break;
}
getch();
}
}
void display(int stack[])
{
int i=0;
if(TOP==-1)
{
printf("Stack is Empty .\n");
return;
}
printf("%d <-- TOP ",stack[TOP]);
for(i=TOP-1;i >=0;i--)
{
printf("\n%d",stack[i]);
}
printf("\n\n");
}
void PUSH(int stack[],int item)
{
if(TOP==MAX-1)
{
printf("\nSTACK is FULL CAN't ADD ITEM\n");
return;
}
TOP++;
stack[TOP]=item;
}
void POP(int stack[])
{
int deletedItem;
if(TOP==-1)
{
printf("STACK is EMPTY.\n");
return;
}
deletedItem=stack[TOP];
TOP--;
printf("%d deleted successfully\n",deletedItem);
return;
}
Input/Output

The sorted array is:


23
23
23
34
45
65
67
89
90
101
Program no.:-6
Write a program for 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[10]={90,23,101,45,65,23,67,89,34,23};
quickSort(arr, 0, 9);
printf("\n The sorted array is: \n");
for(i=0;i<10;i++)
printf(" %d\t", arr[i]);
}
int partition(int a[], int beg, int end)
{

int left, right, temp, loc, flag;


loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}
void quickSort(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);
}
}
Program no.:-7
Write a program for merge sort
#include <iostream>
using namespace std;

void merge(int *,int, int , int );


void mergesort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
int a[30], i, b[30];
cout<<"enter the number of elements:";
for (i = 0; i <= 5; i++) { cin>>a[i];
}
mergesort(a, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<a[i]<<"\t";
}
cout<<"enter the number of elements:";
for (i = 0; i < 5; i++) { cin>>b[i];
}
mergesort(b, 0, 4);
cout<<"sorted array:\n";
for (i = 0; i < 5; i++)
{
cout<<b[i]<<"\t";
}
getch();}
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter the Choice:1
Enter no 2:54
Enter the Choice:1
Enter no 3:98
Enter the Choice:1
Enter no 4:234
Enter the Choice:3

Queue Elements are:


10
54
98
234

Enter the Choice:2

Deleted Element is 10
Enter the Choice:3

Queue Elements are:


54
98
234

Enter the Choice:4


Program no.:-8
Write a program to implement queue and its operation
#include<stdio.h>

#include<conio.h>

#define n 5

voidmain()

intqueue[n],ch=1,front=0,rear=0,i,j=1,x=n;

printf("Queue using Array");

printf("\n1.Insertion \n2.Deletion \n3.Display\n4.Exit");

while(ch)

printf("\nEnter the Choice:");

scanf("%d",&ch);

switch(ch)

{ case1:

if(rear==x)

printf("\n Queue is Full");

else

printf("\n Enter no %d:",j++);

scanf("%d",&queue[rear++]);

break;

case2:

if(front==rear)

{
printf("\n Queue is empty");

else

printf("\n Deleted Element is


%d",queue[front++]);

x++;}

break;

case3:

printf("\n Queue Elements are:\n ");

if(front==rear)

printf("\n Queue is Empty");

else

for(i=front; i<rear; i++)

{ printf("%d",queue[i]);

printf("\n"); }

break;

case4:

exit(0);

default:

printf("Wrong Choice: please see the


options");

getch();}
Input/Output
1.Insert

2.Display

3.Delete
Program no.:-9
Write a program to implement circular queue and its
operations
#include<stdio.h>
#include<cstdlib>
#define max 6
int q[10],front=0,rear=-1;
int main()
{
int ch;
void insert();
void delet();
void display();

printf("\nCircular Queue Operations\n");


printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
while(1)
{
printf("Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4: exit(0);
default:printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is Overflow\n");
else
{
printf("Insert Element :");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is Underflow\n");
exit(0);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted Element is : %d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is Underflow\n");
exit(0);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\n \t%d",q[j]);
printf("\nRear is at %d\n",q[rear]);
printf("\nFront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nRear is at %d\n",q[rear]);
printf("\nFront is at %d\n",q[front]);
}
printf("\n");
}
Input/Output

Linked List : To create and display Singly


Linked List :
----------------------------------------------
---------------
Input the number of nodes : 3

Input data for node 1 : 5

Input data for node 2 : 6

Input data for node 3 : 7

Data entered in the list :

Data = 5

Data = 6

Data = 7
Program no.:-10
Write a program to implement singly linked list
#include <stdio.h>
#include <stdlib.h>

struct node
{
int num;
struct node *nextptr;
}*stnode;

voidcreateNodeList(int n);
voiddisplayList();

intmxain()
{
int n;
printf("\n\n Linked List : To create and display
Singly Linked List :\n");
printf("--------------------------------------------
-----------------\n");

printf(" Input the number of nodes : ");


scanf("%d",&n);
createNodeList(n);
printf("\n Data entered in the list : \n");
displayList();
return0;
}
voidcreateNodeList(int n)
{
struct node *fnNode,*tmp;
int num, i;
stnode=(struct node *)malloc(sizeof(struct node));

if(stnode==NULL)//check whether the fnnode is NULL and if so


no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{

printf(" Input data for node 1 : ");


scanf("%d",&num);
stnode->num = num;
stnode->nextptr=NULL;
tmp=stnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode=(struct node *)malloc(sizeof(struct node));
if(fnNode==NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d",&num);

fnNode->num = num;
fnNode->nextptr=NULL;
tmp->nextptr=fnNode;
tmp=tmp->nextptr;
}
}
}
}
voiddisplayList()
{
struct node *tmp;
if(stnode==NULL)
{
printf(" List is empty.");
}
else
{
tmp=stnode;
while(tmp!=NULL)
{
printf(" Data = %d\n",tmp->num);
tmp=tmp->nextptr;
}
}
}
Enter the element in the list : 1

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 2

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 3

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 4

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 5

If you wish to add more data on the list enter 1 : 0


The list is as follows :
1->2->3->4->5->1
Program no.:-11
Write a program to implement circular linked list
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node * next;
}*head;

voidcreateList(int n);
voiddisplayList();

intmain()
{
int n, data, choice=1;

head = NULL;

while(choice != 0)
{
printf("============================================\n");
printf("CIRCULAR LINKED LIST PROGRAM\n");
printf("============================================\n");
printf("1. Create List\n");
printf("2. Display list\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice)
{
case1:
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
displayList();
break;
case 0:
break;
default:

printf("Error! Invalid choice. Please choose between 0-2");


}

printf("\n\n\n\n\n");
}

return 0;
}
voidcreateList(int n)
{
int i, data;
struct node *prevNode, *newNode;

if(n >= 1)
{

head = (struct node*)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");


scanf("%d", &data);

head->data = data;
head->next = NULL;

prevNode = head;

for(i=2; i<=n; i++)


{
newNode = (struct node*)malloc(sizeof(structnode));

printf("Enter data of %d node: ", i);


scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;

prevNode->next = newNode;

prevNode = newNode;
}
prevNode->next = head;

printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n");


}
}

void displayList()
{
struct node *current;
int n=1;

if(head == NULL)
{
printf("List is empty.\n");
}
else
{

printf("DATA IN THE LIST:\n");

do {
printf("Data %d = %d\n", n, current->data);

current = current->next;
n++;
}while(current != head);
}
}

Вам также может понравиться