SlideShare uma empresa Scribd logo
1 de 46
Baixar para ler offline
INDEX
S.no Content Page
no.
1. WAP of Array implementation using size 3X3 matrix accept
values from user and print them on screen.
2. WAP to insert new value in array at first position or at mid
position given by user and also array holds some items.
3. WAP to create a linked list add some nodes in it and print its
values using simple traversing operation of linked list.
4. WAP to insert new node in linked list at first position, list
already hold some items.
5. WAP insert new node in linked list add at first ,add at end and
add at middle position.
6. WAP to create a doubly linked list add some nodes in it and
print its values using backward traversing.
7. WAP to implement circular list using arrays.
8. WAP to implement circular list using linked list.
9. WAP to implement of stack operations through function Push()
and Pop() using arrays.
10. WAP to implement Queue operation Insert() and Delete()
function in queue using arrays.
11. WAP to create binary tree and traverse them using recursive
function Preorder,Postorder and Inorder traversing.
12. WAP to implement Linear search operation using arrays.
13. WAP to implement Binary search operation using arrays.
14. WAP to implement Bubble sort operation using arrays.
15. WAP to implement Insertion sort operation using arrays.
16. WAP to implement Selection sort operation using arrays.
17. WAP to implement Merge sort operation using arrays.
18. WAP to implement Heap sort operation using arrays.
19. WAP to evaluate Post fix expression using stack.
20. WAP to implement conversion algorithm from Pre fix to Post fix
expression.
Question 1:- WAP of Array implementation using size 3X3 matrix accept values from user and print them on
screen.
#include <stdio.h>
int main()
{
int matrix[10][10];
int i,j,r,c;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Cols :");
scanf("%d",&c);
printf("nEnter matrix elements :n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
printf("nMatrix is :n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("%dt",matrix[i][j]);
}
printf("n"); /*new line after row elements*/
}
return 0;
}
OUTPUT
Enter number of Rows :3
Enter number of Cols :3
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [1,3] : 3
Enter element [2,1] : 4
Enter element [2,2] : 5
Enter element [2,3] : 6
Enter element [3,1] : 7
Enter element [3,2] : 8
Enter element [3,3] : 9
Matrix is :
1 2 3
4 5 6
7 8 9
==========================================================================================
Question 2:- WAP to insert new value in array at first position or at mid position given by user and also array
holds some items.
#include <stdio.h>
int main()
{
int arr[50],n,i,key,loc;
printf("Enter size :");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter element to insert:");
scanf("%d", &key);
printf("Enter loc to insert:");
scanf("%d", &loc);
for(i=(n-1); i>=loc; i--)
{
arr[i+1]=arr[i];
}
arr[loc]=key;
for(i=0;i<n;i++)
{
printf("%dn",arr[i]);
}
return 0;
}
OUTPUT
Enter size :5
Enter 5 elements:10
20
30
40
50
Enter element to insert:90
Enter loc to insert:0
90
10
20
30
40
=======================================================================================
Question 3:- WAP to create a linked list add some nodes in it and print its values using simple traversing
operation of linked list.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
// This function prints contents of linked list starting from the given node
void printList(struct Node *n)
{
while (n != NULL)
{
printf(" %d ", n->data);
n = n->next;
}
}
int main()
{
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL; // allocate 3 nodes in the heap
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1; //assign data in first node
head->next = second; // Link first node with second
second->data = 2; //assign data to second node
second->next = third;
third->data = 3; //assign data to third node
third->next = NULL;
printList(head);
return 0;
}
OUTPUT
1 2 3
==================================================================================
Question 5:- WAP insert new node in linked list add at first,add at end and add at middle position.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* link; // link is a global variable
};
struct node* root= NULL;
int len; // after append fucnction
void addatend(void);
void addatbegin(void);
void addatafter(void);
void display();
int length();
void main()
{
int ch;
while(1)
{
printf("Single linked list operations : n");
printf("1.Add at end n");
printf("2.Add at begin n");
printf("3.Add at after n");
printf("4.Display n");
printf("5.Length");
printf("6.Quit n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : addatend();
break;
case 2 : addatbegin();
break;
case 3 : addatafter();
break;
case 4 : display();
break;
case 5 : len=length();
printf("Length : %dnn",len);
break;
case 6 : exit(1);
default : printf("Invalid input n");
}
}
}
void addatend()
{
struct node* temp; // temp is a local variable
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data :"); // how to read the information from the end user
scanf("%d", &temp->data);
temp->link= NULL; // create a first node
if(root == NULL) //LIST IS EMPTY
{
root = temp;
}
else
{
struct node* p; // how to insert remaining node
p = root;
while(p->link!= NULL) // check every time
{
p = p->link;
}
p->link =temp;
}
}
void addatafter()
{
struct node* temp, *P;
int loc,len,i=1;
printf("Enter location:");
scanf("%d",&loc);
len=length();
if(loc>len)
{
printf("Invalid location n");
printf("currently list is having %d node",len);
}
else
{
P=root;
while(i<loc)
{
P=P->link;
i++;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->link=P->link; // right
P->link=temp; //left
}
}
void addatbegin(void)
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the node data:");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
temp->link=root; // right
root = temp; // left
}
}
void display() //how to display all the element in the list
{
struct node* temp;
temp = root;
if(temp== NULL)
{
printf("List is empty nn");
}
else
{
while(temp!= NULL)
{
printf("%d-->",temp->data); // print link data
temp = temp->link;
}
printf("nn");
} }
int length() // length function
{
int count =0;
struct node* temp;
temp = root ;
while(temp != NULL)
{
count++;
temp= temp->link;
}
return count;
}
OUTPUT
Single linked list operations :
1.Add at end
2.Add at begin
3.Add at after
4.Display
5.Length
6.Quit
Enter your choice: 1
Enter node data :2
Single linked list operations :
1.Add at end
2.Add at begin
3.Add at after
4.Display
5.Length
6.Quit
Enter your choice: 2
Enter the node data:4
Single linked list operations :
1.Add at end
2.Add at begin
3.Add at after
4.Display
5.Length
6.Quit
Enter your choice: 4
4-->2-->
=======================================================================================
Question 6:- WAP to create a doubly linked list add some nodes in it and print its values using backward
traversing.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *root = NULL;
struct node *last = NULL;
void insert();
void printlist();
void printbackward();
int main()
{
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printlist();
printbackward();
return 0;
}
void insert(int data) //Create Linked List
{
struct node *temp;
temp = (struct node*) malloc(sizeof(struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
if(root==NULL) // If head is empty, create new list
{
root = temp;
return;
}
else
{
struct node* P;
P=root;
while(P->right!=NULL) // move to the end of the list
P = P->right;
P->right = temp; // Insert link at the end of the list
last = temp;
temp->left = P;
}
}
void printlist() //display the list
{
struct node *temp ;
temp=root;
printf("n[root] <=>");
while(temp != NULL) //start from the beginning
{
printf(" %d <=>",temp->data);
temp = temp->right;
}
printf(" [NULL]n");
}
void printbackward() //display the list
{
struct node *temp = last;
printf("n[root] <=>");
while(temp != NULL) //start from the beginning
{
printf(" %d <=>",temp->data);
temp = temp->left;
}
printf(" [NULL]n");
}
OUTPUT
[root] <=> 10 <=> 20 <=> 30 <=> 1 <=> 40 <=> 56 <=> [NULL]
[root] <=> 56 <=> 40 <=> 1 <=> 30 <=> 20 <=> 10 <=> [NULL]
==========================================================================================
Question 7:- WAP to implement circular linked list using array
# include<stdio.h>
# define size 5
int cqueue[size];
int front = -1;
int rear = -1;
void delete();
void insert();
void display();
int main()
{
int choice,element;
while(1)
{
printf("1.Insertn");
printf("2.Deleten");
printf("3.Displayn");
printf("4.Quitn");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &element);
insert(element);
break;
case 2 :
delete();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choicen");
}
}
return 0;
}
void insert(int element)
{
if(front==rear+1 || rear==size-1)
{
printf("circular is full");
}
else if(front==-1 && rear==-1)
{
front=rear=0;
cqueue[rear]=element;
}
else if(rear==size-1)
{
rear=0;
cqueue[rear]=element;
}
else
{
rear++;
cqueue[rear]=element;
}
}
void delete()
{
int element;
if(front==-1 && rear==-1)
{
printf("cqueue is empty");
}
else if(front==rear)
{
element=cqueue[front];
front=rear==-1;
}
else if(front==size-1)
{
element=cqueue[front];
front=0;
}
else
{
element=cqueue[front];
front++;
}
}
void display()
{
if(front == -1)
{
printf("Queue is emptyn");
return;
}
printf("Queue elements :n");
if( front <= rear )
while(front <= rear)
{
printf("%d ",cqueue[front]);
front++;
}
else
{
while(front <= size-1)
{
printf("%d ",cqueue[front]);
front++;
}
front = 0;
while(front <= rear)
{
printf("%d ",cqueue[front]);
front++;
}
}
printf("n");
}
OUTPUT
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 3
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 5
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
3 5
1.Insert
2.Delete
3.Display
4.Quit
======================================================================================
Question 8:- WAP to implement circular list using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL,*temp;
void create();
void delete();
void display();
int main()
{
int ch;
while(1)
{
printf("n 1 Enter the element : ");
printf("n 2 Delete the element : ");
printf("n 3 Display the elements : ");
printf("n 4 Exit from main : ");
printf("n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
return 1;
default:
printf("nInvalid choice :");
}
}
return 0;
}
void create()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("nEnter the node value : ");
scanf("%d",&temp->data);
temp->next=NULL;
if(rear==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
rear->next=front;
}
void delete()
{
temp=front;
if(front==NULL)
printf("nUnderflow :");
else
{
if(front==rear)
{
printf("n%d",front->data);
front=rear=NULL;
}
else
{
printf("n%d",front->data);
front=front->next;
rear->next=front;
}
temp->next=NULL;
free(temp);
}
}
void display()
{
temp=front;
if(front==NULL)
printf("nEmpty");
else
{
printf("n");
for(;temp!=rear;temp=temp->next)
printf("n%dt",temp->data);
printf("n%dt",temp->data);
}
}
OUTPUT
1 Enter the element :
2 Delete the element :
3 Display the elements :
4 Exit from main :
Enter your choice : 1
Enter the node value : 4
1 Enter the element :
2 Delete the element :
3 Display the elements :
4 Exit from main :
Enter your choice : 3
4
====================================================================================
Ques 9:-WAP to implement of stack operations through function Push() and Pop() using arrays.
#include <stdio.h>
#include <stdlib.h>
#define CAPACITY 5 // Pre- processor macro
int stack[CAPACITY], top=-1 ;
void push(int);
int pop(void);
int isFull(void);
int isEmpty(void);
void traverse(void);
void main()
{
int ch, item,ele;
while(1)
{
printf("1. Push n");
printf("2. Pop n");
printf("3. Traverse n");
printf("4. Quit n");
printf("Enter your choice :");
scanf("%d", &ch);
switch(ch)
{
case 1 : printf("Enter element :");
scanf("%d", &item);
push(item);
break;
case 2 : item = pop();
if(item==0)
{
printf("stack is underflown");
}
else
{
printf("popped item :%dn", item);
}
break;
case 3 : traverse();
break;
case 4 : exit(0);
default : printf("Invalid input nn");
}
}
}
void push(int ele)
{
if(isFull())
{
printf("stack is overflow n");
}
else
{
top++;
stack[top] = ele;
printf("%d pushed n", ele);
}
}
int isFull()
{
if(top == CAPACITY-1)
{
return 1;
}
else
{
return 0;
}
}
int pop()
{
if(isEmpty())
{
return 0;
}
else
{
return stack[top--];
}
}
int isEmpty()
{
if(top == -1)
{
return 1;
}
else
{
return 0;
}
}
void peek()
{
if(isEmpty())
{
printf("peek element : %d n", stack[top]);
}
}
void traverse()
{
if(isEmpty())
{
printf("Stack is empty n");
}
else
{
int i;
printf("stack elements : n");
for(i=0; i<=top; i++)
{
printf("%d n", stack[i]);
}
}
}
OUTPUT
1. Push
2. Pop
3. Peek
4. Traverse
5. Quit
Enter your choice :1
Enter element :4
4 pushed
1. Push
2. Pop
3. Peek
4. Traverse
5. Quit
========================================================================================
Ques 10:-WAP to implement Queue operation insert() and delete() function in queue using arrays.
#include <stdio.h>
#include <stdlib.h>
# define CAPACITY 5
int queue[CAPACITY];
int front = 0;
int rear = 0;
void delete();
void insert();
int main()
{
int choice,element;
while(1)
{
printf("1.Insertn");
printf("2.Deleten");
printf("4.Quitn");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
break;
case 2 :
delete();
break;
case 4:
break;
default:
printf("Wrong choicen");
}
}
return 0;
}
void insert()
{
if(CAPACITY==rear)
{
printf("Queue is fulln");
}
else
{
int element;
printf("Enter the element :");
scanf("%d",&element);
queue[rear]=element;
rear++;
}
}
void delete()
{
int i;
if(front==rear)
{
printf("Queue is Empty");
}
else
{
printf("deleted : %d", queue[front]);
for(i=1; i<rear-1; i++)
{
queue[i]=queue[i+1];
}
rear--;
}
}
OUTPUT
1.Insert
2.Delete
3.Quit
Enter your choice : 1
Enter the element :4
1.Insert
2.Delete
3.Quit
Enter your choice : 1
Enter the element :6
1.Insert
2.Delete
3.Quit
Enter your choice : 2
deleted : 4
==========================================================================================
Ques 11:- WAP to create binary tree and traverse them using recursive function Preorder,Postorder and
Inorder traversing.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* temp;
temp= (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return(temp);
}
void postorder(struct node* P)
{
if (P == NULL)
return;
postorder(P->left);
postorder(P->right);
printf("%d ", P->data);
}
void inorder(struct node* P)
{
if (P == NULL)
return;
inorder(P->left);
printf("%d ", P->data);
inorder(P->right);
}
void preorder(struct node* P)
{
if (P == NULL)
return;
printf("%d ", P->data);
preorder(P->left);
preorder(P->right);
}
int main()
{
struct node *root;
root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("nPreorder traversal of binary tree is n");
preorder(root);
printf("nInorder traversal of binary tree is n");
inorder(root);
printf("nPostorder traversal of binary tree is n");
postorder(root);
return 0;
}
OUTPUT
Preorder traversal of binary tree is
1 2 4 5 3
Inorder traversal of binary tree is
4 2 5 1 3
Postorder traversal of binary tree is
4 5 2 3 1
=======================================================================================
Ques 12:- WAP to implement Linear search operation using arrays.
#include <stdio.h>
int main()
{
int array[100], search, i, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d integer(s)n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter a number to searchn");
scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (array[i] == search) /* If required element is found */
{
printf("%d is present at location %d.n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.n", search);
return 0;
}
OUTPUT
Enter number of elements in array
4
Enter 4 integer(s)
2 5 8 3
Enter a number to search
8
8 is present at location 3.
==========================================================================================
Ques 13:- WAP to implement Binary search opertation using arrays.
#include <stdio.h>
int main()
{
int i, first, last, middle, n, search, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for (i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
printf("Enter value to findn");
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 isn't present in the list.n", search);
return 0;
}
OUTPUT
Enter number of elements
5
Enter 5 integers
4 5 3 8 9
Enter value to find
8
8 found at location 4.
=========================================================================================
Ques 14:- WAP to implement Bubble sort opertation using arrays.
#include <stdio.h>
#include<stdlib.h>
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1]) /* For decreasing order use < */
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
printf("Sorted list in ascending order:n");
for (i = 0; i < n; i++)
{
printf("%dn", array[i]);
}
return 0;
}
OUTPUT
Enter number of elements
5
Enter 5 integers
5 4 7 9 2
Sorted list in ascending order:
2
4
5
7
9
======================================================================================
Question 15:- WAP to implement Insertion sort operation using arrays.
#include <math.h>
#include <stdio.h>
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
printf("nList after sortingn");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
}
int main()
{
int i;
int arr[5] = { 12, 11, 13, 5, 6 };
printf("List before sortingn");
for(i = 1; i <= arr[4]; i++)
{
printf("%d ", arr[i]);
}
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
OUTPUT
List before sorting
11 13 5 6 0 1
List after sorting
5 6 11 12 13
=======================================================================================
Question 16:- WAP to implement Selection sort operation using arrays.
#include <stdio.h>
int main()
{
int array[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i = 0; i < (n - 1); i++)
{
position = i;
for (j = i + 1; j < n; j++)
{
if (array[position] > array[j])
position = j;
}
if (position != i)
{
swap = array[i];
array[i] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:n");
for (i = 0; i < n; i++)
{
printf("%dn", array[i]);
}
return 0;
}
OUTPUT
Enter number of elements
5
Enter 5 integers
5 8 9 3 2
Sorted list in ascending order:
2
3
5
8
9
======================================================================================
Question 17:- WAP to implement Merge sort operation using arrays.
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main()
{
int i;
printf("List before sortingn");
for(i = 0; i <= max; i++)
{
printf("%d ", a[i]);
}
sort(0, max);
printf("nList after sortingn");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
OUTPUT
List before sorting
10 14 19 26 27 31 33 35 42 44 0
List after sorting
0 10 14 19 26 27 31 33 35 42 44
=====================================================================================
Question 18:- WAP to implement Heap sort operation using arrays.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXSIZE 5
#define MAX 5
void main()
{
int a[MAX],n,i,s,f,item,value;
printf("Enter the number of elements:n");
scanf("%d",&n);
printf("Enter %d elements one by onen",n);
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++)
{
item=a[i];
s=i;
f=(s-2)/2;
while(s>0 && a[f]<item)
{
a[s]=a[f];
s=f;
f=(s-2)/2;
}
a[s]=item;
}
for(i=n-1;i>0;i--)
{
value=a[i];
a[i]=a[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if(i>2 && (a[2]>a[1]))
s=2;
while(s>=0 && value<a[s])
{
a[f]=a[s];
f=s;
s=2*f+1;
if(s+1 <=i-1 && (a[s]<a[s+1]))
s=s+1;
if(s>i-1)
s=-1;
}
a[f]= value;
}
printf("The sorted list is n");
for(i=0; i<n; i++)
{
printf("%dn",a[i]);
}
getch();
}
OUTPUT
Enter the number of elements:
5
Enter 5 elements one by one
6 4 2 8 1
The sorted list is
1
2
6
4
8
=========================================================================================
Question 19:- WAP to evaluate Post fix expression using stack.
#include<stdio.h>
#include<ctype.h>
# define MAXSTACK 100
# define POSTFIXSIZE 100
int stack[MAXSTACK];
int top = -1 ;
void push(int item)
{
if(top >= MAXSTACK -1)
{
printf("stack over flow");
return;
}
else
{
top = top + 1 ;
stack[top]= item;
}
}
int pop()
{
int item;
if(top <0)
{
printf("stack under flow");
}
else
{
item = stack[top];
top = top - 1;
return item;
}
}
void EvalPostfix(char postfix[])
{
int i ;
char ch;
int val;
int A, B ;
for (i = 0 ; postfix[i] != ')'; i++)
{
ch = postfix[i];
if (isdigit(ch))
{
push(ch - '0');
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
A = pop();
B = pop();
switch (ch)
{
case '*':
val = B * A;
break;
case '/':
val = B / A;
break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}
push(val); /* push the value obtained above onto the stack */
}
}
printf( " n Result of expression evaluation : %d n", pop()) ;
}
int main()
{
int i ;
char postfix[POSTFIXSIZE];
printf("Use only four operator +,-,*,/ and also single digit only.n");
printf( "nEnter postfix expression and last used ')' : ");
for (i = 0 ; i <= POSTFIXSIZE - 1 ; i++)
{
scanf("%c", &postfix[i]);
if ( postfix[i] == ')' )
break;
}
EvalPostfix(postfix);
return 0;
}
OUTPUT
Use only four operator +,-,*,/ and also single digit only.
Enter postfix expression and last used ')' : 56+)
Result of expression evaluation : 11
==========================================================================================
Question 20:- WAP to implement conversion algorithm from Pre fix expression to Post fix expression.
#include<stdio.h>
#include<string.h>
void push(char item[],int *top,char s[][20])
{
*top=*top+1;
strcpy(s[*top],item);
}
void *pop(int *top,char s[][20])
{
char *item;
item=s[*top];
*top=*top-1;
return item;
}
void pre_post(char prefix[],char postfix[])
{
char s[20][20];
int top,i;
char symbol,temp[2];
char *op1,*op2;
top=-1;
strrev(prefix);
for(i=0;i<strlen(prefix);i++)
{
symbol=prefix[i];
temp[0]=symbol;
temp[1]='0';
switch (symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
op1=pop(&top,s);
op2=pop(&top,s);
strcpy(postfix,op1);
strcat(postfix,op2);
strcat(postfix,temp);
push(postfix,&top,s);
break;
default:
push(temp,&top,s);
}
}
}
void main()
{
char prefix[20];
char postfix[20];
printf("nn Enter the prefix expression nn");
scanf("%s",prefix);
pre_post(prefix,postfix);
printf("nn The postfix expression is %s nn",postfix);
}
OUTPUT
Enter the prefix expression
+56
The postfix expression is 56+

Mais conteúdo relacionado

Mais procurados

Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File Harjinder Singh
 
The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)jaxLondonConference
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basicsopenbala
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Platonov Sergey
 
T3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerT3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerDavid Muñoz Díaz
 
The best language in the world
The best language in the worldThe best language in the world
The best language in the worldDavid Muñoz Díaz
 
GUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programmingGUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programmingDavid Muñoz Díaz
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat SheetKarlijn Willems
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flowMohammed Sikander
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)stasimus
 

Mais procurados (20)

Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
2 a networkflow
2 a networkflow2 a networkflow
2 a networkflow
 
The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basics
 
C++ file
C++ fileC++ file
C++ file
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
T3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmerT3chFest 2016 - The polyglot programmer
T3chFest 2016 - The polyglot programmer
 
The best language in the world
The best language in the worldThe best language in the world
The best language in the world
 
Java arrays
Java   arraysJava   arrays
Java arrays
 
GUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programmingGUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programming
 
R basics
R basicsR basics
R basics
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat Sheet
 
Collection Core Concept
Collection Core ConceptCollection Core Concept
Collection Core Concept
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)
 

