Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Lab Manual

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 53

MARATHWADA INSTITUTE OF LABORATORY

TECHNOLOGY, AURANGABAD MANUAL


PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 1. Write a program using DIV(J,K) which reads a positive
integer
N>10 and determines whether or not N is a prime number.

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No - 1 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Write a program using DIV(J,K) which reads a positive integer N>10 and determines whether or not

N is a

prime number.

Theory – A positive integer which is only divisible by 1 and iself is known as prime number. For example: 13

is a prime number because it is only divisible by 1 and 13 but, 15 is not prime number because it is divisible

by 1, 3, 5 and 15.

/* C program to check whether a number is prime or not. */

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Explanation:

This program takes a positive integer from user and stores it in variable n. Then, for loop is executed which

checks whether the number entered by user is perfectly divisible by i or not starting with initial value of i

equals to 2 and increasing the value of i in each iteration. If the number entered by user is perfectly divisible

by i then, flag is set to 1 and that number will not be a prime number but, if the number is not perfectly

divisible by i until test condition i<=n/2 is true means, it is only divisible by 1 and that number itself and that

number is a prime number.

//PROGRAM//

#include <stdio.h>

int main()

int n, i, flag=0;

printf("Enter a positive integer: ");

scanf("%d",&n);

for(i=2;i<=n/2;++i)

if(n%i==0)

flag=1;

break;

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


if (flag==0)

printf("%d is a prime number.",n);

else

printf("%d is not a prime number.",n);

return 0;

Observation:

Enter a positive integer: 29

29 is a prime number.

MARATHWADA INSTITUTE OF LABORATORY


TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


EXPERIMENT TITLE: 2. Write a program which counts the number of particular
character/word in the String.

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 2 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Write a program which counts the number of particular character/word in the String.

Theory – This C Program Counts the Number of Words in a given text Or Sentence.

/* A very simple c program printing a string on screen*/

#include <stdio.h>

main()

printf("Hello World\n");

return 0;

Output of above program:

"Hello World"

/*

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


* C Program to Count Number of Words in a given Text Or Sentence

*/

#include <stdio.h>

#include <string.h>

void main()

char s[200];

int count = 0, i;

printf("enter the string\n");

scanf("%[^\n]s", s);

for (i = 0;s[i] != '\0';i++)

if (s[i] == ' ')

count++;

printf("number of words in given string are: %d\n", count + 1);

OBSERVATION:

enter the string

welcome to M.I.Ts Datastructure Lecture!

number of words in given string are: 5

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


MARATHWADA INSTITUTE OF LABORATORY
TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 3. Write the programs for traversing of n item using the array.

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 3 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Write the programs for traversing of n item using the array.

Theory – Given an array of size n, find all elements in array that appear more than n/k times. For example, if
the input arrays is {3, 1, 2, 2, 1, 2, 3, 3} and k is 4, then the output should be [2, 3]. Note that size of array is 8
(or n = 8), so we need to find all elements that appear more than 2 (or 8/4) times. There are two elements that
appear more than two times, 2 and 3.

A simple method is to pick all elements one by one. For every picked element, count its occurrences by
traversing the array, if count becomes more than n/k, then print the element. Time Complexity of this method
would be O(n2).

A better solution is to use sorting. First, sort all elements using a O(nLogn) algorithm. Once the array is
sorted, we can find all required elements in a linear scan of array. So overall time complexity of this method is
O(nLogn) + O(n) which is O(nLogn).

//PROGRAM//

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

int main()
{

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


int mat[20];
int s,max;
printf("\nSize of your Array is : ");
scanf("%d",&s);
printf("\nEnter your %d element of an array : \n", s);

for(int i=0;i<s;i++)
{
scanf("%d",&mat[i]);
}
max=mat[0];

for(int i=1;i<s;i++)
{
if(max < mat[i])
max = mat[i];
}

printf("\nmaximum value element from given %d element is : %d",s,max);


getch();
return 0;
}

OBSERVATION:

Size of your array is 7

Enter your 7 Element of an Array

56

19

48

67

78

84

Maximum value element from given 7 element is :84

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


