0% found this document useful (0 votes)
21 views28 pages

Data Structure Lab Report

BCA 3rd sem - Data Structure lab report

Uploaded by

Prijan Khad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views28 pages

Data Structure Lab Report

BCA 3rd sem - Data Structure lab report

Uploaded by

Prijan Khad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

DATA STRUCTURES

AND
ALGORITHMS
LAB REPORT

------------------------
NAVAKSHITI
Z
COLLAGE
BARDIBAS
2021
TRIBHUVAN UNIVERSITY
Department of Humanities and Social Science
Navakshitiz Collage
Bardibas
LAB REPORT ON
Data Structure and Algorithm

Experiment Date:
Submitted to:
Submission Date: Department of
Submitted by: Amrit Rana Humanities and Social Science
INTRODUCTION TO DATA STRUCTURES

Data Structure is defined as the way in which data is organized in the memory location.

There are 2 types of data structures:

Linear Data Structure:


In linear data structure all the data are stored linearly or contiguously in the memory. All
the data are saved in continuously memory locations and hence all data elements are
saved in one boundary. A linear data structure is one in which we can reach
directly only one element from another while travelling sequentially. The main
advantage, we find the first element, and then it’s easy to find the next data elements.
The disadvantage, the size of the array must be known before allocating the
memory.

The different types of linear data structures are:


• Array
• Stack
• Queue
• Linked List

Non-Linear Data Structure:


Every data item is attached to several other data items in a way that is specific for
reflecting relationships. The data items are not arranged in a sequential structure.

The various types of non linear data structures are:


• Trees
• Graphs
EXPERIMENT - 01
Design, Develop and Implement a menu driven program in C for the following Array
operations
a) Creating Array of N Integer elements.
b) Display of Array elements with suitable headings.
c) Inserting an element (ELEM) at a given valid position (POS).
d) Deleting an element at a given valid position (POS). e. Exit.
Support the program with functions for each of the above operations

ABOUT THE EXPERIMENT:


An Array is a collection of similar / same elements. In this experiment the array can be
represented as single dimensional elements.
Menu driven program in c - language to perform various array operations are
implemented with the he user defined functions as followings;
create()
display()
insert()
del()
exit()

ALGORITHM:
Step 1: Start.
Step 2: Read N value.
Step 3: Read Array of N integer elements Step 4: Print array of N integer elements.
Step 5: Insert an element at given valid position in an array.
Step 6: Delete an element at given valid position from an array. Step 7: Stop.

PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
/* Global variables declaration */ int a[4], n, elem, i, pos;
/*Function Prototype and Definitions*/
void create() //creating an array
{
printf("\nEnter the size of the array elements: "); scanf("%d", &n);
printf("\nEnter the elements for the array:\n"); for(i=0; i<n; i++)
scanf("%d", &a[i]);
} //end of create()

void display() //displaying an array elements


{
int i;
printf("\nThe array elements are:\n"); for(i=0; i<n; i++)
{ printf("%d\t", a[i]);
}
} //end of display()
void insert() //inserting an element in to an array

{ printf("\nEnter the position for the new element: "); scanf("%d", &pos);

printf("\nEnter the element to be inserted: ");

scanf("%d", &elem); for(i=n-1; i>=pos; i--)

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

} a[pos] = elem;

n = n+1;

} //end of insert()

void del() //deleting an array element

{ printf("\nEnter the position of the element to be deleted: ");

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

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

} n = n-1;

printf("\nThe deleted element is = %d", elem);

}//end of delete()

void main()

int ch; clrscr(); do{ printf("\n\n--------Menu-----------\n");

printf("1.Create\n 2.Display\n 3.Insert\n 4.Delete\n 5.Exit\n");

printf("-------------------------------"); printf("\nEnter your choice: ");

scanf("%d", &ch); switch(ch) {

case 1: create();

break;

case 2: display();

break;
case 3: insert();

break;

case 4: del();

break;

case 5: exit(0);

break;

default: printf("\nInvalid choice:\n"); break;

}}

while(ch!=5); getch();

}// end of main

SAMPLE OUTPUT:

--------Menu-----------

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:

10 20 30 40 50

--------Menu-----------

1.Create

2.Display

3.Insert
4.Delete

5.Exit

Enter your choice: 2

