Data Structure Lab Report
Data Structure Lab Report
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.
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()
{ printf("\nEnter the position for the new element: "); scanf("%d", &pos);
{ a[i+1] = a[i];
} a[pos] = elem;
n = n+1;
} //end of insert()
{ a[i] = a[i+1];
} n = n-1;
}//end of delete()
void main()
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: del();
break;
case 5: exit(0);
break;
}}
while(ch!=5); getch();
SAMPLE OUTPUT:
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
10 20 30 40 50
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
10 20 30 40 50
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
10 20 90 30 40 50
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
--------Menu-----------
1.Create
2.Display
3.Insert
4.Delete
5.Exit
10 20 90 30 40
--------Menu-----------
1. Create
2. Display
3.Insert
4.Delete
5.Exit
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.
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."
If you follow the rule of array initialization then you can write the above statement as follows:
char greeting[]
="Hello";
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
void stringmatch()
{ i = m = c = j = 0; while(str[c] ! = '\0')
{ flag = 1;
i = 0; c = m;
} // if ends.
} //end stringmatch()
void main()
{ clrscr();
getch();
} // end of main
SAMPLE OUTPUT:
RUN 1:
Test
Te
Re
Rest
RUN 2:
RUN 3:
Date
Enter a replace string
DATA
LAB EXPRIMENT: 3
Design, Develop and Implement a menu driven program in C for the following operations on
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.
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 5: Stop.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 4
/*PUSH FUNCTION*/
else
/*POP FUNCTION*/
else
} return ret;
else
int i;
else{
printf("\n");
/*MAIN PROGRAM*/
void main()
switch(ch){
palindrome(stack);
break;
default:
printf("\nEND OF EXECUTION");
}
OUTPUT:
----MAIN MENU----
-----
|1|
----MAIN MENU----
------
|2|
------|
1|
----MAIN MENU----
Stack is Overflow
------
|1|
------
|2|
------|
2|
------|
1|
----MAIN MENU----
Popped element is 1
------
|2|
------
|2|
------
|1|
----MAIN MENU----
Stack is Underflow
Stack is Empty
----MAIN MENU----
-----
|1|
----MAIN MENU----
------
|2|
------|
1|
----MAIN MENU----
------
|1|
------
|2|
------
|1 |
----MAIN MENU----
----MAIN MENU----
-----
|1|
------
|3|
------
|2|
------
|1|
----MAIN MENU----
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 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.
A+(B*C-(D/E-F)*G)*H
5E^DCB^
) +*(+ 5E^DCB^A Print
+* Pop Until '('
End 5E^DCB^A+
PROGRAM CODE:
#include<stdio.h>
#include<string.h>
#include<conio.h>
default: return 7;
}}
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.
#include<stdio.h>
#include<conio.h>
#define MAX 4
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
EXPERIME