Deploying the best E-Resources for Software Engineering Students

We at IT Engg Portal, provide all the Computer and IT Engineering students of Pune University with well compiled, easy to learn notes and other E-resources based on the curriculum

Power Point Presentations and Video Lectures for Download

We provide the most recommended power point presentations and Video Lectures from the most prominent Universities for most of the difficult subjects to ease your learning process

Bundling Codes for your Lab Practicals

Deploying the best of available E-Resources for Tech Preparation (Campus Placements)

The Complete Placement Guide

Our Team has worked hard to compile this E-Book for all students heading for Campus Placements. The book is a complete solution for Technical Preparation for Campus Placements.

Pune University's most viewed website for Computer and IT Engineering

With more than 4,00,0000 pageviews from 114 countries over the globe, we are now the most viewed website for Ebooks and other E- Resources in Computer and IT Engineering

Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Friday, October 7, 2011

Stack Operations - Explanation with Animation


Stack Operations


   The two operations that form the majority of the functionality of the stack are the operation of adding to the stack, and the operation of removing items from the stack.
to View a Simple Animation on Stack & Stack Operation

   For historical reasons the operation of adding items to a stack is called push. For similar reasons the operation of removing an item from a stack is usually referred to as pop.
There are a couple more operations that we must consider before we attempt to implement a stack. The first of these is an operation called init. This initialises the stack, and ensures that state of the software is in a known condition before we try to use it. Just about every structure that we will ever use will require an initialisation routine - get into the habit of writing the outline of the init operation first!

    As this is a course in developing software, we must also consider the possibility of errors. The easiest error to envisage for a stack is when a program tries to remove an item from an empty stack. This condition is called underflow, and if undetected will probably cause a series of mysterious errors before the program crashes. The method that we use to detect this problem is to use an operation called isEmpty to tell us if the stack is empty. If we try and take data from an empty stack the error condition is usually called underflow. Similarly there is a possibility that we might run out of space. To counter this we can provide an operation called isFull.

  Using this analysis we can conclude that we need to provide five operations to implement a stack. These operations are:
  1. init
  2. isEmpty
  3. isFull
  4. push
  5. pop
  If we can satisfactorily implement these operations we have implemented a stack. Note that we do not have to say at this stage exactly how these operations are implemented, or exactly how the data is held. There is more than one way of implementing a stack. In this Unit we are going to consider how to implement a stack using an array.

Array Implementation


The first decision that must be made is how big an array to declare to hold the data items in the stack. It is usually prudent to use a constant as the size of the stack. In C a declaration along the lines of:



#define STACKSIZE 10



will declare a stack size of 10 elements (from 0 to 9). If we need to change the size of the stack later we can just change this value and recompile. In addition to the data being held in the stack, we will need to hold data about the stack, in particular the number of entries being held in the stack. We need to hold the data about the stack in the same place as the data in the stack, so this probably means that we need to use a C struct. Trying to use meaningful variable names we have a declaration of:


struct stack{
   int top;
   int items[STACKSIZE];
};

now that we have the declaration of the structure we can make our function prototypes:

void init(struct stack *);
int isEmpty(struct stack);
int isFull(struct stack);
void push(struct stack *, int item);
int pop(struct stack *);

 
Only now do we have to start thinking about exactly how to implement the operations! The method used in this course is to add items at the location indicated by the index top, and then increment top. The first operation then is to write the init function. All this has to do is to set top to zero (the number of items in a newly created stack):

void init(struct stack * a_stack)
{
   a_stack->top=0;
}
Note the use of the "*" and "->" notation to allow us to change the values in the stack rather than copies of the values.

IsEmpty and IsFull
The next function to define is the isEmpty function.
int isEmpty(struct stack a_stack)
{
  if ( a_stack.top <= 0 )
    return TRUE;
  else
    return FALSE;
}
This function returns either true or false, depending on the value of top. If top is less than zero this means that an uncaught underflow has occurred. In theory this should not happen, but it costs us nothing extra to defend against this problem. We could just use the test for equality ( == ) at this point, but badly behaved code could cause a problem if the value of top skips past zero. Similarly, the isFull code looks like this:
int isFull(struct stack a_stack)
{
  if (a_stack.top < STACKSIZE )
    return FALSE;
  else
    return TRUE;
}
The only point to note in this code is the use of the constant STACKSIZE defined earlier. This is the bulk of the supporting code that we need to provide for our implementation of a stack.

Push and Pop

Stack adter init

It would now be appropriate to have a closer look at the push and pop operations.
This depicts the stack after the init operation has been executed. If we are programming in C then the contents of each of the elements of the stack will be filled with arbitrary bit patterns left over from the last time that the memory was used. Other programming languages may be kinder.

Push

The first thing that needs to be performed is a push operation. The diagram shows the changes to be made if the value 10 is pushed onto the stack:
After the push operation, the array holds the pushed value in the location that used to be indexed by top, and top has now moved on to point to the next slot in the array, ready for the next push. The next step is to perform a second push. The second value to be pushed in this example is 20:
Before

















After


Pop
Starting from the previous position, we will now show the process of popping an item from the stack. The stack pointer top is currently pointing to location 2. Aspop is the reverse operation from push, the activities should be carried out in reverse order. This means that top will be decremented, then the item being selected for the pop operation will be returned to the calling program. After the pop operation the stack will look as shown:
Before

                        


After

