Showing posts with label data structures. Show all posts
Showing posts with label data structures. Show all posts

Wednesday, 3 December 2014

Shuffling elements in an array.

Code without using a temporary array, but using the swapping technique. Following the syntax of MingW compiler.


#include "iostream"
using std::cout;
void swap(int &n1, int &n2)
{
int t = n1;
n1 = n2;
n2 = t;
}
int main()
{
const int size = 10;
int array[size]={6,7,8,9,10,1,2,3,4,5};
int j = size/2;
for (int i = 0; i < size/2; ++i,++j)
swap(array[i], array[j]);
for (int i = 0; i < size; ++i)
cout << array[i] << " ";
return 0;
}

Same code with little syntactic changes in the code for Turbo C++ compiler.


#include "iostream.h"
#include "conio.h"
void swap(int &n1, int &n2)
{
int t = n1;
n1 = n2;
n2 = t;
}
void main()
{
clrscr();
const int size = 10;
int array[size]={6,7,8,9,10,1,2,3,4,5};
int j = size/2;
int i;
for (i = 0; i < size/2; ++i,++j)
swap(array[i], array[j]);
for (i = 0; i < size; ++i)
cout << array[i] << " ";
getch();
}

Using another algorithm but not that much efficient as compared to above mentioned, also the implementation is in Turbo C++ styled Syntax only, one can easily port it to MingW or Visual studio rest is same, i.e, keywords and for loops e.t.c,

#include "iostream.h"
#include "conio.h"
void main()
{
clrscr();
const int size = 10;
int array[size]={6,7,8,9,10,1,2,3,4,5};
int array2[size];
int i,j;
for(i = 0, j = 5; i < size/2; ++i)
array2[i] = array[j++];
for (i = 5, j = 0; i < size; ++i)
array2[i] = array[j++];
i=size;
while(i)
array[i] = array2[--i];
for (i = 0; i < size; ++i)
cout << array[i] << " ";
getch();
}
Algorithm efficient, (the swap one) but now implemented in Java. Conditions, loops, if e.t.c works same as in C++ but the syntax is different + Java has got some really easy to understand exceptional handling like the "Widely used exception when playing with arrays is 'ArrayIndexOutOfBoundsException' that doesn't let you access the memory you didn't allocate, hence no infinite loops and you don't get you RAM dumped!" But In Java you don't have access to pointers so there's no pass by reference :( hence no same function as in C++ for swapping in Java but it can be achieved but no pass by reference, tho Array to functions is automatically passed by reference to save the memory ;)


/**
* @author B1nary Geek ;)
* As far as the ar.length is concerned, yes Java is damn
* genius ;)
*/
public class Main {
public static void main(String[] args) {
int[] ar = {6,7,8,9,10,1,2,3,4,5};
int temp, j = 5;
try {
for(int i = 0; i < ar.length/2; i++) {
temp = ar[j];
ar[j++]= ar[i];
ar[i]= temp;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("You are trying to access" +
"the memory you didn\'t allocate.");
}
try {
for(j = 0; j < ar.length; j++)
System.out.print(ar[j] + " ");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("You are trying to access" +
"the memory you didn\'t allocate.");
}
}
}
No download links today, may be I put the download links sometime later.

Sunday, 30 November 2014

Circular Queue of Integers implemented in C++ [Turbo C++ version.]

Turbo C++

Circular Queue


In computer science, a queue (/ˈkjuː/ kew) is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection. Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer. Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes. Common implementations are circular buffers and linked lists. More information can be achieved by clicking this link.
Code implementation.

#include "iostream.h"
#include "conio.h"
const int size = 5;
class Queue
{
int Q[size];
int front;
int rear;
int values;
public:
Queue()
{
front = rear = values = 0;
}
void Enqueue(int n)
{
if (!isFull())
{
Q[rear] = n;
rear = (rear + 1) % size;
values++;
}
}
int Dequeue()
{
if (!isEmpty())
{
front=(front+1)%size;
values--;
return Q[front-1];
}
}
int isFull()
{
if (values == size)
return 1;
return 0;
}
int isEmpty()
{
if (values == 0)
return 1;
return 0;
}
int peek()
{
if (!isEmpty())
return Q[front];
}
void display()
{
cout << "Total values inside the Queue:\n";
int i = values;
int j = front;
while (i)
{
cout << Q[j] << " ";
j = (j + 1) % size;
i--;
}
}
void display(int n)
{
if (n<5 || n>-1)
cout << Q[n] << " ";
}
};

void main()
{
clrscr();
Queue q;
q.Enqueue(1);
q.Enqueue(1);
q.Enqueue(1);
q.Enqueue(1);
q.Dequeue();
q.Dequeue();
q.Dequeue();
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
q.display();
getch();
}

Stack of Objects implemented in C++ [Turbo C++ version.]



Turbo C++



Graphical Representation of stack.
















Code implementation.
#include "iostream.h"
#include "conio.h"
#include "stdio.h"
const int size = 5;
class Book
{
int id;
char name[50];
public:
void getData()
{
cout << "Enter book ID: ";
cin >> id;
cout << "Enter name of the book: ";
gets(name);
}
void showData()
{
cout << "Book ID: " << id << endl;
cout << "Name of the book: "; puts(name); cout << endl;
}
};
class Stack
{
Book b_stack[size];
int top;
public:
Stack()
{
top=0;
}
void push(Book b)
{
if(!isFull())
b_stack[top++]=b;
}
Book pop()
{
if(!isEmpty())
return b_stack[--top];
}
int isFull()
{
if(top==size)
return 1;
return 0;
}
int isEmpty()
{
if(top==0)
return 1;
return 0;
}
Book peek()
{
if(!isEmpty())
return b_stack[top-1];
}
void display();
};
void Stack::display()
{
for (int i = top-1; i > -1; i--)
b_stack[i].showData();
}
void main()
{
clrscr();
Book b;
Stack s;
cout << "Enter data for Books: " << endl;
while(!s.isFull())
{
b.getData();
s.push(b);
}
b=s.peek();
cout << "Peek at the moment:\n";
b.showData();
b=s.pop();
cout << "Popped book:\n";
b.showData();
getch();
cout << "Press any key to continue.\n";
b=s.peek();
cout << "Peek after popping the top most book:\n";
b.showData();
cout << "The books in the stack at the moment are:\n";
s.display();
getch();
}