Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Wednesday, 4 January 2017

Stack Data Structure Implementation using class in Cpp

Stack:-
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Mainly the following three basic operations are performed in the stack:
  • Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
  • Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
  • Peek: Get the topmost item.

Proram:-

#include <bits/stdc++.h>
using namespace std;
#define Capacity 50  //Capacity of stack;
class Stack{
    int top,capacity;
    int *arr;
    public:
    Stack(int size){
        arr = new int(size);
        top = -1;
        capacity=size;
    }
    bool isEmpty();
    bool isFull();
    void push(int );
    void pop();
    void print();
    int peek();   
};

bool Stack::isEmpty(){
    if(top==-1) {
      cout<<"stack is Empty\n"; 
      return true;
    }
    return false;
}

bool Stack::isFull(){
    if(top==capacity-1){
        cout<<"stack is Full\n";
        return true;
    }
    return false;
}

int Stack::peek(){
    if(isEmpty()) return -1;
    else return arr[top];
}

void Stack::push(int value){
    if(isFull()) return;
    cout<<"push the element "<<value<<endl;
    arr[++top]=value;
}

void Stack::pop(){
    if(isEmpty()) return;
    cout<<"pop element is: ";
    cout<<arr[top--]<<endl;
}

void Stack::print(){
  if(isEmpty()) return ;
  cout<<"stack element are: " <<endl;
  for(int i=0;i<=top;i++) cout<<arr[i]<<" ";
}
int main()
{
    Stack stack(Capacity);
    stack.pop();
    stack.push(1);
    stack.push(6);
    stack.push(4);
    stack.push(2);
    stack.push(7);
    cout<<"Top Element of stack is: "<<stack.peek()<<endl;
    stack.push(8);
    stack.pop();
    stack.print();
    return 0;
}

Heap Sort Implementation in Cpp

Heap Sort:-
Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element.

Algorithm:- 
 
Heap Sort Algorithm for sorting in increasing order:
1. Build a max heap from the input data.
2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree.
3. Repeat above steps while size of heap is greater than 1.

 Program:-

#include <bits/stdc++.h>
using namespace std;

void heapify(vector<int>&arr,int n,int i){
    int largest,l,r;
    largest=i;
    l=2*largest+1;
    r=2*largest+2;
    if(l<n && arr[l]>arr[largest]) largest=l;
    if(r<n && arr[r]>arr[largest]) largest=r;
    if(largest!=i){
        swap(arr[largest],arr[i]);
        heapify(arr,n,largest);
    }
}
   
void heapsort(vector<int>&arr,int n){
    for(int i=n/2-1;i>=0;i--){
        heapify(arr,n,i);
    }
    for(int i=n-1;i>0;i--){
        swap(arr[0],arr[i]);
        heapify(arr,i,0);
    }
}

void print(vector<int>&arr){
    for(int i=0;i<arr.size();i++) cout<<arr[i]<<" ";
}
int main()
{
    int n;
    cin>>n;
    vector<int>arr(n);
    for(int i=0;i<n;i++) cin>>arr[i];
    heapsort(arr,n);
    print(arr);
    return 0;
}

Wednesday, 14 December 2016

Queue implementation in cpp.

 Program:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct Queue{
    int front,rear,size;
    int capacity;
    int *array;
};
struct Queue* createqueue(int capacity)   
{
    struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
    queue->capacity = capacity;
    queue->front = 0;
    queue->rear = 0;
    queue->size = 0;
    queue->array = new int(capacity);
    return queue;
}   

int isempty(struct Queue *queue)
{
        return (queue->size==0);
}   

int isfull(struct Queue* queue)
{
    return (queue->size==queue->capacity);
}   
void enque(struct Queue* queue , int element)
{
    if(isfull(queue)) return;
    cout<<"Enqueue element is: "<<element<<endl;
    queue->array[(queue->rear++)%queue->capacity] = element;
    queue->size++;
}

void deque(struct Queue *queue)
{
        if(isempty(queue)) return ;
        cout<<"Dequeue element is: "<<queue->array[(queue->front++)%queue->capacity]<<endl;
        queue->size--;
}  

void printf(struct Queue* queue)
{
    while(queue->front!=queue->rear)
    {
        cout<<"The element is: "<<queue->array[(queue->front++)%queue->capacity]<<endl;
    }   
}   
int size(struct Queue* queue)
{
    return queue->size;
}   
int main() {
    struct Queue *queue = createqueue(5);
    enque(queue,10);
    enque(queue,20);
    enque(queue,30);
    deque(queue);
    deque(queue);
    deque(queue);
    cout<<"Size of Queue: "<<size(queue)<<endl;
    enque(queue,40);
    enque(queue,50);
    enque(queue,60);
    enque(queue,70);
    enque(queue,80);
    enque(queue,90);
    printf(queue);
    return 0;
}