MARATHWADA INSTITUTE OF LABORATORY
TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 4. Write the programs for insertion and deletion of n item
using the
array.

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 4 YEAR: 2014-15

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Class: PART: II SUBJECT: Introduction to Data Structure
B.Sc(I)Year

Aim :- Write the programs for insertion and deletion of n item using the array.

Theory –
Program:

//To Insert an Element in an array//

#include <stdio.h>

int main()
{
int array[100], position, c, n, value;

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]);

printf("Enter the location where you wish to insert an 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;

printf("Resultant array is\n");

for (c = 0; c <= n; c++)


printf("%d\n", array[c]);

return 0;
}

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


OBSERVATION:

Enter number of elements in an array


5
Enter 5 elements
2
3
1
5
6
Enter the location where you wish to insert an element
3
Enter the value to insert
8
Resultant array is
2
3
8
1
5
6

-------------------------------------------------------
Process exited with return value 0
Press any key to continue - - - -

//To Delete an Element in an array//

#include<stdio.h>

void main()

int a[20], n, loc, item,i;

printf("\nEnter size of an array: ");

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


scanf("%d", &n);

printf("\nEnter elements of an array:\n");

for(i=0; i<n; i++)

scanf("%d", &a[i]);

printf("\nEnter location of deletion: ");

scanf("%d", &loc);

item = a[loc-1];

for(i=loc-1; i<n; i++)

a[i] = a[i+1];

n--;

printf("\nITEM deleted: %d", item);

printf("\n\nAfter deletion:\n");

for(i=0; i<n; i++)

printf("\n%d", a[i]);

getch();

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


OBSERVATION:

Enter array size – 5

Enter array elements – 5 2 15 50 23

Enter Location of element to be deleted – 4

Output-

Array after deletion is - 5 2 15 23

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


MARATHWADA INSTITUTE OF LABORATORY
TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 5. Implement Linear and binary search algorithm using C.

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 5 YEAR: 2014-15

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Class: PART: II SUBJECT: Introduction to Data Structure
B.Sc(I)Year

Aim :- Implement Linear and binary search algorithm using C.

Theory –

LINEAR SEARCH

Linear Search ( ):

Description: Here A is an array having N elements. ITEM is the value to be searched.

1. Repeat For J = 1 to N

2. If (ITEM == A[J]) Then

3. Print: ITEM found at location J

4. Return

[End of If]

[End of For Loop]

5. If (J > N) Then

6. Print: ITEM doesn’t exist

[End of If]

7. Exit

// PROGRAM //

#include<stdio.h>
int main(){

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


int a[10],i,n,m,c=0;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements of the array: ");


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

printf("Enter the number to be search: ");


scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");

return 0;
}

OBSERVATION:

Enter the size of an array: 5

Enter the elements of the array: 4 6 8 0 3

Enter the number to be search: 0

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


The number is found

BINARY SEARCH

Binary Search ( ):

Description: Here A is a sorted array having N elements. ITEM is the value to be searched. BEG denotes

first element and END denotes last element in the array. MID denotes the middle value.

1. Set BEG = 1 and END = N

2. Set MID = (BEG + END) / 2

3. Repeat While (BEG <= END) and (A[MID] ≠ ITEM)

4. If (ITEM < A[MID]) Then

5. Set END = MID – 1

6. Else

7. Set BEG = MID + 1

[End of If]

8. Set MID = (BEG + END) / 2

[End of While Loop]

9. If (A[MID] == ITEM) Then

10. Print: ITEM exists at location MID

11. Else

12. Print: ITEM doesn’t exist

[End of If]

13. Exit

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


// PROGRAM //

#include<stdio.h>
int main(){

int a[10],i,n,m,c=0,l,u,mid;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements in ascending order: ");


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

printf("Enter the number to be search: ");


scanf("%d",&m);

l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");

return 0;
}

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


OBSERVATION:

Enter the size of an array: 5

Enter the elements in ascending order: 4 7 8 11 21

Enter the number to be search: 11

The number is found.

MARATHWADA INSTITUTE OF LABORATORY


TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 6. Implement Bubble sort using C.

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Experiment No. - 6 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Implement Bubble sort using C.

Theory – Bubble Sort ( ):

Description: Here A is an unsorted array having N elements.

1. Repeat For J = 1 to N