Semelhante a Most Important C language program

For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfherminaherman
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfwasemanivytreenrco51
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 
hello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docxhello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docxIsaac9LjWelchq
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPeterlqELawrenceb
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfformicreation
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
MO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptMO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptshashankbhadouria4
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptxSanthiya S
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfinfo114
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked listSourav Gayen
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfrozakashif85
 
Im having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfIm having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfrishteygallery
 

Semelhante a Most Important C language program (20)

Final ds record
Final ds recordFinal ds record
Final ds record
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
hello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docxhello- please dont just copy from other answers- the following is the.docx
hello- please dont just copy from other answers- the following is the.docx
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Please solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docxPlease solve the following problem using C++- Thank you Instructions-.docx
Please solve the following problem using C++- Thank you Instructions-.docx
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
MO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptMO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.ppt
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
C program
C programC program
C program
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
Im having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdfIm having trouble figuring out how to code these sections for an a.pdf
Im having trouble figuring out how to code these sections for an a.pdf
 

Mais de TEJVEER SINGH

Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notesTEJVEER SINGH
 
Software testing lecture notes
Software testing  lecture notesSoftware testing  lecture notes
Software testing lecture notesTEJVEER SINGH
 
Introduction to cloud computing
Introduction to cloud computingIntroduction to cloud computing
Introduction to cloud computingTEJVEER SINGH
 
HOW TO DOWNLOAD MICROSOFT WORD IN ANDROID, and How to convert doc file into ...
HOW TO DOWNLOAD MICROSOFT WORD  IN ANDROID, and How to convert doc file into ...HOW TO DOWNLOAD MICROSOFT WORD  IN ANDROID, and How to convert doc file into ...
HOW TO DOWNLOAD MICROSOFT WORD IN ANDROID, and How to convert doc file into ...TEJVEER SINGH
 
Computer graphics unit 4th
Computer graphics unit 4thComputer graphics unit 4th
Computer graphics unit 4thTEJVEER SINGH
 
Multi Banking System
Multi Banking SystemMulti Banking System
Multi Banking SystemTEJVEER SINGH
 
Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...
Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...
Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...TEJVEER SINGH
 
Computer network questions
Computer network questionsComputer network questions
Computer network questionsTEJVEER SINGH
 
#How to install mongoDB and also setup path
#How to install mongoDB and also setup path#How to install mongoDB and also setup path
#How to install mongoDB and also setup pathTEJVEER SINGH
 
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriver
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriverjava.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriver
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriverTEJVEER SINGH
 
Oracle 10g Installation Guide
Oracle 10g Installation GuideOracle 10g Installation Guide
Oracle 10g Installation GuideTEJVEER SINGH
 
