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.
(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();
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;
}
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.
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>
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);