0% found this document useful (0 votes)
35 views8 pages

Syed Hazma Arif 03-135182-043: Task 1: Code

The document contains code for 4 tasks related to stacks in C++. Task 1 defines a stack class with push, pop, isfull, empty and display methods. Task 2 implements a stack using linked lists with push and pop methods. Task 3 defines two stack structures and includes pop and push methods. Task 4 includes functions to reverse an integer by pushing its digits to a stack and popping them back out.

Uploaded by

syed hamza arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

Syed Hazma Arif 03-135182-043: Task 1: Code

The document contains code for 4 tasks related to stacks in C++. Task 1 defines a stack class with push, pop, isfull, empty and display methods. Task 2 implements a stack using linked lists with push and pop methods. Task 3 defines two stack structures and includes pop and push methods. Task 4 includes functions to reverse an integer by pushing its digits to a stack and popping them back out.

Uploaded by

syed hamza arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

SYED HAZMA ARIF

03-135182-043
Task 1:
code:
#include<iostream>

using namespace std;

const int size = 5;

class Stack{

private:

int top;

int stack[size];

public:

void push(int val){

if (isfull()){

cout << "stack overflow";

else

stack[++top] = val;

int pop(){

if (Empty()){

cout << "Vaccum is Empty";

else

return stack[--top];
}

bool isfull(){

if (top == size - 1){

return true;

else

return false;

bool Empty(){

if (top == -1){

return true;

else return false;

Stack(){

top = -1;

void display(){

for (int i = top; i >= 0; i--){

cout << stack[i] << endl;

};

int main(){

Stack obj;

obj.push(1);

obj.push(2);
obj.push(3);

obj.push(4);

obj.display();

obj.pop();

system("pause");

return 0;

Task 2:
code:
#include<iostream>

using namespace std;

const int size = 5;

class Stack{

int data;

Stack *next;

Stack *top;

public:

Stack(){

top = NULL;

void push(int x){

Stack *ptr;

ptr = new Stack;

ptr->data = x;

ptr->next = NULL;

if (top != NULL){

ptr->next = top;
top = ptr;

else

top = ptr;

ptr->next = NULL;

void pop(){

Stack *temp;

if (top == NULL){

cout << "Stack is Empty\n";

return;

else

temp = top;

top = top->next;

delete temp;

void display()

Stack *ptr1 = top;

while (ptr1 != NULL){

cout << ptr1->data;

ptr1 = ptr1->next;

cout << endl;

};
int main(){

Stack obj;

obj.pop();

obj.push(4);

obj.display();

obj.push(3);

obj.display();

system("pause");

return 0;

Task 3:
code:
#include<iostream>

using namespace std;

const int size = 5;

struct stack1{

int stk1[size];

int top=-1;

};

int pop(){

bool isempty(){

if (top == -1)

return true;

else {
return false;

if (isempty()){

cout << "empty stack:";

return;

else

return stk1[top--];

struct stack2{

int stk2[size];

int top = -1;

};

int push(pop())

bool isfull(){

if (top == size - 1)

return true;

else {

return false;

if (isfull())

{
cout << "full stack:";

else

return stk2[++top];

int main(){

pop();

push(2);

system("pause");

return 0;

Task 4:
code:
#include <iostream>

using namespace std;

struct stack{

int top;

};

void push(int number)

while (number != 0)

push(number % 10);

number = number / 10;


}

int reverse(int number)

push(number);

int reverse = 0;

int i = 1;

while (!empty())

reverse = reverse + (top() * i);

pop();

i = i * 10;

return reverse;

int main()

int number;

cout << "enter numbers:";

cin >> number;

cout << reverse(number);

system("pause");

return 0;

You might also like