Sunday, August 24, 2014

Stack - Data Structure C++

// Stack.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class Node{
public:
int data;
Node *next;
};

class Stack {
public:
Node *top;
Node* current;
Node *bottom;
Stack(){
top=NULL;
current=NULL;
bottom=NULL;
}
void gettop(){
cout<<top->data;
}
void pop(){
if(top==NULL)
{
cout<<"Stack is empty: \n";
}
else{
Node *temp;
temp =top;
top=top->next;
delete(temp);
}

}
void push(){
int n;
cout<<"\nEnter the number you want to push : ";
cin >>n;
if(top==NULL){
Node *temp = new Node;
temp->data=n;
temp->next=NULL;
top=temp;
bottom=temp;
delete(temp);
}
else{
Node *temp = new Node;
temp->data=n;
temp->next=top;
top=temp;
}
}

void display(){
current=top;
while(current!=bottom){
cout<<current->data;
cout<<endl;
current=current->next;
}
cout<<current->data;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Stack s;
int option;

cout<<"Enter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;

while(option!='e'){
switch(option){
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.gettop();
break;
case 4:
s.display();
break;
}
cout<<"\nEnter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;
}
return 0;
}

Que - Data Structure C++

// Que.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class Node{
public:
int data;
Node *next;

Node(){
data=0;
next=NULL;
}
};

class Que{
public:
Node *first;
Node *last;
Node *current;

Que(){
first = NULL;
last=NULL;
current=NULL;
}

void enque(int n){
if(last==NULL){
Node *temp=new Node;
temp->data=n;
temp->next=NULL;
first=temp;
last=temp;
delete(temp);
}
else{
Node *temp= new Node;;
temp->data=n;
temp->next=NULL;
last->next=temp;
last=temp;
delete(temp);
}
}
void deque(){
if(last==NULL){
cout<<"\nThe Que is empty\n ";
}
else{
current=first;
first=first->next;
cout<<"Data dequeued is : "<<current->data<<endl;
delete(current);
}
}
void display(){
current=first;
while(current!=last){
cout<<current->data;
current=current->next;
}
cout<<current->data;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Que q;
int option,n;

cout<<"Please select your que operation:\n1: Enque()\n2:Deque\n3: Display\n Enter 'e' to exit\n";
cin>>option;
while(option!='e'){
switch(option){
case 1:
cout<<"\nEnter the data you want to enque: ";
cin>>n;
q.enque(n);
break;
case 2:
q.deque();
break;
case 3:
q.display();
break;
}
cout<<"\nPlease select your que operation:\n1: Enque()\n2:Deque\n3: Display\n Enter 'e' to exit\n";
cin>>option;
}
return 0;
}

Priority Que - Data Structure C++

// P_Que.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class Node{
public:
int data;
int prio;
Node *next;
Node *previous;

Node(){
data=0;
prio=0;
next=NULL;
previous=NULL;
}
};

class P_Que{
public:
Node *first;
Node* last;
Node *current;

P_Que(){
first=NULL;
last=NULL;
current=NULL;
}

void enque(int n,int p){
if(first==NULL){
Node *temp= new Node;
temp->data=n;
temp->prio=p;
temp->next=NULL;
temp->previous=NULL;
first=temp;
last=temp;
}
else{
current=first;
Node *temp =new Node;
temp->data=n;
temp->prio=p;
while(current->next!=NULL){
if(temp->prio>current->prio){
temp->next=current;
temp->previous=current->previous;
current=current->next;
}
}
}
}

void display(){
current=first;
while(current->next!=NULL){
cout<<current->data<<endl;
current=current->next;
}

}
};

int _tmain(int argc, _TCHAR* argv[])
{
P_Que q;
int option,n,p;

cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;

while(option!='e'){
switch(option){
case 1:
cout<<"\nEnter the data you want to enque: ";
cin>>n;
cout<<"\nEnter it's priority: ";
cin>> p;
q.enque(n,p);
break;
case 2:
q.display();
break;
}
cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;
}
return 0;
}

Linked List- Data Structure C++

// Linked_List.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

class Node{
public:
int data;
Node *next;
Node *previous;

Node(){
next=NULL;
previous=NULL;
}
};

