0% found this document useful (0 votes)
119 views

Aim & Algorithm

The document contains several examples of implementing different data structures using arrays and linked lists in C. Example 1 shows implementing a stack using an array. Example 2 shows implementing a queue using an array. Example 3 shows implementing a stack and queue using linked lists. Example 4 shows representing polynomials as linked lists and adding two polynomials by traversing the lists. The examples provide algorithms for performing operations like push, pop, insert, delete on each data structure. The results show the C programs were successfully implemented.

Uploaded by

Sowbarniga Sow
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)
119 views

Aim & Algorithm

The document contains several examples of implementing different data structures using arrays and linked lists in C. Example 1 shows implementing a stack using an array. Example 2 shows implementing a queue using an array. Example 3 shows implementing a stack and queue using linked lists. Example 4 shows representing polynomials as linked lists and adding two polynomials by traversing the lists. The examples provide algorithms for performing operations like push, pop, insert, delete on each data structure. The results show the C programs were successfully implemented.

Uploaded by

Sowbarniga Sow
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/ 17

EX.

NO:1A ARRAY IMPLEMENTATION OF STACK ADT

AIM:

Aim is to write a program in C to implement the stack ADT using array concept that performs
all the operations of stack.
ALGORITHM:

STEP 1: Define an array to store the element.

STEP 2: Get the users’ choice.

STEP 3: If the option is 1 perform creation operation and go to step4.


If the option is 2 perform insertion operation and go to

step5. If the option is 3 perform deletion operation and go to

step6. If the option is 4 perform display operation and go to

step7.

STEP 4: Create the stack. Initially get the limit of stack and the get the items. If the limit of
stack is exceeds print the message unable to create the stack.

STEP 5: Get the element to be pushed. If top pointer exceeds stack capacity. Print Error
message that the stack overflow. If not, increment the top pointer by one and store the
element in the position which is denoted by top pointer.

STEP 6: If the stack is empty, then print error message that stack is empty. If not fetch the
element from the position which is denoted by top pointer and decrement the top
pointer by one

STEP 7: If the top value is not less than the 0 the stack is display otherwise print the message
“stack is empty”.

STEP 8: Stop the execution.

RESULT:
Thus a C program for Stack using ADT was implemented successfully
EX. NO: 1B QUEUE ADT USING ARRAY

AIM: To write a program for Queue using array implementation


ALGORITHM:
1. Define an array which stores queue elements...
2. The operations on the queue are
a. a)INSERT data into the queue
b. b)DELETE data out of queue
3. INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
b. If TOP is NULL
i. The input data is the first node in queue.
ii. The link of the node is NULL.
iii. TOP points to that node.
c. If TOP is NOT NULL
i. The link of TOP points to the new node.
ii. TOP points to that node.
4. DELETE DATA FROM queue
a. If TOP is NULL
i. the queue is empty
b. If TOP is NOT NULL
i. The link of TOP is the current TOP.
ii. The pervious TOP is popped from queue.

5. The queue represented by linked list is traversed to display its content.

RESULT:

Thus a C program for Queue using ADT was implemented successfully


EX.NO:2 ARRAY IMPLEMENTATION OF LIST ADT

AIM: To write a program for List using array implementation.


ALGORITHM:
Step1: Create nodes first, last; next, prev and cur then set the value as
NULL.

Step 2: Read the list operation type.

Step 3: If operation type is create then process the following steps.


1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Assign cur node as NULL.
4. Assign first=last=cur.
Step 4: If operation type is Insert then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
Read the position the Data to be insert.
3. Availability of the position is true then assign cur's node as first and first=cur.
4. If availability of position is false then do following steps.
1. Assign next as cur and count as zero.
2. Repeat the following steps until count less than
position. 1 .Assign prev as next
2. Next as prev of node. 3. Add count by one.
4. If prev as NULL then display the message INVALID POSITION.
5. If prev not qual to NULL then do the following
steps.
1. Assign cur's node as prev's node.
2. Assign prev's node as cur.
Step5: If operation type is delete then do the following steps.
1. Read the position.
2. Check list is Empty .If it is true display the message List empty.
3. If position is first.
1. Assign cur as first.
2. Assign first as first of node.
3. Reallocate the cur from memory.
4. If position is last.
1. Move the current node to prev.
2. cur's node as Null.
3. Reallocate the Last from memory.
4. Assign last as cur.
5. If position is enter Mediate.
1. Move the cur to required position.
2. Move the Previous to cur's previous position
3. Move the Next to cur's Next position.
4. Now assign previous of node as next.
5. Reallocate the cur from
memory. Step 6: If operation is traverse.
1. Assign current as first.
2. Repeat the following steps until cur becomes NULL.

