Posts

Stack in Data Structure

Image
It is an ordered list of elements of data items in which insertion as well as deletion operations takes place at only one end called as top. Stack is also called as push down list or referred as LIFO (Last In First Out). We can implement stack by using static or dynamic allocation of memory. Stack Push-Pop Operations Operations on Stack void initialize(stack *s) int isEmpty(stack *s) int isFull(stack *s) int pop(stack *s) void push (stack *s, int data) void showStackItems (stack *s) void peekElement (stack *s, int index) 1. void initialize (stack * s) It initializes a stack as an empty stack. Initial value of top is set to -1. 2. int isEmpty (stack * s) Function checks whether the stack is empty. It returns 1 or 0 depending on whether the stack is empty or not. 3. int isFull (stack * s) The function checks whether the stack is full. A stack is said to be full when the associated array is completely filled up....

Circular Linked List

Image
I n a Circular Linked List the first and the last elements are adjacent. A linked list can be made circular by storing the address of the first node in the next field of the last node. In a circular linked list, last node is connected back to the first node. In some applications, it is convenient to use circular linked list. A queue data structure can be implemented using a circular linked list, with a single pointer "rear" as the front node can be accessed through the rear node. Implementation of Circular Linked List Circular Linked List Creation and Traversal Algorithm for traversing a circular list is slightly different than the same algorithm for a singly linked list because "NULL" is not encountered. Source Code (Circular Linked List Creation and Traversal) #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; void CircularLinkedListTraversal(stru...

Doubly Linked List

Image
As we know, Singly Linked List is unidirectional as we have the address of only next node. So it can travel only in forward direction. Instead, Doubly Linked List is bidirectional. It is an ordered list of nodes in which, each node contains the address of its next (successor) as well as previous (predecessor) node. Each node has three parts : previous : It is a pointer field contains address of its previous node. info or data : Contains actual value. next : Contains address of next node. Node declaration in Doubly Linked List struct Node { int info; // Data values struct Node *next; // Address of next Node struct Node *prev; // Address of previous Node }; Implementation of Doubly Linked List Doubly Linked List Creation and Traversal Source Code (Doubly Linked List Creation and Traversal) #include <stdio.h> #include <stdlib.h> struct Nod...

Linked List (Singly)

Image
Linked List  is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence. In its most basic form, each node contains: data, and address of next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration.  Linked List vs Arrays In arrays, elements are stored in contiguous memory locations. Also the size of the array is fix. So that if we need to extend the size of array, we have to copy the array to some bigger memory location. In case of inserting and deleting elements in array, we need to shift the elements to the left and right, respectively. 10 20 30 40 50 In Arrays , elements are stored in contig...

Sorting Algorithms in Data Structure

Sorting is a process of arranging information either in ascending or in descending order of key values. In data structures there are techniques available to sort the data. Types of sorting algorithms Bubble sort Insertion sort Selection sort Quick sort Radix sort Merge sort   1. Bubble Sort It is one of the simplest & popular sorting method. The basic idea of this sorting is that in each pass we have to compare a successive pair of element i.e. a[i] & a[i+1]. Interchange these two values, if they are not place in proper order. Place the element in it's correct position in each pass. In first pass largest element is shifted at bottom. In second pass second largest will be shifted to the second last position & so on. Thus to sort n values n-1 passes are required. Algorithm Bubble Sort Step 1: Start Step 2: Set 1 to pass Step 3: Set o to i Step 4: if a[i]> a[i+1] then, Swap a[i] & a[i+1] Step 5: Add I to i. Step 6: If i...

Searching Algorithms in Data Structure

Searching is a process of retriving or locating or finding information matching with some specific value. In data structure there are two types of searching. Linear or sequencial search Binary search   Linear or Sequencial Search It is a simplest form of searching and can be applied for sequencial storage such as array, stack, queue, linked list. Algorithm Linear/Sequencial Search Step 1: Start Step 2: Set 0 to i Step 3: Read and store value to search in val Step 4: if a(i) = val, print value is found at (i+1) position and go to step 8 Step 5: Set i to i + 1 Step 6: If i is less than n then, go to Step 4 Step 7: Print element not found Step 8: Stop n - size of array a - array val - value to be searched in array Source Code #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { int n; printf("Enter the size of array : "); scanf("%d", &n); int arr[n]; for...