Stack Implementation using Array

// Static Implementation of Stack

#include<stdio.h>

#include<conio.h>

#define N 5


int stack[5];

int top = -1;


void push(){

int x;

printf("Enter data: ");

scanf("%d",&x);

if(top==N-1)

printf("Overflow\n");

else{

top++;

stack[top]=x;

}

}


void pop(){

if(top==-1)

printf("Underflow\n");

else{

printf("Popped out element is %d\n",stack[top]);

top--;

}

}


void peek(){

if(top==-1)

printf("Empty\n");

else

printf("Top element is %d\n",stack[top]);

}


void display(){

int i;

if(top==-1)

printf("Empty\n");

else{

printf("Current elements of stack are:\n");

for(i=top;i>=0;i--)

printf("%d\n",stack[i]);

}

}


void main(){

int ch;

do{

printf("Enter your choice:\n 1 for Push 2 for Pop 3 for Peek 4 for Display\n");

scanf("%d",&ch);

switch(ch){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

break;

default:

printf("Invalid Choice\n");

}

}while(ch!=0);

getch();

}


VHDL code for Half Adder

VHDL is one of the two standard HDLs that are supported by IEEE. Another one is Verilog. 

VHDL stands for

V: VHSIC (very high speed integrated circuit)

H: Hardware

D: Description

L: Language


For half adder, let a and b are two input signals, sum and carry are two outputs.

VHDL code:


LIBRARY IEEE;

USE IEEE.std_logic_1164.all;    -- use library_name.package_name.package_parts;


ENTITY half_adder IS

    PORT (a, b: IN BIT;    -- port_name: signal_mode signal_type;

          sum, carry: OUT BIT);    -- IN, OUT, INOUT, BUFFER

END half_adder;


ARCHITECTURE out_data OF half_adder IS

BEGIN

    sum <= a XOR b;    -- sum = a⊕b

    carry <= a AND b;    -- carry = a.b

END out_data;

Binary Search Algorithm

#include<stdio.h>

#include<conio.h>

int BinarySearch(int [],int,int,int);

int a[100],i,n,key,flag,l,r,m;

void main(){

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements (in ascending order): ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter value to be searched: ");

scanf("%d",&key);

flag=BinarySearch(a,0,n-1,key);

if(flag==0)

printf("%d is not found.",key);

else

printf("%d is found at %d position",key,flag+1);

getch();

}

Linear Search Algorithm

#include<stdio.h>
#include<conio.h>
void LinearSearch(int [],int,int);
int a[100],i,n,key;
void main(){
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter value to be searched: ");
scanf("%d",&key);
LinearSearch(a,n,key);
getch();
}

Counting Sort

#include<stdio.h>

#include<conio.h>

void counting_sort(int [],int,int);

int a[100],count[100],b[100],i,k,n;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

k=a[0];

for(i=1;i<n;i++){

if(k<a[i])

k=a[i];

}

counting_sort(a,n,k);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}

Quick Sort

#include<stdio.h>

#include<conio.h>

void quick_sort(int [],int,int);

int partition(int [],int,int);

int a[100],i,j,n,temp,lb,ub,start,end,pivot,loc;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

quick_sort(a,0,n-1);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}