Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Record

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

INDEX

S.NO PROGRAMS
1 Array Implementation
2 Linked list

3 Stack using Array

4 Queue using Array

5 Stack using Linked List

6 Queue using Linked List

7 Binary Tree Traversal

8 Binary Search Tree

9 Linear Search
10 Binary Search

11 Bubble Sort
12 Insertion Sort
13 Evaluation of Postfix Expression
14 Infix to Postfix Conversion
EX NO: 1 ARRAY IMPLEMENTATION

Aim:

To implement array using C language

Algorithm:

Step 1: Start
Step 2: Get the number of elements in the array as input, N.

Create & Display


Step 1: Get n number of elements as input
Step 2: Display all the array elements.

Insert:
Step 1: Get the position where to insert the element and the element to be inserted in the array.
Step 2: Insert the received element in the specific position in the array.

Delete:
Step 1: Get the position from where the element to be deleted.
Step 2: Delete the element in the specific position in the array by reordering the remaining elements.

Search:
Step 1: Get the element to be searched.
Step 2: Search the entire array from the beginning and if the element is found in the array print “Element is
present” and if the element is not in the array then print the message “Element is not present”.
Step 3: End
Program 1:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define maxsize 10
int list[maxsize],n;
void Create();
void Insert();
void Delete();
void Display();
void Search();
void main()
{
int choice;
clrscr();
do
{
printf("\n Array Implementation of List\n");
printf("\t1.create\n");
printf("\t2.Insert\n");
printf("\t3.Delete\n");
printf("\t4.Display\n");
printf("\t5.Search\n");
printf("\t6.Exit\n");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: Create();
break;
case 2: Insert();
break;
case 3: Delete();
break;
case 4: Display();
break;
case 5: Search();
break;
case 6: exit(0);
default: printf("\nEnter option between 1 - 6\n");
break;
}
}while(choice<7);
}

void Create()
{
int i;
printf("\nEnter the number of elements to be added in the list:\t");
scanf("%d",&n);
printf("\nEnter the array elements:\t");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
Display();
}
void Insert()
{
int i,data,pos;
printf("\nEnter the data to be inserted:\t");
scanf("%d",&data);
printf("\nEnter the position at which element to be inserted:\t");
scanf("%d",&pos);
for(i = n-1 ; i >= pos-1 ; i--)
list[i+1] = list[i];
list[pos-1] = data;
n+=1;
Display();
}
void Delete( )
{
inti,pos;
printf("\nEnter the position of the data to be deleted:\t");
scanf("%d",&pos);
printf("\nThe data deleted is:\t %d", list[pos-1]);
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1];
n=n-1;
Display();
}
void Display()
{
int i;
printf("\n*****Elements in the array***\n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}
void Search()
{
intsearch,i,count = 0;
printf("\nEnter the element to be searched:\t");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(search == list[i])
{
count++;
}
}
if(count==0)
printf("\nElement not present in the list");
else
printf("\nElement present in the list");
}
OUTPUT:

Array implementation of list


1. Create
2. Insert
3. Delete
4. Display
5. Search
6. Exit

Enter your choice


1

Enter the number of elements to be added in list


4

Enter the array elements


1
2
3
4
Result:

The array creation and manipulation was executed successfully using C program.
LINKED LIST
EX NO: 2
Singly Linked List

Aim:

To write a c program for implementation of singly linked list.

Algorithm:

Step 1- Start the process.


Step 2- Use the function for inserting at beginning, inserting at end, inserting at specified position, display,
deleting at beginning, deleting at end and deleting at specified position.
Step 3- Implement switch statement for insert menu, display, delete menu and exit.
Step 4- Implement a while loop.
Step 5- Implement if else conditional statements for each of the options in insert menu function definition.
Step 6- Implement if else conditional statements for each of the options in delete emenu function definition.
Step 7- Implement the if else conditional statements inside display function definition.
Step 8- Terminate the process.
Program 2:

#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;
int main()
{
Int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();

while(1)
{
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("\n---- Insert Menu ----");
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;

case 2: display();
break;

case 3: printf("\n---- Delete Menu ----");


printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: delete_beg();
break;
case 2: delete_end();
break;
case 3: delete_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
}
return 0;
}

void insert_beg()
{
Int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;

if(start==NULL) //If list is empty


{
t->next=NULL;
start=t;
}
else
{
t->next=start;
start=t;
}
}

void insert_end()
{
Int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;

if(start==NULL) //If list is empty


{
start=t;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
}

int insert_pos()
{
intpos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}

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


printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;

q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}

q=q->next;
}

t->next=q->next;
q->next=t;
return 0;
}
void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{
q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}

void delete_beg()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}

void delete_end()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
while(q->next->next!=NULL)
q=q->next;

t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}

int delete_pos()
{
int pos,i;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}

printf("Enter position to delete:");


