Showing posts with label algorithms. Show all posts
Showing posts with label algorithms. Show all posts

Tuesday, 13 December 2016

Data Structure & Algorithms | Queue

 DATA STRUCTURE

QUEUE

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

#include<stdio.h>
#include<stdlib.h>
#define Max 5
int a[Max];
int rear=-1;
int front=-1;
void insert(int);
void delete();
void display();
void main()
{
    int c,item;
    do
    {
        printf("\nPress 1:Insert\nPress 2:Delete\nPress 3:Diplay\nPress 0:Exit\n");
        scanf("%d",&c);
        switch(c)
        {
            case 1:printf("Enter Element\n");
                    scanf("%d",&item);
                    insert(item);
                    break;
            case 2:delete();
                    break;
            case 3:display();
                    break;
            case 0:exit(0);
        }
    }while(1);
}
void insert(int item)
{
    if(rear==Max-1)
    printf("Insertion not Possible\n");
    else
    {
        rear++;
        a[rear]=item;
    }
}
void delete()
{
    if(rear==front)
    printf("Deletion not Possible\n");
    else
    {
        front++;
        printf("Deleted Element %d",a[front]);
    }
}
void display()
{
    int i;
    printf("Queue :");
    for(i=front+1;i<=rear;i++)
    printf("%d ",a[i]);
}

,Thanks
Bikki Mahato

Sunday, 6 November 2016

Data Structure & Algorithms | Stack

DATA STRUCTURE

STACK

A stack is a basic data structure that can be logically thought as linear structure represented by a real physical stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of your data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. This structure is used all throughout programming.
The basic implementation of a stack is also called a LIFO (Last In First Out) to demonstrate the way it accesses data, since as we will see there are various variations of stack implementations.
There are basically three operations that can be performed on stacks . They are 1) inserting an item into a stack (push). 2) deleting an item from the stack (pop). 3) displaying the contents of the stack(pip).

#include<stdio.h>
#include<stdlib.h>
#define Max 5
int a[Max];
int top=-1;
void push(int);
void pop();
void display();
void main()
{
    int c,item;
    do
    {
        printf("Press 1:Push\nPress 2:Pop\nPress 3:Display\nPress 4:Exit\n");
        scanf("%d",&c);
        switch(c)
        {
            case 1:printf("Enter Element\n");
                    scanf("%d",&item);
                    push(item);
                    break;
            case 2:pop();
                    break;
            case 3:display();
                    break;
            case 4:exit(0);
        }
    }while(1);
}
void push(int item)
{
    if(top==Max-1)
    printf("\nStack is Full");
    else
    {
        top++;
        a[top]=item;
    }
}
void pop()
{
    if(top==-1)
    printf("\nStack is Empty");
    else
    {
        printf("\nDeleted Element %d",a[top]);
        top--;
    }
}
void display()
{
    int i;
    printf("Stack : ");
    for(i=top;i>=0;i--)
    printf("%d ",a[i]);
    printf("\n");
}

,Thanks
Bikki Mahato