RESULT:

Thus the C program for array implementation of List ADT was created,
executed and output was verified successfully
EX. NO: 3A STACK ADT USING LINKED LIST

AIM:
To write a C program for stack ADT using linked list implementation.

1. Define a struct for each node in the stack. Each node in the stack contains
data and link to the next node. TOP pointer points to last node inserted in
the stack.
2. The operations on the stack are
a. PUSH data into the stack
b. POP data out of stack
3. PUSH DATA INTO STACK
a. Enter the data to be inserted into stack.
b. If TOP is NULL
i. The input data is the first node in stack.
ii. The link of the node is NULL.
iii. TOP points to that node.
c. If TOP is NOT NULL
i. The link of TOP points to the new node.
ii. TOP points to that node.
4. POP DATA FROM STACK

a. 4a.If TOP is NULL


i. the stack is empty
b. 4b.If TOP is NOT NULL
i. The link of TOP is the current TOP.
ii. The pervious TOP is popped from stack.
5. The stack represented by linked list is traversed to display its content.

RESULT:

Thus the C program for array implementation of Stack ADT was created,
executed and output was verified successfully
EX. NO: 3B QUEUE ADT USING LINKED LIST
AIM:
To write a C program for Queue using Linked implementation.
ALGORITHM:
1. Define a struct for each node in the queue. Each node in the queue
Contains data and link to the next node. Front and rear pointer points to
first and last node inserted in the queue.

2. The operations on the queue are


a. INSERT data into the queue
b. DELETE data out of queue
3. INSERT DATA INTO queue
a. Enter the data to be inserted into queue.
b. If TOP is NULL
i. The input data is the first node in
queue. ii. The link of the node is NULL.
iii. TOP points to that node. c.
If TOP is NOT NULL
i. The link of TOP points to the new
node. ii. TOP points to that node.
4. DELETE DATA FROM queue a.
If TOP is NULL
I. the queue is empty b. If
TOP is NOT NULL

i. The link of TOP is the current TOP.


ii. The pervious TOP is popped from queue.
5. The queue represented by linked list is traversed to display its content.
RESULT:

Thus the C program for array implementation of Queue ADT was created, executed
and output was verified successfully
\

EXNO: 4A REPRESENT A POLYNOMIAL AS A LINKED LIST

AIM:

To write program in C to convert given infix expression in to postfix notation


ALGORITHM

1: Get the two polynomials. First polynomial is P1 and second polynomial is P2

2: For addition of two polynomials if exponents of both the polynomials are same then we
add the coefficients. For storing the result we will create the third linked lists say P3.
3: If Exponent of P2 is greater than exponent of P1 then keep the P3 as P2.

4: If Exponent of P2 is greater than exponent of P1 then keep the P3 as P1

5: If Exponent of P2 is equal to the exponent of P1 then add the coefficient of P1 and


coefficient of P2 as coefficient of P3.

6: Continue the above step from 3 to 5 until end of the two polynomials.

7: If any of the polynomial is ended keep P3 as the remaining polynomial.


8: Stop the execution.

RESULT:

Thus the program in C to convert given infix expression in to postfix notation


DATA STRUCTURES LABORATORY

EX.NO:4B CONVERSION OF INFIX EXPRESSION TO POSTFIX NOTATION

AIM:

To write program in C to convert given infix expression to postfix notation

ALGORITHM:

1: Get an infix expression.

2: Scan the expression from left to

right. 3: If any operands come display

it.