scanf("%d",&pos);

q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);

return 0;
}
OUTPUT:

Single linked list menu


1. Insert
2. Delete
3. Display
4. Exit

Enter 1 to insert at the beginning


2 to insert at a specific position
1

Enter the elements to be insert


100

After insert elements

3. for display
100
200
300
Result:

The C program for implementation of singly linked list is successfully created.


EX.NO:3 ARRAY IMPLEMENTATION OF STACK

AIM:

To implement STACK ADT using array in C Language.

ALGORITHM:

Step 1: Start
Step 2: Define a stack size.
Step 3: Read the stack operation.
Step 4: Read the stack element.
Step 5: If the stack operation is push() then
Check if the stack is full, if it is not full add the new element to the top of the stack
Otherwise print “Stack is Full”
Step 6: if the stack operation is pop() then
Check if the stack is empty, if it is not empty delete the top element from the stack
Otherwise print “Stack is Empty”
Step 7: Stop
Program 3:

#include <stdio.h>
#include<conio.h>
// Function declarations
void push();
void pop();
void peek();
// Taking stack array of size 50
int stack[50],i,j,option=0,n,top=-1;
void main ()
{
// Size of stack
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("Stack implementation using array\n");

// Taking user input for the operation to be performed


while(option != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Peek\n4.Exit");
printf("\n Enter your option \n");
scanf("%d",&option);
switch(option)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
printf("Exiting");
break;
}
default:
{
printf("Please Enter valid option");
}
};
}
getch();
}

// Push operation function


void push ()
{
int value;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = value;
}
}

// Pop operation function


void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}

// peek operation function


void peek()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}
OUTPUT:

Enter the number of elements in the stack


4
Choose one from below opotion
1.PUSH
2.POP
3.PEEK
4.EXIT

1
Enter the value
100
Output after pushing 4 elementss
400
300
200
100
Output after poping the 1st elements
300
200
100
RESULT:

Thus the c program to implement stack ADT using array in c language has been executed successfully
EX.NO:4 ARRAY IMPLEMENTATION OF QUEUE

AIM:

To implement QUEUE ADT using array in C Language.

ALGORITHM:
Step 1: Start
Step 2: Define a queue size.
Step 3: Read the queue operation.
Step 4: Read the queue elements
Step 5: If the queue operation is insert() then
Check if the queue is full, if it is not full add the new element to the rear side of the
Queue, Otherwise print “Queue is Full”
Step 6: if the queue operation is delete() then
Check if the queue is empty, if it is not empty delete the element from the front side of
the queue, Otherwise print “Queue is Empty”
Step 7: Stop
Program 4:

#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
intqueue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */

void insert()
{
intadd_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
OUTPUT:

Enter the numder of elements in the queue


4
Enter your choice
2.insert
2.delete
3.display
4.exit

1 enter the value


1000
Output after inserting four element
100
200
300
400
Output aftyer deleting the 1st element
200
300
400
RESULT:

Thus the c program to implement queue ADT using array in c language has been executed successfully..
EX.NO:5 LINKED LIST IMPLEMENTATION OF STACK

AIM:
To implement STACK ADT using Linked List in C Language.

ALGORITHM:
Step 1: Start
Step 2: Read the stack operation.
Step 3: Read the stack element.
Step 4: If the stack operation is push() then add the new element to the top of the stack
Step 5: if the stack operation is pop() then delete the top element from the stack
Step 6: Stop
Program 5:

#include <stdio.h>
#include <stdlib.h>

// Structure to create a node with data and the next pointer


struct node {
int info;
struct node *ptr;
}*top,*top1,*temp;

int count = 0;
// Push() operation on a stack
void push(int data) {
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
printf("Node is Inserted\n\n");
}

int pop() {
top1 = top;

if (top1 == NULL)
{
printf("\nStack Underflow\n");
return -1;
}
else
top1 = top1->ptr;
int popped = top->info;
free(top);
top = top1;
count--;
return popped;
}

void display() {
// Display the elements of the stack
top1 = top;

if (top1 == NULL)
{
printf("\nStack Underflow\n");
return;
}
printf("The stack is \n");
while (top1 != NULL)
{
printf("%d--->", top1->info);
top1 = top1->ptr;
}
printf("NULL\n\n");

int main() {
int choice, value;
printf("\nImplementation of Stack using Linked List\n");
while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped element is :%d\n", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
OUTPUT:

Implementation of stack using linked list choose one from below option
1.PUSH
2.POP
3.PEEK
4.EXIT
Enter the element to be inserted
100
Output after inserting 4 elements
400
300
200
100
Output after deleting the top elements
300
200
100
RESULT:

Thus the c program to implement stack ADT using linked list c language has been executed successfully.
EX.NO:6 LINKED LIST IMPLEMENTATION OF QUEUE

AIM:
To implement QUEUE ADT using Linked List in C Language.

ALGORITHM:
Step 1: Start
Step2: Read the queue operation.
Step 3: Read the queue elements
Step 4: If the queue operation is insert() then add the new element to the rear side of the queue
Step 5: if the queue operation is delete() then delete the element from the front side of the queue
Step 6: Stop
Program 6:

#include <stdio.h>
#include <stdlib.h>
// Structure to create a node with data and the next pointer
struct node {
int data;
struct node * next;
};
struct node * front = NULL;
struct node * rear = NULL;

// Enqueue() operation on a queue


void enqueue(int value) {
struct node * ptr;
ptr = (struct node * ) malloc(sizeof(struct node));
ptr - > data = value;
ptr - > next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear - > next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}

// Dequeue() operation on a queue


int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node * temp = front;
inttemp_data = front - > data;
front = front - > next;
free(temp);
returntemp_data;
}
}

// Display all elements of the queue


void display() {
struct node * temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d--->", temp - > data);
temp = temp - > next;
}
printf("NULL\n\n");
}
}

