




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
You will find in this document over a hundred pages of data structures with c language
Typology: Study notes
1 / 116
This page cannot be seen from the preview
Don't miss anything!
CONTENTS Lecture- 01 Introduction to Data structure Lecture- 02 Search Operation Lecture- 03 Sparse Matrix and its representations Lecture- 04 Stack Lecture- 05 Stack Applications Lecture- 06 Queue Lecture- 07 Linked List Lecture- 08 Polynomial List Lecture- 09 Doubly Linked List Lecture- 10 Circular Linked List Lecture- 11 Memory Allocation Lecture- 12 Infix to Postfix Conversion Lecture- 13 Binary Tree Lecture- 14 Special Forms of Binary Trees Lecture- 15 Tree Traversal Lecture- 16 AVL Trees Lecture- 17 B+-tree Lecture- 18 Binary Search Tree (BST) Lecture- 19 Graphs Terminology Lecture- 20 Depth First Search Lecture- 21 Breadth First Search Lecture- 22 Graph representation Lecture- 23 Topological Sorting Lecture- 24 Bubble Sort Lecture- 25 Insertion Sort Lecture- 26 Selection Sort Lecture- 27 Merge Sort Lecture- 28 Quick sort Lecture- 29 Heap Sort Lecture- 30 Radix Sort Lecture- 31 Binary Search Lecture- 32 Hashing Lecture- 33 Hashing Functions
Module- 1 Lecture- 01 Introduction to Data structures In computer terms, a data structure is a Specific way to store and organize data in a computer's memory so that these data can be used efficiently later. Data may be arranged in many different ways such as the logical or mathematical model for a particular organization of data is termed as a data structure. The variety of a particular data model depends on the two factors - Firstly, it must be loaded enough in structure to reflect the actual relationships of the data with the real world object. Secondly, the formation should be simple enough so that anyone can efficiently process the data each time it is necessary. Categories of Data Structure: The data structure can be sub divided into major types: Linear Data Structure Non-linear Data Structure Linear Data Structure: A data structure is said to be linear if its elements combine to form any specific order. There are basically two techniques of representing such linear structure within memory. First way is to provide the linear relationships among all the elements represented by means of linear memory location. These linear structures are termed as arrays. The second technique is to provide the linear relationship among all the elements represented by using the concept of pointers or links. These linear structures are termed as linked lists. The common examples of linear data structure are: Arrays Queues Stacks Linked lists Non linear Data Structure: This structure is mostly used for representing data that contains a hierarchical relationship among various elements. Examples of Non Linear Data Structures are listed below: Graphs family of trees and table of contents Tree: In this case, data often contain a hierarchical relationship among various elements. The data structure that reflects this relationship is termed as rooted tree graph or a tree.
char 0 int 0 float 0. double 0.0f void wchar_t 0 Insertion Operation Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array. Here, we see a practical implementation of insertion operation, where we add data at the end of the array − Algorithm Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth^ position of LA −
n = n + 1 ; while( j >= k) { LA[j+ 1 ] = LA[j]; j = j - 1 ; } LA[k] = item; printf("The array elements after insertion :\n"); for(i = 0 ; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } } When we compile and execute the above program, it produces the following result − Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after insertion : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 10 LA[4] = 7 LA[5] = 8 Deletion Operation Deletion refers to removing an existing element from the array and re-organizing all elements of an array. Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth^ position of LA.
Lecture- 02 Search Operation You can perform a search for an array element based on its value or its index. Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to find an element with a value of ITEM using sequential search.
Found element 5 at position 3 Update Operation Update operation refers to updating an existing element from the array at a given index. Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to update an element available at the Kth^ position of LA.
Lecture- 03 Sparse Matrix and its representations A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the matrix have 0 value , then it is called a sparse matrix. Why to use Sparse Matrix instead of simple matrix? Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements. Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements.. Example: 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value). Sparse Matrix Representations can be done in many ways following are two common representations:
int k = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 5; j++) if (sparseMatrix[i][j] != 0) { compactMatrix[0][k] = i; compactMatrix[1][k] = j; compactMatrix[2][k] = sparseMatrix[i][j]; k++; } for (int i=0; i<3; i++) { for (int j=0; j<size; j++) printf("%d ", compactMatrix[i][j]); printf("\n"); } return 0; }
pop() − Removing (accessing) an element from the stack. When data is PUSHed onto stack. To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks − peek() − get the top data element of the stack, without removing it. isFull() − check if stack is full. isEmpty() − check if stack is empty. At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always represents the top of the stack, hence named top. The top pointer provides top value of the stack without actually removing it. First we should learn about procedures to support stack functions − peek() Algorithm of peek() function − begin procedure peek return stack[top] end procedure Implementation of peek() function in C programming language − Example int peek() { return stack[top]; } isfull() Algorithm of isfull() function − begin procedure isfull if top equals to MAXSIZE return true else return false endif end procedure Implementation of isfull() function in C programming language − Example bool isfull() { if(top == MAXSIZE) return true; else return false; }
isempty() Algorithm of isempty() function − begin procedure isempty if top less than 1 return true else return false endif end procedure Implementation of isempty() function in C programming language is slightly different. We initialize top at - 1, as the index in array starts from 0. So we check if the top is below zero or - 1 to determine if the stack is empty. Here's the code − Example bool isempty() { if(top == - 1 ) return true; else return false; } Push Operation The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps − Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Adds data element to the stack location, where top is pointing. Step 5 − Returns success. If the linked list is used to implement the stack, then in step 3, we need to allocate space dynamically. Algorithm for PUSH Operation A simple algorithm for Push operation can be derived as follows − begin procedure push: stack, data if stack is full
A simple algorithm for Pop operation can be derived as follows − begin procedure pop: stack if stack is empty return null endif data ← stack[top] top ← top - 1 return data end procedure Implementation of this algorithm in C, is as follows − Example int pop(int data) { if(!isempty()) { data = stack[top]; top = top - 1 ; return data; } else { printf("Could not retrieve data, Stack is empty.\n"); } }
Lecture- 05 Stack Applications Three applications of stacks are presented here. These examples are central to many activities that a computer must do and deserve time spent with them.