2. Repeat For K = 1 to N-J

3. If (A[K] > A[K+1]) Then

4. Interchange A[K] and A[K+1]

[End of If]

[End of Step 2 For Loop]

[End of Step 1 For Loop]

5. Exit.

BUBBLE SORT

#include<stdio.h>
int main()
{

int s,temp,i,j,a[20];

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


printf("Enter total numbers of elements: ");
scanf("%d",&s);

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


for(i=0;i<s;i++)
scanf("%d",&a[i]);

//Bubble sorting algorithm


for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("After sorting: ");


for(i=0;i<s;i++)
printf(" %d",a[i]);

return 0;
}

OBSERVATION:

Enter total numbers of elements: 5

Enter 5 elements: 6 2 0 11 9

After sorting: 0 2 6 9 11

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


MARATHWADA INSTITUTE OF LABORATORY
TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


EXPERIMENT TITLE: 7. Write a program to implement singly linked list that
performs various operation

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 7 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Write a program to implement singly linked list that performs various operation

Theory –

Singly linked lists contain nodes which have a data field as well as a next field, which points to the
next node in line of nodes. Operations that can be performed on singly linked lists include insertion,
deletion and traversal.

/*
* C program to illustrate the operations of singly linked list
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30

struct emp_data
{
int empno;
char empName[MAX];
char designation[MAX];
struct emp_data *next;
};

/* ********************************************************************/
/* Function to insert a node at the front of the linked list. */
/* front: front pointer, id: employee ID, name: employee name */
/* desg: Employee designation */

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


/* Returns the new front pointer. */
/* ********************************************************************/
struct emp_data *insert(struct emp_data *front, int id, char name[],
char desg[])
{
struct emp_data *newnode;

newnode = (struct emp_data*)malloc(sizeof(struct emp_data));

if (newnode == NULL)
{
printf("\n Allocation failed \n");
exit(2);
}
newnode->empno = id;
strcpy(newnode->empName, name);
strcpy(newnode->designation, desg);
newnode->next = front;
front = newnode;
return(front);
}
/* End of insert() */

/* Function to display a node in a linked list */


void print Node(struct emp_data *p)
{
printf("\n Employee Details...\n");
printf("\n Emp No : %d", p->empno);
printf("\n Name : %s", p->empName);
printf("\n Designation : %s\n", p->designation);
printf("-------------------------------------\n");
}
/* End of print Node() */

/* ********************************************************/
/* Function to delete Node a node based on employee number */
/* front: front pointer, id: Key value */
/* Returns: the modified list. */
/* ********************************************************/
struct emp_data* delete Node(struct emp_data *front, int id)
{
struct emp_data *ptr;
struct emp_data *bptr;

if (front->empno == id)
{
ptr = front;
printf("\n Node deleted:");
print Node(front);
front = front->next;
free(ptr);
return(front);
}
for (ptr = front->next, bptr = front; ptr != NULL; ptr = ptr->next,
bptr = bptr->next)
{

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


if (ptr->empno == id)
{
printf("\n Node deleted:");
print Node(ptr);
bptr->next = ptr->next;
free(ptr);
return(front);
}
}
printf("\n Employee Number %d not found ", id);
return(front);
}
/* End of delete Node() */

/* ****************************************************************/
/* Function to search the nodes in a linear fashion based emp ID */
/* front: front pointer, key: key ID. */
/* ****************************************************************/
void search(struct emp_data *front, int key)
{
struct emp_data *ptr;

for (ptr = front; ptr != NULL; ptr = ptr -> next)


{
if (ptr->empno == key)
{
printf("\n Key found:");
print Node(ptr);
return;
}
}
printf("\n Employee Number %d not found ", key);
}
/* End of search() */

/* Function to display the linked list */


void display(struct emp_data *front)
{
struct emp_data *ptr;

for (ptr = front; ptr != NULL; ptr = ptr->next)


{
print Node(ptr);
}
}
/* End of display() */

/* Function to display the menu of operations on a linked list */


