Need a project done?

C++ Programming Developer

Search This Blog

Stack using Link List in C++

Program to create a stack using link list in C++.

//Stack using Link List in C++
#include <iostream>
using namespace std;

Stack in C++ | Program to create a stack in C++

Stack in C++ program is given below:

//Stacks in C++
#include <iostream>
using namespace std;

Swap without using variable - C++


Swap two values without using a variable in C++ program is given below:

//Program to swap 2 numbers without using a variable in C++.
#include <iostream>
using namespace std;

Binary Tree in C++


Binary Tree program in C++ is given below:

See First: Binary Tree in C++ (A relatively easier Binary Tree program)

// Binary Tree in C++
// Binary Tree program to insert generically in the binary tree.

#include "stdafx.h" //Comment This line if it creates error.
#include <iostream>
using namespace std;

Printing Linked List Recursively in C++

Printing Linked List Recursively in C++ Program is given below:


// Printing Linked List Recursively in C++
// Linked List is created by inserting at head and then printing recursively.
#include <iostream>
using namespace std;

Operator Overloading: Overload << and >> operator


Overload << and >> operator program is given below:

// operator overloading
// overloading << and >> operator
#include "stdafx.h" //Delete this line if it creates error
#include <iostream>
using namespace std;
const int LEN = 80;           //maximum length of names
////////////////////////////////////////////////////////////////
class employee                        //employee class
{
private:
char name[LEN];                 //employee name
unsigned long number;
public:
friend istream& operator >> (istream& s, employee& e);
friend ostream& operator << (ostream& s, employee& e);
};
//-------------------------------------------------------------
istream& operator >> (istream& s, employee& e){
cout << "Name: "; s.getline(e.name,LEN,'\n');
cout << "Number: "; s >> e.number;
return s;
}
//-------------------------------------------------------------
ostream& operator << (ostream& s, employee& e){
cout << "Name: " << e.name << endl
<< "Number: " << e.number << endl;
return s;
}
///////////////////////////////////////////////////////////////
int main(){
employee oe; // oe => object of class employee.
cin >> oe;
cout << oe;
return 0;
}

Printing Linked List in Reverse Order Recursively in C++


Printing Linked List in Reverse Order Recursively in C++ Program is given below:

// Printing Linked List in Reverse Order Recursively in C++
// Insertion at head is done in the following program
#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////
struct link                           //one element of list
{
int data;                          //data item
link* next;                        //pointer to next link
};
////////////////////////////////////////////////////////////////
class linklist                        //a list of links
{
private:
link* first;                    //pointer to first link also known as head.
public:
linklist()                      //no-argument constructor
{ first = NULL; }            //no first link
link* getHead(){ return first; }
void additem(int d);            //add data item (one link)
void displayRecursivelyReversed(link* current);                 //display all links in REVERSE ORDER.
};
//-------------------------------------------------------------void
void linklist::additem(int d)         //add data item
{
link *newLink = new link;
newLink->data = d;
//insert at head
newLink->next = first;
first = newLink;
}
//-------------------------------------------------------------void
void linklist::displayRecursivelyReversed(link* current)              //display all links
{
if (current != NULL){
//move to next node.
displayRecursively(current->next);
cout << current->data << endl;
}

}
////////////////////////////////////////////////////////////////
int main()
{
linklist li;       //make linked list
li.additem(25);    //add four items to list
li.additem(36);
li.additem(49);
li.additem(64);
li.displayRecursivelyReversed(li.getHead());      //display entire list
return 0;
}

Linked List using Templates in C++


Linked List using Templates in C++ program is given below:


// linkedlist.cpp
// linked list using templates
// See also: Link List program in C++

#include "stdafx.h" //Remove this line if you see any error.
#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////
template <typename T>
struct link                           //one element of list
{
T data;                          //data item
link* next;                        //pointer to next link
};
////////////////////////////////////////////////////////////////
template <class T>
class linklist                        //a list of links
{
private:
link<T>* first;                    //pointer to first link
public:
linklist()                      //no-argument constructor
{ first = NULL; }            //no first link
void additem(T d);            //add data item (one link)
void display();                 //display all links
};
//-------------------------------------------------------------void
template <class T>
void linklist<T>::additem(T d)         //add data item
{
link<T> *newLink = new link<T>;
newLink->data = d;
//insert at head of link list
newLink->next = first;
first = newLink;
}
//-------------------------------------------------------------void
template <class T>
void linklist<T>::display()              //display all links
{
link<T>* current = first;             //set ptr to first link
while( current != NULL )           //quit on last link
{
cout << current->data << endl;  //print data
current = current->next;        //move to next link
}
}
////////////////////////////////////////////////////////////////

