You are on page 1of 31

College of Business Science & Technology (CBST)

College Code: 5228

Assignment on

Data Structure Practical


Subject Code: CSE-122

Submitted To

Examiner
Department of CSE
National University
Gazipur, Dhaka

Submitted By

Name:
Roll:
Registration:
Semester:
Batch:

Date of Submission 27 May, 2017


Part-1
A.Write a program to implement String operations

#include<stdio.h>
#include<string.h>
#include<conio.h>
char firstString[100],secondString[100];

int main()
{
int i,j,selectedInput,lenght,delPosition,repPosition;
char ch1,ch2;
printf("Enter 1 for insertion\n");
printf("Enter 2 for deletion\n");
printf("Enter 3 for getting length\n");
printf("Enter 4 for Replacement\n");
printf("Enter 5 for concatenation\n");
scanf("%d",&selectedInput);
scanf("%c",&ch1);
if(selectedInput==1)
{
printf("Enter a String\n");
gets(firstString);
printf("Enter a Character to insert\n");
scanf("%c",&ch2);
lenght=strlen(firstString);
firstString[lenght]=ch2;
firstString[++lenght]='\0';
printf("String after insertion %c\ final string:
%s\n",ch2,firstString);
}
else if(selectedInput==2)
{
printf("Enter a String\n");
gets(firstString);
printf("Inter a position for deletion=");
scanf("%d",&delPosition);
lenght=strlen(firstString);
if(delPosition<0 || delPosition>lenght)
{
printf("Invalid position");
}
else if(delPosition==lenght)
{
firstString[delPosition-1]='\0';
}
else
{
int position=delPosition-1;

for(i=position; i<lenght; i++)


{
firstString[i]=firstString[i+1];
}
firstString[i]='\0';
}
printf("After deletion final string:
%s\n",firstString);
}
else if(selectedInput==3)
{
printf("Enter a String\n");
gets(firstString);
lenght=strlen(firstString);
printf("Length=%d\n",lenght);
}

else if(selectedInput==4)
{
printf("Enter a String\n");
gets(firstString);
printf("Enter a position for replacement=");
scanf("%d",&repPosition);
scanf("%c",&ch1);
printf("Enter replace character\n");
scanf("%c",&ch2);
lenght=strlen(firstString);
if(repPosition<0 || repPosition>lenght)
{
printf("Invalid position");
}
else
{
char prvChar;
prvChar=firstString[repPosition-1];
firstString[repPosition-1]=ch2;
printf("After replacing %c to %c final
string:%s\n",prvChar,ch2,firstString);

}
else if(selectedInput==5)
{
int pos=0;

printf("Enter first String\n");

gets(firstString);
printf("Enter second String\n");

gets(secondString);
char concatenatedString[100];
for( i=0; i<strlen(firstString); i++)
{
concatenatedString[pos++]=firstString[i];
}

for( i=0; i<strlen(secondString); i++)


{
concatenatedString[pos++]=secondString[i];
}
concatenatedString[pos]='\0';

printf("concatenatedString=
%s",concatenatedString);

return 0;

}
B. Write a program to sort an array of integers using
merge sort algorithm

void merge(int a[], int low, int mid, int high);

void divide(int a[], int low, int high)


{
if(low<high) // The array has atleast 2 elements
{
int mid = (low+high)/2;
divide(a, low, mid); // Recursion chain to sort
first half of the array
divide(a, mid+1, high); // Recursion chain to sort
second half of the array
merge(a, low, mid, high);
}
}

void merge(int a[], int low, int mid, int high)


{
int i, j, k, m = mid-low+1, n = high-mid;
int first_half[m], second_half[n];
for(i=0; i<m; i++) // Extract first half (already
sorted)
first_half[i] = a[low+i];
for(i=0; i<n; i++) // Extract second half (already
sorted)
second_half[i] = a[mid+i+1];
i = j = 0;
k = low;
while(i<m || j<n) // Merge the two halves
{
if(i >= m)
{
a[k++] = second_half[j++];
continue;
}
if(j >= n)
{
a[k++] = first_half[i++];
continue;
}
if(first_half[i] < second_half[j])
a[k++] = first_half[i++];
else
a[k++] = second_half[j++];
}
}

main()
{
int i, n, a[10];
printf("How many elements in the array? ");
scanf("%d", &n);
printf("Enter array: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);

divide(a, 0, n-1);

printf("\nSorted array: ");


for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
C. writes a program to sort an array of integers
using insertion sort algorithm

#include<stdio.h>
#include<conio.h>

int main() {
int i, j, num, temp, arr[20];

printf("Enter total elements: ");


scanf("%d", &num);

printf("Enter %d elements: ", num);


for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}

for (i = 1; i < num; i++)


{
temp = arr[i];
j = i - 1;
while ((temp < arr[j]) && (j >= 0))
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}

printf("After Sorting: ");


for (i = 0; i < num; i++) {
printf("%d", arr[i]);
}

return 0;
}
D. writes a program to search an element in an array
using binary search algorithm

/* C Program - Binary Search */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
printf("Enter total number of elements :");
scanf("%d",&n);
printf("Enter %d number :", n);
for (i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter a number to find :");
scanf("%d", &search);
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
printf("%d found at location %d\n", search,
middle+1);
break;}
else
{
last = middle - 1;
}
middle = (first + last)/2;}
if(first > last)
{
printf("Not found! %d is not present in the
list.",search);
}
getch();
}
F. write a program to multiply two matrices

#include<stdio.h>

int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;

printf("\nEnter First Matrix : n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}

printf("\nEnter Second Matrix:n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}

printf("The First Matrix is: \n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}

printf("The Second Matrix is : \n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}

//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}

printf("\nMultiplication Of Two Matrices : \n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}

return (0);
}
Part-2

A. Write a program that’s inserts an element in any


location of linked list.

#include<stdio.h>
#include<conio.h>

//Create a basic structure for NODE from which new nodes


can be created.
struct node
{
int data;
struct node *link;
};

//Initialize 3 pointers as globals so that they do not


need to be passed in functions.
struct node *header, *ptr, *temp;

//Prototypes for various user defined functions.


void insert_front();
void insert_end();
void insert_any();
void display();

void main()
{
int choice;
int cont = 1;

//Allocate memory for header node.


header = (struct node *) malloc(sizeof(struct
node));

clrscr();

//Set the content of header node


header->data = NULL;
header->link = NULL;

while(cont == 1)
{
//Display menu to the user
printf("\n1. Insert at front\n");
printf("\n2. Insert at end\n");
printf("\n3. Insert at any position\n");
printf("\n4. Display linked list\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
insert_front();
break;
case 2:
insert_end();
break;
case 3:
insert_any();
break;
case 4:
display();
break;
}

printf("\n\nDo you want to continue? (1 / 0):


");
scanf("%d", &cont);
}

getch();
}

//Function to insert a node at the front of a single


linked list.
void insert_front()
{
int data_value;

printf("\nEnter data of the node: ");


scanf("%d", &data_value);

temp = (struct node *) malloc(sizeof(struct node));

temp->data = data_value;
temp->link = header->link;
header->link = temp;
}

//Function to insert a node at the end of a single linked


list.
void insert_end()
{
int data_value;

printf("\nEnter data of the node: ");


scanf("%d", &data_value);

temp = (struct node *) malloc(sizeof(struct node));

//Traverse to the end of the linked list.


ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}

temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}

//Function to insert a node at any position after a


particular node.
void insert_any()
{
int data_value, key;

printf("\nEnter data of the node: ");


scanf("%d", &data_value);
printf("\nEnter data of the node after which new
node is to be inserted: ");
scanf("%d", &key);

temp = (struct node *) malloc(sizeof(struct node));

//Traverse till key is found or end of the linked


list is reached.
ptr = header;
while(ptr->link != NULL && ptr->data != key)
{
ptr = ptr->link;
}
if(ptr->data == key)
{
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}
else
{
printf("\nValue %d not found\n",key);
}
}

//Function to display the contents of the linked list.


void display()
{
printf("\nContents of the linked list are: \n");
//Print the contents of the linked list starting
from header
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf("%d ", ptr->data);
}
}
B. Write a C program that’s delets an element in any
location of a linked list.

#include<stdio.h>
#include<conio.h>

//Create a basic structure for NODE from which new nodes


can be created.
struct node
{
int data;
struct node *link;
};

//Initialize pointers as globals so that they do not need


to be passed in functions.
struct node *header, *ptr, *ptr1, *temp;

//Prototypes for various user defined functions.


void insert_end();
void delete_front();
void delete_end();
void delete_any();
void display();

void main()
{
int choice;
int cont = 1;

//Allocate memory for header node.


header = (struct node *) malloc(sizeof(struct node));

clrscr();

//Set the content of header node


header->data = NULL;
header->link = NULL;

while(cont == 1)
{
//Display menu to the user
printf("\n1. Insert at end\n");
printf("\n2. Delete from front\n");
printf("\n3. Delete from end\n");
printf("\n4. Delete from anywhere\n");
printf("\n5. Display linked list\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
insert_end();
break;
case 2:
delete_front();
break;
case 3:
delete_end();
break;
case 4:
delete_any();
break;
case 5:
display();
break;
}

printf("\n\nDo you want to continue? (1 / 0):


");
scanf("%d", &cont);
}

getch();
}

//Function to insert a node at the end of a single linked


list.
void insert_end()
{
int data_value;

printf("\nEnter data of the node: ");


scanf("%d", &data_value);

temp = (struct node *) malloc(sizeof(struct node));

//Traverse to the end of the linked list.


ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}

temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}

//Function to delete a node from the front of a linked


list.
void delete_front()
{
//If the list is already empty
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not
possible.\n");
}
else
{
ptr = header->link;
header->link= ptr->link;
free(ptr);
printf("\nNode deleted from the front.\n");
}
}

//Function to delete a node from the end of a linked


list.
void delete_end()
{
if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not
possible.\n");
}
else
{
//Traverse to the end of the list.
ptr = header;
while(ptr->link != NULL)
{
ptr1 = ptr;
ptr = ptr->link;
}
ptr1->link = ptr->link;
free(ptr);
printf("\nNode deleted from the end.\n");
}
}

//Function to delete any node from linked list.


void delete_any()
{
int key;

if(header->link == NULL)
{
printf("\nEmpty Linked List. Deletion not
possible.\n");
}
else
{
printf("\nEnter the data of the node to be
deleted: ");
scanf("%d", &key);

ptr = header;
while((ptr->link != NULL) && (ptr->data != key))
{
ptr1 = ptr;
ptr = ptr->link;
}
if(ptr->data == key)
{
ptr1->link = ptr->link;
free(ptr);
printf("\nNode with data %d deleted.\n",
key);
}
else
{
printf("\nValue %d not found. Deletion not
possible.\n", key);
}
}
}

//Function to display the contents of the linked list.


void display()
{
printf("\nContents of the linked list are: \n");
//Print the contents of the linked list starting from
header
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf("%d ", ptr->data);
}
}
C. Write a program to implement all major stack
operations

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int Data;
struct Node *next;
}*top;

void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}

void push(int value)


{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}

void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}

int main(int argc, char *argv[])


{
int i=0;
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into
Stack: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
D. Write a program to implement all queue stack
operation

/*

* C Program to Implement a Queue using an Array

*/

#include <stdio.h>
#include<conio.h>

#define MAX 50

int queue_array[MAX];

int rear = - 1;

int front = - 1;

void main()

int choice;

while (1)

printf("1.Insert element to queue \n");

printf("2.Delete element from queue \n");

printf("3.Display all elements of queue \n");

printf("4.Quit \n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch (choice)
{

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice \n");

} /*End of switch*/

} /*End of while*/

} /*End of main()*/

insert()

int add_item;

if (rear == MAX - 1)

printf("Queue Overflow \n");

else
{

if (front == - 1)

/*If queue is initially empty */

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

} /*End of insert()*/

delete()

if (front == - 1 || front > rear)

printf("Queue Underflow \n");

return ;

else

printf("Element deleted from queue is :


%d\n", queue_array[front]);

front = front + 1;

}
} /*End of delete() */

display()

int i;

if (front == - 1)

printf("Queue is empty \n");

else

printf("Queue is : \n");

for (i = front; i <= rear; i++)

printf("%d ", queue_array[i]);

printf("\n");

} /*End of display() */
Part-3

B. writes a program that creates and displays the


element of binary tree

# include <stdio.h>
# include <stdlib.h>

typedef struct BST {


int data;
struct BST *lchild, *rchild;
} node;

void insert(node *, node *);


void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);

void main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;

printf("\nProgram For Binary Search Tree ");


do {
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);

switch (choice) {
case 1:
do {
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);

if (root == NULL) /* Tree is not Created */


root = new_node;
else
insert(root, new_node);

printf("\nWant To enter More Elements?


(y/n)");
ans = getch();
} while (ans == 'y');
break;

case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);

tmp = search(root, key, &parent);


printf("\nParent of node %d is %d", tmp->data,
parent->data);
break;

case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}

if (new_node->data > root->data) {


if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root, int key, node **parent) {
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf("\nThe %d Element is Present", temp-
>data);
return temp;
}
*parent = temp;

if (temp->data > key)


temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/*
This function displays the tree in inorder fashion
*/
void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->lchild);
printf("%d", temp->data);
inorder(temp->rchild);
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}

/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);
}
}
C. Write a Program to traverse a binary tree in in-
order process.

//Binary tree Using array

#include<stdio.h>
#include<conio.h>

typedef struct node


{
struct node*left;
struct node*right;
char data;
}node;

node* insert(char c[],int n)


{ node*tree=NULL;
if(c[n]!='\0')
{
tree=(node*)malloc(sizeof(node));
tree->left=insert(c,2*n+1);
tree->data=c[n];
tree->right=insert(c,2*n+2);
}
return tree;
}
//traverse the tree in inorder
void inorder(node*tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%c\t",tree->data);
inorder(tree->right);
}
}

void main()
{
node*tree=NULL;
char
c[]={'A','B','C','D','E','F','\0','G','\0','\0','\0','\0'
,'\0','\0','\0','\0','\0','\0','\0','\0','\0'};
tree=insert(c,0);
inorder(tree);
}

You might also like