The array elements are:

10 20 30 40 50

--------Menu-----------

1.Create

2.Display

3.Insert

4.Delete

5.Exit

Enter your choice: 3

Enter the position for the new element: 2

Enter the element to be inserted: 90

--------Menu-----------

1.Create

2.Display

3.Insert

4.Delete

5.Exit

Enter your choice: 2

The array elements are:

10 20 90 30 40 50
--------Menu-----------

1.Create

2.Display

3.Insert

4.Delete

5.Exit

Enter your choice: 4

Enter the position of the element to be deleted: 5

The deleted element is = 50

--------Menu-----------

1.Create

2.Display

3.Insert

4.Delete

5.Exit

Enter your choice: 2

The array elements are:

10 20 90 30 40

--------Menu-----------

1. Create

2. Display

3.Insert

4.Delete

5.Exit

Enter your choice: 5


EXPERIMENT: 2
Design, Develop and Implement a program in C for the following operations on Strings

a. Read a Main String (STR), a Pattern String (PAT) and a Replace String (REP).
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT
in STR with REP if PAT exists in STR. Repost suitable messages in case PAT
does not exist in STR.
Support the program with functions for each of the above operations. Don’t use built-in functions.

ABOUT THE EXPERIMENT:


Strings are actually one-dimensional array of characters terminated by a null character '\0'.
Thus a null-terminated string contains the characters that comprise the string followed by a null.

The following declaration and initialization create a string consisting of the word "Hello". To hold
the null character at the end of the array, the size of the character array containing the string is
one more than the number of characters in the word "Hello."

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you follow the rule of array initialization then you can write the above statement as follows:

char greeting[]
="Hello";

C language supports a wide range of built-in functions that manipulate null-terminated


strings as follows: strcpy(s1, s2); Copies string s2 into string s1.

strcat(s1, s2); Concatenates string s2 onto the end of string s1. strlen(s1); Returns
the length of string s1. strcmp(s1, s2);Returns 0 if s1 and s2 are the same; less than 0 if
s1<s2; greater than 0 if s1>s2. strchr(s1, ch); Returns a pointer to the first occurrence of
character ch in string s1. strstr(s1, s2); Returns a pointer to the first occurrence of string
s2 in string s1.

ALGORITHM:
Step 1: Start.

Step 2: Read main string STR, pattern string PAT and replace string REP.

Step 3: Search / find the pattern string PAT in the main string STR.

Step 4: if PAT is found then replace all occurrences of PAT in main string STR with REP string. Step 5:
if PAT is not found give a suitable error message.

Step 6: Stop
PROGRAM CODE:

#include<stdio.h>

#include<conio.h>

//Declarations

char str[100], pat[50], rep[50], ans[100]; int i, j, c, m, k, flag=0;

void stringmatch()

