Skip to main content

Operations on Data Structures, C programming

Basically there are six operations one can do on the data structures. They are Traversing, Searching, Sorting, Insertion, Deletion and Merging.

1.  Traversing: Basically to process a data structure if every element of data structure is visited once and only once, such type of operation is called as TRAVERSING. For example to display all the elements of an array, every element is visited once and only once, so it is called as traversing operation.
            Suppose we have an array of 20 students’ average makes, and if we need to calculate the average of group, we visit (read) each individual average and accumulate (sum) them. Then we will find the array are visited once and only once. Then it is traversing of an array.

2.  Insertion: When an element of the same type is added to an existing data structure, the operation we are doing is called as Insertion operation. The element can be added anywhere in the data structure in the data structure. When the element is added in the end it is called as special type addition, Appending. In case of adding an element to the data structure we may come across ‘Overflow’ If the size of the data structure is fixed and it is full, then if we try insertion operation on the data structure it is said to be overflow of data structure or the data structure is full.

           Suppose we have an array of size 20 used to store marks obtained by the students in Computer Science subject. If we have already added 15 students’ marks according to an ascending order, then we can add another student marks at appropriate place, by shifting the already stored element accordingly. Then it is called Insertion operation.

3.  Deletion: When an element is removed from the data structure, the operation we are doing is called as Deletion operation. We can delete an element  from data structure from any position. In case of deleting an element from the data structure we may come across ‘Underflow’. If no elements are stored in the data structure, then if we try deletion operation on the data structure it is said to be underflow of data structure or data structure is empty.

         Suppose we have an array of size 20 used to store marks obtained by the students in Computer Science subject. If we have already added 15 student s’ marks according to an ascending order, then we can delete one student’s marks from any place, by shifting the already  stored elements accordingly. Then it is called Deletion operation.

4.  Searching: When an element is checked for its presence in a data structure, that operation we are doing is called as ‘searching’ operation. The element that is to be searched is called as key element. The searching can be done using either ‘linear search’ or ‘binary search’.

        Suppose we have an array of size 10 and assume elements stored are 1,2,3,23,34,54,56,21,32,33 and assume we want to search the number 23. So here the number ‘23’ is key element. The key is searched through the array starting from the first element till end. When it is compared with the fourth element in it is present in the list (array) of numbers. So search is successful. This type of searching is called as Linear Search because the last element. In order to apply linear search, the list may not be in any particular order but when binary search is applied the list must be an ordered one.

5.  Sorting: When all the elements of array are arranged in either ascending or descending order, the operation used to do this process is called as Sorting. The Sorting can be done using Insertion, Selection or Bubble sort techniques.
           Suppose we have an array of size 10 and assume elements stored are 1,2,3,23,34,54,56,21,32,33. The elements of the array are not in proper order. If they are arranged in ascending order the list (array) becomes 1,2,3,21,23,32,34,54,56.

6.  Merging: When two lists List A and List B of size M and N respectively, of same type of elements, clubbed or joined to produce the third list, List C of size (M+N), and the operation done during the process is called as Merging.

           Suppose we have a list of size 6, containing the elements 1,2,3,4,71,87 and we have another list of size 5, containing the elements 9,13,21,65,67. Here both the lists are in ascending order. We can produce the third list of size 11 that will also be in ascending order, containing the element 1,2,3,4,9,13,21,65,67,71,87. The third list is called the merged list of first and second lists. In order to merge two lists into single list one needs to compare each element from both the lists and one of them will   go the merged list.

Comments

Post a Comment

Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …