SlideShare a Scribd company logo
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+

More Related Content

PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
PDF
Haskell 101
PDF
R Programming: Mathematical Functions In R
DOC
Database management system file
PPSX
Tuga it 2016 - What's New In C# 6
PPSX
What's new in C# 6 - NetPonto Porto 20160116
PDF
Refactoring to Macros with Clojure
PPTX
Poor Man's Functional Programming
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Haskell 101
R Programming: Mathematical Functions In R
Database management system file
Tuga it 2016 - What's New In C# 6
What's new in C# 6 - NetPonto Porto 20160116
Refactoring to Macros with Clojure
Poor Man's Functional Programming

What's hot (20)

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

Similar to Most Important C language program (20)

PDF
TutorialII_Updated____niceupdateprogram.pdf
DOC
Final ds record
PDF
For this homework, you will write a program to create and manipulate.pdf
PDF
pleaase I want manual solution forData Structures and Algorithm An.pdf
DOCX
Data structure and algorithm lab spiral (1) (4) (1).docx
DOC
Ds lab manual by s.k.rath
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
DOCX
hello- please dont just copy from other answers- the following is the.docx
PDF
Sorted number list implementation with linked listsStep 1 Inspec.pdf
DOCX
Please solve the following problem using C++- Thank you Instructions-.docx
PDF
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
PDF
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
PDF
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
PPT
MO 2020 DS Applications of Linked List 1 AB.ppt
PPTX
Singly linked list.pptx
PDF
C program
PDF
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
DOCX
coding in C- Create a function called reverseList that takes the head.docx
PDF
Singly Linked List
PPTX
class 2.pptxdsafe fnbwfl;d,qgyewbjdkqwd;wd
TutorialII_Updated____niceupdateprogram.pdf
Final ds record
For this homework, you will write a program to create and manipulate.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
Data structure and algorithm lab spiral (1) (4) (1).docx
Ds lab manual by s.k.rath
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
Sorted number list implementation with linked listsStep 1 Inspec.pdf
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 isPage 349-350 #4 and #5 Use the Linked List lab.pdf
CS3381 OBJECT ORIENTED PROGRAMMINGLABS_1.pdf
MO 2020 DS Applications of Linked List 1 AB.ppt
Singly linked list.pptx
C program
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
Singly Linked List
class 2.pptxdsafe fnbwfl;d,qgyewbjdkqwd;wd
Ad

More from TEJVEER SINGH (13)

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

Recently uploaded (20)

PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
composite construction of structures.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
OOP with Java - Java Introduction (Basics)
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Welding lecture in detail for understanding
PPTX
additive manufacturing of ss316l using mig welding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
bas. eng. economics group 4 presentation 1.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Foundation to blockchain - A guide to Blockchain Tech
composite construction of structures.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Mechanical Engineering MATERIALS Selection
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
OOP with Java - Java Introduction (Basics)
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Lecture Notes Electrical Wiring System Components
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Welding lecture in detail for understanding
additive manufacturing of ss316l using mig welding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

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+