Notice that the value 20 remains in the array, as we have not actually changed the value. The next time we call the push function the new value will be placed in the location, overwriting the current contents


Wednesday, October 5, 2011

Linked List - Explanation with Animation

     Link-List is a data structure in computer science terms. Its more of an advanced array or rather a dynamic array.Link-list is a grouping of data or records in the form of a chain.Each member of the chain is also known as a "node". Node consists of two fields , one contains the actual record/data and the second field consists of a link to the next member in the chain.




    Link-lists are among the most common data-structures and widely exploited in commercial and personal applications.The only problem with link-lists is , it doesn't allow random access to the data in its entire chain.This means if we want to search for something in the link-list we need to traverse from the first node.Obviously last node will no link or rather a null value. (I call it "earthing" in electrical terms )


    The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant number of operations.


A linked list whose nodes contain two fields: an integer value and a link to the next node


Did you know ? 
Functional languages such as Lisp and Scheme have the data structure built in.In fact, in these languages almost everything is a link-list.


More information on link-lists :
*Each item or member of a linked list is called as node or element.
*Each node contains the address or link to the next node and is known as next pointer or next link.
*The remaining fields are known as the data, value, cargo, payload or information fields.
*Head is the first node and tail is the last node (or vice-verse).
*Address to the head node gives access to the entire list.


Types of Link-Lists:
*Linear or Singly- (as shown in the image above)


*Circular


*Doubly-


Animation for Linked List Operations

 How Link-Lists work :


Adding a node to LinkList at begining :


Adding a node to LinkList after any node or in middle :



More about Linked-List and its usage scenarios :


*Lists are very useful for holding data that is always accessed 'in order' rather than needing to look for a particular item.


*Lists are very useful for holding many items where you need to add or remove items during the course of the program.


*Lists are not best for random access (choose arrays or vectors) or access by key (choose a dictionary or map).









Sunday, September 25, 2011

How Arrays Work? (Explanation with Animation)

Array  is a datatype available in the C language (and other languages also) to store similar type of data elements in contiguous memory locations. The word 'contiguous' is of precise importance here, as it brings the biggest advantage of arrays into picture - 'Faster access to data elements'  

Arrays in C store  datatypes work under a single variable with an index, also known as a subscript. An array is simply a list of variables of the same type.  As such, arrays often help a programmer organize collections of data efficiently and intuitively.
Its the most common data-structure used to store data. Arrays are basically easy to use and traverse. Arrays are popular due to many of its advantages (mentioned below) over other available  data structures in C  Language. Indexing is 0-based and the elements can be accessed in a constant time by using the index of the particular element a as the subscript.

Advantages of using arrays:

    * Easier to use and access
    * Faster access to the elements

Disadvantages of using arrays:

    * Fixed size - the size of the array is static (Can be made dymanic)
    * Complex position-based insertion - if you want to insert an element at a position already covered by some other element then you have to shift right by one position all the elements to the right of that position. This will vacate the position for you to insert the new element at the desired position. The more elements you have to the right of the desired position, the more expensive the process will be.


Example :

int a[5] = {1,2,3,4,5};
a[0] is nothing but value at the first position in the array , which is "1".
also if int i =0, then a[i] = a[0] = 1
Another important aspect is , a[i] is same as i[a].

char name[] = {'h' , 'e' , 'l' , 'l' , 'o' , '\0'}; here a null value '\0' is needed to mark the endof the array as its size is not specified. It can also be declared as char name[] = "hello";

So an array of char is nothing but a string/word and an array of strings is nothing but a sentence.
char string[] = "Who am I ?";

Animations on how arrays work with different functions like delete , insert , reverse etc.

Delete Function :


Insert Function :


Reverse Function :



----------------------------------------------------------------

Friday, August 12, 2011

Data Structures [COMP,IT] - [Sem 3, Sem 4]

Data Structures is  initially introduced in the 3rd sem of Engineering curriculum of Pune University. The subject is then continued in the 4th semester with deeper outlook. This subject has immense importance for every Computer Science and IT Engineering students, as it is most basic subject of any Software stream. All companies target on Data Structures in their tests to in the campus placements. Data Structure covers important topics like the :


  • Stacks
  • Queues
  • Graph
  • Tree
  • Linked List
  • Sorting and Searching Algorithms
      Covering the above topics will help completing a  majority syllabus of Data Structures. There are different forms of  study material available over the internet which can make learning Data Structures easier and more interesting. We have provided a bit of selected and better videos and slides from the internet which will make your task easier to learn and visualize Data structures.



Download Ebooks for Data Structures and Algorithms:

Data Structures through C - Yashwant Kanetkar


Fundamental of Data Structures - Horowitz, Sahani

Download Now
Download Now(CHM File)


Handbook of Data Structures and Appliacations
Dinesh Mehta & Sartaj Sahni





Data Structures and Algorithms in C++
Adam Drozdek




Download Now


Algorithms and Data Structures in C++
 Alan Parker




Download Now
-------------------------------------------------------------------------------------------------------


 Download slides for important topics under Data  Structures : 

-----------------------------------------------------------------------------------
Video Lectures :



Introduction to Data Structures



Introduction to trees



Introduction to Stack Data Structure


Stack Data Structure



Push and Pop Operation ( Animation )

Introduction to Queue Data Structures




Queue Data Structure





Circular Queue



Priority Queues


Introduction to Tree Data Structures



Trees


Introduction to Trees 


Graphs



===========================================