DATA STRUCTURE

DATA STRUCTURE:-

 
Introduction:-



?]...
.\[o\
(Q) What is  linear data Sturcture , where data are assigning into memory in a linear sequence manner. One data records are linked to another data records  that's why it is termeured as linked list.
                   
Singly link list is a dynamic memory allocation technique where, a single node inserted to another node with the help of address. Here first node contains the address of next node and show on..
Last node address parts contains the NULL value. Here NULL ‘/0’ value is indication to the end of node or a last node in a linear link list
Here 1st node connatins 20 value and its next part in address of next node that is 105 is conations.it means that this node connected to next node by using address.


Add caption





 

(Q)What is Data Structure? How Array is differ form Data Structure?

Ans:-

       Data structure is a collection of number of elements or nodes joined by a common logical structure. Data Structure is used to maintain or keep our data in structural formate.

       Array is also a structure as it has a no of elements joined by a common logical structure but is lacks certain main feature of data structure.

 

Difference:-

* The array can contain data of only one type. For ex:- When we declare an array int[10] , then array can have only one int type of data ,but DS has any type of data.

 

*The length of an array becomes fixed at the time of declaration itself which can’t be change after wards, but the length of data structure can be changed any moment at our will.

*Insertion and deletion is very tough process in te array, Which is very easy process in DS.








 (Q) Write an algorithem to insert a node/element into a linked list.

Ans:-

STEP (1) Start

(2)Store a value in new node and also store its next part in NULL

  (3) If the list is empty or a new element comes then new/fresh node is known as header/first node.

(4) If user want to insert node at first position then:-

               (a) Insert new node as header node and its next part in store address of first  node.

               (b) Set new node as header node.

(5) If user wants to insert node at last position then:-

       (a) Insert new node at the last position as:-

           Last node ->next part = new node.

(6) Else If user wants to insert node at any location then:-

        (a) visit on location node and its next part in insert new node address as:-

            Location node->next part= new node.

        (b)insert  new node next part in address of (location->next node).

(7) End. 

 (Q) Create a  linear link list and perform following operations in its:-

         (i) insert node at begin
         (ii)Insert node at end
         (iii) Insert node at any location according to user choice
         (iv) Display all node from link list.
         (v)Del node at begin.
         (vi) Del node at end
         (vii) Del node at loc

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct lll { int roll; struct lll *next;//self referncial structure }; struct lll *head,*fresh,*tmp,*ptr; int tn=0; void ins_at_beg(void);//prototyping of fun void display(void);//prototyping of function void ins_at_end(void);// prototyping of fun void ins_at_loc(void); void del_at_beg(void); void del_at_end(void); void del_at_loc(void); void length (void); main() { int ch ; do { printf("\n press 1 for insert at beg"); printf("\n 2 for insert at end"); printf("\n 3 for insert at location"); printf("\n 4 for display"); printf("\n 5 for del at beg"); printf("\n 6 for del at end"); printf("\n 7 for del at loc"); printf("\n 8 for find length of link list"); printf("\n 9 fornexit"); printf("\n enter ur choice"); scanf("%d",&ch); switch(ch) { case 1: ins_at_beg(); break;//calling of function case 2: ins_at_end();break; case 3: ins_at_loc();break; case 4: display();break; case 5: del_at_beg();break; case 6: del_at_end();break; case 7: del_at_loc();break; case 8: length(); break; default: printf("\n invalid entry re enter ur choice"); } }while(ch!=8); getch(); } void ins_at_beg() { fresh=(struct lll*)malloc (sizeof(struct lll));// dynamically memory allocate printf("\n enter roll no"); scanf("%d",& fresh->roll); fresh->next=NULL; if(head==NULL) { head=fresh; } else { fresh->next=head; head=fresh; } tn++; } void display(void)//definition of fun { if(head==NULL) { printf("\n link list is empty"); getch(); exit(0); } else { ptr=head; while(ptr!=NULL) { printf("\t %d",ptr->roll); ptr= ptr->next; } } getch(); } void ins_at_end() { fresh=(struct lll*)malloc (sizeof(struct lll));// dynamically memory allocate printf("\n enter roll no"); scanf("%d",& fresh->roll); fresh->next=NULL; if(head==NULL) { head=fresh; } else { ptr=head; while(ptr->next!=NULL) { ptr=ptr->next; } ptr->next=fresh; } tn++; } void del_at_end() { if(head==NULL) { printf("\n link list is empty del not possible"); getch(); exit; } else if(head->next==NULL)// only for a single node in link list. { ptr=head; head=NULL; free(ptr); } else { ptr=head; while(ptr->next!=NULL) { tmp=ptr; ptr=ptr->next; } tmp->next=NULL; free(ptr); } tn--; } void ins_at_loc() { int i=1,loc; printf("\n entr location , at which u want to insert"); scanf("%d",& loc); if(loc==1) { ins_at_beg(); } else { fresh=(struct lll*)malloc (sizeof(struct lll));// dynamically memory allocate printf("\n enter roll no"); scanf("%d",& fresh->roll); fresh->next=NULL; ptr=head; while(i<loc&&ptr->next!=NULL) { i++; tmp=ptr; ptr=ptr->next; } tmp->next=fresh; fresh->next=ptr; } tn++; } void del_at_beg() { if(head==NULL) { printf("\n link list is empty del not possible"); getch(); exit; } else { ptr=head; head=head->next; ptr->next=NULL;//ptr ka link head s khatm ho gya. free(ptr); } tn--; } void del_at_loc() { int i=1,loc; printf("\n enter location for deletion"); scanf("%d",&loc); if(loc==1) { del_at_beg(); } else if(loc==tn) { del_at_end(); } else if(loc<tn) { ptr=head; while(i<loc) {i++; tmp=ptr; ptr=ptr->next; } tmp->next=ptr->next; ptr->next=NULL; free(ptr); } } void length (void) { int len=0; if(head==NULL) { printf("\n linked list is emply,so length of lll is zero"); getch(); } else { for (ptr=head;ptr!=NULL;ptr=ptr->next); { len++; } printf("\n total no of node=%d",len); getch(); } }




(Q) Create a  Doubly link list and perform following operations in its:-

         (i) insert node at begin
         (ii)Insert node at end
         (iii) Insert node at any location according to user choice
         (iv) Display all node from link list.
         (v)Del node at begin.
         (vi) Del node at end
         (vii) Del node at loc
          (viii)Display all nodes in reverse order.







#include<stdio.h> #include<conio.h> #include<stdlib.h>

struct dll
{
struct dll *prev;
int roll;
struct dll *next; };
struct dll *head,*tail,*fresh,*tmp,*ptr;

int tn=0; void ins_at_beg(void); void ins_at_end(void);//prototyping of fun void ins_at_loc(void); void del_at_beg(void); void del_at_end(void); void del_at_loc(void); void display(void); void display_rev(void); main() { int ch ; do { printf("\n press 1 for insert at beg"); printf("\n press 2 for insert at end"); printf("\n press 3 for insert at loc"); printf("\n press 4 for del at begin"); printf("\n press 5 for del at end"); printf("\n press 6 for del at loc"); printf("\n press 7 for display"); printf("\n press 8 for display reverse order"); printf("\n 9 for next"); printf("\n enter ur choice"); scanf("%d",&ch); switch(ch) { case 1: ins_at_beg(); break;//calling of function case 2: ins_at_end();break; case 3: ins_at_loc();break; case 4: del_at_beg();break; case 5: del_at_end();break; case 6: del_at_loc();break; case 7:display();break; case 8:display_rev();break; default: printf("\n invalid entry re enter ur choice"); } }while(ch!=9); getch(); } void ins_at_beg() { fresh=(struct dll*)malloc (sizeof(struct dll));// dynamically memory allocate // ye line fresh me new memory allocate krega. printf("\n enter roll no"); scanf("%d",& fresh->roll);// y line fresh me roll/value input krega. fresh->next=NULL;// fresh ka next part me NULL value assign krega. fresh->prev=NULL;// ye line fresh ke previous part me NULL vlaue assign krega. if(head==NULL) { head=fresh; // agar link list me koi v value nhi hoga to fresh vala value head node bn jayga tail=fresh; // dry run } // fresh head tail ptr tmp 22 11 else // 11 11 11 { // 22 22 // 33 33 fresh->next=head; head->prev=fresh; head=fresh; } tn++; } void display(void)//definition of fun { if(head==NULL) { printf("\n link list is empty"); getch(); exit(0); } else { ptr=head; while(ptr!=NULL) { printf("\t %d",ptr->roll); ptr= ptr->next; } } getch(); } void display_rev(void)//definition of fun // 33 22 11 { if(head==NULL) //head ptr tmp tail { //33 33 11 printf("\n link list is empty"); // 22 getch(); // 11 exit(0); // NULL } else { ptr=tail; while(ptr!=NULL) { printf("\t %d",ptr->roll); ptr= ptr->prev; } } getch(); } void ins_at_end() { fresh=(struct dll*)malloc (sizeof(struct dll));// dynamically memory allocate // ye line fresh me new memory allocate krega. printf("\n enter roll no"); scanf("%d",& fresh->roll);// y line fresh me roll/value input krega. fresh->next=NULL;// fresh ka next part me NULL value assign krega. fresh->prev=NULL;// ye line fresh ke previous part me NULL vlaue assign krega. if(head==NULL) { head=fresh; // agar link list me koi v value nhi hoga to fresh vala value head node bn jayga tail=fresh; // dry run } // fresh head tail ptr tmp 22 11 else // 11 11 11 { // 22 22 ptr=head; while(ptr->next!=NULL) { // fresh tail fresh ptr=ptr->next; // 10 20 300 40 50 } tail->next=fresh; fresh->prev=tail; tail=fresh; } tn++; } void ins_at_loc() { int i=1,loc; printf("enter a location where u wants to insert a node"); scanf("%d",&loc); if (loc==1) ins_at_beg(); else if (loc==tn) ins_at_end(); else { ptr=head; fresh=(struct dll*)malloc(sizeof(struct dll)); printf("\n enter roll"); scanf("%d",&fresh->roll); fresh->next=NULL; fresh->prev=NULL; while (i<loc) { tmp=ptr; ptr=ptr->next; i++; } tmp->next=fresh; fresh->prev=tmp; fresh->next=ptr; ptr->prev=fresh; } tn++; } void del_at_beg(void) { if (head==NULL) { printf("\n link list is empty del not possible"); getch(); } else if(head->next==NULL) { ptr=head; head=tail=NULL; printf("\n deleted node=%d",ptr->roll); free(ptr); getch(); } else { tmp=head; head=head->next; tmp->next=NULL; head->prev=NULL; free(tmp); } tn--; } void del_at_end(void) { if(head==NULL) { printf("\n del not possible , link list is empty"); getch(); } /* else if(head->next=NULL) { ptr=head; head=NULL; free(ptr); } */ else { ptr=head; while(ptr->next!=NULL) { tmp=ptr; ptr=ptr->next; } tmp->next=NULL; ptr->prev=NULL; printf("\n deleted node =%d",ptr->roll); free(ptr); } tn--; } void del_at_loc(void) { int i=1,loc; if(head==NULL) { printf("link list is empty, del not possible"); getch(); exit; } else { printf("enter location where u want for delete"); scanf("%d",&loc); if(loc==1) { del_at_beg(); } else if(loc==tn) { del_at_end(); } else { ptr=head; while(i<loc) { i++; tmp=ptr; ptr=ptr->next; } tmp->next=ptr->next; ptr->next->prev=tmp; ptr->next=NULL; printf("deleted node %d",ptr->roll); free(ptr); tn--; } } }//


(Q) prg for circular link list

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct cll { int roll; struct cll *next;//self referncial structure }; struct cll *head,*fresh,*tmp,*ptr; int tn=0; void ins_at_beg(void);//prototyping of fun void display(void); void ins_at_end(void);
void ins_at_loc(void);
void del_at_beg(void);
void del_at_end(void);
void del_at_loc(void);
void search(void); main() { int ch ; do { printf("\n press 1 for insert at beg"); printf("\n press 2 for insert at end");
printf("\n press 3 for insert at end");
printf("\n press 4 for del at beg");
printf("\n press 5 for del at end");
printf("\n press 6 for del at loc"); printf("\n press 7 for display");
printf("\n press 8 for search");
printf("\n press 9 for exit"); printf("\n enter ur choice"); scanf("%d",&ch); switch(ch) { case 1: ins_at_beg(); break;//calling of function case 2: ins_at_end();break;
case 3: ins_at_loc();break;
case 4: del_at_beg();break;
case 5: del_at_end();break;
case 6: del_at_loc();break; case 7: display();break;
case 8: search();break; default: printf("\n invalid entry re enter ur choice"); } }while(ch!=9); getch(); } void ins_at_beg() { fresh=(struct cll*)malloc (sizeof(struct cll));// dynamically memory allocate printf("\n enter roll no"); scanf("%d",& fresh->roll); fresh->next=NULL; if(head==NULL) { head=fresh; fresh->next=head; } else { ptr=head; while(ptr->next!=head) { ptr=ptr->next; } ptr->next=fresh; fresh->next=head; head=fresh; } tn++; } void display() { if(head==NULL) { printf("\n link list is empty"); getch(); exit; } else { ptr=head; do { printf("\n %d,"ptr->roll); ptr=ptr->next; }while(ptr!=head); } getch(); } void ins_at_end() { fresh=(struct cll*)malloc (sizeof (struct cll)); printf("enter data"); scanf("%d",&fresh->roll); fresh->next=NULL; if(head==NULL) { head=fresh; fresh->next=head; } else { ptr=head; while(ptr->next!=head) { ptr=ptr->next; } } }


void ins_at_loc() { int i,loc; printf("\n enter location where u want to insert"); scanf("%d",& loc); if(loc==1) { ins_at_beg(); } else if(loc==tn) { ins_at_end(); } else { ptr=head; fresh=(struct cll*)malloc(sizeof (struct cll)); printf("enter data"); scanf("%d",&fresh->roll); fresh->next=NULL; while(i<loc) { i++; tmp=ptr; ptr=ptr->next; } tmp->next=fresh; fresh->next=ptr; } tn++; } Dry run for insert at loc()



void del_at_beg()
{
if(head==NULL)
{
printf("\n del not possible list is empty");
getch();
exit;
}
else
{
         ptr=head;
          if(ptr->next==head)
          {
             head=NULL;
               free(ptr);
          }
else
{
   while(ptr->next!=head)
   {
    ptr=ptr->next;
   
   }
   
   tmp=head;
   
   head=head->next;
   tmp->next=NULL;
   ptr->next=head;
   
   printf("\n deleted node =%d",tmp->roll);
   free(tmp);
}
}
   tn--;
}


void del_at_end()
{
if(head==NULL)
{
printf("\n del not possible , link list is empty");
getch();
}
else
{
ptr=head;
if(ptr->next==head)
{
head=NULL;
free(ptr);
}
else
{
while(ptr->next!=head)
{
tmp=ptr;
ptr=ptr->next;
}
tmp->next=head;
ptr->next=NULL;
printf("\n deleted node=%d",ptr->roll);
free(ptr);
    }
}
tn--;
}




void del_at_loc()
{
int i=1,loc;
printf("enter location wheree u want to insert");
scanf("%d",&loc);
 
if(loc==1)
{
del_at_beg();
}
  else
  if(loc==tn)
  {
  del_at_end();
 
  }
else
{
ptr=head;
while(i<loc)
{
i++;
tmp=ptr;
ptr=ptr->next;
}
tmp->next=ptr->next;
ptr->next=NULL;
printf("delete node%d", ptr->roll);
free(ptr);
tn--;
}
tn--;
}


void search (void)
{
int n,f=0;
if(head==NULL)
{
printf("\n searching is impossible becauase there is no node");
getch();exit;
}
else
{
printf("enter no which u want to search");
scanf("%d",&n);
ptr=head;
do
{
if(ptr->roll==n)
{
f=1;break;
}
else
{
f=0;
ptr=ptr->next;
}
}while(ptr!=head);
}
         if(f==1)
         {
          printf("data is found");
}
   else
   {
    printf("data is not found");
   }
   
   getch();
}






DOUBLY CIRCULAR LINK LIST:-





















































#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct dcll
{
struct dcll *prev;
 int data;
 
 struct  dcll *next;//self referncial structure
 
};
 struct dcll *head,*tail,*fresh,*tmp,*ptr;
 int tn=0;
                                                                     
 void ins_at_beg(void);//prototyping  of fun
 void ins_at_end(void);  
 void display(void);
  main()
 {
 
  int ch ;
         
  do
  {
  printf("\n press 1 for  insert at beg");
  printf("\n press 2 for insert at end");
  printf("\n press 7 for display");
   printf("\n press 10 for exit");
                                                        
  printf("\n enter ur choice");
  scanf("%d",&ch);                                                                         
                                                                                               
  switch(ch)
  {
  case 1: ins_at_beg(); break;//c
  case 2: ins_at_end();break;
  case 7: display();break;
  default:
  printf("\n invalid entry re enter ur choice");
   
  }
  }while(ch!=10);
   getch();
}
 
 void ins_at_beg()
 {  
  fresh=(struct dcll*)malloc (sizeof(struct dcll));// dynamically memory allocate 
  printf("\n enter data no");
  scanf("%d",& fresh->data);
  fresh->next=NULL;
  fresh->prev=NULL;
 
  if(head==NULL)
  {             
  fresh->next=fresh->prev=fresh;
  head=tail=fresh;
 }
 else
 {
  fresh->prev=tail;
  fresh->next=head;
  head->prev=fresh;
  tail->next=fresh;
  head=fresh;
 }
 tn++;
 }
 
 void display()
 {
  if (head==NULL)
  {
  printf("\n Doubly circular link list is empty ");
  getch();
  exit;
}
else
{
ptr=head;
do
{
printf("\t %d",ptr->data);
ptr=ptr->next;
}while(ptr!=head);
}
getch();
 }
 
 void ins_at_end()
 {
  fresh=(struct dcll*)malloc (sizeof(struct dcll));// dynamically memory allocate 
  printf("\n enter data no");
  scanf("%d",& fresh->data);
  fresh->next=NULL;
  fresh->prev=NULL;
 
  if(head==NULL)
  {             
  fresh->next=fresh->prev=fresh;
  head=tail=fresh;
 }
  else
    {
     fresh->prev=tail;
     tail->next=fresh;
    fresh->next=head;
    head->prev=fresh;
    tail=fresh;
 
     }
   tn++;
 
 }


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct dcll
{
struct dcll *prev;
 int data;
 
 struct  dcll *next;//self referncial structure
 
};
 struct dcll *head,*tail,*fresh,*tmp,*ptr;
 int tn=0;
                                                                     
 void ins_at_beg(void);//prototyping  of fun
 void ins_at_end(void);  
 void ins_at_loc(void);
 void del_at_beg(void);
 void del_at_end(void);
 void del_at_loc(void);
 void display(void);
  main()
 {
 
  int ch ;
         
  do
  {
  printf("\n press 1 for  insert at beg");
  printf("\n press 2 for insert at end");
  printf("\n press 3 for location ");
  printf("\n press 4 for del at begin");
  printf("\n press 5 for del at end");
  printf("\n press 6 for del at loc");
  printf("\n press 7 for display");
 
   printf("\n press 10 for exit");
                                                        
  printf("\n enter ur choice");
  scanf("%d",&ch);                                                                         
                                                                                               
  switch(ch)
  {
  case 1: ins_at_beg(); break;//c
  case 2: ins_at_end();break;
  case 3: ins_at_loc();break;
  case 4: del_at_beg();break;
  case 5: del_at_end();break;
  case 6: del_at_loc();break;
  case 7: display();break;
  default:
  printf("\n invalid entry re enter ur choice");
   
  }
  }while(ch!=10);
   getch();
}
 
 void ins_at_beg()
 {  
  fresh=(struct dcll*)malloc (sizeof(struct dcll));// dynamically memory allocate 
  printf("\n enter data no");
  scanf("%d",& fresh->data);
  fresh->next=NULL;
  fresh->prev=NULL;
 
  if(head==NULL)
  {             
  fresh->next=fresh->prev=fresh;
  head=tail=fresh;
 }
 else
 {
  fresh->prev=tail;
  fresh->next=head;
  head->prev=fresh;
  tail->next=fresh;
  head=fresh;
 }
 tn++;
 }
 
 void display()
 {
  if (head==NULL)
  {
  printf("\n Doubly circular link list is empty ");
  getch();
  exit;
}
else
{
ptr=head;
do
{
printf("\t %d",ptr->data);
ptr=ptr->next;
}while(ptr!=head);
}
getch();
 }
 
 void ins_at_end()
 {
  fresh=(struct dcll*)malloc (sizeof(struct dcll));// dynamically memory allocate 
  printf("\n enter data no");
  scanf("%d",& fresh->data);
  fresh->next=NULL;
  fresh->prev=NULL;
 
  if(head==NULL)
  {             
  fresh->next=fresh->prev=fresh;
  head=tail=fresh;
 }
  else
    {
     fresh->prev=tail;
     tail->next=fresh;
    fresh->next=head;
    head->prev=fresh;
    tail=fresh;
 
     }
   tn++;
 
 }
 
 void ins_at_loc(void)
{
int i=1,loc;
printf("enter ur location where u want to insert");
scanf("%d",&loc);
if(loc==1)
{
ins_at_beg();
}
else
if(loc==tn)
{ins_at_end();
}
else
{
fresh=(struct dcll*)malloc(sizeof (struct dcll));
printf("enter data");
scanf("%d",&fresh->data);
fresh->next=fresh->prev=NULL;
ptr=head;
while(i<loc&&ptr->next!=head)
{
i++;tmp=ptr;ptr=ptr->next;
}
fresh->next=ptr;
ptr->prev=fresh;
fresh->prev=tmp;
tmp->next=fresh;
}
tn++;
}


void del_at_beg()
{
if(head==NULL)
{
printf("list is empty del not possible");
getch();exit;
}
else
{
  ptr=head;
  if(ptr->next==head)
  {
  head=tail=NULL;
  free(ptr);
  }
  else
  {
  (head->next)->prev=tail;
  tail->next=head->next;
  head=head->next;
  ptr->next=NULL;
  ptr->prev=NULL;
  free(ptr);
  }
}
tn--;
}

void del_at_end(void)
{
if(head==NULL)
{
printf("list is empty del not possible");
getch();exit;
}
else
{
  ptr=tail;
  if(ptr->prev==head)
  {
  head=tail=NULL;
  free(ptr);
  }
  else
  {
 
  (tail->prev)->next=head;
  head->prev=tail->prev;
  tail=tail->prev;
  ptr->prev=NULL;
  ptr->next=NULL;
  free(ptr);
  }
  tn--;
  
   }
 }
 
 


void del_at_loc(void)
{
int i=1,loc;
if(head==NULL)
{
printf("link list is empty, del not possible");
getch();
exit;
}
else
{
printf("enter location where u want for delete");
scanf("%d",&loc);
if(loc==1)
{
del_at_beg();
}
else
if(loc==tn)
{
del_at_end();
}
else
{
ptr=head;
while(i<loc)
{
i++;
tmp=ptr;
ptr=ptr->next;
}
tmp->next=ptr->next;
(ptr->next)->prev=tmp;
ptr->next=NULL;
ptr->prev=NULL;
printf("deleted node %d",ptr->data);
free(ptr);
tn--;
}
}
}

(Q) What is Stack? Explain briefly. What are the various types of operations performed on it?

Stack is a linear data structure , which works on the concept of First in last out or Last In First out. The insertion and deletion take position only at one end we can say that Top. Stack can be maintained using array and linklist.

               In the case of Static array, When Stack is fully empty mean to say there are no any there are no any element in stack, its top position is below of first index i.e. -1. The value of -1 is also known as Sentinel  value of Stack.

Operation on Stack:-

A Stack is a linear data structure in which items may be inserted or removed only at one end called the top of the stack.

  A Stack may be seen in our daily life for ex:-

                         The no. of plates in a cafeteria, where every new plates added to the top of stack. Similarly every new plate takes off the stack is also from the top of the stack.

*Advantage of Stack:-

   * Help in Information Hiding:-

           In stack data stored in hidden form , so if some one had already written the functions for handling the stack , then we could use them without need to know the details of how stacks are kept in memory or how the stack operation are performed.

 

 Stack is also called a “Push down list”.

Operations on Stack:-

Push:- When an item is added to a stack , the operation is called push.

POP:- when an item is removed from the stack then we say that we  POP it from stack.

 

*IN stack Operations will be performed  after checking its Status:-

(1) Stack Empty:- When the Stack exists and it has been initialized , then this return TRUE if stack is empty, and FALSE otherwise.

(2) Stack FULL:- When the stack exists and it has been initialized,  then this return true if the stack is full, and false otherwise.

 

·         Stack relate terms:-

The method of writing all operators either before their operands, or after them , is called polish notation.

·         When operators are written before there there operands, it is called the prefix form.

·         When operators are written after there operands , it is called the postfix form/Reverse polish form/ suffix form.

 

For ex:-

The expression  a*b  becomes *ab in prefix form  and ab* in postfix form.



(Q) stack by using link list.





#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


struct stk
{
int data;
struct stk *next;
};
struct stk *top,*fresh;

//top=NULL;

void push(void);//insertion

void pop(void);
void display(void);

main()
{
int ch;
do
{
//clrscr();
printf("\n 1 for push");
      printf("\n 2 for pop");
      printf("\n 3 for display");
printf ("\n 5 for exit");
 
printf("\n enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 5:exit;
default:
printf("\n invalid choice ");
}
 
 
}while(ch!=5);
getch();
}



void push(void)
{
fresh=(struct stk*)malloc(sizeof(struct stk));
printf("\n enter data");
scanf("%d",&fresh->data);
fresh->next=NULL;
if(top==NULL)
{
top=fresh;
}
else
{
fresh->next=top;
top=fresh;
}
}


void display()
{
if(top==NULL)
{
printf("\n stack is empty");
getch();
exit;
}
else
{
// fresh=top;
for( fresh=top;fresh!=NULL;fresh=fresh->next)
{
printf("\n %d",fresh->data);
}
}
getch();
}




void pop()
{
  if(top==NULL)
  {
  printf("\n stack is empty so pop/del not possible");
    getch();
    exit;
  }
  else
  {
  fresh=top;
  top=top->next;
  fresh->next=NULL;
  free(fresh);
 
  }
}








int stk[MAX],top=-1,r;
//prototyping of fun

void push (void);
void display(void);
void peek(void);
void pop (void);
int isempty(void);
int isfull(void);
void update(void);


 main()
{
int ch;
do
{
printf("\n \t 1 for Push/insert");
printf("\n \t 2 for display");
printf("\n \t 3 for pop");
printf("\n \t 4 for peek");
printf("\n \t 5 for isempty ");
printf("\n \t 6 for isfull");
printf("\n \t 7 for update");
printf("\n 8 for exit");
printf("\n enter ur choice");
scanf("%d",& ch);
switch(ch)
{
case 1: push();break;
case 2: display();break;
case 3:pop();break;
case 4:peek();break;
case 5:r=isempty();
           if(r==1)
            printf("\n stack is empty");
        else
             printf("\n stack is full");
             
        break;
        
  case 6:r=isfull();
  
          if(r==1)
            printf("\n stack is Full");
        else
             printf("\n stack is not full");
             
        break;
        
  case 7:update();break;
     case 8: exit;
default:
printf("\n invalid choice");
}
}while(ch!=8);
getch();
}


void push(void)//insertion
{
if(top==MAX-1)
{
printf("\n  stack is full operation not possible, stack over flow");
getch();
exit;
}
else
{
printf("\n enter no in stack");
top++;
scanf("%d",&stk[top]);
}
}



int isempty(void)
{
if(top<MAX-1)
return(1);
else
    return(0);
}
   
   
   
   int isfull(void)
   {
      if(top==MAX-1)
         return(1);
      else
          return(0);
          
   }



void display(void)
{
int i;
if(top==-1)
{
printf("\n stack is empty");
getch();
exit;
}
else
{
for(i=top;i>=0;i--)
  printf("\n %d",stk[i]);
}
}


void pop(void)
{
if(top==-1)
{
printf("\n list is empty, del not possible");
getch();
}
else
{
// printf("\n poped elements=%d",stk[top]);
// top--;
printf("\n poped elemts=%d",stk[top--]);
}
}



void peek(void)
{
if(top==-1)
{
printf("\n stack is empty");
getch();
exit;
}
else
{
printf("\n peeked element=%d",stk[top]);
getch();
}
}



void update(void)
{
int i;
if(top==-1)
{
printf("stack is empty");
printf("%d",stk[top]);
}
else
{
printf("\n enter element to update%d",stk[top]);
scanf("%d",&stk[top]);
}
}



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>





(Q) Prg for Circular Queue


struct cq
{
int data;
struct cq *next;
};
struct cq *head,*tail,*ptr,*tmp,*fresh;

void ins_in_cq(void);//prototyping of fun
void del_in_cq(void);
void search(void);
void display(void);
main()
{
int ch;
head=tail=NULL;

do
{
//clrscr();
//printf("\n \t circular queue Menu");
printf("\n 1. insertiion");
printf("\n \t 2:deletion");
printf("\n \t 3:searching");
printf("\n \t 4:display");
printf("\n 5 exit");


printf("\n enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:ins_in_cq();break;
case 2:del_in_cq();break;
case 3:search();break;
case 4:display();break;
case 5:exit;
default:("\n invalid entry re_enter ur choice");
}
}while(ch!=5);
getch();
}

void ins_in_cq()
{
fresh=(struct cq*)malloc(sizeof(struct cq));//Dyamaic memory allocation 
printf("enter data");
scanf("%d",&fresh->data);
fresh->next=NULL;
if(tail==NULL)
{
head=tail=fresh;
fresh->next=fresh;
}
else
{
fresh->next=head;
tail->next=fresh;
tail=fresh;
}
}

void del_in_cq(void)
{
if(head==NULL)
{
printf("\n list is empty");
getch();
}
else
if(head==tail)
{
ptr=head;
head=tail=NULL;
printf("\n deleted node value%d",ptr->data);
free(ptr);
}
else
{
ptr=head;
head=head->next;
tail->next=head;
ptr->next=NULL;
printf("\n deleted node=%d",ptr->data);
free(ptr);
}
}   

void display(void)
{
if(head==NULL)
{
printf("\n queue is empty");
getch();
return;
}
else
{
ptr=head;
do
{
   printf("\n %4d",ptr->data);
   ptr=ptr->next;
}while(ptr!=head);
}
}



void search(void)
{
int s,f=0;
if(head==NULL)
{
printf("\n Circular q is empty");
getch();
return;
}
printf("\n entered data for searching");
scanf("%d",&s);
ptr=head;
do
{
if(ptr->data==s)
{
f=1; break;
}
ptr=ptr->next;
}while(ptr!=head);
if(f==1)
   printf("\n data is found");
if(f==0)
    printf("\n data is not found");
    

}



(Q)Write  an algorithms to create a BST Tree.

Ans:-

1.  START

2. Select  first node as a root node in given Question.

3. Select another new node and compare with Root node. If selected node is greater than from Root/subroot then arrange it Right side from Root node.

4. Else arrange its left side From root/subroot.

5. Repeat step 3 to 4 until all nodes are not selected from Queue. Else

6. END. 




//Queue by using linkl list


#include<stdio.h>

#include<conio.h>

#include<stdlib.h>


struct que

{

int data;

struct que *next;

};


struct que *fresh,*front,*rear,*ptr;


//Prototyping of fun.


void enq(void);




void deq(void);

void display(void);

main()

{

int ch;

front=NULL;

rear=NULL;

do

{

// clrscr();

 

  printf ("\n 1. Insertion");

  printf("\n 2 for deletion");

  printf("\n 3 for display");

  printf("\n 8 for exit");

  printf("\n enter ur choice");

  scanf("%d",&ch);

  

  switch(ch)

  {

  case 1: enq();break;

  case 2: deq();break;

  case 3: display();break;

  case 8: exit;

  default:

  printf("\n invalid entry, Re_enter ur choice");

  getch();

  }

}while(ch!=8);

}




void enq(void)

{

fresh=(struct que*)malloc(sizeof(struct que));

printf("\n enter a no");

scanf("%d",&fresh->data);

fresh->next=NULL;

if(front==NULL)

{

front=fresh;

rear=fresh;

//front=rear=fresh;

}

else

{

rear->next=fresh;

rear=fresh;

}

}




void display()

{

if (front==NULL)

{

printf("\n Queue is empty");

getch();

exit;

}

else

{

ptr=front;

while(ptr!=NULL)

{

printf("\t %d",ptr->data);

ptr=ptr->next;

}

getch();

}

}



void deq(void)

{

if (front==NULL)

{

printf("\n queue is empty, deletion not possible");

getch();

exit;

}

else

{

ptr=front;

front=front->next;

ptr->next=NULL;

free(ptr);

}

}



    Strictly Binary Tree:-   A Strictly binary tree is a binary tree in which each node  either contain no child or both children.                       

                               


 

 

 

 

Complete Binary Tree:- A Complete binary tree is  a binary tree in which all leaf nodes are placed at the same level . It is also  known as full binary tree. ‘OR’  A strictly binary tree is called a complete binary tree if all leaf node are placed at same level .

                      




          Note:-  A complete binary tree can be also an almost complete binary tree.

 

 

 

(*) Almost  complete Binary Tree:-

                         An almost  complete binary tree is a binary tree that holds following condition/criteria.

·         All the leaf node are placed at bottom level or bottom(bottom-1) level.

·         All the leafs are in left most possible position. 


 






 Heap Tree ->

A heap tree is a complete binary tree or almost complete binary tree  in which each node has either greater value to its child node or less value to its child node.

        There are basically two types of heap  tree.

(i)              Max heap (Descending heap)

(ii)           Min heap

(i)Max heap:- Max heap is a heap tree in which each node has greater value to its child node it is also called descending heap.

 


(II)Min Heap:-

                   Min heap is a hap tree in which each node has less value to its child  node .It is also called ascending heap.

 

 


 



(Q)Construct a heap tree by using following elements.

                 25,35,18,9,46,70,48
















HEAP Sort:-

  This sorting technique is implemented on heap tree technique .It organize the element of heap tree in particular order. In heap sort last element is exchanged / Interchange element with root node and the element (Which is known as root element) is adjusted at proper position by comparing to its children.





 










































 

(@)Construction  of Binary tree with given  preorder  and In-order :-

 

Preorder :- A      B             D             E              F              C             G             H             J                                                                     L              K.

Inorder:-     D      B             F              E              A             G             C             L              J                                             H             K.

 

To construct a binary on the basis of given Preorder and In-order , Follow following steps:-

(1) Pick 1st node from preorder as root node.

(2) Check inorder in A where it  substitute.

Arrange node as leaf node which are from Left side of Root(A) and rest 

arrange in Right side.







(@)Construction  of Binary tree with given  postorder  and In-order :-

 

Postorder :- A    B             D             E              F              C             G             H             J              L              K.

Inorder:-     D      B             F              E              A             G             C             L              J              H             K.

 

To construct a binary on the basis of given Preorder and In-order , Follow following steps:-

(1) Pick last node from postorder as root node.

(2) Check inorder in A where it  substitute.

(3)    Arrange node as leaf node which are from Left side of Root(A) and rest arrange in Right side.














(1)     Binary tree:-

A binary tree is a tree that has at most two children. It is special type of tree in which each node can contain no child, one child  or both children / two children.

*Representation of Binary Tree

·         Array representation.

   ·         Link list represention

Array representation:- 

In this representation there single dimension array are used, first array holds the element of nodes of binary tree. The 2nd arrary holds the index number of left child of each node and third array holds the index no of right child of each node.

 











(*) Extended Binary Tree:-

                                An extended binary tree is generated/ divided from a binary tree by adding some  additional nodes to binary tree is represented as square while original nodes of binary tree is represented as circle.

    The original nodes are called internal nodes  and now added nodes are called external nodes the extended binary tree is also known as 2-Tree.

 

 

NOTE:-

(i)New node sit ached as a child of leaf node or as a child of internal node / Non leaf node  that contains one child.

(ii) Extended Binary tree having N nodes has N+1 external node..

                     

                      

 







There are different kinds of graph.

(i)                  Directed graph:- A directed graph is a graph in which edge be in unidirectional form . In directed graph ways are defined and it move from one vertices to another vertices .Here direction is defined.

 

It means we can visit from v1 from v2 but not vice versa.



 

 

 

 

 

 

 

(II)Undirected graph:- In undirected graph there are no any specific path direction is defined with the edge. Here edges be in bidirectional form.

 


 

 

(iii)Weighted Graph:-A graph is said to be weighted , if every edge in the graph is assigned a data or weight . weight are must be positive and should be greater than zero.

 

 

 

 

 

 

 

 

(III) Unweighted  Graph:- A graph  which has no any weight. This type of graph is known as unweighted graph. Weight are must be positive and should be greater than zero.


                

 

(Iv) Unweighted  Graph:- A graph  which has no any weight. This type of graph is known as unweighted graph. Weight are must be positive and should be greater than zero.

  


 

Traversal scheme:-

In a graph it is a problem how to traverse means say how to visit each vertex, exactly once in a graph. There are two ways to traversal a graph.

BFS:-

(1)This is a way to traverse a graph. In this graph traversing technique we use QUEUE  for all the node of the graph.First we take any node as a starting node then we take all the adjacent node of that starting node . Similarly approach we take for all other adjacent  nodes which are adjacent to the starting node and so on. we have to maintain the visiting status of node in array/queue so that any node can’t traverse again . BFS can be used for directed graph and undirected graph. We shall describe it in relation to undirected graph also. To perform the BFS we should follow the following steps.

 

Step 1.  Select any vertex V in graph and make is the source vertex and make it visited.

step 2: Visit all unvisited vertices which adjacent/concatenate to  visited vertex V

step 3: Now  visit one of the adjacent unvisited  vertexes.

step 4: Repeat step 1 to 3 until all vertices of the graph become visited.

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SPANNING Tree:-

             A Spanning  tree of a graph is an undirected tree consisting of only those edge which are necessary to connect the all vertices(nodes) in the original graph.

                A spanning tree has a properties that for any pair of vertices there exist  only one edge or path between  them, insertion of any edge in a spanning tree from an unique cycle.

            A Tree T is said to be a spanning tree of a connected graph G . If T is a sub graph of G and T contains all vertices of G.

       EX:-

 

 

 

 

                                       Spanning tree of fig. is given below.        

 

 

                                        v1                                                      v2

 

v6                                               E7                                                      E8                                                 v3

                                                                                                                                                           E4

                            E2                         v5                                              v4

                                                                                    E3

                                                   Spanning Tree of upper Fig.

In this example spanning tree is created .An edge of a graph G i.e.  not in a given  in spanning tree T is called CHORED . In the above example branch are E2, E3, E4, E7 , E8 and  CHORED are :- E1 , E6 , E5.

 

 

Minimum Spanning Tree:-

                           Minimum Spanning tree of a weighted graph ,such that the sum of weight of an edge should be minimum spanning tree with the smallest weight in a weighted graph is called a sorted spanning tree or sorted distance spanning tree or Minimal spanning tree. The minimum spanning tree result in a tree that contain only branches from the edge in the original graph. The edge for the minimum spanning tree are choose in such a way that:-

(1)    Every node in a graph must be included in the spanning tree.

(2)     The overall edge weight of spanning tree is the minimum.

 

There are several  way  to find  the minimum spanning tree . Some of these are:-

 

(i)                  Kruskal’s  Algorithem

(ii)                Prim’s    Algorithem

 

(1)    Kruskal’s Algorithem:-

        Suppose  that G is a connected weighted graph of N vertices , we can construct an optimal tree in G by selecting the edge’s for a Tree as follows:-

Step 1. Arrange the edge of G in increasing order of there weight.

Step  2. Then from each successive step select (from all remaining edges of G) another smallest edge that make no cycle with previously selected edge.

Step 3. Stop when n-1 edges have been selected otherwise go to step 2.

 


 

                        v6

                                                                         v7                                         v   v3

 

 

                                                                                                            v4

                                                                                                                                      

 

 

step 4 . select minimum weighted edge from remain graph. so after select vertex v2 to v3 with weight 16.after select this as:-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Traversal scheme:-

In a graph it is a problem how to traverse means say how to visit each vertex, exactly once in a graph. There are two ways to traversal a graph.

BFS:-

(1)This is a way to traverse a graph. In this graph traversing technique we use QUEUE  for all the node of the graph.First we take any node as a starting node then we take all the adjacent node of that starting node . Similarly approach we take for all other adjacent  nodes which are adjacent to the starting node and so on. we have to maintain the visiting status of node in array/queue so that any node can’t traverse again . BFS can be used for directed graph and undirected graph. We shall describe it in relation to undirected graph also. To perform the BFS we should follow the following steps.

 

Step 1.  Select any vertex V in graph and make is the source vertex and make it visited.

step 2: Visit all unvisited vertices which adjacent/concatenate to  visited vertex V

step 3: Now  visit one of the adjacent unvisited  vertexes.

step 4: Repeat step 1 to 3 until all vertices of the graph become visited.

 

 

 

 

 

 

 

 

 

                                              

 

 

                           

 

Now in Queue after delete node 1 ,  node 2 be a fornt node.Now in Queue front node is 2 so we explore node 2 . Its adjacent nodes are node 4 and 5 so these nodes(node 4 and node 5) are inserted at Rear position of Queue as:-

 

Now node 2 is deleted from queue so after delete node 3 be a front node as:-

 

Now node 3 is deleted from queue because it’s a front node, so its adjacent nodes are node 6 and node 7 so after explore node 3  node 6 and 7 is inserted at rear end and node 3 (front node) is deleted from queue so node 4 be a front node as:-

Now node 4 is front node and its adjacent nodes 2 are already visited/maintained in queue, so we only delete node 4 from Front end and after delete node 4 node 5 be a front node. as:-

Now node 5 be a front node and its adjacent node is 2 which are already visited in queue so we only delete node 5 form front end and after delete node 5 node 6 be a front node. as:-

Now node 6 be a fornt node and its adjacent node is 3 which are already visited in queue so we only delete node 6 from front end after delete node 6 node 7 be a fornt node as:-

Now node 7 be a front node and its adjacent node is 3 which are already visited in queue so we only delete node 7 from front end after delete node nothing remain in queue as:-

Now all nodes are deleted from queue . Here Front/Rear located at 8th position which are not declared in queue

so Stop.

 




Comments

Post a Comment

Popular posts from this blog

Microsoft Visual Foxpro

Visual Basic 6.0