int main() {
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", & value);
enqueue(value);
break;
case 2:
printf("Popped element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
return 0;
}
Output:

1 - Enqueue

2 – Dequeue

3- Display

4- Exit

Enter choice: 1

Enter data: 14

Enter choice: 1

Enter data: 85

Queue size: 3

Enter choice: 2

Dequeued value: 14

Enter choice: 4
RESULT:

Thus the c program to implement Queue ADT using linked list c language has been executed successfully.
EX NO: 7 BINARY TREE TRAVERSAL

AIM:
To write a C program to implement binary Tree Traversal..

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the structure for node
STEP 3: Declare all variables
STEP 4: Recursively apply pre-order, post-order and in-order
STEP 5: Display the nodes
STEP 7: Stop
Program 7:

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void printPostorder(struct node* node)
{
if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree
printPostorder(node->right);
// now deal with the node
printf("%d ", node->data);
}

void printInorder(struct node* node)


{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
printf("%d ",node->data);

/* now recur on right child */


printInorder(node->right);
}

/* Given a binary tree, print its nodes in preorder*/


void printPreorder(struct node* node)
{
if (node == NULL)
return;

/* first print data of node */


printf("%d ", node->data);
/* then recur on left sutree
*/ printPreorder(node->left);

/* now recur on right subtree */


printPreorder(node->right);
}
/* Driver program to test above functions*/
int main()
{
struct node *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");
printPreorder(root);
printf("\nInorder traversal of binary tree is \n");
printInorder(root);
printf("\nPostorder traversal of binary tree is \n");
printPostorder(root);
getchar();
return 0;

}
Output:

Preorder traversal of binary tree is

12453

Inorder traversal of binary tree is

42513

Postorder traversal of binary tree is

45231
Result:

Thus the c program to implement Binary tree traversal has been executed successfully.
EX. NO: 8
BINARY SEARCH TREE

AIM:

To write a C program to perform Insert, Delete, Search an element into a binary search tree.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the structure for node
STEP 3: Declare all variables
STEP 4: Recursively perform Insert, Delete and Search for elements in the Binary Tree
STEP 5: Display the nodes
STEP 7: Stop
Program 8:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;

returnnewNode;
}
struct node* insert(struct node* root, int data)
{
if (root == NULL) return createNode(data);

if (data < root->data)


root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}

void inorder(struct node* root){


if(root == NULL) return;
inorder(root->left); printf("%d -
>", root->data); inorder(root-
>right);
}
int main(){
struct node *root = NULL;
root = insert(root, 8);
insert(root,3);
insert(root,1);
insert(root,6);
insert(root,7);
insert(root,10);
insert(root,14);
insert(root, 4);
inorder(root);
}
OUTPUT:

1 ->3 ->4 ->6 ->7 ->8 ->10 ->14 ->


RESULT:

Thus the C program for implementation of binary search tree has been executed successfully.
EX. NO: 9
LINEAR SEARCH

AIM:

To write a C program to perform linear search in one dimensional array.

ALGORITHM:
Step 1: Get an array as input
Step 2: Get the element to be searched let it be x
Step 3: Search the array for the element x starting from the beginning of the array.
Step 4: If the element x is in the array then display “element found” or display “element not found”.
Program 9:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,m,c=0; clrscr();
printf("Enter the size of an array: "); scanf("%d",&n);
printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: "); scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
if(a[i]==m)
{
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list"); else
printf("The number is found"); getch();
}
Output:
Result:

Thus the Linear search program has been executed successfully.


EX.NO: 10
BINARY SEARCH

AIM:
To Write a C program to perform Binary Search in one dimensional array.

ALGORITHM:
Step 1: Get an array as input.
Step 2: Get the element to be searched as input, let it be x.
Step 3: Sort the given array.
Step 4: find the mid element of the array.
Step 5: If the element x is less than the value of the mid element then do the searching in the left side of the
mid element or do the searching in the right side of the mid element.
Step 6: Repeat step 4 and 5 unit the element is found or the number of elements to be searched is zero.
Program 10:

#include<stdio.h>
#include<conio.h>
void main()
{

int a[10],i,n,m,c=0,l,u,mid;
clrscr();
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){ u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found."); else
printf("The number is found.");

getch();
}
Output:
Result:

Thus the Binary search program has been executed successfully


EX. NO: 11
BUBBLESORT

AIM:

To write a C program to perform bubble sort for the given numbers.

ALGORITHM:
Step 1: Start the program.
Step 2: Get an array of numbers as input(i.e) list. num is the total number of elements in the array
Step 3: for(i=0;i<num-1;i++)
for(j=i+1;j<=num-i;j++).
if(list[j]>list[j+1]).
:do temp=list[j]. list[j]=list[j+1]. List[j+1]=temp.
Step 4:Display the elements present in the array

51
Program 11:

#include<stdio.h>
#include<conio.h>
void main()
{
int list[20],num,min,temp,i,j;
clrscr();
printf("\n enter the number of elements(max.20) \t");
scanf("%d",&num);
printf("\n enter the elements:");
for(i=0;i<num;i++)
scanf("%d",&list[i]);
for(i=0;i<num-1;i++)
for(j=0;j<num-1-i;j++)
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
printf("\n after sorting elements are:");
for(i=0;i<num;i++)
printf("%d \n",list[i]);
getch();
}

52
OUTPUT:

53
RESULT:

Thus a C program to perform the bubble sort for the given numbers is executed and verified
successfully.

54
EX.NO: 12
INSERTION SORT

Aim

To sort the given numbers in ascending order using Insertion sort technique

Algorithm

Step 1: Start the program.


Step 2: Get an array of numbers as input (i.e) list. N is the total number of elements in the array arr[].
Step 3: Iterate from arr[1] to arr[N] over the array.
Step 4: Compare the current element (key) to its predecessor.
If the key element is smaller than its predecessor, compare it to the elements before. Move the greater
elements one position up to make space for the swapped element.
Step 5: Print the sorted numbers

55
Program 12:

// C program for insertion sort


#include <stdio.h>
#include <math.h>
#include<conio.h>

/* Function to sort an array using insertion sort*/


voidinsertionSort(intarr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while (j >= 0 &&arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// A utility function to print an array of size n


voidprintArray(intarr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

/* Driver program to test insertion sort */


void main()
{
intarr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
getch();
}

56
Output:

5 6 11 12 13

57
Result:

The given numbers were arranged in ascending order using Insertion sort technique.

58
EX. NO:13
EVALUATION OF POSTFIX EXPRESSION

Aim:

To evaluate the given Postfix Expression using Stack

Algorithm:
Step 1: Create a stack to store operands.
Step 2. Scan the given expression from left to right.
Step 3. a) If the scanned character is an operand, push it into the stack.
b) If the scanned character is an operator, POP 2 operands from stack and perform operation and
PUSH the result back to the stack.
Step 4. Repeat step 3 till all the characters are scanned.
Step 5. When the expression is ended, the number in the stack is the final result.

59
Program 13:

#include<stdio.h>
#include<conio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

void main()
{
charexp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
60
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
getch()
}

61
Output:

Enter the expression :: 245+*

The result of expression 245+* = 18

62
Result:

Thus the given Postfix Expression was evaluated using Stack.

63
EX. NO: 14 Infix to Postfix Conversion

Aim:

To convert the given infix expression to postfix expression using Stack.

Algorithm:

Step 1: Start the program


Step 2: Scan all the symbols one by one from left to right in the given Infix Expression.
Step 3: If the reading symbol is an operand, then immediately append it to the Postfix Expression.
Step 4: If the reading symbol is left parenthesis ‘( ‘, then Push it onto the Stack.
Step5: If the reading symbol is right parenthesis ‘)’, then Pop all the contents of the stack until the respective left
parenthesis is popped and append each popped symbol to Postfix Expression.
Step 6: If the reading symbol is an operator (+, –, *, /), then Push it onto the Stack. However, first, pop the operators
which are already on the stack that have higher or equal precedence than the current operator and append them to the
postfix. If an open parenthesis is there on top of the stack then push the operator into the stack.
Step 7: If the input is over, pop all the remaining symbols from the stack and append them to the postfix.
Step 8: Stop the program

64
Program 14:

#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{

65
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}

66
Output Test Case 1:

Enter the expression :a+b*c

abc*+

Output Test Case 2:

Enter the expression : (a+b)*c+(d-a)

ab+c*da-+

67
Result:

Thus the given Infix to Postfix Conversion was evaluated using Stack.

68

You might also like