void menu()
{
printf("---------------------------------------------\n");
printf("Press 1 to INSERT a node into the list \n");
printf("Press 2 to DELETE a node from the list \n");
printf("Press 3 to DISPLAY the list \n");
printf("Press 4 to SEARCH the list \n");
printf("Press 5 to EXIT \n");

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


printf("---------------------------------------------\n");
}
/* End of menu() */

/* Function to select the option */


char option()
{
char choice;

printf("\n\n>> Enter your choice: ");


switch(choice=getche())
{
case '1':
case '2':
case '3':
case '4':
case '5': return(choice);
default : printf("\n Invalid choice.");
}
return choice;
}
/* End of option() */

/* The main() program begins */


void main()
{
struct emp_data *link List;
char name[21], desig[51];
char choice;
int eno;

link List = NULL;


printf("\n Welcome to demonstration of singly linked list \n");
menu();
do
{
/* choose operation to be performed */
choice = option();
switch(choice)
{
case '1':
printf("\n Enter the Employee Number : ");
scanf("%d", &eno);
printf("Enter the Employee name : ");
fflush(stdin);
gets(name);
printf("Enter the Employee Designation : ");
gets(desig);
link List = insert(link List, eno, name, desig);
break;
case '2':
printf("\n\n Enter the employee number to be deleted: ");
scanf("%d", &eno);
link List = delete Node(link List, eno);
break;
case '3':

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


if (link List == NULL)
{
printf("\n List empty.");
break;
}
display(link List);
break;
case '4':
printf("\n\n Enter the employee number to be searched: ");
scanf("%d", &eno);
search(link List, eno);
break;
case '5': break;
}
} while (choice != '5');
}

OBSERVATION:
Welcome to demonstration of singly linked list
---------------------------------------------
Press 1 to INSERT a node into the list
Press 2 to DELETE a node from the list
Press 3 to DISPLAY the list
Press 4 to SEARCH the list
Press 5 to EXIT
---------------------------------------------

>> Enter your choice: 1

Enter the Employee Number : 12


Enter the Employee name : ram
Enter the Employee Designation : HR

>> Enter your choice: 3

Employee Details...

Emp No : 12
Name : ram
Designation : HR
-------------------------------------

>> Enter your choice:


Invalid choice.

>> Enter your choice: 4

Enter the employee number to be searched: 12

Key found:
Employee Details...

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Emp No : 12
Name : ram
Designation : HR
-------------------------------------

>> Enter your choice:


Invalid choice.

>> Enter your choice: 2

Enter the employee number to be deleted: 12

Node deleted:
Employee Details...

Emp No : 12
Name : ram
Designation : HR
-------------------------------------

>> Enter your choice:


Invalid choice.

>> Enter your choice: 4

Enter the employee number to be searched: 1

Employee Number 1 not found

>> Enter your choice:


Invalid choice.

>> Enter your choice: 5

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri
MARATHWADA INSTITUTE OF LABORATORY
TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 8. Write a program which reads words WORD1 and
WORD2 and then replaces each occurrence of word1 in text by word2

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 8 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Write a program which reads words WORD1 and WORD2 and then replaces each occurrence of
word1 in text by word2

Theory –

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char a[50],b[50];

int i,j;

clrscr();

printf("\n Please Give The STRING OF A : ");

scanf("%s",a);

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


printf("\n Please Give The STRING OF B : ");

scanf("%s",b);

strcat(a,b);

printf("\n Concatenation of a string is %s .",a);

getch();

OBSERVATION:

Please Give The STRING OF A : MODI

Please Give The STRING OF B : BHAVESH

Concatenation of a string is MODIBHAVESH .

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri
MARATHWADA INSTITUTE OF LABORATORY
TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 9. Write the programs for push and pop
operation using the stacks.

DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 9 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Write the programs for push and pop operation using the stacks.

Theory –

PUSH & POP OPERATION OF STACK

// Algorithm for Push ( ): //

Description: Here STACK is an array with MAX locations. TOP points to the top most element and ITEM is

the value to be inserted.

1. If (TOP == MAX) Then [Check for overflow]

2. Print: Overflow

3. Else

4. Set TOP = TOP + 1 [Increment TOP by 1]

5. Set STACK[TOP] = ITEM [Assign ITEM to top of STACK]

6. Print: ITEM inserted

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


[End of If]

7. Exit