class LinkedList{
private:
Node *head;
Node *current;
Node *end;
public:
LinkedList()
{
head=NULL;
current=NULL;
end=NULL;
}
void addElement()
{
Node *temp;
temp=new Node;
cout<<"\nEnter element data: ";
cin>> temp->data;
if(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
current=head;
while(current->next != NULL)
{
current=current->next;
}
current->next =temp;
}
}

void remove()
{
int n;
cout<<"Enter the element you want to remove: ";
cin>>n;
current=head;
Node *previous=current;
while(current->data!=n)
{
previous=current;
current =current->next;
}
Node *temp=current;
previous->next=current->next;
delete temp;
}

void countElements()
{
int count=0;
current =head;
while(current->next!=NULL)
{
current=current->next;
count++;
}
count++;
cout<<"The total number of elements are: "<<count;
}

void display(){
current=head;
cout<<"The list contains the following elements:\n";
while(current->next!=NULL)
{
cout<<current->data<<endl;
current=current->next;
}
cout<<current->data;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
LinkedList a;
int option;

cout<< "Enter you choice:\n1:Add Element\n2:Remove Element\n3:Total number of elements\n4:Display List\nEnter 'e' to end\n";
cin>>option;

while(option!='e'){

switch (option){
case 1:
a.addElement();
break;
case 2:
a.remove();
break;
case 3:
a.countElements();
break;
case 4:
a.display();
break;
}
cout<< "Enter you choice:\n1:Add Element\n2:Remove Element\n3:Total number of elements\n4:Display List\nEnter 'e' to end\n";
cin>>option;
}
return 0;
}

Bank Account Project C++

// Bank_Accout.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

class BankAccount{
private:
int x;
int balance;
public:
void withdraw(){
cout<<"Enter the amount you want to withdraw: ";
cin >> x;
balance-=x;
cout<< "Your remaining balance is: "<<balance;
}

void deposit(){
cout<<"Enter the amount you want to deposit: ";
cin >> x;
balance+=x;
cout<< "Your new balance is: "<<balance;
}

void showBalance(){
cout<< "Your current balance is: "<<balance;
}

BankAccount(){
cout<<"Enter the amount in the account: ";
cin>>balance;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
BankAccount a;
int n;

cout<< "Enter your choice: \n1: Withdraw amount\n2: Desposit amount\n3: Check Balance";
cin >> n;

while(n!='e'){

cout<< "Enter your choice: \n1: Withdraw amount\n2: Desposit amount\nCheck Balance";
cin >> n;

switch (n){
case 1:
a.withdraw();
break;
case 2:
a.deposit();
break;
case 3:
a.showBalance();
break;
default:
cout<< "Enter a valid option\n";

}

}
_getch();
return 0;
}

Tic Tac Toe Game C++

// TicTacToe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

char map[9] = {'0','1','2','3','4','5','6','7','8',};

void sortp1(int p[]);
void sortp2(int p[]);

void display(int, int);
void display(int, int);

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int p1[5];
int p2[5];
int map[9];
int t=0;


cout<<  "0" << " | " << "1" << " | " << "2" <<endl;
cout<< "========="<<endl;
cout<<  "3" << " | " << "4" << " | " << "5" <<endl;
cout<< "========="<<endl;
cout<<  "6" << " | " << "7" << " | " << "8" <<endl;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2,p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1,p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2, p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2,p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

cout<< "\nTurn: Player 2=>";
cin>> p2[t];
display(2,p2[t]);

t++;

cout<< "\nTurn: Player 1=>";
cin>> p1[t];
display(1, p1[t]);

//for(int j=0; j<5; j++)
//{
// cout<<p1[j] << "\n";
//}
//for(int j=0; j<4; j++)
//{
// cout<<p2[j] << "\n";
//}

sortp1(p1);
sortp2(p2);

if( p1[0] == 0 && p1[1] == 1 && p1[2] == 2 || p1[0] == 3 && p1[1] == 4 && p1[2] == 5 || p1[0] ==6 && p1[1] == 7 && p1[2] ==8
||p1[0] ==0 && p1[1] ==3 && p1[2] ==6 || p1[0] ==1 && p1[1] ==4 && p1[2] ==7 || p1[0] ==2 && p1[3] ==5 && p1[6] ==8
|| p1[0] == 0 && p1[1] ==4 && p1[2] == 8 || p1[0] == 2 && p1[1] == 4 && p1[2] == 6)
{
cout << "Congratulations Player 1 , You Win!";
}
else

if( p2[0] == 0 && p2[1] == 1 && p2[2] == 2 || p2[0] == 3 && p2[1] ==4 && p2[2] == 5 || p2[0] == 6 && p2[1] == 7 && p2[2] == 8
||p2[0] ==0 && p2[1] ==3 && p2[2] ==6 || p2[0] ==1 && p2[1] ==4 && p2[2] ==7 || p2[0] ==2 && p2[3] ==5 && p2[6] ==8
|| p2[0] == 0 && p2[1] ==4 && p2[2] == 8 || p2[0] == 2 && p2[1] ==4 && p2[2] == 6)
{
cout << "Congratulations Player 2 , You Win!";
}
else
cout << "It's a draw";

getch();
return 0;
}

void sortp1(int p[])
{
for(int i=0; i<3;i--)
{
for(int i=0; i<4;i++)
{
int temp;
if(p[i] >p[i+1])
{
temp = p[i];
p[i]=p[i+1];
p[i+1]=temp;
}
}
}

/*for(int j=0; j<5; j++)
{
cout<<p[j] << "\n";
}*/
}

void sortp2(int p[])
{
int temp;
for(int i=0; i<2;i--)
{
for(int i=0; i<3;i++)
{

if(p[i] >p[i+1])
{
temp = p[i];
p[i]=p[i+1];
p[i+1]=temp;
}
}
}
/*for(int j=0; j<4; j++)
{
cout<<p[j] << "\n";
}*/
}

void display(int a, int p)
{




if(a ==1)
{
map[p]='X';
}
if(a == 2)
{
map[p] = 'O';
}

cout<< map[0] << " | " << map[1] << " | " << map[2] <<endl;
cout<< "========="<<endl;
cout<<  map[3] << " | " <<map[4] << " | " << map[5] <<endl;
cout<< "========="<<endl;
cout<<  map[6] << " | " <<map[7] << " | " << map[8] <<endl;

}

AVL Tree

#include "avltree.h"
#include <stdlib.h>
#include "fatal.h"

struct AvlNode
{
ElementType Element;
AvlTree  Left;
AvlTree  Right;
int      Height;
};

AvlTree MakeEmpty( AvlTree T )
{
if( T != NULL )
{
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
}

Position Find( ElementType X, AvlTree T )
{
if( T == NULL )
return NULL;
if( X < T->Element )
return Find( X, T->Left );
else
if( X > T->Element )
return Find( X, T->Right );
else
return T;
}

Position FindMin( AvlTree T )
{
if( T == NULL )
return NULL;
else
if( T->Left == NULL )
return T;
else
return FindMin( T->Left );
}

Position FindMax( AvlTree T )
{
if( T != NULL )
while( T->Right != NULL )
T = T->Right;

return T;
}


static int Height( Position P )
{
if( P == NULL )
return -1;
else
return P->Height;
}


static int Max( int Lhs, int Rhs )
{
return Lhs > Rhs ? Lhs : Rhs;
}


/* This function can be called only if K2 has a left child */
/* Perform a rotate between a node (K2) and its left child */
/* Update heights, then return new root */

static Position SingleRotateWithLeft( Position K2 )
{
Position K1;

K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;

K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1;
K1->Height = Max( Height( K1->Left ), K2->Height ) + 1;

return K1;  /* New root */
}


/* This function can be called only if K1 has a right child */
/* Perform a rotate between a node (K1) and its right child */
/* Update heights, then return new root */

static Position SingleRotateWithRight( Position K1 )
{
Position K2;

K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;

K1->Height = Max( Height( K1->Left ), Height( K1->Right ) ) + 1;
K2->Height = Max( Height( K2->Right ), K1->Height ) + 1;

return K2;  /* New root */
}


/* This function can be called only if K3 has a left */
/* child and K3's left child has a right child */
/* Do the left-right double rotation */
/* Update heights, then return new root */

static Position DoubleRotateWithLeft( Position K3 )
{
/* Rotate between K1 and K2 */
K3->Left = SingleRotateWithRight( K3->Left );

/* Rotate between K3 and K2 */
return SingleRotateWithLeft( K3 );
}


/* This function can be called only if K1 has a right */
/* child and K1's right child has a left child */
/* Do the right-left double rotation */
/* Update heights, then return new root */

static Position DoubleRotateWithRight( Position K1 )
{
/* Rotate between K3 and K2 */
K1->Right = SingleRotateWithLeft( K1->Right );

/* Rotate between K1 and K2 */
return SingleRotateWithRight( K1 );
}



AvlTree Insert( ElementType X, AvlTree T )
{
if( T == NULL )
{
/* Create and return a one-node tree */
T = malloc( sizeof( struct AvlNode ) );
if( T == NULL )
FatalError( "Out of space!!!" );
else
{
T->Element = X; T->Height = 0;
T->Left = T->Right = NULL;
}
}
else
if( X < T->Element )
{
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T );
else
T = DoubleRotateWithLeft( T );
}
else
if( X > T->Element )
{
T->Right = Insert( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == 2 )
if( X > T->Right->Element )
T = SingleRotateWithRight( T );
else
T = DoubleRotateWithRight( T );
}
/* Else X is in the tree already; we'll do nothing */

T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
return T;
}


AvlTree Delete( ElementType X, AvlTree T )
{
printf( "Sorry; Delete is unimplemented; %d remains\n", X );
return T;
}

ElementType Retrieve( Position P )
{
return P->Element;
}