Professional Documents
Culture Documents
(ETCS-255).
Semester: 3rd
Group: I8
1 Introduction to data
structure
2 write a program to
insert any value
entered by the user at
a particular location
in an array
5 write a program to
create a singly linked
list
6 write a program to
search a given
element in a singly
linked list
8 write a program to
implement stack
using array, push and
pop
Implement a
9 doubly linked list
in c
implement a
circular queue
10
1 Implement a
1 queue using
singly linked list
12 Implement in
order, pre order
and post order on
binary search
tree
.
13 Implement a
stack using
singly linked
list.
14 Implement
insertion, merge,
selection sort
15 implement queue
using array
16 implement
circular linked
list
17 create and
display binary
search tree
EXPERIMENT-1
AIM- Introduction to data structures.
The elements of array share the same variable name but each one carries
a different index number known as subscript. The array can be one
dimensional, two dimensional or multidimensional.
Stack: Stack is a linear list in which insertion and deletions are allowed
only at one end, called top.
ALGORITHM-
1. Get the element to be inserted, say x
2. Then get the position at which this element is to be inserted, say
pos
3. Then shift the array elements from this position to one position
forward, and do this for all the other elements next to pos.
4. Insert the element x now at the position pos, as this is now
empty.
CODE:
OUTPUT:
EXPERIMENT-3
AIM- Write a program to perform binary search on array
ALGORITHM-
1. Compare x with the middle element.
2. If x matches with the middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in
the right half sub array after the mid element. So we recur for the
right half.
4. Else (x is smaller) recur for the left half.
CODE-
OUTPUT:
EXPERIMENT-4
AIM- write a program to perform linear search on array
ALGORITHM-
1. Start from the leftmost element of arr[] and one by one compare
x with each element of arr[]
2. If x matches with an element, return the index.
3. If x doesn’t match with any of elements, return -1.
CODE:
OUTPUT:
EXPERIMENT-5
AIM- Write a program to create a singly linked list
CODE:
OUTPUT:
EXPERIMENT-6
ALGORITHM-
1) If head is NULL, return false.
2) If head's key is same as x, return true;
3) Else return search (head->next, x)
CODE:
OUTPUT:
EXPERIMENT-7
AIM-write a program to create a linked list to insert and delete a
node.
ALGORITHM-
CODE:
OUTPUT:
EXPERIMENT -9
ALGORITHM:
1. Insertion
2. Deletion
3. Display
Insertion
In a double linked list, the insertion operation can be performed
in three ways as follows...
→ previous as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULLto newNode
→ next and newNode to head.
Step 4 - If it is not Empty then, assign head to newNode
→ next and newNode to head.
→ next as NULL.
• Step 2 - Check whether list is Empty (head == NULL) • Step
3 - If it is Empty, then assign NULLto newNode →
previous and newNode to head.
• Step 4 - If it is not Empty, then, define a node pointer temp
and initialize with head.
• Step 5 - Keep moving the temp to its next node until it
reaches to the last node in the list (until temp → next is equal
to NULL).
• Step 6 - Assign newNode to temp → next and temp to
newNode → previous.
Deletion
In a double linked list, the deletion operation can be performed in
three ways as follows...
CODE-
I rave seend (temp2 n ) ;
sea rch ( ) ;
SOPt(),
update();
/oid c °eate (
)
int data;
temp (struct c eate();
node )
temp prev ;
temp next h; h p rev
temp next ;
temp; h temp;
( , data ; temp ndata;
count ;
void inse z2 ( )
void insertl() (h
(h c eate ( ) ; h tenp,
temp1 h;
c ease
void
OUTPUT:
EXPERIMENT-10
ALGORITHM:
1. Insertion:
Step 1: IF (REAR+1)%MAX =
FRONT Write " OVERFLOW "
Goto step 4
[End OF
IF]
2. Deletion:
Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of
IF]
IF] Step 4:
EXIT
CODE:
#include<stdi
o.h> # define
MAX 5
int
cqueue_arr[MA
X]; int front = -
1; int rear = -1;
void insert(int
item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{ printf("Queue
Overflow n");
return;
}
if(front == -1)
{ front
= 0;
rear =
0; }
else
{ if(rea
r ==
MAX-
1) rear
= 0; else rear = rear+1;
} cqueue_arr[rear] =
item ; } void deletion()
{ if(front == -1)
{ printf("Queue
Underflown"); return
;
}
printf("Element deleted from queue is :
%dn",cqueue_arr[front]); if(front == rear)
{ fro
nt =
-1;
rear
=-
1; } else
{ if(fron
t ==
MAX-1) front =
0; else front =
front+1;
} } void
display() {
int front_pos = front,rear_pos
= rear; if(front == -1)
{ printf("Queue
is emptyn");
return;
} printf("Queue
elements :n");
if( front_pos <=
rear_pos )
while(front_pos <=
rear_pos) { printf("%d
",cqueue_arr[front_pos
]); front_pos++; } else
{
while(front_pos <= MAX-1)
{ printf("%d
",cqueue_arr[front_pos
]) front_pos++;
}
front_pos = 0;
while(front_pos <=
rear_pos) { printf("%d
",cqueue_arr[front_pos
]); front_pos++;
}}
printf("n
"); } int
main() {
int
choice,item;
do
{
printf("HEMANT(07914802719)\
n1.I nsertion\n"); printf("2.Deletion\
n"); printf("3.Display n\n");
printf("4.Quit\n"
);
printf("Enter your choice
: ");
scanf("%d",&choice); switch(choice)
{ case 1 : printf("Input the element for
insertion in queue : "); scanf("%d",
&item); insert(ite m); break; case 2 :
deletio n(); break; case 3: display
();
break;
case 4:
break
;
defau
lt:
printf("Wrong choicen");
}
}while(choice
!=4); return 0;
}
OUTPUT:
EXPERIMENT -11
ALGORITHM:
Operations
To implement queue using linked list, we need to set the following
things before implementing actual operations.
Step 1 - Include all the header files which are used in the
program. And declare all the user defined functions.
Step 2 - Define a 'Node' structure with two members data
and next.
Step 3 - Define two Node pointers 'front' and 'rear' and set
both to
NULL.
Step 4 - Implement the main method by displaying Menu of
list of operations and make suitable function calls in the
main method to perform user selected operation.
CODE:
OUTPUT:
EXPERIMENT-12
ALGORITHM:
if T is NULL {
T->data = data;
T->left = NULL;
T->right = NULL;
return T;
2.
3.
1.
2.
It this case, node is cut from the tree and algorithm links
single child (with it's subtree) directly to the parent of the
removed node. Example. Remove 18 from a BST.
3.
1
.
Node to be deleted has two children.
2.
3.
contains the same values {5, 19, 21, 25}. To transform first tree
into second one, we can do following:
CODE:
OUTPUT:
EXPERIMENT -13
ALGORITHM:
⚫ Step 1 - Include all the header files which are used in the
program. And declare all the user defined functions.
⚫ Step 2 - Define a 'Node' structure with two members data and
next.
⚫ Step 3 - Define a Node pointer 'top' and set it to NULL.
⚫ Step 4 - Implement the main method by displaying Menu with
list of operations and make suitable function calls in the main
method.
CODE:
OUTPUT:
EXPERIMENT -14
ALGORITHM:
Sorting Efficiency
program.
Type of Sorting
• Bubble Sort
• Selection Sort
• Insertion Sort
• Quick Sort
• Merge Sort
• Heap Sort
• Shell Sort
• Bucket or Radix Sort
Selection Sort
Insertion Sort
a[$ ] k ey ; r , i nt si ze )
(a[j] a[j j)
v oi d me r ge1 i nt a [ § , i nt T , i nt ir, i nt
i nt I emp [ s i ze ;
(a[ij a[jj/
I emp [ k a [ i ;
I emp [ k a [ q ;
k ;
l exp [ k
a[i];
a[j];
I exp [ k
a p te up p ;
\ oi d me n ge s a at (i nt a n n [ ] , int T , i nt n , int s i ze )
int mi d ( 1 n ) ;
v oi d qu i c k s o at ( i nt a n n [ , i nt s , i nt e )
OUTPUT:
EXPERIMENT -15
AIM- program to implement queue using array
ALGORITH –
INSERTION
• Step 1: if rear=max-1
Write overflow
Go to step
(End of if)
• Step 2 : if front =-1 and rear =-1
Set front = rear=0
Else
Set rear =rear+1
(End of if)
• Step 3 : set queue[rear]=num
• Step 4 : exit
DELETION
• Step 1: if front =-1 or front > rear
Write underflow
Else
Set val =queue[front]
Set front = front +1
[end of if]
• Step 2 :exit
CODE:
OUTPUT :
EXPERIMENT-16
AIM: write a program to implement circular linked list
ALGORITHM :
INSERT
• Step 1 - Create a newNode with given value.
• Step 2 - Check whether list is Empty (head == NULL)
• Step 3- set head = newNode and newNode→next = head .
• Step 4 - If it is Not Empty then, define a Node pointer 'temp'
and initialize with 'head'.
• Step 5 - Keep moving the 'temp' to its next node until it
reaches to the last node (until 'temp → next == head').
• Step 6 - Set 'newNode → next =head', 'head = newNode' and
'temp → next = head'.
DELETE
CODE:
OUTPUT: