Professional Documents
Culture Documents
C
C
PRACTICAL FILE
RAHUL KHANNA
760/IT/14
IT II
Rahul Khanna
760/IT/14
INDEX
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
Q1. Write a program to search an Element in an array using iterative binary search
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[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.\n", search);
return 0;
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
Q2. Write a program to search an Element in an array using recursive binary search
#include<stdio.h>
#include<stdlib.h>
#define size 10
int binsearch(int[], int, int, int);
int main() {
int num, i, key, position;
int low, high, list[size];
printf("\nEnter the total number of elements");
scanf("%d", &num);
printf("\nEnter the elements of list :");
for (i = 0; i < num; i++) {
scanf("%d", &list[i]);
}
low = 0;
high = num - 1;
printf("\nEnter element to be searched : ");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf("\nNumber present at %d", (position + 1));
} else
printf("\n The number is not present in the list");
return (0);
}
int binsearch(int a[], int x, int low, int high) {
int mid;
if (low > high)
return -1;
mid = (low + high) / 2;
if (x == a[mid]) {
return (mid);
} else if (x < a[mid]) {
binsearch(a, x, low, mid - 1);
} else {
binsearch(a, x, mid + 1, high);
}}
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include <stdio.h>
int main()
{
int data[100],i,n,step,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1])
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include <iostream>
using namespace std;
void print (int [], int);
void selection_sort (int [], int);
int main ()
{
int min_ele_loc;
int ar[] = {10, 2, 45, 18, 16, 30, 29, 1, 1, 100};
cout << "Array initially : ";
print (ar, 10);
selection_sort (ar, 10);
cout << "Array after selection sort : ";
print (ar, 10);
return 0;
}
void selection_sort (int ar[], int size)
{
int min_ele_loc;
for (int i = 0; i < 9; ++i)
{
min_ele_loc = i;
for (int j = i + 1; j < 10; ++j)
{
if (ar[j] < ar[min_ele_loc])
min_ele_loc = j;
}
swap (ar[i], ar[min_ele_loc]);
}
}
void print (int temp_ar[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << temp_ar[i] << " ";
}
cout << endl;
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t
= array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d ", array[c]);
}
return 0;
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}}
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++){
printf("%d ",merge[i]);
}
return 0;
}
void partition(int arr[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
IT II
Rahul Khanna
760/IT/14
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
OUTPUT
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
}}
void delete_element()
{
int element;
if(front==-1)
{
printf("\n Underflow");
}
element=queue[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
printf("\n The deleted element is: %d",element);
}}
void display_queue()
{ int i;
if(front==-1)
printf("\n No elements to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
printf("\t %d",queue[i]); }}
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include<iostream>
using namespace std;
struct node
{
int value;
node * addr;
};
node* createlist()
{
node *head=0;
node*current,*prev;
int value;
cin>>value;
while(value!=-1)
{
current=new node;
current->value=value;
current->addr=0;
if(head==0)
{
head=current;
}
else
{
prev->addr=current;
}
prev=current;
cin>>value;
}
return head;
IT II
Rahul Khanna
760/IT/14
}
node* findk(node*head,int k)
{
while(k&&head)
{
head=head->addr;
k--;
}
return head;
}
void insertatk(node*&head,int k)
{
if(k==0)
{
node*inserted=new node;
inserted->addr=head;
head=inserted;
cin>>inserted->value;
}
else
{
node*inserted=new node;
node*prev=findk(head,k-1);
inserted->addr=prev->addr;
prev->addr=inserted;
cin>>inserted->value;
}
}
int lengthoflinkedlist(node *head)
{
int l=0;
while(head)
{
head=head->addr;
l++;
}
return l;
}
node *reverselinkedlistrec(node * &head,node* current,node*prev)
{
if(current==0)
{
head=prev;
return head;
}
node* nextaddr=current->addr;
head=reverselinkedlistrec(head,nextaddr,current);
current->addr=prev;
return head;
}
void searchel(node * head,int el)
{
IT II
Rahul Khanna
760/IT/14
while(head)
{
if(head->value==el)
{
cout<<"found";
return;
}
head=head->addr;
}
cout<<"not found";
return;
}
node* middle(node * head)
{
node *sptr=head,*fptr=head;
while(fptr&&fptr->addr)
{
sptr=sptr->addr;
fptr=fptr->addr->addr;
}
return sptr;
}
void printlistrec(node * head,node* current,node*prev)
{
if(current==0)
{
return;
}
node* nextaddr=current->addr;
cout<<current->value<<' ';
printlistrec(head,nextaddr,current);
}
void alternateprintlistrec(node* head)
{
if(!head)
{
return;
}
cout<<head->value<<' ';
if(head->addr)
alternateprintlistrec(head->addr->addr);
return;
}
node* removeduplicates(node * head)
{
int a[100000];
while(head)
{
a[head->value]++;
head=head->addr;
}
IT II
Rahul Khanna
760/IT/14
int k=0;
while(head)
{
if(a[head->value])
{
if(k==0)
{
head=head->addr;
}
else
{
node *temp=findk(head,k-1);
temp->addr=temp->addr->addr;
}
}
k++;
head=head->addr;
}
return head;
}
int main()
{
node *head1=createlist();
cout<<endl;
head1=removeduplicates(head1);
cout<<endl;
alternateprintlistrec(head1);
cout<<endl;
cout<<"value at middle element is = "<<middle(head1)->value<<endl;
printlistrec(head1,head1,0);
cout<<endl;
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
i)Program to implement a doubly linked list & provide insertion,deletion & display operations
ii) Program to implement circular doubly linked list.
i)
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*start;
class double_llist
{
public:
void create_list(int value);
void add_begin(int value);
void add_after(int value, int position);
void delete_element(int value);
void search_element(int value);
void display_dlist();
double_llist()
{
start = NULL;
}};
int main()
{
int choice, element, position;
double_llist dl;
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at begining"<<endl;
cout<<"3.Add after position"<<endl;
cout<<"4.Delete"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Count"<<endl;
cout<<"7.Reverse"<<endl;
cout<<"8.Quit"<<endl;
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
if (start == NULL)
{
temp->prev = NULL;
start = temp;
} else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}}
void double_llist::add_begin(int value)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->prev = NULL;
temp->info = value;
temp->next = start;
start->prev = temp;
start = temp;
cout<<"Element Inserted"<<endl;
}
void double_llist::add_after(int value, int pos)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
} struct node *tmp, *q;
int i;
q = start;
for (i = 0;i < pos - 1;i++)
{
q = q->next;
if (q == NULL)
{
cout<<"There are less than ";
cout<<pos<<" elements."<<endl;
return;
}
}
tmp = new(struct node);
tmp->info = value;
if (q->next == NULL)
{
q->next = tmp;
IT II
Rahul Khanna
760/IT/14
tmp->next = NULL;
tmp->prev = q;
}
else
{
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;
}
cout<<"Element Inserted"<<endl;
}
void double_llist::delete_element(int value)
{ struct node *tmp, *q;
if (start->info == value)
{
tmp = start;
start = start->next;
start->prev = NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
} q = start;
while (q->next->next != NULL)
{
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = q->next;
}
if (q->next->info == value)
{
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element "<<value<<" not found"<<endl;
}
void double_llist::display_dlist()
{ struct node *q;
if (start == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
} q = start;
IT II
Rahul Khanna
760/IT/14
OUTPUT
ii)
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
struct node *prev;
};
typedef struct node n;
n* create_node(int);
void add_node();
void insert_at_first();
void insert_at_end();
void insert_at_position();
void delete_node_position();
void sort_list();
void update();
void search();
void display_from_beg();
void display_in_rev();
n *new, *ptr, *prev;
n *first = NULL, *last = NULL;
IT II
Rahul Khanna
760/IT/14
int number = 0;
void main()
{
int ch;
printf("\nlinked list\n");
printf("1.insert at beginning \n2.insert at end\n3.insert at position\n4.sort linked list\n5.delete node at
position\n6.updatenodevalue\n7.search element\n8.displaylist from beginning\n9.display list from
end\n10.exit ");
while (1)
{
printf("\n enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1 :
insert_at_first();
break;
case 2 :
insert_at_end();
break;
case 3 :
insert_at_position();
break;
case 4 :
sort_list();
break;
case 5 :
delete_node_position();
break;
case 6 :
update();
break;
case 7 :
search();
break;
case 8 :
display_from_beg();
break;
case 9 :
display_in_rev();
break;
case 10 :
exit(0);
case 11 :
add_node();
break;
default:
printf("\ninvalid choice");
}
}
}
n* create_node(int info)
IT II
Rahul Khanna
760/IT/14
{
number++;
new = (n *)malloc(sizeof(n));
new->val = info;
new->next = NULL;
new->prev = NULL;
return new;
}
void add_node()
{
int info;
printf("\nenter the value you would like to add:");
scanf("%d", &info);
new = create_node(info);
if (first == last && first == NULL)
{
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
last->next = new;
new->prev = last;
last = new;
last->next = first;
first->prev = last;
}
}
void insert_at_first()
{
int info;
printf("\nenter the value to be inserted at first:");
scanf("%d",&info);
new = create_node(info);
if (first == last && first == NULL)
{
printf("\ninitially it is empty linked list later insertion is done");
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
new->next = first;
first->prev = new;
first = new;
first->prev = last;
last->next = first;
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
else
{
if (number < pos)
printf("\n node cant be deleted at position as it is exceeding the linkedlist length");
else
{
for (ptr = first,i = 1;i <= number;i++)
{
prevnode = ptr;
ptr = ptr->next;
if (pos == 1)
{
number--;
last->next = prevnode->next;
ptr->prev = prevnode->prev;
first = ptr;
printf("%d is deleted", prevnode->val);
free(prevnode);
break;
}
else if (i == pos - 1)
{
number--;
prevnode->next = ptr->next;
ptr->next->prev = prevnode;
printf("%d is deleted", ptr->val);
free(ptr);
break;
}}}}}
void update()
{
int oldval, newval, i, f = 0;
printf("\n enter the value old value:");
scanf("%d", &oldval);
printf("\n enter the value new value:");
scanf("%d", &newval);
if (first == last && first == NULL)
printf("\n list is empty no elemnts for updation");
else
{
for (ptr = first, i = 0;i < number;ptr = ptr->next,i++)
{
if (ptr->val == oldval)
{
ptr->val = newval;
printf("value is updated to %d", ptr->val);
f = 1;
}
}
if (f == 0)
IT II
Rahul Khanna
760/IT/14
OUTPUT
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void push(struct node** head_ref, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int detectloop(struct node *list)
{
struct node *slow_p = list, *fast_p = list;
while(slow_p && fast_p &&
fast_p->next )
{
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p)
{
printf("Found Loop");
return 1;
}}
return 0;
}
int main()
{
struct node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);
head->next->next->next->next = head;
detectloop(head);
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include <stdio.h>
#include <malloc.h>
struct node {
struct node * left;
char data;
struct node * right;
};
struct node *constructTree( int );
void inorder(struct node *);
char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };
int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };
int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };
void main() {
struct node *root;
root = constructTree( 0 );
printf("In-order Traversal: \n");
inorder(root);
}
struct node * constructTree( int index ) {
struct node *temp = NULL;
if (index != -1) {
temp = (struct node *)malloc( sizeof ( struct node ) );
temp->left = constructTree( leftcount[index] );
temp->data = array[index];
temp->right = constructTree( rightcount[index] );
}
return temp;
}
void inorder( struct node *root ) {
if (root != NULL) {
inorder(root->left);
printf("%c\t", root->data);
inorder(root->right);
}
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
OUTPUT
IT II
Rahul Khanna
760/IT/14
OUTPUT
IT II
Rahul Khanna
760/IT/14
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 100
typedef struct stack
{
int data[MAX];
int top;
}stack;
int priority(char);
void init(stack *);
int empty(stack *);
int full(stack *);
char pop(stack *);
void push(stack *,char);
char top(stack *);
int main()
{
stack s;
char x;
int token;
init(&s);
printf("Enter infix expression:");
while((token=getchar())!='n')
{
if(isalnum(token))
printf("%c",token);
else
if(token == '(')
push(&s,'(');
else
{
if(token == ')')
while((x=pop(&s))!='(')
printf("%c",x);
else
{
while(priority(token)<=priority(top(&s)) && !empty(&s))
{
x=pop(&s);
printf("%c",x);
}
push(&s,token);
}} }
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
getch();
}
IT II
Rahul Khanna
760/IT/14
int priority(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
void init(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
else
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
void push(stack *s,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
char pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
char top(stack * s)
{
return(s->data[s->top]);
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
break;
case '/':
ans=b/a;
break;
case '^':
ans=b^a;
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
OUTPUT
IT II
Rahul Khanna
760/IT/14
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
int main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
printf("\nProgram For Binary Search Tree ");
do {
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;
else
insert(root, new_node);
printf("\nWant To enter More Elements?(y/n)");
ans = getch();
} while (ans == 'y');
break;
case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
printf("\nParent of node %d is %d", tmp->data, parent->data);
break;
case 3:
if (root == NULL)
IT II
Rahul Khanna
760/IT/14
IT II
Rahul Khanna
760/IT/14
}
}
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);
}
}
OUTPUT
IT II
Rahul Khanna
760/IT/14