Posts

Showing posts from June, 2021

Main Data Structures

Image
  These are the Main Data Structures Arrays Stacks Queues Linked Lists Trees Graphs Hash Tables 1. Array   An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays. Here’s an image of a simple array of size 4, containing elements (1, 2, 3 and 4). Each data element is assigned a positive numerical value called the Index , which corresponds to the position of that item in the array. The majority of languages define the starting index of the array as 0. The following are the two types of arrays: One-dimensional arrays (as shown above) Multi-dimensional arrays (arrays within arrays)   Basic Operations Following are the basic operations supported by an array. Traverse − print all the array elements one by one. Insertion − Adds an element at the given index. Deletion − Deletes an element at the given index. Search − Searches an element using the given index or by the value. Update − Updates an e...

Program to print the elements of an array in reverse order

Image
 In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on. Above array in reversed order: Algorithm STEP 1: START STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5} STEP 3: PRINT "Original Array:" STEP 4: REPEAT STEP 5 for(i=0; i<arr.length ; i++) STEP 5: PRINT arr[i] STEP 6: PRINT "Array in reverse order" STEP 7: REPEAT STEP 8 for(i= arr.length-1; i>=0; i--) STEP 8: PRINT a[i] STEP 9: END Program: public   class  ReverseArray {        public   static   void  main(String[] args) {              //Initialize array               int  [] arr =  new   int  [] { 1 ,  2 ,  3 ,  4 ,  5 };      ...