Monday, October 28, 2013

Design a Queue using Cirucular array

 //Queue Data Structure
typedef struct {
    int front,rear;
    int size;
    int *data;
}Que;

Que * CreateQue (int size)
{
    Que * temp = (Que *) malloc (sizeof (Que));
    temp->front = -1;
    temp->rear = -1;
    temp->size =size;
    temp->data = (int *) malloc (size * (sizeof(int)));
    return temp;
}

 int isFull (Que *q)
{
    return ( (q->rear+1)%q->size == q->front);
}

int isEmpty (Que *q)
{
    return (q->front == -1);
}

void enQue(Que *q , int data)
{
 if (isFull(q)){
        printf("\nQueue is Full\n");
 }
 else{
    q->rear = ((q->rear+1)%q->size);
    q->data[q->rear] = data;
    if(q->front==-1)
        q->front=q->rear;
    }
}

int deQue (Que *q)
{
    int data;
 if (isEmpty(q)){
     printf("\nQueue is empty\n");
     return -1;
 }
 data = q->data[q->front];
 if(q->front == q->rear){
     q->front = -1 ;
     q->rear = -1;
 }
 else
 q->front = (q->front+1)%q->size;
  return data;
}

void DeleteQue (Que *q)
{
  if (q){
      if(q->data)
          free(q->data);
      free(q);
      }
}

Thursday, October 24, 2013

Write a method to reverse a Stack inplace using push and pop operations only


We need two methods

one to recursively pop all stack elements. if you push them back in the same func . you willl get same stack as stack is a LIFO.

so to reverse you need another function to take the popped elements in reverse order and push them in the same order.

Below code does the same
void insertAtBtm(struct Stack *stk,int data)
{
    int temp;
    if (isEmpty(stk)) {
        push(stk,data);
        return ;
    }
    temp = pop(stk);
    insertAtBtm(stk,data);
    push(stk,temp);
}


void revStack (struct Stack *stk)
{
    int data;
    if (isEmpty(stk))
        return;
    data = pop(stk);
    revStack(stk);
    insertAtBtm(stk,data);
 }

Saturday, October 19, 2013

Design a Stack using araay and Dynamic array

#include <stdio.h>
#include <malloc.h>

struct mystack {
    int* data;
    int top;
    int size;
};


struct mystack* createStack (void )
{
    struct mystack *temp = (struct mystack *) 
                            malloc(sizeof(struct mystack));
    if (temp == NULL)
        return NULL ;
    temp->top =-1;
    temp->size = 1;
    temp->data = (int *) malloc (sizeof(int) * temp->size);
    return temp;
}

int isEmpty(struct mystack *stk)
{
    return(stk->top == -1);
}

int isFull(struct mystack *stk)
{
    return(stk->top == (stk->size)-1);
}

void expand (struct mystack *temp)
{
  printf("expanding ..curent size %d\n",temp->size);
  temp->size*=2;
  temp->data = (int *) realloc(temp->data,temp->size
                                * sizeof(int));
}

void disp(struct mystack* stk)
{
    int i =0;
    printf("\n***Total elements*** are %d \n"
                                     ,stk->top+1);
    for(i=0;i<=stk->top;i++)
        printf("%d   ",stk->data[i]);
    printf("\n************ \n");
}

void push (struct mystack *stk, int data)
{
    if (isFull(stk))
    {
        printf("Stack full\n");
        expand(stk);
    }
  stk->top++;
  printf("pushing %d in position %d\n",data,(stk)->top);
  (stk)->data[(stk)->top]=data;
}

int pop (struct mystack *stk)
{
    if (isEmpty(stk))
    {
        printf("empty stack\n");
        return -1;
    }
    int temp;
    temp = (stk)->data[(stk)->top];
  printf("popping %d from position %d\n",
                         temp,(stk)->top);
  (stk)->top--;
  return temp;
}

