DS Record
DS Record
DS Record
S.NO PROGRAMS
1 Array Implementation
2 Linked list
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:
Algorithm:
Step 1: Start
Step 2: Get the number of elements in the array as input, N.
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:
The array creation and manipulation was executed successfully using C program.
LINKED LIST
EX NO: 2
Singly Linked List
Aim:
Algorithm:
#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;
void insert_beg()
{
Int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
void insert_end()
{
Int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;
int insert_pos()
{
intpos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}
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;
}
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:
3. for display
100
200
300
Result:
AIM:
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");
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:
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:
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>
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;
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);
}
}
Output:
12453
42513
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);
Thus the C program for implementation of binary search tree has been executed successfully.
EX. NO: 9
LINEAR SEARCH
AIM:
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:
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:
AIM:
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
55
Program 12:
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:
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:
62
Result:
63
EX. NO: 14 Infix to Postfix Conversion
Aim:
Algorithm:
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:
abc*+
ab+c*da-+
67
Result:
Thus the given Infix to Postfix Conversion was evaluated using Stack.
68