Cs6212 Programming and Data Structures Laboratory I Laboratory Manual
Cs6212 Programming and Data Structures Laboratory I Laboratory Manual
Page 1
LIST OF EXERCISES:
1. Greatest of n numbers using array
2 a. Sorting of n numbers using function
2 b. String Manipulations String Copy, Concatenation, Split
2 c. Alphabetic Arrangement of Strings using function
2 d. Sum and Average of m to n Numbers using pointers.
3.a. Implementation and operation of a linked list using structure in C
3 b.Implementation of Doubly Linked List.
4 a. Read & Write operations in File using sequential access
4 b. Read & Write operations in File using random access
5 a. Implementation of Stack ADT using Array
5 b. Implementation of Queue ADT using Array
5 c. Implementation of Stack ADT using Linked List
5 d. Implementation of Queue ADT using Linked List
5 e. Stack Application - Infix to Postfix Conversion
5 f. Implementation of Polynomial ADT.
6 a. Implementation of Insertion sort
6 b. Implementation of Selection sort
6 c. Implementation of Shell sort
6 d. Implementation of Bubble sort
6 e. Implementation of Merge sort
6 f. Implementation of Quick sort
7 a. Implementation of Linear Search using array
SARANATHAN COLLEGE OF ENGINEERING
Page 2
EX NO: 1
AIM:
To write a C program to find the greatest of n numbers using array.
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 3
EX NO: 2a
AIM:
To write a C program to sort n numbers using function.
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 4
AIM:
To write a C program to perform string manipulation.
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 5
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 6
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 7
ALGORITHM:
Step 1: Start
Step 2:Read the value of ch
Step 3:If ch=1 call create list function
Step 4:If ch=2 call insertlist function
Step 5:If ch=3 call deletefirst function
Step 6:If ch=4 call viewlist function
Step 7:Repeat step 3 while (ch!=5)
Step 8:Stop
ALGORITHM FOR CREATE LIST
Step 1: Read value of item
Step 2:Allocate the memory for new node
Step 3:Assign values to new node data part
Step 4:Assign new node address part as null
ALGORITHM FOR INSERT LIST
Step 1: Read the value of item
Step 2:Allocate the memory for new node
Step 3:Assign values of data field as null else make link fields of all new nodes to point
Step 4:Set the external pointer from the starting node to new node
Page 8
RESULT:
INPUT
EX NO: 3b
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
AIM:
To write a C program to implement the Doubly linked list.
SARANATHAN COLLEGE OF ENGINEERING
Page 9
ALGORITHM:
Step 1:Start
Step 2:Read the value of ch
Step 3:If ch=1 call create list function
Step 4:If ch=2 call insert list function
Step 5:If ch=3 call delete firstt function
Step 6:If ch=4 call view list function
Step 7:If ch=5 call the exit function
Step 8:Repeat step 2 while (ch!=5)
Step 9:Stop
ALGORITHM FOR CREATE LIST
Step 1:Read the value of item
Step 2:Allocate the memory for newly assigned nodes
Step 3:Assign the data to the data field
Step 4:Assign forward and backward link as Null
ALGORITHM FOR INSERT LIST
Step 1:Read the value of item
Step 2:Allocate a new node and assign the item to the data part
Step 3:If head is NULL then Assign link of new node to head and blink of new node to
NULL
Step 4: Return
Page 10
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 11
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 12
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 13
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 14
EX NO: 5b
AIM:
To write a C program to implement queue ADT using array.
ALGORITHM:
Enqueue:
Step 1: Check if queue is not full.
Step 2: If it is full then return queue overflow and item cannot be inserted.
Step 3: If not, check if rear value is -1, if so then increment rear and by 1; if not increment front
by 1.
Step 4. Store the item in the new value of front.
Dequeue:
Step 1: Check if queue is not empty.
Step 2: If it is empty, return queue underflow and dequeue operation cannot be done.
Step 3: If not, check if rear and front are equal, if so assign -1 to front and rear; if not decrement
front by 1.
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 15
EX NO: 5c
AIM:
To write a C program to implement stack ADT using linked list.
ALGORITHM:
A) Push Operation:
1. To push an element into the stack, assign the element to be inserted in the data field of the new
node.
2. Assign the reference field of the new node as NULL.
3. Check if top is not NULL, if so, then assign the value of top in the reference field of
new node.
4. Assign the address of the new node to the top.
B) Pop Operation:
1. To pop an element from the stack, check whether the top of the stack is NULL.
2. If so, then return stack is empty and element cannot be popped from the stack.
3. Else, assign the top value to a temporary node.
4. Now assign the value in the reference field of the node pointed by top to the top value.
5. Return the value in the data field of the temporary node as the element deleted and
delete the temporary node.
C) Returning Top
Step:1 Return top node.
Page 16
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 17
EX NO: 5d
AIM:
To write a C program to implement queue ADT using linked list.
ALGORITHM:
Enqueue:
Step1: Create a new node and allocate memory space for the new node.
Step2: Assign the element to be inserted in the data field of the new node.
Step3: Assign NULL to the address field of the new node.
Step4: Check if rear and front pointers are NULL.
Step5: If so, then make the front and rear pointers to point to new node.
Step6: If not, then assign address of the new node as the rear pointer value.
Dequeue:
Step1: Check if queue is not empty.
Step2: If it is empty, return queue underflow and dequeue operation cannot be done.
Step3: If not, assign the front->next value as the new front pointer and free the deleted node.
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 18
EX NO: 5e
AIM:
To write a C program to implement infix to postfix conversion.
ALGORITHM:
Step 1:Read the infix expression and find the length of the expression
Step 2:If the current character is open parenthesis ( then push it into the stack
Step 3:When the current character is an operand then add it to the result
Step 4When the current character is operator check the priority of scanned operator with top of
the stack. If the priority of the scanned character is greater than top of the stack
Step 5: If the element of the current operator is less than or equal to the top of the stack then
i) pop the top character from the stack
ii) push the scanned operator into the stack
Step 6: When the current character is closing parenthesis then pop all the characters above
opening parenthesis if any from the stack
Step7: Return the result
Step 8: End
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 19
EX NO: 5f
AIM:
To write a C program to perform operation of polynomial ADT.
ALGORITHM:
Step 1:while poly1 and poly2 are not null repeat step 2
Step 2:if power of the two term are equal then add the coefficients of poly1 and poly2 and create
list poly3 and advance the pointers of poly1 and poly2 else goto step 3
Step 3:copy remaining terms from the non empty polynomial in to the poly3.
Step 4:return the poly 3 list.
Step5:Stop.
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 20
EX NO: 6a
AIM:
To write a C program to implement the insertion sort.
ALGORITHM:
Step1:Get the n elements to be sorted.
Step2: The ith element is compared from (i-1)th to 0th element and placed in proper position
according to ascending value.
Step 3: Repeat the above step until the last element.
Step 4: print the list.
Step 5: Stop.
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 21
EX NO: 6b
AIM:
To write a C program to implement the selection sort.
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 22
EX NO: 6c
AIM:
To write a C program to implement the shell sort.
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 23
EX NO: 6d
AIM:
To write a C program to implement the bubble sort.
ALGORITHM:
Step 1:Get the n elements to be sorted.
Step 2: Compare the first two elements of the array and swap if necessary.
Step 3 Then, again second and third elements are compared and swapped if it is necessary and
continue this process until last and second last element is compared and swapped.
Step 4: Repeat the above two steps n-1 times and print the result.
Step5: Stop.
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 24
EX NO: 6e
AIM:
To write a C program to implement the merge sort.
ALGORITHM:
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 25
AIM:
To write a C program to implement the quick sort.
ALGORITHM:
Step1:Pick an element, called a pivot, from the list.
Step2:Reorder the list so that all elements which are less than the pivot come before the pivot
and so that all elements greater than the pivot come after it.
Step3:After this partitioning, the pivot is in its final position. This is called the partition
operation.
Step4:Recursively sort the sub-list of lesser elements and the sub-list of greater elements
Step 5: Print the list.
Step 6: Stop
RESULT:
INPUT
EX NO: 7a
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 26
AIM:
To write a C program to implement the linear search.
ALGORITHM:
Linear Search
Step1: Read n numbers and search value.
Step2: If search value is equal to first element then print value is found.
Step3: Else search with the second element and so on.
Step4: Stop
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 27
AIM:
To write a C program to implement the binary search.
ALGORITHM:
Step1: Read n numbers and search value.
Step2: calculate middle=(lower bound+higher bound)/2
Step 3: If search value is equal to middle element then print value is found.
Step3: If search value is less than middle element then repeat Step 2 with higher bound middle-1
search left half of list else repeat Step 2 with lower bound middle+1 search right half of list.
Step 4: Stop
RESULT:
INPUT
EXPECTED
OUTPUT
ACTUAL OUTPUT
RESULT
Page 28