{ i = m = c = j = 0; while(str[c] ! = '\0')

{ if(str[m] = = pat[i]) // ...... matching

{ i++; m++; if(pat[i] = = '\0') //.....found occurrences.

{ flag = 1;

//.... copy replace string in ans string.

for(k = 0; rep[k] != '\0'; k++, j++) ans[j] = rep[k];

i = 0; c = m;

} // if ends.

else //... mismatch

ans[j] = str[c]; j++; c++; m = c; i = 0;

}//else ends } //end of while ans[j] = '\0';

} //end stringmatch()

void main()

{ clrscr();

printf("\nEnter a main string \n"); gets(str);

printf("\nEnter a pattern string \n"); flushall(); gets(pat);

printf("\nEnter a replace string \n");

flushall(); gets(rep); stringmatch(); if(flag = = 1)


printf("\nThe resultant string is\n %s" , ans);

else printf("\nPattern string NOT found\n");

getch();

} // end of main

SAMPLE OUTPUT:

RUN 1:

Enter a main string

Test

Enter a pattern string

Te

Enter a replace string

Re

The resultant string is

Rest

RUN 2:

Enter a main string

This is Data Structure lab

Enter a pattern string

Data Structure Enter a replace string

Data structure with C

The resultant string is

This is Data structure with C lab

RUN 3:

Enter a main string

This is Data Structure lab

Enter a pattern string

Date
Enter a replace string

DATA

Pattern string NOT found

LAB EXPRIMENT: 3

Design, Develop and Implement a menu driven program in C for the following operations on

STACK of integers (Array implementation of stack with maximum size MAX)

a.Push an element on to stack


b.Pop an element from stack.
c.Demonstrate how stack can be used to check palindrome.
d.Demonstrate Overflow and Underflow situations on stack.
e.Display the status of stack.
f.Exit.
Support the program with appropriate functions for each of the above operations.

ABOUT THE EXPERIMENT:


A stack is an abstract data type (ADT), commonly used in most programming languages. It is
named stack as it behaves like a real-world stack.

A real-world stack allows operations at one end only. For example, we can place or remove a
card or plate from top of the stack only. Likewise, Stack ADT allows all data operations at one
end only. At any given time, we can only access the top element of a stack.

This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element
which is placed (inserted or added) last is accessed first. In stack terminology, insertion operation
is called PUSH operation and removal operation is called POP operation.

Below given diagram tries to depict a stack and its operations −

Push POP
A A
A stack can be implemented by means of Array, Structure, Pointer and Linked-List. Stack can
either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to
implement stack using arrays which makes it a fixed size stack implementation.

Basic Operations:
• push() - pushing (storing) an element on the stack.
• pop() - removing (accessing) an element from the stack.
To use a stack efficiently we need to check status of stack as well. For the same purpose, the
following functionality is added to stacks;

• peek() − get the top data element of the stack, without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.

ALGORITHM:
Step 1: Start.

Step 2: Initialize stack size MAX and top of stack -1.

Step 3: Push integer element on to stack and display the


contents of the stack. if stack is full give a message as
‘Stack is Overflow’.

Step 3: Pop element from stack along with display


the stack contents. if stack is empty give a
message as ‘Stack is Underflow’.

Step 4: Check whether the stack contents are


Palindrome or not.

Step 5: Stop.

PROGRAM CODE:

#include<stdio.h>

#include<conio.h>

#define MAX 4

int stack[MAX], item;

int ch, top = -1, count = 0, status = 0;

/*PUSH FUNCTION*/

void push(int stack[], int item)


{

if (top == (MAX-1)) printf("\n\nStack is Overflow");

else

{ stack[++top] = item; status++;

/*POP FUNCTION*/

int pop(int stack[])

int ret; if(top == -1) printf("\n\nStack is Underflow");

else

{ ret = stack[top--]; status--;

printf("\nPopped element is %d", ret);

} return ret;

/* FUNCTION TO CHECK STACK IS PALINDROME OR NOT */

void palindrome(int stack[])

{ int i, temp; temp = status; for(i=0; i<temp; i++)

{ if(stack[i] == pop(stack)) count++;

} if(temp==count) printf("\nStack contents are Palindrome");

else

printf("\nStack contents are not palindrome");

/*FUNCTION TO DISPLAY STACK*/

void display(int stack[])


{

int i;

printf("\nThe stack contents are:"); if(top == -1) printf("\nStack is


Empty");

else{

for(i=top; i>=0; i--) printf("\n ------\n| %d |", stack[i]);

printf("\n");

/*MAIN PROGRAM*/

void main()

{ clrscr(); do{ printf("\n\n----MAIN MENU----\n"); printf("\n1.


PUSH (Insert) in the Stack"); printf("\n2. POP (Delete) from the
Stack"); printf("\n3. PALINDROME check using Stack"); printf("\
n4. Exit (End the Execution)"); printf("\nEnter Your Choice: ");
scanf("%d", &ch);

switch(ch){

case 1: printf("\nEnter a element to be pushed: "); scanf("%d",


&item); push(stack, item); display(stack); break;

case 2: item=pop(stack); display(stack); break; case 3:

palindrome(stack);

break;

case 4: exit(0); break;

default:

printf("\nEND OF EXECUTION");

}//end switch }while (ch != 4); getch();

}
OUTPUT:

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 1

Enter an element to be pushed: 1

The stack contents are:

-----

|1|

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 1 Enter an element to be pushed: 2

The stack contents are:

------

|2|

------|

1|

(-------- AFTER THE 4 TIMES PUSH OPERATION -------)

----MAIN MENU----

1. PUSH (Insert) in the Stack


2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 1

Enter an element to be pushed: 9

Stack is Overflow

The stack contents are:

------

|1|

------

|2|

------|

2|

------|

1|

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 2

Popped element is 1

The stack contents are:

------

|2|
------

|2|

------

|1|

(-------- AFTER THE 4 TIMES POP OPERATION -------)

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 2

Stack is Underflow

The stack contents are:

Stack is Empty

(-------- CHECKING FOR PALINDROME OR NOT USING STACK-------)

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 1

Enter an element to be pushed: 1

The stack contents are:

-----

|1|
----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 1

Enter an element to be pushed: 2

The stack contents are:

------

|2|

------|

1|

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 1

Enter a element to be pushed: 1

The stack contents are:

------

|1|
------

|2|

------

|1 |

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 3

Stack contents are is Palindrome

(-------- CHECKING FOR PALINDROME OR NOT USING


STACK-------)

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 1

Enter an element to be pushed: 1

The stack contents are:

-----

|1|

(AFTER 3 TIMES PUSH)


----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack 4. Exit (End the


Execution)

Enter Your Choice: 1

Enter an element to be pushed: 3

The stack contents are:

------

|3|

------

|2|

------

|1|

----MAIN MENU----

1. PUSH (Insert) in the Stack

2. POP (Delete) from the Stack

3. PALINDROME check using Stack

4. Exit (End the Execution)

Enter Your Choice: 3

Stack contents are not Palindrome

LAB EXPERIMENT: 4
Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix Expression.
Program should support for both parenthesized and free parenthesized expressions with the operators: +,
-, *, /, %( Remainder), ^ (Power) and alphanumeric operands.
ABOUT THE EXPERIMENT:
Infix: Operators are written in-between their operands. Ex: X + Y Prefix: Operators are written before their
operands. Ex: +X Y postfix: Operators are written after their operands. Ex: XY+ Examples of Infix, Prefix, and
Postfix

Infix Prefix Postfix


Expression Expression Expression
A+B +AB AB+

A+B*C +A*BC ABC*+

Infix to prefix conversion Expression = (A+B^C)*D+E^5 Step 1. Reverse the infix expression.
5^E+D*)C^B+A(
Step 2. Make Every '(' as ')' and every ')' as '(' 5^E+D*(C^B+A)
Step 3. Convert expression to postfix form.

Step 4. Reverse the expression. +*+A^BCD^E


Step 5. Result +*+A^BCD^E5

A+(B*C-(D/E-F)*G)*H

Expression Stack Output Comment


5^E+D*(C^B+A) Empty - Initial
^E+D*(C^B+A) Empty 5 Print
E+D*(C^B+A) ^ 5 Push
+D*(C^B+A) ^ 5E Push
D*(C^B+A) + 5E^ Pop And Push
*(C^B+A) + 5E^D Print
(C^B+A) +* 5E^D Push
C^B+A) +*( 5E^D Push

^B+A) +*( 5E^DC Print


B+A) +*(^ 5E^DC Push
+A) +*(^ 5E^DCB Print
A) +*(+ Pop And Push

5E^DCB^
) +*(+ 5E^DCB^A Print
+* Pop Until '('

End 5E^DCB^A+

End Empty 5E^DCB^A+*+ Pop Every element


ALGORITHM:
Step 1: Start.
Step 2: Read an infix expression with parenthesis and without parenthesis.
Step 3: convert the infix expression to postfix expression. Step 4: Stop

PROGRAM CODE:

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

int F(char symbol)


{
switch(symbol)
{ case '+' case '-': return 2;
case ‘*’:
case ‘/’: return 4;
case '^': case '$': return 5;
case '(':
case '#': return -1; default: return 8;
}
}

int G(char symbol)


{
switch(symbol)
{ case '+'
case '-': return 1;
case ‘*’:
case ‘/’: return 3;
case '^': case '$': return 6; return 0;
case '(': return 9;
case ')': return 0;

default: return 7;
}}

void infix_postfix(char infix[], char postfix[])


{ int top, j, i;
char s[30], symbol; top = -1; s[++top] = '#'; j = 0;
for(i=0; i < strlen(infix); i++)
{ symbol = infix[i]; while(F(s[top]) > G(symbol))
{ postfix[j] = s[top--]; j++; }
if(F(s[top]) != G(symbol)) s[++top] = symbol;
else
top--;
}
while(s[top] != '#')
{ postfix[j++] = s[top--];
} postfix[j] = '\0';
}

void main()
{ char infix[20], postfix[20];
clrscr();
printf("\nEnter a valid infix expression\n");
flushall(); gets(infix); infix_postfix(infix,postfix); printf("\nThe infix expression is:\n"); printf ("%s",infix);
printf("\nThe postfix expression is:\n"); printf ("%s",postfix); getch();
}

OUTPUT:
Enter a valid infix expression (a+(b-c)*d) The infix expression is: (a+(b-c)*d) The postfix expression is:
abc-d*+
LAB EXPREMENT: 5
Design, Develop and Implement a menu driven Program in C for the following operations on Circular
QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations

ALGORITHM:
Step 1: Start.
Step 2: Initialize queue size to MAX.
Step 3: Insert the elements into circular queue. If queue is full give a message as ‘queue is overflow”
Step 4: Delete an element from the circular queue. If queue is empty give a message as ‘queue is
underflow’.
Step 5: Display the contents of the queue. Step 6: Stop.

ABOUT THE EXPERIMENT:


Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last node is
connected back to the first node to make a circle. Circular linked list fallows the First in First Out
principle. Elements are added at the rear end and the elements are deleted at front end of the queue.
The queue is considered as a circular queue when the positions 0 and MAX-1 are adjacent. Any
position before front is also after rear.

A circular queue looks like

Consider the example with Circular Queue implementation:


PROGRAM CODE:

#include<stdio.h>
#include<conio.h>
#define MAX 4

int ch, front = 0, rear = -1, count=0; char q[MAX], item;

void insert()
{ if(count == MAX) printf("\nQueue is Full");
else {
rear = (rear + 1) % MAX; q[rear]=item;
count++;
}
}

void del()
{ if(count == 0) printf("\nQueue is Empty");
else
{
if(front > rear && rear==MAX-1)
{
front=0; rear=-1; count=0;
}

else
{
item=q[front];
printf("\nDeleted item is: %c",item);
front = (front + 1) % MAX;
count--;
}
}
}

void display()
{ int i, f=front, r=rear; if(count == 0) printf("\nQueue is Empty");
else
{
printf("\nContents of Queue is:\n"); for(i=f; i<=r; i++)
{ printf("%c\t", q[i]);
f = (f + 1) % MAX;
}
}
}
void main()
{ clrscr(); do
{ printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit"); printf("\nEnter the choice: "); scanf("%d",
&ch); flushall(); switch(ch)
{
case 1: printf("\nEnter the character / item to be inserted: "); scanf("%c", &item); insert();
break;
case 2: del(); break;
case 3: display();
break;
case 4: exit(0);
break;
}}
while(ch!=4); getch();
}

POUTPUT:
1. Insert 2. Delete 3. Display
Enter the choice: 1 4. Exit
Enter the character / item to be inserted:
A
1. Insert 2. Delete 3. Display
Enter the choice: 1
4. Exit
Enter the character / item to be inserted:
B
1. Insert 2. Delete 3. Display
Enter the choice: 1 4. Exit
Enter the character / item to be inserted:
C
1. Insert 2. Delete 3. Display
Enter the choice: 1 4. Exit
Enter the character / item to be inserted:
D
1. Insert 2. Delete 3. Display
Enter the choice: 3

Contents of Queue is:


A B C D
4. Exit
1. Insert 2. Delete 3. Display
Enter the choice: 1 4. Exit
Enter the character / item to be inserted:
Queue is Full
F
1. Insert 2. Delete 3. Display
Enter the choice: 2 4. Exit

Deleted item is: A

1. Insert 2. Delete 3. Display


Enter the choice: 2

Deleted item is: B


4. Exit
1. Insert 2. Delete 3. Display
Enter the choice: 3

Contents of Queue is:


C D
4. Exit
1. Insert 2. Delete 3. Display
Enter the choice: 1 4. Exit
Enter the character / item to be inserted:
K
1. Insert 2. Delete 3. Display Enter the choice: 3
Contents of Queue is:
C D K
1. Insert 2. Delete 3. Display
Enter the choice: 4 4. Exit
4. Exit

EXPERIME

You might also like