Está en la página 1de 13

SYLLABUS

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB 0 0 3 2

1. Basic Programs for C++ Concepts 2. Array implementation of List Abstract Data Type (ADT) 3. Linked list implementation of List ADT 4. Cursor implementation of List ADT 5. Stack ADT - Array and linked list implementations The next two exercises are to be done by implementing the following source files (a) Program source files for Stack Application 1 (b) Array implementation of Stack ADT (c) Linked list implementation of Stack ADT (d) Program source files for Stack Application 2 An appropriate header file for the Stack ADT should be #included in (a) and (d) 6. Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c)) 7. Queue ADT Array and linked list implementations 8. Search Tree ADT - Binary Search Tree 9. Heap Sort 10. Quick Sort

AIM:
1. Write

a program in C to implement List ADT using arrays. .

ALGORITHM: Algorithm for Insertion: Step 1: Get the key and the position of the element to be inserted. Step 2: Check whether the list is full. If it is full then print The List is full. The insertion is not possible and exit else go to the following steps. Step 3: Push the element to the next position by one step from the position specified for the insertion. Step 4: Increment the size by 1. Step 5: Exit. Algorithm for Deletion: Step 1: Get the element to be deleted. Step 2: Check whether the list is empty. If it is empty then print The List is empty. If deletion is not possible and exit and go to the following steps. Step 3: Check whether the element is present in the list or not. If it is present then go to step 3.1 else print The element is not present in the List and exit. Step 3.1: Push all the element from the position of the element to be deleted to the previous position in the list. Step 3.2: Decrement the size by 1. Step 4: Exit. Algorithm to find the k th element:

Step 1: Get the position (k) from to be deleted. Step 2: Check whether the k value is in the list. Step 3: If it is present then print the element else print The element is not present in the List in the given position. Step 4: Exit. Algorithm for makeEmpty: Step 1: Check whether the List is empty. If it is empty then print The List is empty and exit else go to the following step. Step 2: Assign size = -1 Step 3: Exit.

AIM:

2.Write a program in C to

implement List ADT using pointers. ALGORITHM: Algorithm for Insertion: Step 1: Get an Element and the position from the user to be inserted. Step 2: Create a new node which holds the data to be inserted. Step 3: Assign the new element to the data part of the new node and Null part to the next node. Step 4: Traverse the Linked list until the position of the insertion and the previous node of the insertion. Step 5: Assign the new node pointer to point to the next node of the List.

Step 6: Assign the previous node to point to the new node. Step 7: Exit. Algorithm to find a node: Step 1: Get the value for the element to be found. Step 2: If the List is not Null do the following steps else print The List is Null and exit. Step 2.1 : Traverse the List until the element is found. Step 2.2 : If found return the position of the element else print The element is not found. Algorithm for Deletion: Step 1: Step 2: Get the key for deletion. Check whether the List is Null. If it is Null then print The List is Null. Deletion is not possible and exit else go to the following steps. Step 2.1 : Check whether the element is found. If not found then print The element is not found and exit else go to the following steps. Step 2.1.1 : Assign the previous node to the found element as the temporary node. Step 2.1.2 : Make the next pointer to the temporary node to print to the succeeding node of the element to be deleted. Step 3: Exit.

3. Write a program for implementing Stack ADT using arrays.


AIM:
o

ALGORITHM :

A stack object is an ordered collection of zero or more elements of some type, such that elements can be added and removed only at one designated end called the top. If the all the elements are of the same type, it is a homogeneous stack, otherwise it is a heterogeneous stack. o Operations include initialization, push an item onto the stack, pop and item off the stack, check if the stack is empty or full, look at the element on the top of the stack. o Trying to pop an element off an empty stack is called underflow. o Whilst a stack is conceptually unbounded, in implementation the maximum number of elements on a stack may be fixed - thus eventually successive Pushs (without matching Pops) will cause the stack to overflow. o As the last item pushed onto a stack is the first item popped of the stack, stacks are also known as LIFOs (last in, first out). o Applications o Cars in a non-circular driveway Function calls Code formatting and expression syntax Representation and evaluation of arithmetic expressions Algorithm for Push: Step 1 : Get the key (value) to be added in the Stack. Step 2 : Check whether the Stack is full, if it is full then print Insertion is not Possible. Stack is full and exit else go to step 2.1 Step 2.1 Top is incremented. Step 2.2 The key is inserted at the top of the Stack [ key is data item ]. Step 3: Exit. Algorithm for Pop: Step 1: Check whether the Stack is empty. If it is empty then print The Stack is empty. The deletion is not possible and exit else go to the following step. Step 2: Delete or return the element on the top. Step 3: Top should be decremented and return the value.

Step 4: Exit. Algorithm for MakeEmpty: Step 1: Check whether the Stack is empty. If it is empty then print The Stack is empty and exit else go to the following step. Step 2: Top should be decremented and make the Stack as empty. Step 3: Exit.

AIM: 4.Write a program to implement Stack ADT using pointers. ALGORITHM: Stack (Linked List representation) Algorithm for Push: Step 1: Get the value of the element to be inserted. Step 2: Create a new node and assign the value of the element to the data part of the new node. Step 3: Assign Null to the pointer of the node. Step 4: Top node should point to the new node. Step 5: Make the new node as the top node. Step 6: Exit. Algorithm for Pop:

Step 1: Check whether the Stack is empty. If it is empty then print Stack is empty and exit else go to the following steps. Step 1.1 : Find previous of top. Step 1.2 : Assign NULL to the pointer of previous.

