DS Lab Manual (With Code)
DS Lab Manual (With Code)
Deletion: In this array operation, we delete an element from the particular index of an array.
This deletion operation takes place as we assign the value in the consequent index to the
current index.
Searching: Searching an element in the array using a key. The key element sequentially
compares every value in the array to check if the key is present in the array or not.
1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable 'i' as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
Deletion:
To delete a specific element from an array, a user must define the position from which the
array's element should be removed. The deletion of the element does not affect the size of an
array. Furthermore, we should also check whether the deletion is possible or not in an array.
Searching: To understand the working of linear search algorithm, let's take an unsorted array.
It will be easy to understand the working of linear search with an example.
• Now, start from the first element and compare K with each element of the array.
The value of K, i.e., 41, is not matched with the first element of the array. So, move
to the next element. And follow the same process until the respective element is
found.
• Now, the element to be searched is found. So algorithm will return the index of the
element matched.
Source Code
#include<stdio.h>
#include<stdlib.h>
int a[20];
int n, p, i , pos, val;
void create();
void display();
void insert();
void del();
void search();
int main() {
int choice;
do{
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice) {
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
del();
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice. Enter valid input.\n");
break;
}
}while(choice!=5);
return 0;
}
void create() {
printf("Enter the size of the array elements : ");
scanf("%d",&n);
printf("Enter the elements for the array : ");
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
void display() {
printf("The array elements are : ");
for(i=0;i<n;i++) {
printf("%d ",a[i]);
}
printf("\n");
void insert() {
printf("Enter the position(index) for the new element : ");
scanf("%d",&pos);
printf("Enter the element to be inserted : ");
scanf("%d",&val);
if(pos < 0 || pos > n) {
printf("Invalid position.\n");
} else {
for(i=n-1;i>=pos;i--) {
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}
}
void del() {
printf("Enter the position(index) of the element to be deleted : ");
scanf("%d",&pos);
if( pos < 0 || pos >= n) {
printf("Invalid position.\n");
} else {
val=a[pos];
for(i=pos;i<n-1;i++) {
a[i]=a[i+1];
}
n=n-1;
printf("The deleted element is : %d\n",val);
}
Output:
1.Create⏎
2.Display⏎
3.Insert⏎
4.Delete⏎
5.Exit⏎
Enter·your·choice·:·1
Enter·the·size·of·the·array·elements·:·5
Enter·the·elements·for·the·array·:·1 2 3 4 5
1.Create⏎
2.Display⏎
3.Insert⏎
4.Delete⏎
5.Exit⏎
Enter·your·choice·:·2
The·array·elements·are·:·1·2·3·4·5·⏎
1.Create⏎
2.Display⏎
3.Insert⏎
4.Delete⏎
5.Exit⏎
Enter·your·choice·:·3
Enter·the·position(index)·for·the·new·element·:·3
Enter·the·element·to·be·inserted·:·8
1.Create⏎
2.Display⏎
3.Insert⏎
4.Delete⏎
5.Exit⏎
Enter·your·choice·:·2
The·array·elements·are·:·1·2·3·8·4·5·⏎
1.Create⏎
2.Display⏎
3.Insert⏎
4.Delete⏎
5.Exit⏎
Enter·your·choice·:·4
Enter·the·position(index)·of·the·element·to·be·deleted·:·3
The·deleted·element·is·:·8⏎
1.Create⏎
2.Display⏎
3.Insert⏎
4.Delete⏎
5.Exit⏎
Enter·your·choice·:·5
2. Write a program to find minimum and maximum elements in an array.
Problem statement:
To find minimum and maximum elements in an array.
Description :
In this program to input elements in an array from user, find maximum and minimum
element in array. Initially we consider the array’s first element to be both maximum and
minimum value. As and when we traverse the array, we compare the value of the elements in
the array with that of current value of ‘max’ or ‘min’ and respectively assign the values if the
current value is not maximum for ‘max’ and not minimum for ‘min’.
Algorithm :
Step 1 : Take the size of an array from a user and stored it in variable n.
Step 2 : Take n element from the user and store it in an array .
Step 3 : We initialize two variables min and max with arr[0],to store the maximum and
minimum.
Step 4: Now we traverse the array from 1=1 to n-1 and compare each element with min and
max.
Step 5 : Start loop at 1, using index i. Check if arr[i] < min; if so, set min to arr[i] Check if
arr[i] > max; if so, set max to arr[i]
• If(arr[i]<min):we have found a value arr[i] smaller than the minimum so far.so we
update min with arr[i].i.e min=arr[i].
• If(arr[i]>max):We have found a value arr[i] greater than the maximum so far.so ,we
update max with arr[i],i.e max=arr[i].
Step 6 : At end of loop, the minimum and maximum values of the array will be stored in the
variables min and max.
Tracing :
Maximum :
Minimum :
Source Code:
#include <stdio.h>
int main() {
int numbers[100], numCount, i;
int largest = -2147483648, smallest = 2147483647; // initialize to min and max values of
int
scanf("%d", &numCount);
return 0;
}
Output:
5
52 63 45 12 90
Maximum·element·is·90⏎
Minimum·element·is·12⏎
Algorithm :
1. Firstly, input the size n and the elements of the array arr.
2. Initialize an auxiliary array rarr of size same as the array arr.
3. Run a for loop with an integer i=0, where i<0 and i increments by 1 in each iteration.
4. Inside the for loop, assign rarr[i] to be arr[n−i−1].
5. Then copy the elements of the array arr into the array rarr .
6. Print the array rarr
Tracing :
For example, Suppose given an array:
Source Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
}
// logic to reverse the array
int* left_ptr = arr;
int* right_ptr;
int temp;
for(i = 0; i < num; i++) {
if(i == num - 1) {
right_ptr = (arr + i);
}
}
while(left_ptr < right_ptr) {
temp = *right_ptr;
*right_ptr = *left_ptr;
*left_ptr = temp;
right_ptr--;
left_ptr++;
}
4. Write a C program to insert an element at begin and delete at end in Singly Linked
List.
Problem statement:
To insert an element at begin and delete at end in Singly Linked List.
Description:
Given a linked list, the task is to insert a new node at the beginning/start/front of the
linked list.
• Step 1: IF pointer = NULL Then Go to step 7 that is exit otherwise follow step2
[END OF IF]
• Step 2: SET New_Node = pointer
• Step 3: SET pointer = pointer → next
• Step 4: SET New_Node → data= value
• Step 5: SET New_Node →next =head
• Step 6: SET head = New_Node
• Step 7: Exit
Tracing:
There are two scenarios in which, a node is deleted from the end of the linked list.
1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be deleted.
When list has only one node, which is indicated by the condition, that the head points to
the same node as the tail, the removal is quite simple. Algorithm disposes the node, pointed
by head (or tail) and sets both head and tail to NULL.
Second case: This operation is a bit more tricky, than removing the first node, because
algorithm should find a node, which is previous to the tail first.
#include "InsAtBeginAndDelEnd.c"
void main() {
NODE first = NULL;
int x, op;
while(1) {
printf("1.Insert At Begin 2.Delete at End 3.Traverse the List 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter an element : ");
scanf("%d", &x);
first = insertAtBegin(first, x);
break;
case 2:if (first == NULL) {
printf("Single Linked List is empty so deletion
is not possible\n");
} else {
first = deleteAtEnd(first);
}
break;
case 3: if (first == NULL) {
printf("Single Linked List is empty\n");
} else {
printf("The elements in SLL are : ");
traverseList(first);
}
break;
case 4: exit(0);
}
}
}
InsAtBeginAndDelEnd
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
NODE createNode() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
Algorithm:
Begin:
If (head != NULL) then
prevNode ← head
head ← head.next
curNode ← head
prevNode.next ← NULL
While (head != NULL) do
head ← head.next
curNode.next ← prevNode
prevNode ← curNode
curNode ← head
End while
head ← prevNode
End if
End
Tracing:
1. Create two more pointers other than head namely prevNode and curNode that will hold
the reference of previous node and current node respectively.
Make sure that prevNode points to first node i.e. prevNode = head.
head should now point to its next node i.e. the second node head = head->next.
curNode should also points to the second node i.e. curNode = head.
2. Now, disconnect the previous node i.e. the first node from others. We will make sure
that it points to none. As this node is going to be our last node. Perform operation
prevNode→next=NULL
3. Move head node to its next node i.e. head = head->next.
4. Now, re-connect the current node to its previous node i.e. curNode->next = prevNode;
5. Point the previous node to current node and current node to head node. Means they
should now point to prevNode = curNode; and curNode = head.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void reverseList();
void displayList();
void createList(int n) {
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL) {
printf("Unable to allocate memory.");
}
else {
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
for(i=2; i<=n; i++) {
newNode = (struct node *)malloc(sizeof(struct node));
/* If memory is not allocated for newNode */
if(newNode == NULL) {
printf("Unable to allocate memory.");
break;
}
else {
scanf("%d", &data);
newNode->data = data; // Link the data field of newNode with data
newNode->next = NULL; // Link the address field of newNode with NULL
temp->next = newNode; // Link previous node i.e. temp to the newNode
temp = temp->next;
}
}
}
}
void reverseList() {
struct node *prevNode, *curNode;
if(head != NULL) {
prevNode = head;
curNode = head->next;
head = head->next;
prevNode->next = NULL; // Make first node as last node
while(head != NULL) {
head = head->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = head;
}
head = prevNode; // Make last node as head
}
}
void displayList() {
struct node *temp;
if(head == NULL) {
printf("List is empty.");
}
else {
temp = head;
while(temp != NULL) {
printf("%d ", temp->data); // Print the data of current node
temp = temp->next;
}
}
}
void check(int n){
int x;
printf("Enter no.of nodes: ");
scanf("%d", &n);
if (n>0){
while (true){
printf("Enter data: ");
createList(n);
reverseList();
printf("Reversed the list: ");
displayList();
break;
}
}
else {
printf("List size must be greater than zero:\n");
check(n);
}
}
void main(){
int n;
check(n);
}
Output:
Enter·no.of·nodes:·4
Enter·data:·1 2 3 4
Reversed·the·list:·4·3·2·1·
6. Implementation of double ended queue using linked list – inject, eject and display
operations.
Operations on Deque :
Mainly the following four basic operations are performed on queue :
Example:
Algorithm for Insertion at Rear end :
Example:
1. IF front == NULL
2. print "Underflow"
3. ELSE
4. Initialize temp = front
5. front = front->next
6. IF front == NULL
7. rear = NULL
8. ELSE
9. front->prev = NULL
10 Deallocate space for temp
Example:
1. IF front == NULL
2. print "Underflow"
3. ELSE
4. Initialize temp = rear
5. rear = rear->prev
6. IF rear == NULL
7. front = NULL
8. ELSE
9. rear->next = NULL
10 Deallocate space for temp
Example:
#include <stdio.h>
#include <stdlib.h>
#include "DequeueListInjectEject.c"
int main() {
int op, x;
while (1) {
printf("1.Inject 2.Eject 3.Display 4.Exit\n");
printf("Enter your option : ");
scanf("%d", & op);
switch (op) {
case 1:
printf("Enter element : ");
scanf("%d", & x);
inject(x);
break;
case 2:
eject();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}
DequeueListInjectEject
struct queue {
int data;
struct queue *next;
};
typedef struct queue *DeQueue;
DeQueue front = NULL, rear = NULL;
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·1
Enter·element·:·12
Successfully·inserted·at·rear·side.⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·1
Enter·element·:·13
Successfully·inserted·at·rear·side.⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·1
Enter·element·:·14
Successfully·inserted·at·rear·side.⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·3
Elements·in·the·double·ended·queue·:·⏎
12·13·14·⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·2
Deleted·element·14·from·the·rear·side.⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·12
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·3
Elements·in·the·double·ended·queue·:·⏎
12·13·⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·2
Deleted·element·13·from·the·rear·side.⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·3
Elements·in·the·double·ended·queue·:·⏎
12·⏎
1.Inject·2.Eject·3.Display·4.Exit⏎
Enter·your·option·:·4
7.Implement a doubly linked list and perform various operations to understand its
properties and applications.
1. Insertion
Adding a node to a doubly linked list is similar to adding a node to a linked list. The only
extra work is to handle the pointer to the previous node. It is faster than linked lists because we
just need to change the pointers of adjacent elements of the element we are inserting.
• Point prev of the first node to newNode (now the previous head is the second node)
• Point head to newNod
New node
2. Set the next pointer of new node and previous node
• assign the value of next from previous node to the next of newNode
• assign the address of newNode to the next of previous node
3. Set the prev pointer of new node and the next node
Final list
III. Insertion at the End
Let's add a node with value 6 at the end of the doubly linked list.
New node
2. Set prev and next pointers of new node and the previous node
If the linked list is empty, make the newNode as the head node. Otherwise, traverse to
the end of the doubly linked list and
Similar to insertion, we can also delete a node from 3 different positions of a doubly linked
list.
Original doubly linked list
Finally, free the memory of del_node. And, the linked will look like this
Final list
If del_node is an inner node (second node), we must have to reset the value of next and prev
of the nodes before and after the del_node.
Assign the value of next of del_node to the next of the first node.
Assign the value of prev of del_node to the prev of the third node.
Finally, we will free the memory of del_node. And, the final doubly linked list looks like this.
Final list
Final list
3.Traversal
We can traverse the doubly linked list in both directions. We can go to the previous
node by following the previous pointers and similarly go to the next nodes by following the
next pointers to perform some specific operations like searching, sorting, display, etc.
Step 1. Set a pointer current to the head of the doubly linked list.
Step 2. If the doubly linked list is empty (i.e., current is null), then the traversal is complete.
Step 3. While the current pointer is not null:
a. Process the data of the current node.
b. Move the current pointer to the next node (current = current->next).
or
b. Move the 'current' pointer to the previous node (current = current->prev)
Step 4. The traversal is complete when the 'current' pointer becomes null.
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{ char ssn[25], name[25], dept[10], designation[25];
int sal;
long long int phone;
struct node *llink;
struct node *rlink;
};
NODE create()
{ NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode == NULL)
{ printf("Running out of memory");
exit(0);
}
printf("Enter ssn, Name, Department, Designation, Salary, PhoneNo of employee: ");
scanf("%s %s %s %s %d %lld", enode->ssn, enode->name, enode->dept, enode-
>designation, &enode->sal, &enode->phone);
enode->llink = NULL;
enode->rlink = NULL;
count++;
return enode;
}
NODE insertfront()
{ NODE temp;
temp = create();
if(first == NULL)
{ return temp; }
temp->rlink = first;
first->llink = temp;
return temp;
}
void display()
{ NODE cur;
int nodeno=1;
cur = first;
if(cur == NULL)
printf("DLL is Empty\n");
else{
while(cur != NULL)
{ printf("SSN:%s| Name:%s| Department:%s| Designation:%s| Salary:%d|
Phone no:%lld", cur->ssn, cur->name,cur->dept, cur->designation, cur->sal, cur->phone);
cur = cur->rlink;
nodeno++;
printf("\n");
}
printf("No of employees: %d\n",count);
}
}
NODE deletefront()
{ NODE temp;
if(first == NULL)
{ printf("DLL is empty\n");
return NULL;
}
if(first->rlink == NULL)
{ printf("employee with ssn: %s is deleted\n", first->ssn);
free(first);
count--;
return NULL;
}
temp = first;
first = first->rlink;
temp->rlink = NULL;
first->llink = NULL;
printf("employee with ssn: %s is deleted\n",temp->ssn);
free(temp);
count--;
return first;
}
NODE insertend()
{ NODE cur, temp;
temp = create();
if(first == NULL)
{ return temp; }
cur = first;
while(cur->rlink != NULL)
{ cur = cur->rlink; }
cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE deleteend()
{ NODE prev,cur;
if(first == NULL)
{ printf("DLL is empty\n");
return NULL;
}
if(first->rlink == NULL)
{ printf("employee with ssn: %s is deleted\n",first->ssn);
free(first);
count--;
return NULL;
}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{ prev=cur;
cur = cur->rlink;
}
cur->llink = NULL;
printf("employee with ssn: %s is deleted\n",cur->ssn);
free(cur);
prev->rlink = NULL;
count--;
return first;
}
void main()
{ int ch,i,n;
while(1){
printf("1: Create DLL of Employee Nodes");
printf("\n2: DisplayStatus");
printf("\n3: InsertAtEnd");
printf("\n4: DeleteAtEnd");
printf("\n5: InsertAtFront");
printf("\n6: DeleteAtFront");
printf("\n7: Exit");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{ case 1 : printf("Enter no of Employees: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertend();
break;
case 2 : display();
break;
case 3 : first = insertend();
break;
case 4 : first = deleteend();
break;
case 5 : first = insertfront();
break;
case 6 : first = deletefront();
break;
case 7 : exit(0);
default: printf("Please Enter valid choice\n");
}
}
}
Output:
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
DLL·is·Empty⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·1
Enter·no·of·Employees:·2
Enter·ssn,·Name,·Department,·Designation,·Salary,·PhoneNo·of·employee:·CT156 Hema Support
PSE 30000 1234567890
Enter·ssn,·Name,·Department,·Designation,·Salary,·PhoneNo·of·employee:·CT188 Prasanth
Support PSE 30000 1234567890
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
SSN:CT156|·Name:Hema|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:1234567
890⏎
SSN:CT188|·Name:Prasanth|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:12345
67890⏎
No·of·employees:·2⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·3
Enter·ssn,·Name,·Department,·Designation,·Salary,·PhoneNo·of·employee:·CT226 Swathi Support
PSE 30000 1234567890
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
SSN:CT156|·Name:Hema|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:1234567
890⏎
SSN:CT188|·Name:Prasanth|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:12345
67890⏎
SSN:CT226|·Name:Swathi|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:123456
7890⏎
No·of·employees:·3⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·4
employee·with·ssn:·CT226·is·deleted⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
SSN:CT156|·Name:Hema|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:1234567
890⏎
SSN:CT188|·Name:Prasanth|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:12345
67890⏎
No·of·employees:·2⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·5
Enter·ssn,·Name,·Department,·Designation,·Salary,·PhoneNo·of·employee:·CT156 Bhanu Support
PSE 34000 1234567890
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
SSN:CT156|·Name:Bhanu|·Department:Support|·Designation:PSE|·Salary:34000|·Phone·no:123456
7890⏎
SSN:CT156|·Name:Hema|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:1234567
890⏎
SSN:CT188|·Name:Prasanth|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:12345
67890⏎
No·of·employees:·3⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
SSN:CT156|·Name:Bhanu|·Department:Support|·Designation:PSE|·Salary:34000|·Phone·no:123456
7890⏎
SSN:CT156|·Name:Hema|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:1234567
890⏎
SSN:CT188|·Name:Prasanth|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:12345
67890⏎
No·of·employees:·3⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·6
employee·with·ssn:·CT156·is·deleted⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·4
employee·with·ssn:·CT188·is·deleted⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
SSN:CT156|·Name:Hema|·Department:Support|·Designation:PSE|·Salary:30000|·Phone·no:1234567
890⏎
No·of·employees:·1⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·6
employee·with·ssn:·CT156·is·deleted⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
DLL·is·Empty⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·4
DLL·is·empty⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·6
DLL·is·empty⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·3
Enter·ssn,·Name,·Department,·Designation,·Salary,·PhoneNo·of·employee:·198 Tanjiro Anime
Hero 49000 1029384756
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·2
SSN:198|·Name:Tanjiro|·Department:Anime|·Designation:Hero|·Salary:49000|·Phone·no:102938475
6⏎
No·of·employees:·1⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·4
employee·with·ssn:·198·is·deleted⏎
1:·Create·DLL·of·Employee·Nodes⏎
2:·DisplayStatus⏎
3:·InsertAtEnd⏎
4:·DeleteAtEnd⏎
5:·InsertAtFront⏎
6:·DeleteAtFront⏎
7:·Exit⏎
Please·enter·your·choice:·7
8.Implement a circular linked list and perform insertion, deletion, and traversal.
Problem statement: To implement a circular linked list and perform insertion, deletion, and
traversal.
Description: The circular linked list is a linked list where all nodes are connected to form a
circle. In a circular linked list, the first node and the last node are connected to each other which
forms a circle. There is no NULL at the end.
1. Insertion operation
2. Deletion operation
3. Traversal operation
Insertion Operation
Operation Description
Insertion at beginning Inserts the element at the beginning of the linked list
Insertion at end Insert the element at the end of the list
Insertion after certain node Insert element after specific element
Deletion Operation
Operation Description
Deletion at beginning delete the element from the beginning of the linked list
Deletion at end delete the element from the end of the list
Deletion after specific node delete element after specific element
Traversal Operation
Operation Description
Traversal Visiting every node in a list at least once
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the circular linked list...
After Insertion
We can use the following steps to insert a new node at end of the circular linked list...
Before Insertion
After Insertion
We can use the following steps to insert a new node after a node in the circular linked list...
We can use the following steps to delete a node from beginning of the circular linked list...
We can use the following steps to delete a node from end of the circular linked list...
We can use the following steps to delete a specific node from the circular linked list...
• Step 1 - Check whether list is Empty (head == NULL)
• Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
• Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
• Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to
the last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its
next node.
• Step 5 - If it is reached to the last node then display 'Given node not found in the
list! Deletion not possible!!!'. And terminate the function.
• Step 6 - If it is reached to the exact node which we want to delete, then check whether
list is having only one node (temp1 → next == head)
• Step 7 - If list has only one node and that is the node to be deleted then set head =
NULL and delete temp1 (free(temp1)).
• Step 8 - If list contains multiple nodes then check whether temp1 is the first node in
the list (temp1 == head).
• Step 9 - If temp1 is the first node then set temp2 = head and keep moving temp2 to
its next node until temp2 reaches to the last node. Then set head = head → next,
temp2 → next = head and delete temp1.
• Step 10 - If temp1 is not first node then check whether it is last node in the list
(temp1 → next == head).
• Step 1 1- If temp1 is last node then set temp2 → next = head and delete temp1
(free(temp1)).
• Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1
→ next and delete temp1 (free(temp1)).
We can use the following steps to display the elements of a circular linked list...
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node *NODE;
NODE createNodeInCLL() {
NODE temp;
temp = (NODE) malloc(sizeof(struct node));
temp -> next = NULL;
return temp;
}
void main() {
NODE first = NULL;
int x, pos, op;
while(1) {
printf("1.Insert 2.Delete 3.Print 4.Exit\n");
printf("Enter your option: ");
scanf("%d", &op);
switch(op) {
case 1: printf("Enter a position: ");
scanf("%d", &pos);
if (pos <= 0) {
printf("No such position in CLL so insertion is
not possible\n");
} else {
printf("Enter an element: ");
scanf("%d", &x);
first = insertAtPositionInCLL(first, pos, x);
}
break;
case 2: if (first == NULL) {
printf("Circular Linked List is empty so deletion
is not possible\n");
} else {
printf("Enter position : ");
scanf("%d", &pos);
first = deleteAtPositionInCLL(first, pos);
}
break;
case 3: if (first == NULL) {
printf("Circular Linked List is empty\n");
} else {
printf("The elements in CLL are: ");
traverseListInCLL(first);
}
break;
case 4: exit(0);
}
}
}
Output:
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·1
Enter·an·element:·1
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·2
Enter·an·element:·2
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·3
Enter·an·element:·3
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·4
Enter·an·element:·4
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·5
Enter·an·element:·5
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·6
Enter·an·element:·6
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·3
The·elements·in·CLL·are:·1·-->·2·-->·3·-->·4·-->·5·-->·6·-->·⏎
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·2
Enter·position·:·3
The·deleted·element·from·CLL·:·3⏎
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·2
Enter·position·:·3
The·deleted·element·from·CLL·:·4⏎
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·2
Enter·position·:·3
The·deleted·element·from·CLL·:·5⏎
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·3
The·elements·in·CLL·are:·1·-->·2·-->·6·-->·⏎
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·3
Enter·an·element:·3
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·4
Enter·an·element:·4
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·1
Enter·a·position:·5
Enter·an·element:·5
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·3
The·elements·in·CLL·are:·1·-->·2·-->·3·-->·4·-->·5·-->·6·-->·⏎
1.Insert·2.Delete·3.Print·4.Exit⏎
Enter·your·option:·4
9.Implement a stack using arrays.
Description: The stack is a linear data structure following the last-in-first-out (LIFO) order,
meaning the most recently added element is the first to be removed. This tutorial will
implement a basic stack in C using an array. We will cover the two primary operations
performed on a stack: pushing an element (adding to the stack) and popping an element
(removing from the stack).
Step 3: Check if the stack is full or not by comparing top with (MAX-1)
If the stack is full, Then print "Stack Overflow" i.e, stack is full and cannot
be pushed with another element
Step 5: Stop
Step 4: Check if the stack is empty or not by comparing top with base of array i.e 0
If top is less than 0, then stack is empty, print "Stack Underflow"
Step 5: Else, If top is greater than zero the stack is not empty, then store the value pointed by
top in a variable x=a[top] and decrement top by 1. The popped element is x.
Example:
Source code ( a) :
#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 10
#include "StackOperations.c"
int main() {
int op, x;
while(1) {
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit\n");
printf("Option: ");
scanf("%d", &op);
switch(op) {
case 1:
printf("element: ");
scanf("%d", &x);
push(x);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
exit(0);
}
}
}
StackOperations
int arr[STACK_MAX_SIZE];// declare the size of the array
int top = -1;// define the top to -1
}
void display() {
if (top < 0) {
printf("Stack is empty\n");
} else {
printf("Elements: ");
for(int i = top; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}
}
void pop() {
int x;
if(top < 0) {
printf("Stack is underflow\n");
} else {
x = arr[top];
top = top - 1;
printf("Popped value: %d\n",x);
}
}
void peek(){
int x;
if(top < 0) {
printf("Stack is underflow\n");
} else {
x = arr[top];
printf("Peek value: %d\n",x);
}
}
void isEmpty() {
if (top < 0) {
printf("Stack is empty\n");
} else {
printf("Stack is not empty\n");
}
}
Output:
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·4
Stack·is·empty⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·2
Stack·is·underflow⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·3
Stack·is·empty⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·5
Stack·is·underflow⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·1
element:·25
Successfully·pushed⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·1
element:·26
Successfully·pushed⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·3
Elements:·26·25·⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·2
Popped·value:·26⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·4
Stack·is·not·empty⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·5
Peek·value:·25⏎
1.Push·2.Pop·3.Display·4.Is·Empty·5.Peek·6.Exit⏎
Option:·6
10.Write a program to evaluate a postfix expression using a stack.
#include <ctype.h>
#include <stdio.h>
#define STACK_MAX_SIZE 20
int stack [STACK_MAX_SIZE];
int top = -1;
}*/
Output:
Enter·the·postfix·expression·:·234+-
Result·:·-5⏎
11. Implement a program to check for balanced parentheses using a stack.
Example:
Input:
“({[]})”
Output:
true
Stack Expression Action
({[]}) Push
( {[]}) Push
({ []}) Push
({ }) Pop
( ) Pop
Empty
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define STACK_MAX_SIZE 30
char arr[STACK_MAX_SIZE];
int top = -1;
void push(char element) {
if(top == STACK_MAX_SIZE - 1) {
printf("Stack is overflow\n");
} else {
top = top + 1;
arr[top] = element;
}
}
char pop() {
long int x;
if(top < 0) {
printf("Stack is underflow\n");
} else {
x = arr[top];
top = top - 1;
}return x;
}
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isBalanced(char exp[]) {
int n = strlen(exp);
int i = 0;
char x;
for(i=0; i<n ;i++) {
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[') {
push(exp[i]);
} else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']') {
if(isempty()) {
return 0;
} else {
switch(exp[i]) {
case ')' :
x = pop();
if(x == '{' || x == '[') {
return 0;
}
break;
case ']' :
x = pop();
if(x == '{' || x == '(') {
return 0;
}
break;
case '}' :
x = pop();
if(x == '[' || x == '(') {
return 0;
}
break;
}
}
}
}
return isempty();
}
void main() {
char ch[80], temp;
printf("Enter an expression: ");
scanf("%s", ch);
if(isBalanced(ch) == 1) {
printf("balanced\n");
} else {
printf("not balanced\n");
}
}
Output:
Enter·an·expression:·1+2*3+(3+4)
balanced⏎
12. Implement a queue using linked lists.
Description: A queue is a linear data structure that follows the First In, First Out (FIFO)
principle, which means that the element which is inserted first in the queue will be the first one
to be removed from the queue. A good example of a queue is a queue of customers purchasing
a train ticket, where the customer who comes first will be served first.
A linked queue is shown here:
• Insertion
• Deletion
#include <stdlib.h>
#include <stdio.h>
#include "QueueOperationsLL.c"
int main() {
int op, x;
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit\n");
printf("Option: ");
scanf("%d",&op);
switch(op) {
case 1:
printf("element: ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}
}
“QueueOperationsLL.c":
struct queue {
int data;
struct queue *next;
};
if(front == NULL) {
front = temp;
} else {
rear -> next = temp;
}
rear = temp;
printf("Successfully inserted\n");
}
}
void dequeue() {
Q temp = NULL;
if(front == NULL) {
printf("Queue is underflow\n");
} else {
temp = front;
if (front == rear) {
front = rear = NULL;
} else {
front = front -> next;
}
printf("Deleted value: %d\n", temp -> data);
free(temp);
}
}
void display() {
if(front == NULL) {
printf("Queue is empty\n");
} else {
Q temp = front;
printf("Elements: ");
while(temp != NULL) {
printf("%d ", temp -> data);
temp = temp -> next;
}
printf("\n");
}
}
void size() {
int count =0;
if(front == NULL) {
printf("Queue size: 0\n");
} else {
Q temp = front;
while(temp != NULL) {
temp = temp -> next;
count = count + 1;
}
printf("Queue size: %d\n",count);
}
}
void isEmpty() {
if(front == NULL ) {
printf("Queue is empty\n");
} else {
printf("Queue is not empty\n");
}
}
Out put:
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·2
Queue·is·underflow⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·3
Queue·is·empty⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·4
Queue·is·empty⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·5
Queue·size:·0⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·1
element:·44
Successfully·inserted⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·1
element:·55
Successfully·inserted⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·1
element:·66
Successfully·inserted⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·1
element:·67
Successfully·inserted⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·3
Elements:·44·55·66·67·⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·2
Deleted·value:·44⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·2
Deleted·value:·55⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·5
Queue·size:·2⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·4
Queue·is·not·empty⏎
1.Enqueue·2.Dequeue·3.Display·4.Is·Empty·5.Size·6.Exit⏎
Option:·6
13. Implement a Circular Queue using arrays.
Problem statement: To implement a Circular Queue using arrays.
Description: A Circular Queue is an extended version of a normal queue where the last
element of the queue is connected to the first element of the queue forming a circle. The
operations are performed based on FIFO (First In First Out) principle. It is also called 'Ring
Buffer'.
#include <stdio.h>
#include<stdlib.h>
#include<stdio_ext.h>
#define MAX 6
int cq[MAX];
int front = -1, rear = -1;
void insert(int);
void delete();
void display();
void main()
{ int ch, item;
while(1)
{ printf("~~Main Menu~~");
printf("\n=> 1. Insertion and Overflow Demo");
printf("\n=> 2. Deletion and Underflow Demo");
printf("\n=> 3. Display");
printf("\n=> 4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
__fpurge(stdin);
switch(ch)
{ case 1: printf("Enter the element to be inserted: ");
scanf("%d", &item);
insert(item);
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("Please enter a valid choice\n");
}
}
}
void delete()
{ char item;
if(front == -1)
{ printf("~~~Circular Queue Underflow~~~\n"); }
else
{ item = cq[front];
printf("Deleted element from the queue is: %d\n",item );
if(front == rear)
front = rear = -1;
else
front = (front+1)%MAX;
}
}
void display ()
{ int i ;
if(front == -1)
{ printf("~~~Circular Queue Empty~~~\n"); }
else
{ printf("Circular Queue contents are:\n");
for(i = front; i != rear ; i = (i+1)%MAX)
{ printf("%d ", cq[i]); }
printf("%d\n", cq[i]);
}
}
Out put :
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·1
Enter·the·element·to·be·inserted:·1
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·1
Enter·the·element·to·be·inserted:·2
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·1
Enter·the·element·to·be·inserted:·3
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·1
Enter·the·element·to·be·inserted:·4
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·1
Enter·the·element·to·be·inserted:·5
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·3
Circular·Queue·contents·are:⏎
1·2·3·4·5⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·1
Enter·the·element·to·be·inserted:·6
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·3
Circular·Queue·contents·are:⏎
1·2·3·4·5·6⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·1
Enter·the·element·to·be·inserted:·7
~~~Circular·Queue·Overflow~~~⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·3
Circular·Queue·contents·are:⏎
1·2·3·4·5·6⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·2
Deleted·element·from·the·queue·is:·1⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·2
Deleted·element·from·the·queue·is:·2⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·2
Deleted·element·from·the·queue·is:·3⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·3
Circular·Queue·contents·are:⏎
4·5·6⏎
~~Main·Menu~~⏎
=>·1.·Insertion·and·Overflow·Demo⏎
=>·2.·Deletion·and·Underflow·Demo⏎
=>·3.·Display⏎
=>·4.·Exit⏎
Enter·Your·Choice:·4
14.Write a Program to convert an infix expression to postfix and evaluate it.
Description: Infix expression: The expression of the form “a operator b” (a + b) i.e., when
an operator is in-between every pair of operands.
Postfix expression: The expression of the form “a b operator” (ab+) i.e., When every pair of
operands is followed by an operator.
To convert infix expression to postfix expression, use the stack data structure. Scan the infix
expression from left to right. Whenever we get an operand, add it to the postfix expression and
if we get an operator or parenthesis add it to the stack by maintaining their precedence.
Example:
• 1st Step: Here i = 0 and exp[i] = ‘a’ i.e., an operand. So add this in the postfix
expression. Therefore, postfix = “a”.
Iterate the expression from left to right and keep on storing the operands into a stack. Once an
operator is received, pop the two topmost elements and evaluate them and push the result in
the stack again.
Algorithm:
Follow the steps mentioned below to evaluate postfix expression using stack:
• Create a stack to store operands (or values).
• Scan the given expression from left to right and do the following for every scanned
element.
o If the element is a number, push it into the stack.
o If the element is an operator, pop operands for the operator from the stack.
Evaluate the operator and push the result back to the stack.
• When the expression is ended, the number in the stack is the final answer.
Example: Consider the expression: exp = “2 3 1 * + 9 -“
• Scan 3, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to
top)
• Scan *, it’s an operator. Pop two operands from stack, apply the * operator on
operands. We get 3*1 which results in 3. We push the result 3 to stack. The stack now
becomes ‘2 3’.
• Scan +, it’s an operator. Pop two operands from stack, apply the + operator on
operands. We get 3 + 2 which results in 5. We push the result 5 to stack. The stack
now becomes ‘5’.
Evaluate + operator and push result in stack
• Scan 9, it’s a number. So we push it to the stack. The stack now becomes ‘5 9’.
• Scan -, it’s an operator, pop two operands from stack, apply the – operator on
operands, we get 5 – 9 which results in -4. We push the result -4 to the stack. The
stack now becomes ‘-4’.
Source code :
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int i, j;
char token;
if (isdigit(token)) {
postfix[j++] = token;
} else if (token == '(') {
push(&operatorStack, token);
} else if (token == ')') {
while (!isStackEmpty(&operatorStack) && peek(&operatorStack) != '(') {
postfix[j++] = pop(&operatorStack);
}
pop(&operatorStack); // Pop '('
} else if (isOperator(token)) {
while (!isStackEmpty(&operatorStack) && getPrecedence(peek(&operatorStack)) >=
getPrecedence(token)) {
postfix[j++] = pop(&operatorStack);
}
push(&operatorStack, token);
}
}
// Pop remaining operators from the stack
while (!isStackEmpty(&operatorStack)) {
postfix[j++] = pop(&operatorStack);
}
postfix[j] = '\0';
}
if (isdigit(token)) {
push(&operandStack, token - '0'); // Convert char to int
} else if (isOperator(token)) {
operand2 = pop(&operandStack);
operand1 = pop(&operandStack);
switch (token) {
case '+':
push(&operandStack, operand1 + operand2);
break;
case '-':
push(&operandStack, operand1 - operand2);
break;
case '*':
push(&operandStack, operand1 * operand2);
break;
case '/':
push(&operandStack, operand1 / operand2);
break;
case '%':
push(&operandStack, operand1 % operand2);
break;
}
}
}
int main() {
char infixExpression[MAX_SIZE];
char postfixExpression[MAX_SIZE];
infixToPostfix(infixExpression, postfixExpression);
return 0;
}
Out put :
Infix·expression:·2+3*4
Postfix·expression:·234*+⏎
Result:·14⏎
15. Create a program to determine whether a given string is a palindrome or not using a
stack.
Problem statement: To determine whether a given string is a palindrome or not using a stack.
Description: A string is said to be palindrome if the reverse of the string is the same as the
string.
• Find the length of the string say len. Now, find the mid as mid = len / 2.
• Push all the elements till mid into the stack i.e. str[0…mid-1].
• If the length of the string is odd then neglect the middle character.
• Till the end of the string, keep popping elements from the stack and compare them
with the current character i.e. string[i].
• If there is a mismatch then the string is not a palindrome. If all the elements match
then the string is a palindrome.
Example: Consider the string "radar". Its character comparison would be as follows:
index: 0 1 2 3 4
value: r a d a r
To compare it with the reverse of itself, the following logic is used:
1. The 0th character in the char array, string1, is the same as the 4th character in the
same string.
2. 1st character is the same as 3rd character.
3. 2nd character is the same as 2nd character.
4. . . . .
5. ith character is the same as the 'length-i-1'th character.
6. If any of the above conditions fails, the flag is set to true(1), implying that the string is
not a palindrome.
7. By default, the value of the flag is false(0). Hence, the string is a palindrome if all the
conditions are satisfied.
Source code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Stack {
int top;
char items[100];
};
// Write a function to initialize the stack
void initializeStack(struct Stack* stack) {
stack->top = -1;
}
// Write a function to check if the stack is empty
int isStackEmpty(struct Stack* stack) {
return (stack->top == -1);
}
// Write a function to push a character onto the stack
void push(struct Stack* stack, char item) {
if (stack->top == 99) {
printf("Stack overflow\n");
exit(EXIT_FAILURE);
}
stack->items[++stack->top] = item;
}
int i, len;
// Push the characters of the first half of the string onto the stack
len = strlen(str);
for (i = 0; i < len / 2; ++i) {
push(&charStack, tolower(str[i])); // Convert to lowercase for case-insensitivity
}
// If the string has an odd length, skip the middle character
if (len % 2 != 0) {
i++;
}
return 1; // Palindrome
}
int main() {
char inputString[100];
printf("String: ");
scanf("%s", inputString);
if (isPalindrome(inputString)) {
printf("%s is a palindrome\n", inputString);
} else {
printf("%s is not a palindrome\n", inputString);
}
return 0;
}
Output:
String:·Madam
Madam·is·a·palindrome⏎
16.Write a program to implement graph traversal using BFS.
Description: The Breadth First Search (BFS) algorithm is used to search a graph data
structure for a node that meets a set of criteria. It starts at the root of the graph and visits all
nodes at the current depth level before moving on to the nodes at the next depth level.
BFS Algorithm
4. If the queue is empty, every node on the graph has been visited, or there is no path from
the initial node to the goal node.
5. If the goal node was found, return the path that was followed.
Example: Let us understand the working of the algorithm with the help of the following
example.
Remove node 0 from the front of queue and visited the unvisited neighbours and push into
queue.
Step 4: Remove node 1 from the front of queue and visit the unvisited neighbours and push
them into queue.
Remove node 1 from the front of queue and visited the unvisited neighbours and push
Step 5: Remove node 2 from the front of queue and visit the unvisited neighbours and push
them into queue.
Remove node 2 from the front of queue and visit the unvisited neighbours and push them into
queue.
Step 6: Remove node 3 from the front of queue and visit the unvisited neighbours and push
them into queue.
As we can see that every neighbours of node 3 is visited, so move to the next node that are in
the front of the queue.
Remove node 3 from the front of queue and visit the unvisited neighbours and push them into
queue.
Steps 7: Remove node 4 from the front of queue and visit the unvisited neighbours and push
them into queue.
As we can see that every neighbours of node 4 are visited, so move to the next node that is in
the front of the queue.
Remove node 4 from the front of queue and visit the unvisited neighbours and push them into
queue.
Source code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 99
struct node {
struct node *next;
int vertex;
};
typedef struct node * GNODE;
GNODE graph[20];
int visited[20];
int queue[MAX], front = -1,rear = -1;
int n;
int isEmptyQueue() {
if(front == -1 || front > rear)
return 1;
else
return 0;
}
int deleteQueue() {
int deleteItem;
if(front == -1 || front > rear) {
printf("Queue Underflow\n");
exit(1);
}
deleteItem = queue[front];
front = front+1;
return deleteItem;
}
void BFS(int v) {
int w;
insertQueue(v);
while(!isEmptyQueue()) {
v = deleteQueue( );
printf("\n%d",v);
visited[v]=1;
GNODE g = graph[v];
for(;g!=NULL;g=g->next) {
w=g->vertex;
if(visited[w]==0) {
insertQueue(w);
visited[w]=1;
}
}
}
}
void main() {
int N, E, s, d, i, j, v;
GNODE p, q;
printf("Enter the number of vertices : ");
scanf("%d",&N);
printf("Enter the number of edges : ");
scanf("%d",&E);
for(i=1;i<=E;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
q=(GNODE)malloc(sizeof(struct node));
q->vertex=d;
q->next=NULL;
if(graph[s]==NULL) {
graph[s]=q;
} else {
p=graph[s];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter Start Vertex for BFS : ");
scanf("%d", &v);
printf("BFS of graph : ");
BFS(v);
printf("\n");
}
Output:
Enter·the·number·of·vertices·:·5
Enter·the·number·of·edges·:·5
Enter·source·:·1
Enter·destination·:·2
Enter·source·:·1
Enter·destination·:·4
Enter·source·:·4
Enter·destination·:·2
Enter·source·:·2
Enter·destination·:·3
Enter·source·:·4
Enter·destination·:·5
Enter·Start·Vertex·for·BFS·:·1
BFS·of·graph·:·⏎
1⏎
2⏎
4⏎
3⏎
5⏎
17.Write a program to implement graph traversal using DFS.
Algorithm:
We start from vertex 1, the DFS algorithm starts by putting it in the Visited list and putting all
its adjacent vertices in the stack.
Next, we visit the element at the top of stack i.e. 2 and go to its adjacent nodes. Since 1 has
already been visited, we visit 3 instead.
Vertex 3 has an unvisited adjacent vertex in 5, so we add that to the top of the stack and visit
it.
After we visit the last element 4, it doesn't have any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph.
Source code:
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *next;
int vertex;
};
typedef struct node * GNODE;
GNODE graph[20];
int visited[20];
int n;
void DFS(int i) {
GNODE p;
printf("\n%d",i);
p=graph[i];
visited[i]=1;
while(p!=NULL) {
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
void main() {
int N,E,i,s,d,v;
GNODE q,p;
printf("Enter the number of vertices : ");
scanf("%d",&N);
printf("Enter the number of edges : ");
scanf("%d",&E);
for(i=1;i<=E;i++) {
printf("Enter source : ");
scanf("%d",&s);
printf("Enter destination : ");
scanf("%d",&d);
q=(GNODE)malloc(sizeof(struct node));
q->vertex=d;
q->next=NULL;
if(graph[s]==NULL)
graph[s]=q;
else {
p=graph[s];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}
for(i=0;i<n;i++)
visited[i]=0;
printf("Enter Start Vertex for DFS : ");
scanf("%d", &v);
printf("DFS of graph : ");
DFS(v);
printf("\n");
}
Output:
Enter·the·number·of·vertices·:·6
Enter·the·number·of·edges·:·7
Enter·source·:·1
Enter·destination·:·2
Enter·source·:·1
Enter·destination·:·4
Enter·source·:·4
Enter·destination·:·2
Enter·source·:·2
Enter·destination·:·3
Enter·source·:·4
Enter·destination·:·5
Enter·source·:·4
Enter·destination·:·3
Enter·source·:·3
Enter·destination·:·6
Enter·Start·Vertex·for·DFS·:·1
DFS·of·graph·:·⏎
1⏎
2⏎
3⏎
6⏎
4⏎
5⏎
18.Write a program to implement linear probing.
Description: Linear probing in Hashing is a collision resolution method used in hash tables.
Collisions occur when two keys produce the same hash value, attempting to map to the same
array index. Linear probing deals with these collisions by searching for the next available slot
linearly in the array until an empty slot is found.
Procedure: Below is a hash function that calculates the next location. If the location is empty
then store value otherwise find the next location.
Following hash function is used to resolve the collision in:
h(k, i) = [h(k) + i] mod m
Where
Therefore, for a given key k, the first location is generated by [h(k) + 0] mod m, the first time
i=0.
If the location is free, the value is stored at this location. If value successfully stores then
probe count is 1 means location is founded on the first go.
If location is not free then second probe generates the address of the location given by [h(k)
+ 1]mod m.
Similarly, if the generated location is occupied, then subsequent probes generate the address
as [h(k) + 2]mod m, [h(k) + 3]mod m, [h(k) + 4]mod m, [h(k) + 5]mod m, and so on, until
a free location is found.
Step 03:
Step 04:
Step 05:
Step 07:
h(k) = 2k + 5
h(8) = 2*8 + 5 = 21
h(k, i) = [h(k) + i] mod m
h(8, 0) = [21 + 1] mod 10 = 2
"HashingLinearProbing.c":
#define SIZE 10
int HashTable[SIZE];
int hash(int x) {
return x % SIZE;
}
void insert(int x) {
int index = hash(x);
int start = index;
while (HashTable[index] != -1) {
if (HashTable[index] == -1) {
break;
}
index = (index+1) % SIZE;
if(index == start) {
printf("Hash table is full.\n");
return;
}
}
HashTable[index] = x;
printf("Successfully inserted.\n");
}
void deleteElement(int x) {
int index = hash(x);
int start = index;
while (HashTable[index] != x) {
if (HashTable[index] == x) {
break;
}
index = (index+1) % SIZE;
if(index == start) {
printf("Element not found. So cannot delete the element.\n");
return;
}
}
HashTable[index] = -1;
printf("Successfully deleted.\n");
}
void search(int x) {
int index = hash(x);
int start = index;
while (HashTable[index] != x) {
if (HashTable[index] == x) {
break;
}
index = (index+1) % SIZE;
if(index == start) {
printf("Element not found.\n");
return;
}
}
printf("Element found.\n");
}
void print() {
int i=0;
for(;i<SIZE;i++) {
if(HashTable[i] != -1)
printf("[%d]=>%d ",i,HashTable[i]);
}
printf("\n");
}
Out put :
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·1
Enter·an·element·to·be·inserted·:·11
Successfully·inserted.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·1
Enter·an·element·to·be·inserted·:·22
Successfully·inserted.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·1
Enter·an·element·to·be·inserted·:·33
Successfully·inserted.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·1
Enter·an·element·to·be·inserted·:·43
Successfully·inserted.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·1
Enter·an·element·to·be·inserted·:·53
Successfully·inserted.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·4
[1]=>11·[2]=>22·[3]=>33·[4]=>43·[5]=>53·⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·1
Enter·an·element·to·be·inserted·:·44
Successfully·inserted.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·4
[1]=>11·[2]=>22·[3]=>33·[4]=>43·[5]=>53·[6]=>44·⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·3
Enter·an·element·to·be·searched·:·34
Element·not·found.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·2
Enter·an·element·to·be·deleted·:·33
Successfully·deleted.⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·4
[1]=>11·[2]=>22·[4]=>43·[5]=>53·[6]=>44·⏎
1.Insert·2.Delete·3.Search·4.Print·5.Exit⏎
Enter·your·option·:·5
19. Write a program to implement binary tree traversals using linked list with recursion.
Problem statement: To implement binary tree traversals using linked list with recursion.
Description : Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which
have only one logical way to traverse them, trees can be traversed in different ways.
A Tree Data Structure can be traversed in following ways:
1.Depth First Search or DFS
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
2. Level Order Traversal or Breadth First Search or BFS
3. Boundary Traversal
4. Diagonal Traversal
Tree Traversals
• Start with root node 30 .print 30 and recursively traverse the left subtree.
• next node is 20. now 20 have subtree so print 20 and traverse to left subtree of 20 .
• next node is 15 and 15 have subtree so print 15 and traverse to left subtree of 15.
• 5 is next node and 5 have no subtree so print 5 and traverse to right subtree of 15.
• next node is 18 and 18 have no child so print 18 and traverse to right subtree of 20.
• 25 is right subtree of 20 .25 have no child so print 25 and start traverse to right subtree of 30.
• next node is 40. node 40 have subtree so print 40 and then traverse to left subtree of 40.
• next node is 35. 35 have no subtree so print 35 and then traverse to right subtree of 40.
• next node is 50. 50 have subtree so print 50 and traverse to left subtree of 50.
• next node is 45. 45 have no subtree so print 45 and then print 60(right subtree) of 50.
• our final output is {30 , 20 , 15 , 5 , 18 , 25 , 40 , 35 , 50 , 45 , 60}
• We start from 30, and following Post-order traversal, we first visit the left subtree 20. 20 is also
traversed post-order.
• 15 is left subtree of 20 .15 is also traversed post order.
• 5 is left subtree of 15. 5 have no subtree so print 5 and traverse to right subtree of 15 .
• 18 is right subtree of 15. 18 have no subtree so print 18 and then print 15. post order traversal
for 15 is finished.
• next move to right subtree of 20.
• 25 is right subtree of 20. 25 have no subtree so print 25 and then print 20. post order traversal
for 20 is finished.
• next visit the right subtree of 30 which is 40 .40 is also traversed post-order(40 have subtree).
• 35 is left subtree of 40. 35 have no more subtree so print 35 and traverse to right subtree of 40.
• 50 is right subtree of 40. 50 should also traversed post order.
• 45 is left subtree of 50. 45 have no more subtree so print 45 and then print 60 which is right
subtree of 50.
• next print 50 . post order traversal for 50 is finished.
• now print 40 ,and post order traversal for 40 is finished.
• print 30. post order traversal for 30 is finished.
• our final output is {5 , 18 , 15 , 25 , 20 , 35 , 45 , 60 , 50 , 40 , 30}
Source code :
// Program for linked implementation of complete binary tree
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include"TreeStructure.c"
#include"Traversals.c"
// For Queue Size
#define SIZE 100000
int i;
for (i = 0; i < size; ++i)
queue->array[i] = NULL;
return queue;
}
if (hasOnlyOneItem(queue))
queue->front = queue->rear = -1;
else
++queue->front;
return temp;
}
// If the left child of this front node doesn’t exist, set the
// left child as the new node
if (!front->left)
{
front->left = NULL;
front->left = temp;
}
// If the right child of this front node doesn’t exist, set the
// right child as the new node
else if (!front->right)
{
front->right = NULL;
front->right = temp;
}
// If the front node has both the left child and right child,
// Dequeue() it.
if (hasBothChild(front))
{
Dequeue(queue);
}
}
// Enqueue() the new node for later insertions
Enqueue(temp, queue);
}
while(1){
printf("Enter value : ");
scanf("%d",&ele);
if(ele==-1){
break;
}
else{
insert(&T1, ele, queue1);
}
}
inorder(T1);
printf("\n");
preorder(T1);
printf("\n");
postorder(T1);
}
"TreeStructure.c" :
// A tree node
struct node
{
int data;
struct node *right,*left;
struct node *root;
};
// A queue node
struct Queue
{
int front, rear;
int size;
struct node* *array;
};
"Traversals.c" :
void inorder( struct node *ptr)
{
// write your code here to compute
if(ptr!=NULL)
{
inorder(ptr->left);
printf("%d ",ptr->data);
inorder(ptr->right);
}
// and print inorder traversal
}
Out put :
Enter·value·:·45
Enter·value·:·89
Enter·value·:·56
Enter·value·:·12
Enter·value·:·45
Enter·value·:·26
Enter·value·:·78
Enter·value·:·45
Enter·value·:·-1
45·12·89·45·45·26·56·78·⏎
45·89·12·45·45·56·26·78·⏎
45·12·45·89·26·78·56·45·
20.Write a c program to implement topological sorting on graph.
Problem statement: To implement topological sorting on graph.
Description : Topological Sorting is the process in which the main goal is to find an ordering
of vertices in a directed acyclic graph (DAG) that places vertex u before vertex v for any
directed edge (u, v). “Topological order” is the name given to this linear arrangement. If a
DAG contains cycles, it might not have any topological ordering at all.
Topological Sort Example
Step 2: Write the vertex which has in-degree 0 (zero) in solution. Here vertex 1 has in-degree 0. So,
Decrease in-degree count of vertices who are adjacent to the vertex which recently added to the
solution.
Here vertex 1 is recently added to the solution. Vertices 2 and 3 are adjacent to vertex 1. So decrease
the in-degree count of those and update.
Vertex in-degree
Already added
1
to solution
2 0
3 0
4 2
Again repeat the same thing which we have done in step1 that, write the vertices which have degree 0
in solution.
Here we can observe that two vertices (2 and 3) have in-degree 0 (zero). Add any vertex into the
solution.
Note that, you may add vertex 2 into solution, and I may add vertex 3 to solution. That means the
solution to topological sorting is not unique.
Now add vertex 3.
Solution is: 1->3->
Again decrease the in-degree count of vertices which are adjacent to vertex 3.
Updated result is:
Vertex in-degree
Already added
1
to solution
2 0
Already added
3
to solution
4 1
Now add vertex 2 to solution because it only has in-degree 0.
Solution is: 1->3->2->
Updated result is:
Vertex in-degree
Already added
1
to solution
Already added
2
to solution
Already added
3
to solution
4 0
Source code :
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,v,count,topo_order[MAX],indeg[MAX];
create_graph();
count = 0;
return 0;
}/*End of main()*/
int isEmpty_queue()
{
if(front == -1 || front > rear )
return 1;
else
return 0;
}/*End of isEmpty_queue()*/
int delete_queue()
{
int del_item;
if (front == -1 || front > rear)
{
//printf("\nQueue Underflow\n");
exit(1);
}
else
{
del_item = queue[front];
front = front+1;
return del_item;
}
}/*End of delete_queue() */
int indegree(int v)
{
int i,in_deg = 0;
for(i=0; i<n; i++)
if(adj[i][v] == 1)
in_deg++;
return in_deg;
}/*End of indegree() */
void create_graph()
{
int i,max_edges,origin,destin;
Output:
Enter·the·number·of·vertices·:·4
Enter·the·number·of·edges·:·4
Enter·source·:·1
Enter·destination·:·4
Enter·source·:·2
Enter·destination·:·1
Enter·source·:·2
Enter·destination·:·3
Enter·source·:·4
Enter·destination·:·3
The·topological·order·is:2·1·4·3·⏎