Tuesday 26 April 2016

Queue with the help of dynamic array in C

queues can implemented in various ways 
now we will discuss the implementation of queue with the help of dynamic array 

#include<stdio.h>
struct queue1
{
    int *m;
    int size;
    int front;
    int rear;

};
typedef struct queue1 queue;
void insertion(queue *q1,int value)
{
    if(q1->front == -1)
    {
        q1->front++;
        q1->m[++q1->rear]=value;
    }
    else if(q1->rear == q1->size-1)
    {
        printf("the queue is already full ");

    }
    else
    {
        q1->m[++q1->rear]=value;
    }

}

int deletion(queue* q1)
{
    if(q1->front == -1)
    {
        printf ("the queue is already empty");
    }
    if (q1->rear == 1 )
    {
        q1->rear = -1;
        q1->front = -1;

    }
    else
    {
        int i = q1->front ;
        for (i=q1->front ; i<q1->rear; i++)
        {
            q1->m[i]=q1->m[i+1];

        }
        q1->rear--;
        return q1->m[q1->rear+1];
    }

}

void print (queue *q1)
{
    int i= q1->front;
    if (q1->front != -1)
    {


    for(i=q1->front;i<=q1->rear;i++)
    {
        printf("%d \n",q1->m[i]);
    }
    printf("end");}
}


void main()
{
    queue q1;
    printf("enter the size of the array");
    scanf("%d",&q1.size);
    q1.m= (int *)malloc(sizeof(int)*q1.size);
    q1.front = -1;
    q1.rear = -1;
    insertion(&q1,10);
    insertion(&q1,11);
    insertion(&q1,15);
    insertion(&q1,7);
    print(&q1);
    int a =deletion(&q1);
    print(&q1);

}

Queue with the help of linked list in C

Queues can implemented various ways 
main three steps are 
1> using linked list 
2> using array 
3> stack 


lets see with the help of linked list 

#include<stdio.h>
struct node1{
int value ;
struct node1 *next;
};
typedef struct node1 node ;
struct queue1{
    node *front;
    node *end;
};
typedef struct queue1 queue;

void insertion(queue *q,int data)
{
    node *newnode=(node *)malloc(sizeof(node));
    newnode->value= data;
    newnode->next= NULL;
    if(isempty(q))
    {
        q->front= newnode;
        q->end = newnode;

    }
    else{
        newnode->next = q->end;
        q->end=newnode;
    }

}
int deletion(queue *qm)
{
    int x;
    if (isempty(qm))
    {
        printf("the queue is already empty");
    }
    else
    {
        node *temp=qm->end;
        if (temp->next==NULL)
        {
            free(temp);
            qm->front=NULL;
            qm->end= NULL;
        }
        else
        {
        while(temp->next->next!=NULL)
        {
            temp=temp->next;
        }
        temp->next=NULL;
         x= qm->front->value;
        free(qm->front);
        qm->front=temp;
        return x;
        }
    }

}
isempty(queue *qm)
{
    if(qm->end==NULL)
    {
        return 1;
    }
    else
    return 0;

}
void print(queue qm )
{
    while(qm.end!=NULL)
    {
        printf("%d\n",qm.end->value);
        qm.end=qm.end->next;
    }
}

void main()
{
    queue q1;
    q1.front=NULL;
    q1.end=NULL;
    insertion(&q1,10);
    insertion(&q1,11);
    insertion(&q1,15);
    insertion(&q1,7);
    print(q1);
    int a =deletion(&q1);
    print(q1);
}

Stack implementation with the help of dynamic array C

Stack implementation can be done in three ways .
I discussed the first one ie with the help of linked list in my  previous blog 

lets with the help of stack implementation with dynamic array 

#include<stdio.h>
struct node1{
int value ;
struct node *next;
};
typedef struct node1 node;


void push (int **topptr, int data)
{
    node *newnode= (node*)malloc(sizeof(node));
    newnode->value= data ;
    {
        newnode->next = *topptr;
        *topptr= newnode;
    }
}
int  pop(int **topptr)
{
    int x;
    if(*topptr==NULL)
    {
        printf("the stack is already empty");
    }
    else
    {
        x=**topptr;
        node *tmp= *topptr;
        *topptr= tmp->next;
        free(tmp);
    }
    return x;

}
void print(node *hp)
{
    if(hp==NULL)
    {
        printf("list is empty");
    }
    while(hp!=NULL)
    {
        printf("%d\n",hp->value);
        hp=hp->next;
    }
}

void main()
{
    node *top=NULL;
    push (&top,10);
    push (&top,11);
    push (&top,15);
    push (&top,7);
    print(top);
    pop(&top);
    print(top);
}

Stack implementation with the help of linked list in C

Stack implementation can be done in three ways
1> using arrays
2> using linked lists
3> using 2 queues


let's see the program for stack implementation with the help of linked list


#include<stdio.h>
struct node1{
    int top, size;
    int *sptr;

};
typedef struct node1 stack;
void push(stack *s,int value )
{
    if(s->top!=s->size-1)
    {
        s->top ++;
        s->sptr[s->top] = value;
    }
    else
    {
        printf("stack is already full so can't enter %d \n",value);
    }

}
int  pop(stack *s)
{
   if (isempty(s))
   {
       printf("stack is already empty ");
        return 0;
   }
   else
    {
        int x= s->sptr[s->top];
        s->top = s->top -1 ;
        return x;
    }
}
int isempty(stack *s)
{
    if(s->top==-1)
    {
        return 1;
    }
    else
        return 0;
}
void print(stack s1)
{
    int i ;
    for ( i=0;i<=(s1.top);i++)
    {
       printf("%d\n",s1.sptr[i]) ;
    }

}
void main()
{
    stack s1;
    printf("enter the size of stack");
    scanf("%d",&s1.size);
    s1.sptr=(int*)malloc(sizeof(int)*(s1.size));
    s1.top=-1;
    int m=pop(&s1);
    printf("%dabc\n",m);
    push(&s1,8);
    push(&s1,23);
    push(&s1,43);
    push(&s1,21);
    push(&s1,54);
    print(s1);
    m=pop(&s1);

    printf("the value after pop is %d",m);

}