Friday, August 9, 2013

What Is a Data Structure?


A data structure is a particular way of storing and organising data in computer so that it can be used efficiently. Data structures make it easy to manage huge amounts of data for instance databases and internet indexing services. Data structures are used in almost every program or software.




Advantage and Disadvantage of singly Linked list and Doubly Linked list

Que-    Advantage and Disadvantage of singly Linked list and Doubly Linked list


SINGLY LINKED LIST
*ADVANTAGE :-
1) Insertions and Deletions can be done easily.
2) It does not need movement of elements for insertion and deletion.
3) It space is not wasted as we can get space according to our requirements.
4) Its size is not fixed.
5) It can be extended or reduced according to requirements.
6) Elements may or may not be stored in consecutive memory available,even then we can store the data in computer.
7) It is less expensive.


*DISADVANTAGE :-
1) It requires more space as pointers  are also stored  with information.
2) Different amount of time is required to access each element.
3) If we have to go to a particular element then we have to go through all those elements that come before that element.
4) we can not traverse it from last & only from the beginning.
5) It is not easy to sort the elements stored in the linear linked list.



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

DOUBLY LINKED LIST

*ADVANTAGE :-

1) We can traverse in both direction i.e from starting to end & as well as from end to starting.
2) It is easy to reverse the linked list.
3) If we are at a node,the we can go at any node.But in linked list,it is not possible to reach the previous node.



*DISADVANTAGE :-

1) It requires more space per space per node because extra field is required for pointer to previous node.
20 Insertion and Deletion take more time than linear linked list because more pointer operations are required than linear linked list. 



Recursive algorithms

What is an recursive algorithm?

A recursive algorithm calls itself which usually passes the return value as a parameter to the algorithm again. This parameter is the input while the return value is the output.

What is an recursive algorithm?

Recursive algorithm is a method of simplification that divides the problem into sub-problems of the same nature. The result of one recursion is the input for the next recursion. The repletion is in the self-similar fashion. The algorithm calls itself with smaller input values and obtains the results by simply performing the operations on these smaller values. Generation of factorial, Fibonacci number series are the examples of recursive algorithms.







Que- write a program to construct B.S.T or it`s traversing algorithm in recursive way.

              #include<stdio.h>
              #include<conio.h>
              #include<process.h>
          
               struct BST*LC;

               int data;
               struct BST*RC;

               struct BST*start=NULL;
               void create( );
               void insert (int d);
               void inorder (struct BST*tmp);
               void preorder(struct BST*tmp);
               void postorder (struct BST*tmp);
               void main( )
               {
               int ch;
               while(1)
               {
               printf("press 1 for create\n");
               printf("press 2 for insert\n");
               printf("press 3 for inorder traversing\n");
               printf("press 4 for preorder traversing\n");
               printf("press 5 for postorder traversing\n");
               printf("press 6 for exit\n");
               printf("enter your choice\n")
               scanf("%d",&ch);
               switch(ch)
               {
               case 1:
               create();
               break;
               case 2:
               printf("enter data\n");
               scanf("%d",&d);
               insert(d);
               break;
               case 3:
              inorder(start);
              getch();
              break;
              case 4:
             preorder(start);
             getch();
             break;
             case 5:
            postorder(start);
            getch();
            break;



void creat()
{
struct BST*tmp;
if(start==NULL)
{
tmp=(struct BST*)malloc(sizeof(struct BST));
tmp-->LC=tmp-->RC=NULL;
printf("enter value\n");
scanf("%d",&tmp-->data);
start=tmp;
}
}


void insert(int d)
{
struct BST* tmp,*tmp1;
tmp=start;
while(1)
{
if(d==tmp-->data)
{
printf("duplicate not allow\n");
return;
}

if(d>tmp-->data)
{
if(tmp-->RC!= NULL)
{
tmp=tmp-->RC;
}
else
{
tmp1=(struct BST*)malloc(sizeof(struct BST));
tmp-->RC=tmp;
tmp-->RC=tmp1-->LC=NULL;
tmp-->data=d;
return;
}
else
{
if(tmp-->LC!=NULL)
{
tmp=tmp-->LC;
}
else
{
tmp1=(struct BST*)malloc(sizeof(struct BST));
tmp1-->LC=tmp1-->RC=NULL;
tmp1-->data=d;
tmp-->LC=tmp;
return;
}
}
}
}