Important dbms practical question
Important dbms practical  questionImportant dbms practical  question
Important dbms practical questionTEJVEER SINGH
 

Mais de TEJVEER SINGH (13)

Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notes
 
Software testing lecture notes
Software testing  lecture notesSoftware testing  lecture notes
Software testing lecture notes
 
Introduction to cloud computing
Introduction to cloud computingIntroduction to cloud computing
Introduction to cloud computing
 
HOW TO DOWNLOAD MICROSOFT WORD IN ANDROID, and How to convert doc file into ...
HOW TO DOWNLOAD MICROSOFT WORD  IN ANDROID, and How to convert doc file into ...HOW TO DOWNLOAD MICROSOFT WORD  IN ANDROID, and How to convert doc file into ...
HOW TO DOWNLOAD MICROSOFT WORD IN ANDROID, and How to convert doc file into ...
 
Computer graphics unit 4th
Computer graphics unit 4thComputer graphics unit 4th
Computer graphics unit 4th
 
Multi Banking System
Multi Banking SystemMulti Banking System
Multi Banking System
 
Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...
Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...
Design principle of pattern recognition system and STATISTICAL PATTERN RECOGN...
 
Computer network questions
Computer network questionsComputer network questions
Computer network questions
 
#How to install mongoDB and also setup path
#How to install mongoDB and also setup path#How to install mongoDB and also setup path
#How to install mongoDB and also setup path
 
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriver
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriverjava.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriver
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracaleDriver
 