4: If the incoming symbol in an operator and has more priority then the symbol into the stack.

5: If the incoming operator has less priority than the stack symbol then copy the symbol at

the

Top of the stack and then print until the condition becomes false and push the following

operator on the stack.

6: If the symbol is ‘)’ then copy operators from top of the stack. Deletion opening

parenthesis is from top of the stack.

7: Stop the process.


RESULT:

Thus the program in C to convert given infix expression to postfix notation

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


DATA STRUCTURES LABORATORY

EXNO: 5 IMPLEMENTATION BINARY TREE AND OPERATIONS OF


BINARY TREES

AIM:
To write a C program Implementation Binary Tree and Operations of Binary Trees
ALGORITHM
1. Start from root.
2. Compare the inserting element with root, if less than root, then visit for left, else visit for right.
3. If element to search is found anywhere, return true, else return false

Result:

Thus the program in C is implemented Binary Tree and Operations of Binary Trees.
EX. NO: 6 IMPLEMENTATION OF BINARY SEARCH TREE

AIM:
To write a C program to implementation of binary search tree.
ALGORITHM:

1. Declare function create (), search (), delete (), Display ().
2. Create a structure for a tree contains left pointer and right pointer.
3. Insert an element is by checking the top node and the leaf node and the
operation will be performed.
4. Deleting an element contains searching the tree and deleting the item.
5. Display the Tree elements.
RESULT:

Thus the C program for binary search tree was created, executed and output was
verified successfully.
EX NO: 7 IMPLEMENTATION OF AVL TREE

AIM:-

To write a C program to implement insertion in AVL trees.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


DATA STRUCTURES LABORATORY
ALGORITHM:-

1. Initialize all variables and functions.


2. To insert and element read the
value. 3.Check whether root is null
4. If yes make the new value as root.
5. Else check whether the value is equal to root value.
6. If yes, print “duplicate value”.
7. Otherwise insert the value at its proper position and balance the tree using
rotations.
8. To display the tree values check whether the tree is null.
9. If yes, print “tree is empty”.
10. Else print all the values in the tree form and in order of the
tree. 11. Repeat the steps 2 to 10 for more values.
12. End

RESULT

Thus the ‘C’ program to implement an AVL trees. Produce its pre-Sequence, In-Sequence, and Post-
Sequence traversals
EXNO: 8 IMPLEMENTATION OF PRIORITY QUEUE USING HEAPS

AIM:
To write a C program to implement Priority Queue using Binary Heaps.
ALGORITHM:

1. Initialize all necessary variables and functions.

2. Read the choices.

3. For insertion, read the element to be inserted.

4. If root is NULL, assign the given element as root.

5. If the element is equal to the root, print “Duplicate value”.

6. Else if element value is less than the root value, insert element at the left of the root.

7. Else insert right side of the root.

8. For deletion, get the priority for maximum or minimum.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


DATA STRUCTURES LABORATORY
9. If maximum, it deletes the root and rearranges the tree.

10. If minimum, it deletes the leaf.

11. End of the program

RESULT:

Thus the Priority Queue using Binary Heap is implemented and the
result is verified successfully

Ex: 9.A GRAPH REPRESENTATIONS

AIM:

To write a C program implement adjacent matrix and adjacency list.


ALGORITHM:

1. Create a graph with getting no. of vertices and no. of edges

2. Implement adjacency matrix

3. Implement adjacency list

4. Close the program


RESULT:

Thus the C program implemented adjacent matrix and adjacency list


EX.NO.9.B Implement DFS and BFS graph traversal

Aim:
To write a C program implement DFS and BFS graph traversal.
ALGORITHM:

DFS
1. Define a Stack of size total number of vertices in the graph.
2. Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
3. Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not
visited and push it on to the stack.
4. Repeat step 3 until there are no new vertex to be visit from the vertex on top of the stack.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES LABORATORY
5. When there is no new vertex to be visit then use back tracking and pop one vertex from the stack.
6. Repeat steps 3, 4 and 5 until stack becomes Empty.
7. When stack becomes Empty, then produce final spanning tree by removing unused edges from
the graph