void deleteStack (struct mystack *stk)
{
    if (stk)
    {
        if (stk->data)
            free(stk->data);
        free(stk);
    }
}

int main (void )
{
    //create a stack
    struct mystack *stk = NULL;
    stk = createStack();
    if (stk == NULL)
        return -1;
    push(stk,10);
    push(stk,3);
    push(stk,7);
    disp(stk);
    pop(stk);
    push(stk,8);
    push(stk,4);
    pop(stk);
    push(stk,9);
    push(stk,89);
    push(stk,11111111);
    push(stk,49);
    pop(stk);
    disp(stk);
    deleteStack(stk);
    return 0;
}

Stack Implementation using a Linked List


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

typedef struct _node {
    int data;
    struct _node *next;
} node;

struct Stack
{
    int count;
    node *top;
};

 struct Stack * createStack ()
{
    struct Stack *temp = (struct Stack *)
                         malloc(sizeof (struct Stack));
    if (!temp)
        return NULL;
    temp->count =0;
    temp->top=NULL;
    return temp;
}

int  push (struct Stack *stk, int data)
{
    node * newnode = (node *) malloc (sizeof (node));
    if (!newnode)
        return -1;
    newnode->data = data;
    newnode->next = stk->top;
    stk->count++;
    stk->top = newnode;
    printf("after pushing %d size of stack is %d\n",
            data,stk->count);
        return 0;
}

int isEmpty (struct Stack *stk)
{
  if (stk->count < 1 || !stk->top ) {
      printf("Stack empty\n");
      return 1;
  }
  return 0;
}


 int disp (struct Stack *stk)
{
    printf("******  displaying Stack *******\n");
    if (isEmpty(stk))
        return -1;
    node *trav = stk->top;
    while (trav){
        printf (" %d -> ",trav->data);
        trav = trav->next;
    }
    printf("NULL \n******  end Stack *******\n");
    return 0;
}
 int pop (struct Stack *stk)
{
    int data;
    if (isEmpty(stk))
        return -1;
    node *temp = stk->top;
    stk->top = stk->top->next;
    printf("popping %d\n",temp->data);
    data = temp->data;
    free (temp);
    stk->count--;
    return data;
}

 void deleteStack (struct Stack *stk)
{
    if (stk){
        while(stk->top){
            node *trav = stk->top;
            stk->top=trav->next;
            free(trav);
        }
    free (stk);
    }
}
int main ()
{
    struct Stack* myStack  = createStack();
    if (!myStack)
        printf("error aloocation\n");
    pop  (myStack);
    push (myStack,10);
    pop  (myStack);
    disp(myStack);
    push (myStack,10);
    push (myStack,1);
    disp(myStack);
    push (myStack,9);
    push (myStack,5);
    disp(myStack);
    pop  (myStack);
    deleteStack(myStack);
    return 0;
}

Wednesday, August 7, 2013

Array Dynamically

2- D array creation dynamically

A dynamic 2D array is basically an array of pointers to arrays. You should initialize it using a loop:
In CPP

// in CPP 

int** ary = new int*[Rows];
for(int i = 0; i < Rows; ++i)
    ary[i] = new int[Rows]; 

and then clean up would be: 

for(int i = 0; i < sizeY; ++i)  { 
    delete [] ary[i]; } 
 delete [] ary;

in  C
 
 
double** Make2DDoubleArray(int arraySizeX, int arraySizeY) {
double** theArray;
theArray = (double**) malloc(arraySizeX*sizeof(double*));
for (int i = 0; i < arraySizeX; i++)
   theArray[i] = (double*) malloc(arraySizeY*sizeof(double));
   return theArray;
} 

Then, inside the code, i would use something like
double** myArray = Make2DDoubleArray(nx, ny);
Voila!

Of course, do not forget to remove your arrays from memory once you're done using them. To do this

for (i = 0; i < nx; i++){
   free(myArray[i]);
}
free(myArray);