Oracle 10g Installation Guide
Oracle 10g Installation GuideOracle 10g Installation Guide
Oracle 10g Installation Guide
 
Important dbms practical question
Important dbms practical  questionImportant dbms practical  question
Important dbms practical question
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
 

Último

data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 

Último (20)

NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 

Most Important C language program

  • 1. INDEX S.no Content Page no. 1. WAP of Array implementation using size 3X3 matrix accept values from user and print them on screen. 2. WAP to insert new value in array at first position or at mid position given by user and also array holds some items. 3. WAP to create a linked list add some nodes in it and print its values using simple traversing operation of linked list. 4. WAP to insert new node in linked list at first position, list already hold some items. 5. WAP insert new node in linked list add at first ,add at end and add at middle position. 6. WAP to create a doubly linked list add some nodes in it and print its values using backward traversing. 7. WAP to implement circular list using arrays. 8. WAP to implement circular list using linked list. 9. WAP to implement of stack operations through function Push() and Pop() using arrays. 10. WAP to implement Queue operation Insert() and Delete() function in queue using arrays. 11. WAP to create binary tree and traverse them using recursive function Preorder,Postorder and Inorder traversing. 12. WAP to implement Linear search operation using arrays. 13. WAP to implement Binary search operation using arrays. 14. WAP to implement Bubble sort operation using arrays. 15. WAP to implement Insertion sort operation using arrays. 16. WAP to implement Selection sort operation using arrays. 17. WAP to implement Merge sort operation using arrays. 18. WAP to implement Heap sort operation using arrays. 19. WAP to evaluate Post fix expression using stack. 20. WAP to implement conversion algorithm from Pre fix to Post fix expression.
  • 2. Question 1:- WAP of Array implementation using size 3X3 matrix accept values from user and print them on screen. #include <stdio.h> int main() { int matrix[10][10]; int i,j,r,c; printf("Enter number of Rows :"); scanf("%d",&r); printf("Enter number of Cols :"); scanf("%d",&c); printf("nEnter matrix elements :n"); for(i=0;i< r;i++) { for(j=0;j< c;j++) { printf("Enter element [%d,%d] : ",i+1,j+1); scanf("%d",&matrix[i][j]); } } printf("nMatrix is :n"); for(i=0;i< r;i++) { for(j=0;j< c;j++) { printf("%dt",matrix[i][j]); } printf("n"); /*new line after row elements*/ } return 0; }
  • 3. OUTPUT Enter number of Rows :3 Enter number of Cols :3 Enter matrix elements : Enter element [1,1] : 1 Enter element [1,2] : 2 Enter element [1,3] : 3 Enter element [2,1] : 4 Enter element [2,2] : 5 Enter element [2,3] : 6 Enter element [3,1] : 7 Enter element [3,2] : 8 Enter element [3,3] : 9 Matrix is : 1 2 3 4 5 6 7 8 9 ========================================================================================== Question 2:- WAP to insert new value in array at first position or at mid position given by user and also array holds some items. #include <stdio.h> int main() { int arr[50],n,i,key,loc; printf("Enter size :"); scanf("%d",&n); printf("Enter %d elements:",n); for(i=0; i<n; i++) { scanf("%d", &arr[i]);
  • 4. } printf("Enter element to insert:"); scanf("%d", &key); printf("Enter loc to insert:"); scanf("%d", &loc); for(i=(n-1); i>=loc; i--) { arr[i+1]=arr[i]; } arr[loc]=key; for(i=0;i<n;i++) { printf("%dn",arr[i]); } return 0; } OUTPUT Enter size :5 Enter 5 elements:10 20 30 40 50 Enter element to insert:90 Enter loc to insert:0 90 10 20 30 40
  • 5. ======================================================================================= Question 3:- WAP to create a linked list add some nodes in it and print its values using simple traversing operation of linked list. #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *next; }; // This function prints contents of linked list starting from the given node void printList(struct Node *n) { while (n != NULL) { printf(" %d ", n->data); n = n->next; } } int main() { struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; // allocate 3 nodes in the heap head = (struct Node*)malloc(sizeof(struct Node)); second = (struct Node*)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); head->data = 1; //assign data in first node head->next = second; // Link first node with second second->data = 2; //assign data to second node second->next = third; third->data = 3; //assign data to third node
  • 6. third->next = NULL; printList(head); return 0; } OUTPUT 1 2 3 ================================================================================== Question 5:- WAP insert new node in linked list add at first,add at end and add at middle position. #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* link; // link is a global variable }; struct node* root= NULL; int len; // after append fucnction void addatend(void); void addatbegin(void); void addatafter(void); void display(); int length(); void main() { int ch; while(1) { printf("Single linked list operations : n"); printf("1.Add at end n"); printf("2.Add at begin n"); printf("3.Add at after n");
  • 7. printf("4.Display n"); printf("5.Length"); printf("6.Quit n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1 : addatend(); break; case 2 : addatbegin(); break; case 3 : addatafter(); break; case 4 : display(); break; case 5 : len=length(); printf("Length : %dnn",len); break; case 6 : exit(1); default : printf("Invalid input n"); } } } void addatend() { struct node* temp; // temp is a local variable temp = (struct node*)malloc(sizeof(struct node)); printf("Enter node data :"); // how to read the information from the end user scanf("%d", &temp->data); temp->link= NULL; // create a first node if(root == NULL) //LIST IS EMPTY
  • 8. { root = temp; } else { struct node* p; // how to insert remaining node p = root; while(p->link!= NULL) // check every time { p = p->link; } p->link =temp; } } void addatafter() { struct node* temp, *P; int loc,len,i=1; printf("Enter location:"); scanf("%d",&loc); len=length(); if(loc>len) { printf("Invalid location n"); printf("currently list is having %d node",len); } else { P=root; while(i<loc) {
  • 9. P=P->link; i++; } temp=(struct node*)malloc(sizeof(struct node)); temp->link=P->link; // right P->link=temp; //left } } void addatbegin(void) { struct node* temp; temp=(struct node*)malloc(sizeof(struct node)); printf("Enter the node data:"); scanf("%d",&temp->data); temp->link=NULL; if(root==NULL) { root=temp; } else { temp->link=root; // right root = temp; // left } } void display() //how to display all the element in the list { struct node* temp; temp = root; if(temp== NULL) {
  • 10. printf("List is empty nn"); } else { while(temp!= NULL) { printf("%d-->",temp->data); // print link data temp = temp->link; } printf("nn"); } } int length() // length function { int count =0; struct node* temp; temp = root ; while(temp != NULL) { count++; temp= temp->link; } return count; } OUTPUT Single linked list operations : 1.Add at end 2.Add at begin 3.Add at after 4.Display 5.Length 6.Quit
  • 11. Enter your choice: 1 Enter node data :2 Single linked list operations : 1.Add at end 2.Add at begin 3.Add at after 4.Display 5.Length 6.Quit Enter your choice: 2 Enter the node data:4 Single linked list operations : 1.Add at end 2.Add at begin 3.Add at after 4.Display 5.Length 6.Quit Enter your choice: 4 4-->2--> ======================================================================================= Question 6:- WAP to create a doubly linked list add some nodes in it and print its values using backward traversing. #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *left; struct node *right; }; struct node *root = NULL; struct node *last = NULL;
  • 12. void insert(); void printlist(); void printbackward(); int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); printlist(); printbackward(); return 0; } void insert(int data) //Create Linked List { struct node *temp; temp = (struct node*) malloc(sizeof(struct node)); temp->data = data; temp->left = NULL; temp->right = NULL; if(root==NULL) // If head is empty, create new list { root = temp; return; } else { struct node* P; P=root;
  • 13. while(P->right!=NULL) // move to the end of the list P = P->right; P->right = temp; // Insert link at the end of the list last = temp; temp->left = P; } } void printlist() //display the list { struct node *temp ; temp=root; printf("n[root] <=>"); while(temp != NULL) //start from the beginning { printf(" %d <=>",temp->data); temp = temp->right; } printf(" [NULL]n"); } void printbackward() //display the list { struct node *temp = last; printf("n[root] <=>"); while(temp != NULL) //start from the beginning { printf(" %d <=>",temp->data); temp = temp->left; } printf(" [NULL]n"); } OUTPUT
  • 14. [root] <=> 10 <=> 20 <=> 30 <=> 1 <=> 40 <=> 56 <=> [NULL] [root] <=> 56 <=> 40 <=> 1 <=> 30 <=> 20 <=> 10 <=> [NULL] ========================================================================================== Question 7:- WAP to implement circular linked list using array # include<stdio.h> # define size 5 int cqueue[size]; int front = -1; int rear = -1; void delete(); void insert(); void display(); int main() { int choice,element; while(1) { printf("1.Insertn"); printf("2.Deleten"); printf("3.Displayn"); printf("4.Quitn"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : printf("Input the element for insertion in queue : "); scanf("%d", &element); insert(element); break; case 2 : delete();
  • 15. break; case 3: display(); break; case 4: break; default: printf("Wrong choicen"); } } return 0; } void insert(int element) { if(front==rear+1 || rear==size-1) { printf("circular is full"); } else if(front==-1 && rear==-1) { front=rear=0; cqueue[rear]=element; } else if(rear==size-1) { rear=0; cqueue[rear]=element; } else { rear++;
  • 16. cqueue[rear]=element; } } void delete() { int element; if(front==-1 && rear==-1) { printf("cqueue is empty"); } else if(front==rear) { element=cqueue[front]; front=rear==-1; } else if(front==size-1) { element=cqueue[front]; front=0; } else { element=cqueue[front]; front++; } } void display() { if(front == -1) { printf("Queue is emptyn");
  • 17. return; } printf("Queue elements :n"); if( front <= rear ) while(front <= rear) { printf("%d ",cqueue[front]); front++; } else { while(front <= size-1) { printf("%d ",cqueue[front]); front++; } front = 0; while(front <= rear) { printf("%d ",cqueue[front]); front++; } } printf("n"); } OUTPUT 1.Insert 2.Delete 3.Display 4.Quit
  • 18. Enter your choice : 1 Input the element for insertion in queue : 3 1.Insert 2.Delete 3.Display 4.Quit Enter your choice : 1 Input the element for insertion in queue : 5 1.Insert 2.Delete 3.Display 4.Quit Enter your choice : 3 Queue elements : 3 5 1.Insert 2.Delete 3.Display 4.Quit ====================================================================================== Question 8:- WAP to implement circular list using linked list. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node *front=NULL,*rear=NULL,*temp; void create(); void delete();
  • 19. void display(); int main() { int ch; while(1) { printf("n 1 Enter the element : "); printf("n 2 Delete the element : "); printf("n 3 Display the elements : "); printf("n 4 Exit from main : "); printf("n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: delete(); break; case 3: display(); break; case 4: return 1; default: printf("nInvalid choice :"); } } return 0; }
  • 20. void create() { struct node* temp; temp=(struct node*)malloc(sizeof(struct node)); printf("nEnter the node value : "); scanf("%d",&temp->data); temp->next=NULL; if(rear==NULL) front=rear=temp; else { rear->next=temp; rear=temp; } rear->next=front; } void delete() { temp=front; if(front==NULL) printf("nUnderflow :"); else { if(front==rear) { printf("n%d",front->data); front=rear=NULL; } else { printf("n%d",front->data);
  • 21. front=front->next; rear->next=front; } temp->next=NULL; free(temp); } } void display() { temp=front; if(front==NULL) printf("nEmpty"); else { printf("n"); for(;temp!=rear;temp=temp->next) printf("n%dt",temp->data); printf("n%dt",temp->data); } } OUTPUT 1 Enter the element : 2 Delete the element : 3 Display the elements : 4 Exit from main : Enter your choice : 1 Enter the node value : 4 1 Enter the element : 2 Delete the element : 3 Display the elements : 4 Exit from main :
  • 22. Enter your choice : 3 4 ==================================================================================== Ques 9:-WAP to implement of stack operations through function Push() and Pop() using arrays. #include <stdio.h> #include <stdlib.h> #define CAPACITY 5 // Pre- processor macro int stack[CAPACITY], top=-1 ; void push(int); int pop(void); int isFull(void); int isEmpty(void); void traverse(void); void main() { int ch, item,ele; while(1) { printf("1. Push n"); printf("2. Pop n"); printf("3. Traverse n"); printf("4. Quit n"); printf("Enter your choice :"); scanf("%d", &ch); switch(ch) { case 1 : printf("Enter element :"); scanf("%d", &item); push(item); break; case 2 : item = pop();
  • 23. if(item==0) { printf("stack is underflown"); } else { printf("popped item :%dn", item); } break; case 3 : traverse(); break; case 4 : exit(0); default : printf("Invalid input nn"); } } } void push(int ele) { if(isFull()) { printf("stack is overflow n"); } else { top++; stack[top] = ele; printf("%d pushed n", ele); } } int isFull()
  • 24. { if(top == CAPACITY-1) { return 1; } else { return 0; } } int pop() { if(isEmpty()) { return 0; } else { return stack[top--]; } } int isEmpty() { if(top == -1) { return 1; } else { return 0; }
  • 25. } void peek() { if(isEmpty()) { printf("peek element : %d n", stack[top]); } } void traverse() { if(isEmpty()) { printf("Stack is empty n"); } else { int i; printf("stack elements : n"); for(i=0; i<=top; i++) { printf("%d n", stack[i]); } } } OUTPUT 1. Push 2. Pop 3. Peek 4. Traverse 5. Quit Enter your choice :1
  • 26. Enter element :4 4 pushed 1. Push 2. Pop 3. Peek 4. Traverse 5. Quit ======================================================================================== Ques 10:-WAP to implement Queue operation insert() and delete() function in queue using arrays. #include <stdio.h> #include <stdlib.h> # define CAPACITY 5 int queue[CAPACITY]; int front = 0; int rear = 0; void delete(); void insert(); int main() { int choice,element; while(1) { printf("1.Insertn"); printf("2.Deleten"); printf("4.Quitn"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1 : insert();
  • 27. break; case 2 : delete(); break; case 4: break; default: printf("Wrong choicen"); } } return 0; } void insert() { if(CAPACITY==rear) { printf("Queue is fulln"); } else { int element; printf("Enter the element :"); scanf("%d",&element); queue[rear]=element; rear++; } } void delete() { int i; if(front==rear)
  • 28. { printf("Queue is Empty"); } else { printf("deleted : %d", queue[front]); for(i=1; i<rear-1; i++) { queue[i]=queue[i+1]; } rear--; } } OUTPUT 1.Insert 2.Delete 3.Quit Enter your choice : 1 Enter the element :4 1.Insert 2.Delete 3.Quit Enter your choice : 1 Enter the element :6 1.Insert 2.Delete 3.Quit Enter your choice : 2 deleted : 4 ========================================================================================== Ques 11:- WAP to create binary tree and traverse them using recursive function Preorder,Postorder and Inorder traversing.
  • 29. #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* left; struct node* right; }; struct node* newNode(int data) { struct node* temp; temp= (struct node*)malloc(sizeof(struct node)); temp->data = data; temp->left = NULL; temp->right = NULL; return(temp); } void postorder(struct node* P) { if (P == NULL) return; postorder(P->left); postorder(P->right); printf("%d ", P->data); } void inorder(struct node* P) { if (P == NULL) return; inorder(P->left); printf("%d ", P->data);
  • 30. inorder(P->right); } void preorder(struct node* P) { if (P == NULL) return; printf("%d ", P->data); preorder(P->left); preorder(P->right); } int main() { struct node *root; root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); printf("nPreorder traversal of binary tree is n"); preorder(root); printf("nInorder traversal of binary tree is n"); inorder(root); printf("nPostorder traversal of binary tree is n"); postorder(root); return 0; } OUTPUT Preorder traversal of binary tree is 1 2 4 5 3 Inorder traversal of binary tree is
  • 31. 4 2 5 1 3 Postorder traversal of binary tree is 4 5 2 3 1 ======================================================================================= Ques 12:- WAP to implement Linear search operation using arrays. #include <stdio.h> int main() { int array[100], search, i, n; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d integer(s)n", n); for (i = 0; i < n; i++) scanf("%d", &array[i]); printf("Enter a number to searchn"); scanf("%d", &search); for (i = 0; i < n; i++) { if (array[i] == search) /* If required element is found */ { printf("%d is present at location %d.n", search, i+1); break; } } if (i == n) printf("%d isn't present in the array.n", search); return 0; } OUTPUT Enter number of elements in array 4
  • 32. Enter 4 integer(s) 2 5 8 3 Enter a number to search 8 8 is present at location 3. ========================================================================================== Ques 13:- WAP to implement Binary search opertation using arrays. #include <stdio.h> int main() { int i, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d",&n); printf("Enter %d integersn", n); for (i = 0; i < n; i++) { scanf("%d",&array[i]); } printf("Enter value to findn"); 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
  • 33. last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d isn't present in the list.n", search); return 0; } OUTPUT Enter number of elements 5 Enter 5 integers 4 5 3 8 9 Enter value to find 8 8 found at location 4. ========================================================================================= Ques 14:- WAP to implement Bubble sort opertation using arrays. #include <stdio.h> #include<stdlib.h> int main() { int array[100], n, i, j, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (i = 0; i < n; i++) { scanf("%d", &array[i]); } for (i = 0 ; i < n - 1; i++) {
  • 34. for (j = 0 ; j < n - i - 1; j++) { if (array[j] > array[j+1]) /* For decreasing order use < */ { swap = array[j]; array[j] = array[j+1]; array[j+1] = swap; } } } printf("Sorted list in ascending order:n"); for (i = 0; i < n; i++) { printf("%dn", array[i]); } return 0; } OUTPUT Enter number of elements 5 Enter 5 integers 5 4 7 9 2 Sorted list in ascending order: 2 4 5 7 9 ====================================================================================== Question 15:- WAP to implement Insertion sort operation using arrays. #include <math.h>
  • 35. #include <stdio.h> /* Function to sort an array using insertion sort*/ void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } void printArray(int arr[], int n) { int i; printf("nList after sortingn"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("n"); } int main() { int i; int arr[5] = { 12, 11, 13, 5, 6 }; printf("List before sortingn"); for(i = 1; i <= arr[4]; i++)
  • 36. { printf("%d ", arr[i]); } int n = sizeof(arr) / sizeof(arr[0]); insertionSort(arr, n); printArray(arr, n); return 0; } OUTPUT List before sorting 11 13 5 6 0 1 List after sorting 5 6 11 12 13 ======================================================================================= Question 16:- WAP to implement Selection sort operation using arrays. #include <stdio.h> int main() { int array[100], n, i, j, position, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (i = 0; i < n; i++) { scanf("%d", &array[i]); } for (i = 0; i < (n - 1); i++) { position = i; for (j = i + 1; j < n; j++) {
  • 37. if (array[position] > array[j]) position = j; } if (position != i) { swap = array[i]; array[i] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:n"); for (i = 0; i < n; i++) { printf("%dn", array[i]); } return 0; } OUTPUT Enter number of elements 5 Enter 5 integers 5 8 9 3 2 Sorted list in ascending order: 2 3 5 8 9 ====================================================================================== Question 17:- WAP to implement Merge sort operation using arrays. #include <stdio.h>
  • 38. #define max 10 int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 }; int b[10]; void merging(int low, int mid, int high) { int l1, l2, i; for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if(a[l1] <= a[l2]) b[i] = a[l1++]; else b[i] = a[l2++]; } while(l1 <= mid) b[i++] = a[l1++]; while(l2 <= high) b[i++] = a[l2++]; for(i = low; i <= high; i++) a[i] = b[i]; } void sort(int low, int high) { int mid; if(low < high) { mid = (low + high) / 2; sort(low, mid); sort(mid+1, high); merging(low, mid, high); } else { return; } } int main() {
  • 39. int i; printf("List before sortingn"); for(i = 0; i <= max; i++) { printf("%d ", a[i]); } sort(0, max); printf("nList after sortingn"); for(i = 0; i <= max; i++) printf("%d ", a[i]); } OUTPUT List before sorting 10 14 19 26 27 31 33 35 42 44 0 List after sorting 0 10 14 19 26 27 31 33 35 42 44 ===================================================================================== Question 18:- WAP to implement Heap sort operation using arrays. #include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAXSIZE 5 #define MAX 5 void main() { int a[MAX],n,i,s,f,item,value; printf("Enter the number of elements:n"); scanf("%d",&n); printf("Enter %d elements one by onen",n); for(i=0; i<n; i++) {
  • 40. scanf("%d", &a[i]); } for(i=0;i<n;i++) { item=a[i]; s=i; f=(s-2)/2; while(s>0 && a[f]<item) { a[s]=a[f]; s=f; f=(s-2)/2; } a[s]=item; } for(i=n-1;i>0;i--) { value=a[i]; a[i]=a[0]; f=0; if(i==1) s=-1; else s=1; if(i>2 && (a[2]>a[1])) s=2; while(s>=0 && value<a[s]) { a[f]=a[s]; f=s; s=2*f+1;
  • 41. if(s+1 <=i-1 && (a[s]<a[s+1])) s=s+1; if(s>i-1) s=-1; } a[f]= value; } printf("The sorted list is n"); for(i=0; i<n; i++) { printf("%dn",a[i]); } getch(); } OUTPUT Enter the number of elements: 5 Enter 5 elements one by one 6 4 2 8 1 The sorted list is 1 2 6 4 8 ========================================================================================= Question 19:- WAP to evaluate Post fix expression using stack. #include<stdio.h> #include<ctype.h> # define MAXSTACK 100 # define POSTFIXSIZE 100
  • 42. int stack[MAXSTACK]; int top = -1 ; void push(int item) { if(top >= MAXSTACK -1) { printf("stack over flow"); return; } else { top = top + 1 ; stack[top]= item; } } int pop() { int item; if(top <0) { printf("stack under flow"); } else { item = stack[top]; top = top - 1; return item; } } void EvalPostfix(char postfix[]) {
  • 43. int i ; char ch; int val; int A, B ; for (i = 0 ; postfix[i] != ')'; i++) { ch = postfix[i]; if (isdigit(ch)) { push(ch - '0'); } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { A = pop(); B = pop(); switch (ch) { case '*': val = B * A; break; case '/': val = B / A; break; case '+': val = B + A; break; case '-': val = B - A; break; } push(val); /* push the value obtained above onto the stack */
  • 44. } } printf( " n Result of expression evaluation : %d n", pop()) ; } int main() { int i ; char postfix[POSTFIXSIZE]; printf("Use only four operator +,-,*,/ and also single digit only.n"); printf( "nEnter postfix expression and last used ')' : "); for (i = 0 ; i <= POSTFIXSIZE - 1 ; i++) { scanf("%c", &postfix[i]); if ( postfix[i] == ')' ) break; } EvalPostfix(postfix); return 0; } OUTPUT Use only four operator +,-,*,/ and also single digit only. Enter postfix expression and last used ')' : 56+) Result of expression evaluation : 11 ========================================================================================== Question 20:- WAP to implement conversion algorithm from Pre fix expression to Post fix expression. #include<stdio.h> #include<string.h> void push(char item[],int *top,char s[][20]) { *top=*top+1; strcpy(s[*top],item);
  • 45. } void *pop(int *top,char s[][20]) { char *item; item=s[*top]; *top=*top-1; return item; } void pre_post(char prefix[],char postfix[]) { char s[20][20]; int top,i; char symbol,temp[2]; char *op1,*op2; top=-1; strrev(prefix); for(i=0;i<strlen(prefix);i++) { symbol=prefix[i]; temp[0]=symbol; temp[1]='0'; switch (symbol) { case '+': case '-': case '*': case '/': case '^': op1=pop(&top,s); op2=pop(&top,s); strcpy(postfix,op1);
  • 46. strcat(postfix,op2); strcat(postfix,temp); push(postfix,&top,s); break; default: push(temp,&top,s); } } } void main() { char prefix[20]; char postfix[20]; printf("nn Enter the prefix expression nn"); scanf("%s",prefix); pre_post(prefix,postfix); printf("nn The postfix expression is %s nn",postfix); } OUTPUT Enter the prefix expression +56 The postfix expression is 56+