Posts

Showing posts from August 25, 2013

Describe how a single array can be used to implement two stacks.

#include<stdio.h> #include<stdlib.h> #define size 10 /* Macro definition */ int top1 = -1, top2 = size; /* Global variable declaration */ int a[size]; int main() {     int ch, n1, n2;     while (1) {         printf("\n=====================\n");         printf("-- Menu Selection --|\n");         printf("=====================\n");         printf("1. Push |\n");         printf("2. Pop |\n");         printf("3. Display |\n");         printf("0. Exit |\n");         printf("=====================\n");         printf("Enter choice(0-3):");         scanf("%d", &ch);         switch (ch) {             case 1:                 printf("Enter n1:");   ...

Array Implementation of queue using C

#include<stdio.h> #include<stdlib.h> #define size 5 #define TRUE 1 #define FALSE 0 typedef struct {     int a[size];     int front;     int rear; } queue; queue *q; int main() /* main() function begins program execution */ {     int ch; /* variable declarion for choice in switch case */     int num; /* variable declaration for data */     q = (queue*) malloc(sizeof (queue)); /*Memory Alloaction*/     q->front = -1;     q->rear = -1;     while (1) {         printf("\nARRAY IMPLEMENTATION OF CQUEUE.\n");         printf("\n======================\n");         printf("|-- Menu Selection --|\n");         printf("======================\n");         printf("| 1. Enqueue |\n");         printf("| 2. Dequeue |\n");         prin...

Linked List Implementation of a stack

#include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 #define defCELL(ETYPE,CELL,STACK) typedef struct slist{ ETYPE data;struct slist *next;}CELL; defCELL(int, CELL, slist) CELL *top = NULL, *temp;    //Global declaration int push(int n);                      /*prototype declaration*/ int pop(); int display(); int clear(); int isempty(); int main() {     int ch, n; //Local variable declaration     while (1) {         printf("\n***Menu Selection***\n");         printf("1: PUSH\n");         printf("2: POP\n");         printf("3: CLEAR\n");         printf("4: ISEMPTY\n");         printf("0: EXIT\n");         printf("********************\n");         printf("Enter your choice(0-4):");    ...

Array Implementation of stack using C

#include<stdio.h> #include<stdlib.h> #define size 5 #define TRUE 1 #define FALSE 0 typedef struct //STRUCTURE DEFINITION {     int data[size];     int top; } stack; stack *stk; //GLOBAL VARIABLE DECLARATION int push(int); //prototype declarations int pop(); int display(); int isfull(); int clear(); int isempty(); int main() {     int n, i, ch;     stk = (stack*) malloc(sizeof (stack)); //DYNAMIC MEMORY ALLOCATION     stk->top = -1;     while (1) {         printf("\n***Menu Selection***\n");         printf("1: Push\n");         printf("2: Pop\n");         printf("3: Clear\n");         printf("4: IsEmpty\n");         printf("5: IsFull\n");         printf("0: exit.\n");         printf("********************\n");     ...

Addition, Substraction & Multiplication of two polynomials using C++

#include<iostream> #include<stdlib.h> #define RESET_COLOR "\e[m" #define GREEN  "\e[32m" using namespace std; class polynomial { public:     int *coeff, degree; /* variable declaration */     int get_data(); /*function declaration */     int display(int *coeff, int degree);     void addition(polynomial P1, polynomial P2);     void substraction(polynomial P1, polynomial P2);     void multiplication(polynomial P1, polynomial P2); }; int polynomial::display(int *coeff, int degree) {     int i, j;     for (i = degree; i >= 0; i--) {         cout << coeff[i] << "x^" << i;         if ((i - 1) != -1)             cout << "+";     }     cout << "\n";     return 0; } int polynomial::get_data() {     int i;     cout ...

Addition, Substraction & Multiplication of two 20 digit(Random) numbers using C++.

#include<iostream> #include<stdlib.h> #define RESET_COLOR "\e[m" #define GREEN  "\e[32m" #define A  0 #define B  9 #define N  20 #define SWAP(a,b)\ {    \  a=a+b;  \     b=a-b;      \     a=a-b;      \ } using namespace std; class Big_Number_Operation { public:     int *a; //variable declaration     void random_number_generator(); //Prototype declaration     void display(int *a, int n);     void addition(Big_Number_Operation N1, Big_Number_Operation N2);     void substraction(Big_Number_Operation N1, Big_Number_Operation N2);     void multiplication(Big_Number_Operation N1, Big_Number_Operation N2); }; void Big_Number_Operation::random_number_generator() {     int i;     a = new int[N];     for (i = 0; i < N; i++)         a[i] = A + rand() % (B ...

Implementation of 8 sorting Algorithms using Abstract Class & Templates in C++.

Problem: Implementation of 8 sorting Algorithms using Abstract Class in C++ which includes : -         o Bubble sort         o Selection Sort         o Insertion Sort         o Shell Sort         o Quick Sort         o Merge Sort         o Heap Sort         o Radix Sort         Solution : #include<iostream> #include<stdlib.h> #include<string.h> using namespace std; template <class T> class sort { public:     virtual void sorting(T *a, int beg, int n) = 0;     void swap(T &a, T &b);     void createlist(T *a, int n);     void display(T *a, int n); }; template <class T> void sort<T>::createlist(T *a, int n) {     int i;     for (i = 0; i < n; i++) {       ...

Program to count the number of substring and word withing a given string

package assignment1; /**  * @author snlkjha  */ public class Pattern {         public static int substringCount(String pattern, String text) {         int M = pattern.length(),N = text.length(),count=0;   // Variable decalartion                 for(int i=0;i<=N-M;i++) {             int j=0;             while(j<M && text.charAt(i+j)==pattern.charAt(j))   j++;             if (j == M) {                 count++;                 i=i+M;             }                   }         return count;                               } ...