int main()
{
linklist<int> li;       //make linked list
li.additem(25);    //add four items to list
li.additem(36);
li.additem(49);
li.display();      //display entire list

linklist<char> lch;      //make linked list
lch.additem('a');    //add four items to list
lch.additem('b');
lch.additem('c');
lch.display();      //display entire list

return 0;
}

Binary Tree in C++

Binary Tree in C++ Program is given below:
// Binary Tree in C++
// A very basic Binary Tree program in C++

#include "stdafx.h" //Comment this line if it creates any error.
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////
struct nodeType{
nodeType();
int info;
nodeType* father;
nodeType* left;
nodeType* right;
};
nodeType::nodeType(){
father = left = right = NULL;
}
//////////////////////////////////////////////////////////////
class binaryTree{
public:
binaryTree(){  root = NULL; }
//------------------------------------------------------------
nodeType* makeTree(int x){
nodeType* p = new nodeType;
p->info = x;
p->father = NULL;
p->right = NULL;
p->left = NULL;
root = p;
return p;
}
//------------------------------------------------------------
void setLeft(nodeType* p, int x){
if (p == NULL){
cout << "Void Insertion!" << endl;
}
else if (p->left != NULL){
cout << "Invalid Insertion" << endl;
}
else{
p->left = new nodeType;
p->left->info = x;
p->left->right = NULL;
p->left->left = NULL;
p->left->father = p;
}
}
//------------------------------------------------------------
void setRight(nodeType* p, int x){
if (p==NULL){
cout << "Void Insertion" <<endl;
}
else if (p->right != NULL){
cout << "Invalid Insertion" << endl;
}
else{
p->right = new nodeType;
p->right->info = x;
p->right->left = NULL;
p->right->right = NULL;
p->right->father = p;
}
}
//------------------------------------------------------------
nodeType* getRoot(){
return root;
}
//------------------------------------------------------------
void print(nodeType* p){
cout << p->info << endl;

if (p!=NULL && p->left == NULL && p->right == NULL)
return ;
if (p!=NULL && p->left != NULL && p->right == NULL)
print (p->left);
if (p!=NULL && p->left == NULL && p->right != NULL)
print (p->right);
if (p!=NULL && p->left != NULL && p->right != NULL){
print (p->left);
print (p->right);
}
}

//----------------------------------------------------------------
//----------------------------------------------------------------
private:
nodeType* root;
};
//////////////////////////////////////////////////////////////
int main(){
binaryTree obj;
        // We insert 3 nodes in Binary Tree. You can try inserting more
obj.makeTree(5);
obj.setLeft(obj.getRoot(),4);
obj.setRight(obj.getRoot(),6);
obj.print(obj.getRoot());
return 0;
}


Now see Generic Binary Tree in C++ Program to insert at the correct position in the binary tree.

Link List program in C++

Link List Program in C++ is given below:

// linklist.cpp
// linked list program in C++
// Compiler: Microsoft Visual Studio 2012

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

////////////////////////////////////////////////////////////////
struct link                           //one element of list
{
int data;                          //data item
link* next;                        //pointer to next link
};
////////////////////////////////////////////////////////////////
class linklist                        //a list of links
{
private:
link* first;                    //pointer to first link
public:
linklist()                      //no-argument constructor
{ first = NULL; }            //no first link
void additem(int d);            //add data item (one link)
void display();                 //display all links
};
//-------------------------------------------------------------void
void linklist::additem(int d)         //add data item
{
link *newLink = new link;
newLink->data = d;
//insert at head
newLink->next = first;
first = newLink;
}
//-------------------------------------------------------------void
void linklist::display()              //display all links
{
link* current = first;             //set ptr to first link
while( current != NULL )           //quit on last link
{
cout << current->data << endl;  //print data
current = current->next;        //move to next link
}
}
////////////////////////////////////////////////////////////////
int main()
{
linklist li;       //make linked list
li.additem(25);    //add four items to list
li.additem(36);
li.additem(49);
li.additem(64);
li.display();      //display entire list
return 0;
}