// Algorithm for Pop ( )://

Description: Here STACK is an array with MAX locations. TOP points to the top most element.

1. If (TOP == 0) Then [Check for underflow]

2. Print: Underflow

3. Else

4. Set ITEM = STACK[TOP] [Assign top of STACK to ITEM]

5. Set TOP = TOP - 1 [Decrement TOP by 1]

6. Print: ITEM deleted

[End of If]

7. Exit

// PROGRAM //

#include <stdio.h>

#include <conio.h>

#define MAXSIZE 5

struct stack /* Structure definition for stack */

int stk[MAXSIZE];

int top;

};

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


typedef struct stack STACK;

STACK s;

/* Function declaration/Prototype*/

void push (void);

int pop(void);

void display (void);

void main ()

int choice;

int option = 1;

clrscr ();

s.top = -1;

printf ("STACK OPERATION\n");

while (option)

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


{

printf ("------------------------------------------\n");

printf (" 1 --> PUSH \n");

printf (" 2 --> POP \n");

printf (" 3 --> DISPLAY \n");

printf (" 4 --> EXIT \n");

printf ("------------------------------------------\n");

printf ("Enter your choice\n");

scanf ("%d", &choice);

switch (choice)

case 1: push();

break;

case 2: pop();

break;

case 3: display();

break;

case 4: return;

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


fflush (stdin);

printf ("Do you want to continue(Type 0 or 1)?\n");

scanf ("%d", &option);

/*Function to add an element to the stack*/

void push ()

int num;

if (s.top == (MAXSIZE - 1))

printf ("Stack is Full\n");

return;

else

printf ("Enter the element to be pushed\n");

scanf ("%d", &num);

s.top = s.top + 1;

s.stk[s.top] = num;

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


return;

/*Function to delete an element from the stack*/

int pop ()

int num;

if (s.top == - 1)

printf ("Stack is Empty\n");

return (s.top);

else

num = s.stk[s.top];

printf ("poped element is = %d\n", s.stk[s.top]);

s.top = s.top - 1;

return(num);

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


/*Function to display the status of the stack*/

void display ()

int i;

if (s.top == -1)

printf ("Stack is empty\n");

return;

else

printf ("\nThe status of the stack is\n");

for (i = s.top; i >= 0; i--)

printf ("%d\n", s.stk[i]);

printf ("\n");

/*---------------------------------------------------------------------------

Output

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


STACK OPERATION

------------------------------------------

1 --> PUSH

2 --> POP

3 --> DISPLAY

4 --> EXIT

------------------------------------------

Enter your choice

Enter the element to be pushed

23

Do you want to continue(Type 0 or 1)?

------------------------------------------

1 --> PUSH

2 --> POP

3 --> DISPLAY

4 --> EXIT

------------------------------------------

Enter your choice

Enter the element to be pushed

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


45

Do you want to continue(Type 0 or 1)?

------------------------------------------

1 --> PUSH

2 --> POP

3 --> DISPLAY

4 --> EXIT

------------------------------------------

Enter your choice

Enter the element to be pushed

78

Do you want to continue(Type 0 or 1)?

------------------------------------------

1 --> PUSH

2 --> POP

3 --> DISPLAY

4 --> EXIT

------------------------------------------

Enter your choice

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


3

The status of the stack is

78

45

23

Do you want to continue(Type 0 or 1)?

------------------------------------------

1 --> PUSH

2 --> POP

3 --> DISPLAY

4 --> EXIT

------------------------------------------

Enter your choice

poped element is = 78

Do you want to continue(Type 0 or 1)?

------------------------------------------

1 --> PUSH

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


2 --> POP

3 --> DISPLAY

4 --> EXIT

------------------------------------------

Enter your choice

The status of the stack is

45

23

Do you want to continue(Type 0 or 1)?

-----------------------------------------------------------------------------*/

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


MARATHWADA INSTITUTE OF LABORATORY
TECHNOLOGY, AURANGABAD MANUAL
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE: 10. Write the programs for insertion and deletion of n item
from the queues.

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


DEPARTMENT: COMPUTER SCIENCE LABORATORY: MIT-B.Sc(II)Sem

Experiment No. - 10 YEAR: 2014-15

Class: PART: II SUBJECT: Introduction to Data Structure


B.Sc(I)Year

Aim :- Write the programs for insertion and deletion of n item from the queues.

Theory –

// Algorithm Queue Insert ( ):

Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE.
ITEM is the value to be inserted.

1. If (REAR == N) Then [Check for overflow]

2. Print: Overflow

3. Else

4. If (FRONT and REAR == 0) Then [Check if QUEUE is empty]

(a) Set FRONT = 1

(b) Set REAR = 1

5. Else

6. Set REAR = REAR + 1 [Increment REAR by 1]

[End of Step 4 If]

7. QUEUE[REAR] = ITEM

8. Print: ITEM inserted

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


[End of Step 1 If]

9. Exit

// Algorithm Queue Delete ( ): //

Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of

the QUEUE.

1. If (FRONT == 0) Then [Check for underflow]

2. Print: Underflow

3. Else

4. ITEM = QUEUE[FRONT]

5. If (FRONT == REAR) Then [Check if only one element is left]

(a) Set FRONT = 0

(b) Set REAR = 0

6. Else

7. Set FRONT = FRONT + 1 [Increment FRONT by 1]

[End of Step 5 If]

8. Print: ITEM deleted

[End of Step 1 If]

9. Exit

// PROGRAM //

#include <stdio.h>

#include <conio.h>

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


#include <stdlib.h>

int q[25], n ,front=-1 , rear=-1 , item;

void insertion()

if((rear==n) && (front==(rear+1))||(front==rear+1))

printf(“\nQueue Overflow\n”);

else if (rear==0)

front = rear = 1;

else if(rear==n)

rear=1;

else

rear=rear+1;

printf(“Enter the item : “);

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


scanf(“%d”,&item);

q[rear] = item;

printf(“%d is inserted\n\n”,item);

void deletion()

if(front==0)

printf(“\nQueue Underflow\n\n”);

item=q[front];

if(front==rear)

front=0;

rear=0;

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


else if (front=n)

front=1;

else

front=front+1;

printf(“\n%d is deleted\n\n”,item);

void show()

for(int i=0;i<=rear;i++)

printf(“%d\t”,q[i]);

int main()

int op;

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


printf(“Enter the size of the queue : “);

scanf(“%d”,&n);

do

printf(“\n1 : Insert”);

printf(“\n2 : Delete”);

printf(“\n3 : Print”);

printf(“\n4 : Exit”);

printf(“\nEnter your choice : “);

scanf(“%d”,&op);

switch(op)

case 1:

insertion();

break;

case 2:

deletion();

break;

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


case 3:

show();

break;

//default:

// printf(“Invalid Option. Try again.”);

}while(op!=4);

