ADSA
ADSA
Algorithm:
Step 1: Start
Step 2: Initialize and declare variables using structure and arrays. Define the required size of
header files
Step 3: Enter the operations to perform in list as
1)Create
2)Search
3)Display
4)Exit
Step 4: Based on the operations choosing, the list elements are structured.
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void search();
void display();
int a,b[20],n,e,i,pos;
void main()
{
int m;
char ch;
do
{
printf("\n main Menu");
Result:
Thus a C program for array implementation of list is executed successfully.
Aim: To write and implement a program in C for array implementation of Stack and Queue
ADTs.
Algorithm: Stack
Step 1: Start
Step 2: Checks if the stack is full.
Step 3: If the stack is full, produces an error and exit.
Step 4: For Insertion
If the stack is not full, increments top to point next empty space.
Adds data element to the stack location, where top is pointing.
Step 5: For Deletion
If the stack is not empty, accesses the data element at which top is pointing.
Decreases the value of top by 1.
Step 6: Stop
Program:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
Output:
Enter the size of STACK[MAX=100]:10
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12
EXIT POINT
Algorithm: Queue
Step 1: Start
Step 2: Check if the queue is full.
Step 3: If the queue is full, produce overflow error and exit.
Step 4: For insertion
If the queue is not full, increment rear pointer to point the next empty space.
Add data element to the queue location, where the rear is pointing.
Step 5: For Deletion
If the queue is not empty, access the data where front is pointing.
Increment front pointer to point to the next available data element.
Step 6: Stop
Output:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Result:
Thus a C program using array implementation of Stack and Queue ADTs is implemented
successfully.
Aim: To write and implement a C program Linked list implementation of List ADTs.
node* getnode()
{
node *n;
n=(node*)malloc(sizeof(node));
n->next=NULL;
return(n);
}
void create()
{
char ch=’y';
head->next=NULL;
do
{
newnode =getnode();
printf(“\n enter the element:”);
scanf(“%d”,&newnode->data);
if(head->next==NULL)
head->next=newnode;
else
temp->next=newnode;
temp=newnode;
void display()
{
printf(“\n\nthe list is:”);
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf(“\t%d”,temp->data);
}
getch();
}
void insert()
{
int pos;
temp=head->next;
newnode=getnode();
printf(“\n\nenter the new element & pos element aft to be inserted:”);
scanf(“%d%d”,&newnode->data,&pos);
while(temp->data!=pos&&temp->next!=NULL)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
printf(“\nnew node inserted…”);
display();
void first()
{
newnode=getnode();
printf(“\nenter the new element:”);
scanf(“%d”,&newnode->data);
newnode->next=head->next;
head->next=newnode;
printf(“\nnew node inserted at first…”);
display();
}
void del()
{
int pos;
node *temp1;
temp=head->next;
printf(“\n enter the pos element:”);
scanf(“%d”,&pos);
temp1=head;
while(temp->data!=pos&&temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
free(temp);
printf(“\n\nnode deleted…”);
display();
void main()
{
int op;
do
{
clrscr();
printf(“\n\n\t\tlinked list implementation of list”);
printf(“\n1.create\n2.display\n3.insert\n4.insert first\n5.search\n6.delete\n7.exit”);
printf(“\n enter the option:”);
Result:
Thus a C program using Linked list implementation of List ADTs is implemented successfully.
Aim: To write and execute a C program to convert infix to postfix using stack.
Algorithm:
Step 1: Start
Step 2: Push “(“onto Stack, and add “)” to the end of X.
Step 3: Scan X from left to right and repeat Step 3 to 6 for each element of X until
the Stack is empty.
Step 4: If an operand is encountered, add it to Y.
Step 5: If a left parenthesis is encountered, push it onto Stack.
Step 6: If an operator is encountered ,then:
Repeatedly pop from Stack and add to Y each operator (on the top of
Stack) which has the same precedence as or higher precedence than
operator.
Add operator to Stack.
[End of If]
Step 7: If a right parenthesis is encountered ,then:
Repeatedly pop from Stack and add to Y each operator (on the top of
Stack) until a left parenthesis is encountered.
Remove the left Parenthesis.
[End of If]
[End of If]
Step 8: Stop.
Program:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char 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(top != -1)
{
printf("%c ",pop());
}return 0;
}
Output:
Enter the expression : a+b*c
abc*+
Result:
Thus a C program to convert infix to postfix using stack is implemented successfully.
Algorithm:
Step 1: Start
Step 2: Create a Binary Tree.
Step 3: Preorder(tree)
Visit the root.
Traverse the left subtree, i.e., call Preorder(left-subtree)
Traverse the right subtree, i.e., call Preorder(right-subtree)
Step 4: Inorder(tree)
Traverse the left subtree, i.e., call Inorder(left-subtree)
Visit the root.
Traverse the right subtree, i.e., call Inorder(right-subtree)
Step 5: Postorder(tree)
Traverse the left subtree, i.e., call Postorder(left-subtree)
Traverse the right subtree, i.e., call Postorder(right-subtree)
Visit the root.
Step 6: Stop
Program:
// C program for different tree traversals
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
Result:
Thus a C program to implement operations in binary trees is implemented successfully.
Aim: To write and execute a C program for implementation of Binary search trees.
Algorithm:
Step 1: Start
Step 2: Create a root node
Step 3: Starting from the root. Compare the inserting element with root,
if less than root, then recursively call left subtree, else
recursively call right subtree.
Step 4: After reaching the end, just insert that node at left(if less than
current) or else right.
Step 5: Print the inorder elements of the tree
Program:
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
// create a new BST node
struct node* newNode(int item)
{
struct node* temp
= (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
return node;
}
// Driver Code
int main()
{
struct node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
Result:
Thus a C program for binary search tree implementation is executed successfully.
Aim: To write and execute a program in C for implementing linear search technique.
Algorithm:
Linear_Search(a, n, val) // 'a' is the given array, 'n' is the size of given array, 'val' is the value to s
earch
Step 1: Start
Step 2: set pos = -1 and i = 1
Step 3: repeat step 4 while i <= n
Step 4: if a[i] == val
set pos = i
print pos
go to step 6
[end of if]
set i = i + 1
[end of loop]
Step 5: if pos = -1
print "value is not present in the array "
[end of if]
Step 6: exit
Program:
#include <stdio.h>
int linearSearch(int a[], int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
Output:
Result:
Thus a C program for implementing linear search is executed successfully.
Aim: To write a execute C programs for Implementation of Sorting algorithms: Insertion Sort,
Quick Sort, Merge Sort.
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one pos
ition ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
insert(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Output:
return 0;
}
Output:
Output:
Result:
Thus a C program for Implementation of Sorting algorithms: Insertion Sort, Quick Sort, Merge
Sort is executed successfully.
Aim: To write and execute C program for Spanning Tree Implementation – Prim’s Algorithm.
Algorithm:
Step 1: Randomly choose any vertex.The vertex connecting to the edge having least weight is
usually selected.
Step 2:
Find all the edges that connect the tree to new vertices.
Find the least weight edge among those edges and include it in the existing tree.
If including that edge creates a cycle, then reject that edge and look for the next least
weight edge.
Step 3: Keep repeating step-02 until all the vertices are included and Minimum Spanning Tree
(MST) is obtained.
Program:
#include<stdio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
printf("n Enter the number of nodes:");
scanf("%d",&n);
Result:
Thus a C program for Implementation of Prim’s Algorithm is executed successfully.
Aim: To write and execute C programs for Implementation of Shortest Path Algorithms
Dijkstra's Algorithm.
Algorithm:
Step 1 : Create a set shortPath to store vertices that come in the way of
the shortest path tree.
Step 2 : Initialize all distance values as INFINITE and assign distance
values as 0 for source vertex so that it is picked first.
Step 3 : Loop until all vertices of the graph are in the shortPath.
Step 3.1 : Take a new vertex that is not visited and is nearest.
Step 3.2 : Add this vertex to shortPath.
Step 3.3 : For all adjacent vertices of this vertex update distances. Now check every
adjacent vertex of V, if sum of distance of u and weight of edge is elss the
update it.
Program:
#include <limits.h>
#include <stdio.h>
#define V 9
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSolution(int dist[], int n) {
printf("Vertex Distance from Source ");
for (int i = 0; i < V; i++)
Result:
Thus a C program for Implementation of Shortest Path Algorithms Dijkstra's Algorithm is
executed successfully.