See also: Link list using Templates

Reading link list from File in C++

To Read a link list from file, we've adopted the following procedure:
1. Write each node individually to a file in the function of getData().
2. Read Each node from file one by one and insert it in the link list .


//Reading link list from File in C++

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

class linkList{
public:
void getData();
void insert(linkList **head, linkList **tail); // insert at tail
void printAll(linkList *head); // Display all records.
private:
char name[100];
int rollNo;
linkList *next;
};
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::getData(){
cout << "Enter Name: "; cin >> name;
cout << "Enter Roll Number: "; cin >> rollNo;
next = NULL;
ofstream os("nodes of my link list.dat", ios::binary | ios::out | ios::app);
os.write( reinterpret_cast <char*> (this), sizeof(*this) );
os.close();
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::insert(linkList **head, linkList **tail){ // insert at tail
if (*head == NULL && *tail == NULL){
*head = (this);
*tail = (this);
}
else{
(*tail)->next = this;
*tail = this;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::printAll(linkList *head){
for (linkList *temp = head; temp!=NULL; temp=temp->next){
cout << "Name: " << temp->name << endl
<<  "Roll Number: " << temp->rollNo << endl << endl;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
/////////////////////////////////////////////////// Main() //////////////////////////////////////
int main(){
linkList *pointerOfLinkList  = 0, *head = 0, *tail = 0;

ifstream is;
while (true){
cout << "1. Insert Node\n"
<< "2. Print All\n"
<< "0. Press IT to load link list from file.\t";
int opt; cin >> opt;

switch(opt){
case 1:
pointerOfLinkList = new linkList;
pointerOfLinkList->getData();
pointerOfLinkList->insert(&head,&tail);
cout << "Inserted" << endl;
break;
case 2:
pointerOfLinkList->printAll(head);
break;
case 8:
break;
case 9:
break;
case 0:
is.open("nodes of my link list.dat", ios::binary | ios::in);

pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
while (!is.eof()){
pointerOfLinkList->insert(&head,&tail);
pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
}
is.close();
break;
default:
cout << "Wrong Key pressed" << endl;
}
_getch();
system("cls");
}
return 0;
}

Insert at tail Link List


//Program to insert at tail in Link List using Classes.

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

class linkList{
public:
void getData();
void insert(linkList **head, linkList **tail); // insert at tail
void printAll(linkList *head); // Display all records.
private:
char name[100];
int rollNo;
linkList *next;
};
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::getData(){
cout << "Enter Name: "; cin >> name;
cout << "Enter Roll Number: "; cin >> rollNo;
next = NULL;
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::insert(linkList **head, linkList **tail){ // insert at tail
if (*head == NULL && *tail == NULL){
*head = (this);
*tail = (this);
}
else{
(*tail)->next = this;
*tail = this;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::printAll(linkList *head){
for (linkList *temp = head; temp!=NULL; temp=temp->next){
cout << "Name: " << temp->name << endl
<<  "Roll Number: " << temp->rollNo << endl << endl;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
/////////////////////////////////////////////////// Main() //////////////////////////////////////
int main(){
linkList *pointerOfLinkList  = 0, *head = 0, *tail = 0;


while (true){
cout << "1. Insert Node\n"
<< "2. Print All\n"
<< "0. Exit\t";
int opt; cin >> opt;

switch(opt){
case 1:
pointerOfLinkList = new linkList;
pointerOfLinkList->getData();
pointerOfLinkList->insert(&head,&tail);
cout << "Inserted" << endl;
break;
case 2:
pointerOfLinkList->printAll(head);
break;
case 8:
break;
case 9:
break;
case 0:
return 0;
default:
cout << "Wrong Key pressed" << endl;
}
_getch();
system("cls");
}
return 0;
}

Initialize Static Array in C++


Initialize Static Array in C++ program is given below:

#include <iostream>
using namespace std;

class myClass{
private:
static int array[100]; //Static Array in C++
public:
void display();
};
////////////////////////////////////////////////////////////////
int myClass::array[100] = {0}; //Initialize Static Array in C++

void myClass::display(){
for (int i = 0; i<100; i++){
cout << "array[" << i << "] = " << array[i] << endl;
}
}
//``````````````````````````````````````````````````````````````

int main(){
myClass obj;
obj.display();
return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."