Step 1.3 : Free the top and make the previous as top. Step 2 : Exit

AIM: 5.Write a program for implementing Queue ADT using arrays. ALGORITHM: A queue object is an ordered collection of zero or more elements of some type, such that elements can be added only at one designated end called the rear, and removed only at one designated end called the front. Operations include initialization, enqueue an item onto the rear of the queue, dequeue an item from the front of the queue, check if the queue is empty or full, look at the element at the front of the queue. Trying to dequeue an item off an empty queue is called underflow. Whilst a queue is conceptually unbounded, in implementation the maximum number of elements on a queue may be fixed - thus eventually successive Enqueues (without matching Dequeues) will cause the stack to overflow. As the first item enqueued onto a queue is the first item dequeued from the queue, queues are also known as FIFOs (first in, first out). Applications Cars in a circular driveway Job scheduling in the operating system Algorithm for Enqueue: Step 1: Get the value of the key. Step 2: Check whether the queue is full. If it is full then print The Queue is full. The Insertion is not possible and exit else go to the following steps. Step 3: Increment rear. The key is entered at the rear. Step 4: Exit. Algorithm for Dequeue:

Step 1: Check whether the queue is empty. If it is empty then print The Queue is empty. The Deletion is not possible and exit else go to the following steps. Step 2: Delete the front element. Increment the front by one. Step 3: Exit. Algorithm for MakeEmpty: Step 1: Check whether the Queue is empty. If it is empty then print The Queue is empty and exit else go to the following step. Step 2: Delete or return the element on the front. Front should be incremented and make the Queue as empty. AIM: 6.Write a program to implement Queue ADT using pointers. ALGORITHM: Algorithm for Insertion: Step 1 : Get the value of the element to be inserted. Step 2 : Create the new node and assign the value of the element to the data part of the new node. Step 3 : Assign NULL to the pointer of the node. Step 4 : Rear node should point to the new node. Step 5 : Make the new node as the rear node. Algorithm for Deletion: Step 1: Check whether the queue is empty. If it is empty then print The Queue is empty. The Deletion is not possible and exit else go to the following steps.

Step 2: Make the front node to point to the next node of the very first node of the Queue. Step 3: Exit. AIM: 9. Write a program in C to perform heap sort. ALGORITHM: Step 1 : Start the process. Step 2 : Read the total no elements and the elements one by one. Step 3 : Until there is no element to be sorted do the following steps. Step 3.1 : Find minimum element from the array. Step 3.2 : Remove the element from the array. Move the element to the resultant array. Step 3.3 : Decrement the no of elements by one. Step 3.4 : Print the elements in the resultant array which is in order. Step 4: Stop the process. Second Method: Step 3.2 : Move the elements to the n th position of the same array. Step 3.3 : Increment n by 1. Step 3.4 : Print the elements of the array in the reverse order after arranging. PROGRAM: #include<stdio.h> #define leftchild(i)(2*(i)+1) void percdown(int a[],int i,int n)

{ int child,temp; for(temp=a[i];leftchild(i)<n;i=child) { child=leftchild(i); if(child!=n-1&&a[child+1]>a[child]) child++; if(temp<a[child]) a[i]=a[child]; else break; } a[i]=temp; } void heapsort(int a[],int n) { int i,f; for(i=n/2;i>=0;i--) percdown(a,i,n); for(i=n-1;i>0;i--) { f=a[0]; a[0]=a[i];

a[i]=f; percdown(a,0,i); } } main() { int i,n,a[20]; printf("enter the size of array:"); scanf("%d",&n); printf("enter the array elements"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } heapsort(a,n); printf("sorted elements are"); for(i=0;i<n;i++) printf("%d\n",a[i]); } AIM: 10.Write a program in C to perform quick sort. Quick sort,is based on the divide-and-conquer paradigm.It is a sorting algorithm with worst case running time O(n^2) on an input array of n numbers.In spite of this slow worst case running time,quicksort is often the best practical choice for sorting because it is remarkably efficient on the average : its expected running time is O(nlgn) and the constants hidden in the O-notation are

quite small.Here is the three-step divide-and-conquer process for sorting a typical array A[p . . . r] DIVIDE:The array >A is partitioned (rearranged) into two nonempty subarrays A[p . . q] and A[q+1 . . r].The index q is computed as a part of this partitioning procedure. CONQUER:The two subarrays A[p . . q] and A[q+1 . . r] are sorted by recursive calls to quicksort. COMBINE:Since the subarrays are sorted in place, no work is needed to combine them : the entire array A is now sorted. ALGORITHM: The steps are: Pick an element, called a pivot, from the list. 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 (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. 1 1 Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
1 1 1 1

AIM: 11.Write a program in C to construct binary search tree. . ALGORITHM: Step 1 : Get the new value to be inserted. Step 2 : Create a new node. Assign the value to the data part. Assign NULL to the pointer part. Step 3 : Check whether the Tree is NULL or not. If it is equal to NULL then make the new node as a single node tree. Else if Tree is not equal to NULL then do the following steps. Step 3.1 : If the new value is lesser than the node value then, if there exist left node of the current node then continue the process with left node. Else make the new node as the left node of current node.

Step 3.2 : If the new node is greater than the node value then, if there exist right node of the current node then, continue the process of the current node. Step 3.3 : Else make the new node as right node of the current node. Step 3.4 : If the new value is equal to the node value then, stop the process and there is no insertion because the value is already exist in the Tree.

También podría gustarte