Data Structure Using C/C++
Practical File
SCSI CS 01 02 12 C 0022
(Session 2019-2020)
Submitted By : Submitted To:
Ginesh Goyal Ms. Priya Bansal
MCA 2ndsemester Assistant Professor
Roll No. :190619 CS & IT
Date of exam: 09/04/2021
Central University of Haryana, Mahendergarh
(Department of Computer Science and Information Technology)
INDEX
S. Program Name Signature
NO
.
1 Write a program for converting a given infix
expression to Postfix from using stack.
2 Write a program to implementation of the
operations on Queue using array.
3 Write a program to implement Insertion Sort.
4 Write a program to insertion and deletion of element
on array.
5 Write a program for bubble Sort.
6 Write a program for tree traversal : in-order post-
order .
7 Write a program for Merge Sort.
8 Write a program in C to insert a new node at the
end of a Singly Linked List
Program: 1
Write a program for converting a given infix expression to Postfix from using
stack.
Source Code:
#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()
printf("\n***Converting a Given Expression of Infix to Postfix from using Stack***\n\n");
char exp[100];
char *e, x;
printf("Enter the Infix expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
printf("The Postfix expression is : ");
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
while((x = pop()) != '(')
printf("%c ", x);
else
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
while(top != -1)
printf(" %c ",pop());
return 0;
Output:
Program: 2
Write a program to implementation of the operations on Queue using
array.
Source code:
#include<stdio.h>
#define n 5
int main()
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("\n***Implementation of the Operations on Queue using Array***\n\n");
printf("===Queue using Array===");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
case 1:
if(rear==x)
printf("\n Queue is Full");
else
printf("\n Enter no. which you are inserting %d:",j++);
scanf("%d",&queue[rear++]);
break;
case 2:
if(front==rear)
printf("\n Queue is empty");
else
printf("\n Deleted Element is %d\n",queue[front++]);
x++;
break;
case 3:
printf("\nDisplay Elements of Queue are:\n");
if(front==rear)
printf("\n Queue is Empty");
else
for(i=front; i<rear; i++)
printf("%d",queue[i]);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
return 0;
Output:
Program: 3
Write a program to implement Insertion Sort.
Source code:
#include<stdio.h>
#include<string.h>
struct stud
char* name;
int no;
int age;
int rollNo;
int dob;
};
int main()
struct stud t;
int i=0,j=0,n=3;
struct stud s[n];
s[0].no = 1;
s[0].name = "Ginesh";
s[0].rollNo =190619;
s[0].age = 23;
s[0].dob =1996;
s[1].no = 2;
s[1].name = "Harshit";
s[1].rollNo = 190620;
s[1].age = 22;
s[1].dob = 1997;
s[2].no = 3;
s[2].name = "Deepak";
s[2].rollNo =190618;
s[2].age = 24;
s[2].dob =1995;
printf("\n***Student Records sorted by Name Using Insertion Sorting***\n");
printf("\n\tUnsorted Student Records:");
printf("\n-------------------------------------------------------------\n");
printf("Sr.no.\tStudent Name\tRoll no.\tAge\tDOB in year\n");
printf("-------------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("\n%d\t%-10s\t%d\t\t%d\t%d\n",s[i].no,s[i].name,s[i].rollNo,s[i].age,s[i].dob);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(s[i].name,s[j].name)>0)
t=s[i];
s[i]=s[j];
s[j]=t;
}
printf("\n\tSorted Student Records:");
printf("\n-------------------------------------------------------------\n");
printf("Sr.no.\tStudent Name\tRoll no.\tAge\tDOB in year\n");
printf("-------------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("\n%d\t%-10s\t%3d\t\t%3d\t%d\n",s[i].no,s[i].name,s[i].rollNo,s[i].age,s[i].dob);
return 0;
Output:
Program: 4
Write a program to insertion and deletion of element on array.
Source code:
#include <stdio.h>
int main()
int array[100], position, c, n, value, operation;
char isContinue;
printf("\n***To Insert & Delete an Element on Array***\n\n");
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
do
printf("press 1 for insert OR press 2 delete \n");
scanf("%d", &operation);
if(operation == 1 )
// to insert value in array.
printf("Enter the location where you wish to insert element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
{
array[c+1] = array[c];
array[position-1] = value;
n = n+1;
else if(operation == 2)
// to delete value in array.
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if (position >= n+1)
printf("Deletion of element is not possible as position is out of total length\n");
else
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
n= n-1;
else
printf("please enter correct choice\n");
printf("press 1 for insert OR press 2 delete \n");
continue;
}
printf("Resultant array is\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
printf("press # to continue... \n");
scanf(" %c", &isContinue);
}while(isContinue == '#');
Output:
Program: 5
Write a program for bubble Sort.
Source code:
#include <stdio.h>
struct student
int no;
char* name;
int rollNo;
int age;
int dob;
} s[100];
void bubbleSortDesc(struct student stud_list[], int s);
int main()
int i,n =3;
struct student s[n];
s[0].no = 1;
s[0].name = "Ginesh";
s[0].rollNo =190619;
s[0].age = 23;
s[0].dob =1996;
s[1].no = 2;
s[1].name = "Harshit";
s[1].rollNo = 190620;
s[1].age = 22;
s[1].dob = 1997;
s[2].no = 3;
s[2].name = "Deepak";
s[2].rollNo =190618;
s[2].age = 24;
s[2].dob =1995;
printf("\n");
printf("***Student Records sorted by descending order of Age Using Bubble Sorting***\n");
printf("\n\tUnsorted Student Records:");
printf("\n-----------------------------------------------------------\n");
printf("Sr.no.\tStudent Name\tRoll no.\tAge\tDOB in year\n");
printf("-----------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("\n%d\t%-10s\t%d\t\t%d\t%d\n",s[i].no,s[i].name,s[i].rollNo,s[i].age,s[i].dob);
bubbleSortDesc(s, n);
printf("\n\tSorted Student Records:");
printf("\n------------------------------------------------------------\n");
printf("Sr.no.\tStudent Name\tRoll no.\tAge\tDOB in year\n");
printf("------------------------------------------------------------\n");
for(i=0;i<n;i++)
printf("\n%d\t%-10s\t%d\t\t%d\t%d\n",s[i].no,s[i].name,s[i].rollNo,s[i].age,s[i].dob);
return 0;
void bubbleSortDesc(struct student stud_list[100], int s)
{
int i, j;
struct student temp;
for (i = 0; i < s - 1; i++)
for (j = 0; j < (s - 1-i); j++)
if (stud_list[j].age < stud_list[j + 1].age)
temp = stud_list[j];
stud_list[j] = stud_list[j + 1];
stud_list[j + 1] = temp;
}Output:
Program: 6
Write a program for tree traversal : in-order post-order .
Source code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL)
return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
// Create a new Node
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("Inorder traversal \n");
inorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
Output:
Program: 7
Write a program for Merge Sort.
Source code:
#include <stdio.h>
#include<string.h>
#include<time.h>
struct stud
char* name;
int no;
int age;
int rollNo;
int dob;
};
int main()
int size=3;
int i ;
clock_t start,end;
struct stud list[size];
list[0].no = 1;
list[0].name = "Ginesh";
list[0].rollNo =190619;
list[0].age = 23;
list[0].dob =1996;
list[1].no = 2;
list[1].name = "Harshit";
list[1].rollNo = 190620;
list[1].age = 22;
list[1].dob = 1997;
list[2].no = 3;
list[2].name = "Deepak";
list[2].rollNo =190618;
list[2].age = 24;
list[2].dob =1995;
printf("\n");
printf("***Student Records sorted by Roll no. Using Merge Sorting***\n\n");
printf("\n\tUnsorted Student Records:");
printf("\n---------------------------------------------------------------\n");
printf("Sr.no.\tStudent Name\tRoll no.\tAge\tDOB in year\n");
printf("---------------------------------------------------------------\n");
start= clock();
for(i=0;i<size;i++)
{
printf("\n%d\t%-10s\t%d\t\t%d\t%d\n",list[i].no,list[i].name,list[i].rollNo,list[i].age,list[i].dob);
partition(list, 0, size - 1);
end=clock();
printf("\n\tSorted Student Records:");
printf("\n---------------------------------------------------------------\n");
printf("Sr.no.\tStudent Name\tRoll no.\tAge\tDOB in year\n");
printf("---------------------------------------------------------------\n");
for(i = 0;i < size; i++)
//printf("%d ",list[i].rollNo);
printf("\n%d\t%-10s\t%d\t\t%d\t%d\n",list[i].no,list[i].name,list[i].rollNo,list[i].age,list[i].dob);
return 0;
void partition(int list[],int low,int high)
int mid;
if(low < high)
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
void mergeSort(struct stud list[],int low,int mid,int high)
{
int i, mi, k, lo;
struct stud temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
if (list[lo].rollNo <= list[mi].rollNo)
temp[i] = list[lo];
lo++;
else
temp[i] = list[mi];
mi++;
i++;
if (lo > mid)
for (k = mi; k <= high; k++)
temp[i] = list[k];
i++;
else
{
for (k = lo; k <= mid; k++)
temp[i] = list[k];
i++;
for (k = low; k <= high; k++)
list[k] = temp[k];
Output:
Program: 8
Write a program in C to insert a new node at the end of a Singly Linked
List
Source code:
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;
void createList(int n);
void insertNodeAtEnd(int data);
void displayList();
int main()
int n, data;
/*
* Create a singly linked list of n nodes
*/
printf("\n***Insert a New Node at the end of a Singly Linked List***\n\n ");
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
/*
* Insert data at the end of the singly linked list
*/
printf("\nEnter data to insert at end of the list: ");
scanf("%d", &data);
insertNodeAtEnd(data);
printf("\nData in the list \n");
displayList();
return 0;
/*
* Create a list of n nodes
*/
void createList(int n)
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
/*
* If unable to allocate memory for head node
*/
if(head == NULL)
printf("Unable to allocate memory.");
else
/*
* Reads data of node from the user
*/
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
/*
* Create n nodes and adds to linked list
*/
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
printf("Enter the data of node %d: ", i);
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;
}
}
printf("\n====Single Linked List Created====\n");
/*
* Create a new node and inserts at the end of the linked list.
*/
void insertNodeAtEnd(int data)
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
printf("Unable to allocate memory.");
else
newNode->data = data; // Link the data part
newNode->next = NULL;
temp = head;
// Traverse to the last node
while(temp != NULL && temp->next != NULL)
temp = temp->next;
temp->next = newNode; // Link address part
printf("\n===Data Inserted at the End in a Single Linked List===\n");
/*
* Display entire list
*/
void displayList()
struct node *temp;
/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
printf("List is empty.");
else
temp = head;
while(temp != NULL)
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
Output: