Professional Documents
Culture Documents
Ginesh Goyal Data Structures Practical File
Ginesh Goyal Data Structures Practical File
Ginesh Goyal Data Structures Practical File
Practical File
SCSI CS 01 02 12 C 0022
(Session 2019-2020)
Source Code:
#include<stdio.h>
#include<ctype.h>
char stack[100];
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;
return 1;
return 2;
return 0;
}
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
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;
while(ch)
scanf("%d",&ch);
switch(ch)
case 1:
if(rear==x)
else
scanf("%d",&queue[rear++]);
break;
case 2:
if(front==rear)
else
x++;
break;
case 3:
if(front==rear)
else
printf("%d",queue[i]);
printf("\n");
break;
case 4:
exit(0);
default:
}
}
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;
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-------------------------------------------------------------\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("-------------------------------------------------------------\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()
char isContinue;
scanf("%d", &n);
scanf("%d", &array[c]);
do
scanf("%d", &operation);
if(operation == 1 )
scanf("%d", &position);
scanf("%d", &value);
array[c+1] = array[c];
array[position-1] = value;
n = n+1;
else if(operation == 2)
scanf("%d", &position);
else
array[c] = array[c+1];
n= n-1;
else
continue;
}
printf("%d\n", array[c]);
}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];
int main()
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("\n-----------------------------------------------------------\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------------------------------------------------------------\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;
{
int i, j;
temp = stud_list[j];
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;
};
// Inorder traversal
if (root == NULL)
return;
inorderTraversal(root->left);
inorderTraversal(root->right);
// postorderTraversal traversal
postorderTraversal(root->left);
postorderTraversal(root->right);
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
root->left = createNode(value);
return root->left;
root->right = createNode(value);
return root->right;
int main() {
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
inorderTraversal(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;
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("\n---------------------------------------------------------------\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);
end=clock();
printf("\n---------------------------------------------------------------\n");
printf("---------------------------------------------------------------\n");
//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;
int mid;
{
int i, mi, k, lo;
lo = low;
i = low;
mi = mid + 1;
temp[i] = list[lo];
lo++;
else
temp[i] = list[mi];
mi++;
i++;
temp[i] = list[k];
i++;
else
{
temp[i] = list[k];
i++;
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 {
}*head;
void displayList();
int main()
int n, data;
/*
*/
scanf("%d", &n);
createList(n);
displayList();
/*
*/
scanf("%d", &data);
insertNodeAtEnd(data);
displayList();
return 0;
/*
*/
void createList(int n)
int data, i;
/*
*/
if(head == NULL)
else
/*
* Reads data of node from the user
*/
scanf("%d", &data);
temp = head;
/*
*/
if(newNode == NULL)
break;
else
scanf("%d", &data);
temp = temp->next;
}
}
/*
* Create a new node and inserts at the end of the linked list.
*/
if(newNode == NULL)
else
newNode->next = NULL;
temp = head;
temp = temp->next;
/*
* Display entire list
*/
void displayList()
/*
*/
if(head == NULL)
printf("List is empty.");
else
temp = head;
while(temp != NULL)
}
Output: