Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 24 September 2015

Data Structure Programs




TO DOWNLOAD MORE DATA STRUCTURE PROGRAM'S CLICK HERE:-DATA_STRUCTURE_PROGRAMS



Data structure is a particular way of storing and organizing information  in a computer  so that it can be retrieved and used most productively. 

Data structures are important for the following reasons:
   1. Data structures are used in almost every program or software  system.
   2. Specific data structures are essential ingredients of many        
       



1. Stack Implementation(All operations)


#include<stdio.h>
#include<stdlib.h>
 
#define MAX_SIZE 5
 
int stack[MAX_SIZE];
void push();
int pop();
void traverse();
int is_empty();
int top_element();
int top = -1;

main()
{
   int element, choice;
   clrscr();
   do
   {
  clrscr();
      printf("Stack Operations.\n");
      printf(" 1. Insert into stack (Push operation).\n");
      printf(" 2. Delete from stack (Pop operation).\n");
      printf(" 3. Print top element of stack.\n");
      printf(" 4. Check if stack is empty.\n");
      printf(" 5. Traverse stack.\n");
      printf(" 6. Exit.\n");
      printf("Enter your choice : ");
      scanf("%d",&choice);

      switch ( choice )
      {
  case 1:
   clrscr();
     if ( top == MAX_SIZE - 1 )
    printf("Error:  Stack is Overflow\n\n");
     else
     {
        printf("Enter the value to insert : ");
        scanf("%d",&element);
        push(element);
     }
     getch();
     break;

  case 2:
        clrscr();
     if ( top == -1 )
        printf("Error: Stack is Underflow.\n\n");
     else
     {
        element = pop();
        printf("Element removed from stack is = %d ", element);
     }
        getch();
     break;

  case 3:
        clrscr();
     if(!is_empty())
     {
        element = top_element();
        printf("Element at the top of stack is %d\n\n", element);
     }
     else
        printf("Stack is empty.\n\n");
        getch();
     break;

  case 4:
  clrscr();
     if(is_empty())
        printf("Stack is empty.\n\n");
     else
        printf("Stack is not empty.\n\n");
        getch();
     break;

  case 5:
   clrscr();
     traverse();
     getch();
     break;

  case 6:
     exit(0);
      }
   } while(choice!=6);
}

void push(int value)
{
   top++;
   stack[top] = value;
}

int pop()
{
   int element;
 
   if ( top == -1 )
      return top;
 
   element = stack[top];
   top--;
 
   return element;
}   
 
void traverse()
{
   int d;
 
   if ( top == - 1 )
   {
      printf("Stack is empty.\n\n");
      return;
   }   
 
   printf("There are %d elements in stack.\n", top+1);
 
   for ( d = top ; d >= 0 ; d-- )
      printf("%d\n", stack[d]);
   printf("\n");
}  
 
int is_empty()
{
   if ( top == - 1 )
      return 1;
   else
      return 0;
}
 
int top_element()
{
   return stack[top];
}

2. Queue Implementation (All Operation)




#include<conio.h>
#include<stdio.h>
#define N 5

int queue[N]={0};
int rear=0,front=0;

void insert(void);
void del(void);
void disp(void);
void cre(void);

void main()
{
    int user=0;
    clrscr();
     do
      {
 clrscr();
 printf("\nTHE SIZE OF QUEUE IS =  %d",N);
 printf("\n\n  1. INSERT  A New Element");
 printf("\n  2. DELETE A First Element");
 printf("\n  3. DISPLAY A Queue List/Element");
 printf("\n  4. CREATE A New Queue Struture");
 printf("\n  5. EXIT\n\nEnter Your Choice : ");
 scanf("%d",&user);
 switch(user)
 {
     case 1:
     clrscr();
  insert();
  break;
     case 2:
     clrscr();
  del();
  getch();
  break;
     case 3:
        clrscr();
  disp();
  getch();
  break;
     case 4:
        clrscr();
  cre();
  getch();
  break;
     case 5:
        clrscr();
  printf("\n\t THANK U");
  getch();
  break;
 }
     } while(user!=5);
}



/*********************Functions********************/
void insert(void)
{
    int t;
    if(rear<N)
    {
 printf("\nENTER A VALUE IN QUEUE : ");
 scanf("%d",&t);
 queue[rear]=t;
 rear++;
    }
    else
    {
    clrscr();
 printf("\nQUEUE OVERFLOW!!!!!!!!!!!!!!!");
 getch();
    }
}
void del(void)
{
    int i;
    printf("\n %d gets deleted.........",queue[front]);
    queue[front]=0;
    front++;//pk
  }
}
void disp(void)
{
    int i;
    printf("Queue Elements :\n\n");
    for(i=front;i<rear;i++)
    {
 printf("\n\t %d",queue[i]);
    }
}
void cre(void)
{

    int t;
    printf("\nENTER A VALUE IN QUEUE :");
    scanf("%d",&t);
    front=0;
    queue[front]=t;
    rear=front+1;
}
3. Circlural Queue All Operation)


//... C Language program to implement basic operations on a Circular Queue

# include < stdio.h >
# include < conio.h >
# define max 10

void main()
{
 int cqueue[max], data;
 int front, rear, reply, option;

 clrscr();

 //... Initialise Circular Queue

 front = rear = 0;

 do
 {
  clrscr();
  printf("\n C Language program to implement basic operations on a Circular Queue \n");
  printf("\n 1. Insert number in a Circular Queue");
  printf("\n 2 .Delete number from Circular Queue");
  printf("\n 3. Exit \n");
  printf("\n Select proper option ( 1 / 2 / 3 ) : ");
  scanf("%d", &option);
  switch(option)
  {
   case 1 : // insert
          clrscr();
    printf("\n Enter number: ");
    scanf("%d", &data);
    reply = insq(cqueue, &front, &rear, &data);
    if(reply == -1)
     printf("\n Circular Queue is Full \n");
    else
     printf("\n Number %d is inserted in a Circular Queue \n", data);
    break;
   case 2 : // delete
          clrscr();
    reply = delq(cqueue, &front, &rear,&data);
    if(reply == -1)
     printf("\n Circular Queue is Empty \n");
    else
     printf("\n %d is deleted from Circular Queue \n", data);
     printf("\n");
     getch();
    break;
   case 3 : exit(0);
  } // switch
 }while(1);
} // main

int insq(int cqueue[max], int *front, int *rear, int *data)
{
 if((*rear + 1) % max == *front)
  return(-1);
 else
 {
  *rear = (*rear + 1) % max;
  cqueue[*rear] = *data;
  return(1);
 } // else
} // insq

int delq(int cqueue[max], int *front, int *rear, int *data)
{
 if(*front == *rear)
  return(-1);
 else
 {
  *front = (*front + 1) % max;
  *data = cqueue[*front];
  return(1);
 } // else
} // delq

4. Linear Link List  All Operation)




//... C Language program to implement basic operations on a Circular Queue

# include < stdio.h >
# include < conio.h >
# define max 10

void main()
{
 int cqueue[max], data;
 int front, rear, reply, option;

 clrscr();

 //... Initialise Circular Queue

 front = rear = 0;

 do
 {
  clrscr();
  printf("\n C Language program to implement basic operations on a Circular Queue \n");
  printf("\n 1. Insert number in a Circular Queue");
  printf("\n 2 .Delete number from Circular Queue");
  printf("\n 3. Exit \n");
  printf("\n Select proper option ( 1 / 2 / 3 ) : ");
  scanf("%d", &option);
  switch(option)
  {
   case 1 : // insert
          clrscr();
    printf("\n Enter number: ");
    scanf("%d", &data);
    reply = insq(cqueue, &front, &rear, &data);
    if(reply == -1)
     printf("\n Circular Queue is Full \n");
    else
     printf("\n Number %d is inserted in a Circular Queue \n", data);
    break;
   case 2 : // delete
          clrscr();
    reply = delq(cqueue, &front, &rear,&data);
    if(reply == -1)
     printf("\n Circular Queue is Empty \n");
    else
     printf("\n %d is deleted from Circular Queue \n", data);
     printf("\n");
     getch();
    break;
   case 3 : exit(0);
  } // switch
 }while(1);
} // main

int insq(int cqueue[max], int *front, int *rear, int *data)
{
 if((*rear + 1) % max == *front)
  return(-1);
 else
 {
  *rear = (*rear + 1) % max;
  cqueue[*rear] = *data;
  return(1);
 } // else
} // insq

int delq(int cqueue[max], int *front, int *rear, int *data)
{
 if(*front == *rear)
  return(-1);
 else
 {
  *front = (*front + 1) % max;
  *data = cqueue[*front];
  return(1);
 } // else
} // delq
5. Binary Search Tree All Operation)



//... C Language program to implement basic operations on a Circular Queue

# include < stdio.h >
# include < conio.h >
# define max 10

void main()
{
 int cqueue[max], data;
 int front, rear, reply, option;

 clrscr();

 //... Initialise Circular Queue

 front = rear = 0;

 do
 {
  clrscr();
  printf("\n C Language program to implement basic operations on a Circular Queue \n");
  printf("\n 1. Insert number in a Circular Queue");
  printf("\n 2 .Delete number from Circular Queue");
  printf("\n 3. Exit \n");
  printf("\n Select proper option ( 1 / 2 / 3 ) : ");
  scanf("%d", &option);
  switch(option)
  {
   case 1 : // insert
          clrscr();
    printf("\n Enter number: ");
    scanf("%d", &data);
    reply = insq(cqueue, &front, &rear, &data);
    if(reply == -1)
     printf("\n Circular Queue is Full \n");
    else
     printf("\n Number %d is inserted in a Circular Queue \n", data);
    break;
   case 2 : // delete
          clrscr();
    reply = delq(cqueue, &front, &rear,&data);
    if(reply == -1)
     printf("\n Circular Queue is Empty \n");
    else
     printf("\n %d is deleted from Circular Queue \n", data);
     printf("\n");
     getch();
    break;
   case 3 : exit(0);
  } // switch
 }while(1);
} // main

int insq(int cqueue[max], int *front, int *rear, int *data)
{
 if((*rear + 1) % max == *front)
  return(-1);
 else
 {
  *rear = (*rear + 1) % max;
  cqueue[*rear] = *data;
  return(1);
 } // else
} // insq

int delq(int cqueue[max], int *front, int *rear, int *data)
{
 if(*front == *rear)
  return(-1);
 else
 {
  *front = (*front + 1) % max;
  *data = cqueue[*front];
  return(1);
 } // else
} // delq
6. Add Two Polinormals



#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
       int coeff;
       int pow;
       struct link *next;
       };
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
 char ch;
 do
 {
//  clrscr();
  printf("\n Enter coeff : ");
  scanf("%d",&node->coeff);
  printf(" Enter power : ");
  scanf("%d",&node->pow);
  node->next=(struct link*)malloc(sizeof(struct link));
  node=node->next;
  node->next=NULL;
  printf("\n   Continue(y/n):");
  ch=getch();
 }
 while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
 while(node->next!=NULL)
 {
  printf("%dx^%d",node->coeff,node->pow);
  node=node->next;
  if(node->next!=NULL)
   printf(" + ");
 }
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
     while(poly1->next &&  poly2->next)
     {
      if(poly1->pow>poly2->pow)
      {
       poly->pow=poly1->pow;
       poly->coeff=poly1->coeff;
       poly1=poly1->next;
       }
      else if(poly1->pow<poly2->pow)
      {
       poly->pow=poly2->pow;
       poly->coeff=poly2->coeff;
       poly2=poly2->next;
       }
      else
      {
       poly->pow=poly1->pow;
       poly->coeff=poly1->coeff+poly2->coeff;
       poly1=poly1->next;
       poly2=poly2->next;
       }
      poly->next=(struct link *)malloc(sizeof(struct link));
      poly=poly->next;
      poly->next=NULL;
     }
     while(poly1->next || poly2->next)
     {
      if(poly1->next)
      {
       poly->pow=poly1->pow;
       poly->coeff=poly1->coeff;
       poly1=poly1->next;
       }
      if(poly2->next)
      {
       poly->pow=poly2->pow;
       poly->coeff=poly2->coeff;
       poly2=poly2->next;
       }
       poly->next=(struct link *)malloc(sizeof(struct link));
       poly=poly->next;
       poly->next=NULL;
       }
}
main()
{
      char ch;
      do{
      clrscr();
      poly1=(struct link *)malloc(sizeof(struct link));
      poly2=(struct link *)malloc(sizeof(struct link));
      poly=(struct link *)malloc(sizeof(struct link));
      printf("Enter 1st number : ");
      create(poly1);
      clrscr();
      printf("Enter 2nd number : ");
      create(poly2);
      clrscr();
      printf("1st Number : ");
      show(poly1);
      printf("\n2nd Number : ");
      show(poly2);
      polyadd(poly1,poly2,poly);
      printf("\n\n  Add of Two polynomial : ");
      show(poly);
      ch=getch();
      }
      while(ch=='y' || ch=='Y');
}
7. Reversed of Enter Nodes


#include<stdio.h>
#include<conio.h>
struct list
{
 int info;
 struct list *next;
};
typedef struct list list;
void getnode(int);
void traverse();
void remov();
void rev();
list *start=NULL;
void main()
{
 int ch,n;
 char ch1;
do
{
 clrscr();
 printf("1. Add  New Node\n2. Traverse of All Nodes \n3. Delete of Last Nodes\n4. Revser List ELemenet\n5. Exit\nEnter your choice: ");
 scanf("%d",&ch);
switch(ch)
{
 case 1:
  clrscr();
  printf("Enter  Value :");
  scanf("%d",&n);
  getnode(n);

  break;
   case 2:
  clrscr();
  printf("Show All Element of List\n\n");
  traverse();
  getch();
  break;
   case 3:
    clrscr();
  remov();
  getch();
  break;
      case 4:
      clrscr();
 printf("Your List is Reversed !!!!!!!!!\n\n");
  rev();
  break;
       case 5:
  exit(1);
  break;
 }
  printf("\nDo youy want to continue? press y or n");
  ch1=getche();
}while(ch1=='y'|| ch1=='Y');
}
void getnode(int n)
{
  list *temp,*ptr;
  ptr=(list*)malloc(sizeof(list));
  if(ptr==NULL)
    printf("No space available");
   else
   {
     ptr->info=n;
     ptr->next=NULL;
     if(start==NULL)
       start=ptr;
    else
    {
      temp=start;
      while(temp->next!=NULL)
       temp=temp->next;
       temp->next=ptr;
    }
  }
 }
 void traverse()
 {
  list *ptr;
  ptr=start;
  while(ptr!=NULL)
  {
    printf("%d ",ptr->info);
    ptr=ptr->next;
  }
 }
   void remov()
  {
   list *cur,*prev,*ptr;
   if(start==NULL)
   {
   printf("List is empty");
    return;
   }
   else
   {
//    ptr = cur;
    cur = start;
    while(cur->next!=NULL)
    {
      prev = cur;
      cur = cur->next;
  }
  if(prev == NULL)
    start = NULL;
   else
   {
    prev->next = cur->next;
   }
 }
    printf("Enter element is delete = %d ",cur->info);
    free(cur);
  }
     void rev()
  {
   list *pre,*forwd,*cur;
    pre=NULL;
    cur=start;
    forwd=cur->next;
    start->next=NULL;

    while(forwd!=NULL)
    {
    pre=cur;
    cur=forwd;
    forwd=forwd->next;
    cur->next=pre;
   }
   start=cur;

 }



All Sorting Of DS using 

1. Bubble Sorting




#include<stdio.h>
#include<conio.h>
#include<stdio.h>
void bubble_sort(int [],int);
void main()
{
    int a[5],n,i,ch;
    clrscr();

     printf("\nHow Many Elements To  Be Inserted : ");
     scanf("%d",&n);
     printf("\nEnter array elements : \n");
     for(i=0;i<n;i++)
      scanf("  %d",&a[i]);



     bubble_sort(a,n);
     getch();
}



//--------------------------------- Function
void bubble_sort(int a[],int n)
{
int i,j,k,temp;
   printf("\nUnsorted Data :  ");
    for(k=0;k<n;k++)
  printf("%5d",a[k]);
  printf("\n");
    for(i=1;i< n;i++)
    {
  for(j=0;j< n-1;j++)
  if(a[j]>a[j+1])
        {
        temp=a[j];
        a[j]=a[j+1];
        a[j+1]=temp;
        }
    printf("\nAfter pass  %d :  ",i);
 for(k=0;k< n;k++)
      printf("%5d",a[k]);
    }
}





2. Setection Sorting



//selection sorting

#include<stdio.h>
#include<conio.h>
void main()
{
   int array[100], n, c, d, position, swap;
   clrscr();
   printf("\n\nHow MAny Element to You Stored (Array Size) : ");
   scanf("%d", &n);

   printf("\nEnter %d integers Element :\n", n);
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
  if ( array[position] > array[d] )
     position = d;
      }
      if ( position != c )
      {
  swap = array[c];
  array[c] = array[position];
  array[position] = swap;
      }
   }

   printf("\nSorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);

  getch();
}
3. Quick Sorting


// quick sorting
#include<stdio.h>
#include<conio.h>
#define max 10
int a[max], n, i, l, h;
void main()
 {
  void input(void);
  input();
  getch();
  }

void input(void)
 {
  void quick_sort(int a[], int l , int h);
  void output(int a[], int n);
   clrscr();
   printf("\n\nHow MAny Element to You Stored (Array Size) : ");
   scanf("%d",&n);
   printf("\n");
   printf("Enter the element :  \n");
    for(i=0;i<=n-1;i++)
     {
       scanf("%d",&a[i]);
       }
      l = 0;
      h = n - 1;
      quick_sort(a, l, h);
       printf("\n\nSorted Array :\n");
       output(a, n);
  }

void quick_sort(int a[], int l, int h)
 {
  int temp , key, low, high;
  low = l;
  high = h;
   key = a[(low + high)/2];
    do
     {
      while(key > a[low])
       {
 low++;
 }
       while(key < a[high])
 {
  high--;
  }
 if(low <= high)
  {
   temp = a[low];
   a[low++] = a[high];
   a[high--] = temp;
   }
      }
    while(low <= high);
     if(l < high)
      quick_sort(a, l, high);
       if(low < h)
 quick_sort(a, low, h);
      }

void output(int a[],int n)
 {
  for(i=0;i<=n-1;i++)
   {
    printf("%d\n",a[i]);
    }
  }

4. Merge Sortin




// marge sorting

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

void merge_split(int a[],int first,int last);
void merge(int a[],int f1,int l1,int f2,int l2);
int a[25],b[25];

void main()
{
 int i,n;
 clrscr();
 printf("\n\nHow MAny Element to You Stored (Array Size) : ");
 scanf("%d",&n);
 printf("\nEnter the elements\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 merge_split(a,0,n-1);
 printf("\n\nSorted list : ");
 for(i=0;i<n;i++)
  printf("\n%d",a[i]);
 getch();
}

void merge_split(int a[],int first,int last)
{
 int mid;
 if(first<last)
 {
  mid=(first+last)/2;
  merge_split(a,first,mid);
  merge_split(a,mid+1,last);
  merge(a,first,mid,mid+1,last);
 }
}

void merge(int a[],int f1,int l1,int f2,int l2)
{
 int i,j,k=0;
 i=f1;
 j=f2;
 while(i<=l1 && j<=l2)
 {
  if(a[i]<a[j])
   b[k]=a[i++];
  else
   b[k]=a[j++];
  k++;
 }
 while(i<=l1)
  b[k++]=a[i++];
 while(j<=l2)
  b[k++]=a[j++];
 i=f1;
 j=0;
 while(i<=l2 && j<k)
  a[i++]=b[j++];
}

No comments:

Post a Comment