BFS
1. Define a Queue of size total number of vertices in the graph.
2. Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
3. Visit all the adjacent vertices of the vertex which is at front of the Queue which is not visited
and insert them into the Queue.
4. When there is no new vertex to be visit from the vertex at front of the Queue then delete that
vertex from the Queue.
5. Repeat step 3 and 4 until queue becomes empty.
6. When queue becomes Empty, then produce final spanning tree by removing unused edges from
the graph

RESULT:

Thus the C program implemented DFS and BFS graph traversal.

Ex: 10. IMPLEMENTATION OF SEARCHING ALGORITHMS LINEAR

SEARCH AND BINARY SEARCH

AIM:

To write a C Program to implement different searching techniques – Linear and Binary search.
ALGORITHM:
Linear Search:
1. Read the search element from the user
2. Compare, the search element with the first element in the list.
3. If both are matching, then display "Given element found!!!" and terminate the function
4. If both are not matching, then compare search element with the next element in the list.
5. Repeat steps 3 and 4 until the search element is compared with the last element in the list.
6. If the last element in the list is also doesn't match, then display "Element not found!!!" and
terminate the function.
Binary search is implemented using following steps...

1. Read the search element from the user


2. Find the middle element in the sorted list
3. Compare, the search element with the middle element in the sorted list.
4. If both are matching, then display "Given element found!!!" and terminate the function
5. If both are not matching, then check whether the search element is smaller or larger than
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES LABORATORY
middle element.
6. If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5 for the
left sub list of the middle element.
7. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for the
right sub list of the middle element.
8. Repeat the same process until we find the search element in the list or until sub list contains
only one element.
9. If that element also doesn't match with the search element, then display "Element not found in
the list!!!" and terminate the function.

RESULT

Thus the C Program to implement different searching techniques – Linear and Binary search

Ex. No: 11.a BUBBLE SORT

AIM:

To write a C program to implement the concept of bubble sort


ALGORITHM:

1: Start.
2: Repeat Steps 3 and 4 for i=1 to 10
3: Set j=1
4: Repeat while j<=n

(A) if a[i] < a[j]

Then interchange a[i] and a[j]

[End of if]

(B) Set j = j+1


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES LABORATORY
[End of Inner Loop]

[End of Step 1 Outer Loop]

5: Stop.
RESULT:

Thus a C program for the concept of bubble sort was implemented successfully.
Ex. No: 11.B MERGE SORT
AIM:
To write a C program to implement the concept of merge sort.
ALGORITHM:

1: Start.

2: First you divide the number of elements by 2 and separate them

as two. 3: Divide those two which are divided by 2.

4: Divide them until you get a single element.

5: Start comparing the starting two pair of elements with each other and place them in ascending order.
6: When you combine them compare them so that you make sure they are sorted.

7: When all the elements are compared the array will be surely sorted in an ascending

order.

8: Stop.

RESULT:
Thus a C program for the concept of merge sort was implemented successfully.

Ex. No: 11.c QUICK SORT

AIM:

To write a C program to implement the concept of Quick sort.


ALGORITHM:

1: Start.
2: Choose any element of the array to be the pivot.
3: Divide all other elements (except the pivot) into two partitions.
o All elements less than the pivot must be in the first partition.
o All elements greater than the pivot must be in the second partition.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES LABORATORY
4: Use recursion to sort both partitions.

5: Join the first sorted partition, the pivot, and the second sorted partition.

6: Stop
RESULT:

Thus the C program to implement the concept of Quick sort.

Ex. No: 12 HASHING TECHNIQUES

AIM: To write a C program to implement hash table.


ALGORITHM:

1. Create a structure, data (hash table item) with key and value as data.

2. Now create an array of structure, data of some certain size (10, in this case). But, the size of array
must be immediately updated to a prime number just greater than initial array capacity (i.e. 10, in
this case).
3. A menu is displayed on the screen.

4. User must choose one option from four choices given in the menu

5. Perform all the operations

6. Stop the program


RESULT: Thus the C program to implement Hash Table is done successfully.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


DATA STRUCTURES LABORATORY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

You might also like