printf(“\n—THE END—\n”);

OBSERVATION:
Enter the size of the queue : 4
1 : Insert
2 : Delete
3 : Print
4 : Exit

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Enter your choice : 1
Enter the Item : 5
5 is Inserted

1 : Insert
2 : Delete
3 : Print
4 : Exit
Enter your choice : 1
Enter the Item : 80
80 is Inserted

1 : Insert
2 : Delete
3 : Print
4 : Exit
Enter your choice : 2

80 is Inserted

1 : Insert
2 : Delete
3 : Print
4 : Exit

Enter your choice : 2

Queue underflow

5 is Deleted

1 : Insert
2 : Delete
3 : Print
4 : Exit
Enter your choice :

List of Expeiments in Intro. To Data Structure:

1. Write a program using DIV(J,K) which reads a positive integer

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


N>10 and determines whether or not N is a prime number.

2. Write a program which counts the number of particular


character/word in the String.

3. Write the programs for traversing of n item using the array.

4. Write the programs for insertion and deletion of n item using the
array.

5. Implement Linear and binary search algorithm using C.

6. Implement Bubble sort using C.

7. Write the programs for traversing of n item from the linked list.

8 Write a program which reads words WORD1 and WORD2 and then
replaces each occurrence of word1 in text by word2

9. Write the programs for push and pop operation using the stacks.

10. Write the programs for insertion and deletion of n item from the
queues.

Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri


Prepared By: Prof. R.R.Sontakke Verified By: Prof. S.W.Quadri

You might also like