1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
//============================================================================
// Name : DataStructure.cpp
// Author : Ahsan Ali
// Version : 1.0
// Copyright : All Rights Reserved.. Ahsan Corp.
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
using namespace std;
#define MAX_ITEMS 10
class Student
{
private:
int Reg_ID;
int Cell_No;
string Name;
public:
Student(int ID, int No, string N)
{
Reg_ID=ID;
Cell_No=No;
Name=N;
}
Student()
{
cout<<"Default constructor called automatically\n";
}
void SetRollNo(int no)
{
Reg_ID= no;
}
int GetRollNo()
{
return Reg_ID;
}
void Display()
{
cout<<"Name: "<<Name<<endl;
cout<<"Registration ID: "<<Reg_ID<<endl;
cout<<"Contact # "<<Cell_No<<endl;
}
void operator=(Student stdnt)
{
Name=stdnt.Name;
Reg_ID=stdnt.Reg_ID;
Cell_No=stdnt.Cell_No;
}
};
template <class itemType>
class Stack
{
private:
int top;
itemType data[MAX_ITEMS];
public:
Stack() // Default constructor
{
top=-1;
}
void push(itemType item);
itemType pop();
bool isEmpty();
bool isFull();
void MakeEmpty();
void Display();
};
template <class itemType>
void Stack <itemType>:: push (itemType item)
{
if (isFull())
{
cout<<"Stack is full, can not push more items\n";
return;
}
top++;
data[top]= item;
}
template <class itemType>
itemType Stack <itemType>:: pop()
{
if(isEmpty())
{
cout<<"Stack is empty, can not pop more items\n";
//return 0;
}
else
{
top--;
return data[top];
}
}
template <class itemType>
bool Stack<itemType>:: isEmpty()
{
return (top==-1);
}
template <class itemType>
bool Stack<itemType>:: isFull()
{
return (top==MAX_ITEMS-1);
}
template <class itemType>
void Stack<itemType>:: Display()
{
int top_flag=top;
Student Temp_Student= data[top--];
for (int k=0; k<=top_flag; k++)
{
Temp_Student.Display();
Temp_Student= data[top];
top--;
}
top=top_flag;
}
int main()
{
Student Stud1(130231,431,"Tariq"), Stud2(130332,432,"Saleem"), Stud3(130433,433,"Mateen"), Stud4(130534,434,"Haris");
Student Stud5(130635,435,"Khizar"), Stud6(130736,436,"Ahmad"), Stud7(130837,437,"Ahsan"), Stud8(130938,438,"Shoaib");
Stack <Student> S1;
S1.pop();
S1.push(Stud1);
S1.push(Stud2);
S1.push(Stud3);
S1.push(Stud4);
S1.push(Stud5);
S1.push(Stud6);
S1.push(Stud7);
S1.push(Stud8);
cout<<"Calling stack display function\n";
S1.Display();
return 0;
}
|