0% found this document useful (0 votes)
20 views18 pages

STACK

programming

Uploaded by

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

STACK

programming

Uploaded by

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

STACK

PSEUDO CODE(ARRAY)
TOP IS FIXED
void push(int A[], int m, int top) {
if (is Full() {
cout << "Stack is full";
} else {
int item;
cout << "Enter a value: ";
cin >> item;
A[top] = item;
m = m + 1; }}
int pop(int A[], int m, int top) {
if (is Empty()) {
cout << "Stack is empty";
return -1; // Return error code
} else {
int temp = A[top];
m = m - 1; // Update size
return temp;
}
}
TOP IS NOT FIXED
void push( int A [], int m,int top){
If (is full,()){
cout <<" stack is full";}
Else {
int item;
cout « Enter a value;
cin > item;
top = top + 1;
A[top] = item;
m= m + 1; } }
Int pop ( int A [], int m,int top){
Int main ()
If (is empty()){
cout <<" stack is empty";
Return -1
}
Else {
int temp;
Temp=A[top]
top = top - 1;
m= m - 1;
Return temp;}}
TWO WAY STACK:
void pushLeft(int A[], int &topLeft, int &topRight, int maxSize) {
if (isFull(topLeft, topRight, maxSize)) {
cout << "Stack is full" << endl;
} else {
int item;
cout << "Enter a value to push from left: ";
cin >> item;
topLeft = topLeft - 1;
A[topLeft] = item;
}
}
void pushRight(int A[], int &topLeft, int &topRight, int maxSize) {
if (isFull(topLeft, topRight, maxSize)) {
cout << "Stack is full" << endl;
} else {
int item;
cout << "Enter a value to push from right: ";
cin >> item;
topRight = topRight + 1;
A[topRight] = item;
}
}
int popLeft(int A[], int &topLeft, int &topRight) {
if (isEmpty(topLeft, topRight)) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = A[topLeft];
topLeft = topLeft + 1;
return temp;
}
}
int popRight(int A[], int &topLeft, int &topRight) {
if (isEmpty(topLeft, topRight)) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = A[topRight];
topRight = topRight - 1;
return temp;
}
}

IMPLEMENTATION
TOP IS FIXED
#include <iostream>
using namespace std;
bool isFull(int m, int maxSize) {
return m >= maxSize;
}
bool isEmpty(int m) {
return m == 0;}
void push(int A[], int maxSize, int top, int &m) {
if (isFull(m, maxSize)) {
cout << "Stack is full" << endl;
} else {
int item;
cout << "Enter a value: ";
cin >> item;
A[top] = item;
m = m + 1;
}
}
int pop(int A[], int &m, int top) {
if (isEmpty(m)) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = A[top];
m = m - 1;
return temp;
}
}

int main() {
const int maxSize = 5;
int A[maxSize];
int m = 0;
int top = 2;
push(A, maxSize, top, m);
cout << "Popped value: " << pop(A, m, top) << endl;
return 0;
}
TOP IS NOT FIXED
#include <iostream>
using namespace std;
bool isFull(int top, int maxSize) {
return top >= maxSize - 1;
}
bool isEmpty(int top) {
return top < 0;
}

void push(int A[], int &top, int &m, int maxSize) {


if (isFull(top, maxSize)) {
cout << "Stack is full" << endl;
} else {
int item;
cout << "Enter a value: ";
cin >> item;
top = top + 1;
A[top] = item;
m = m + 1;
}
}
int pop(int A[], int &top, int &m) {
if (isEmpty(top)) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = A[top];
top = top - 1;
m = m - 1;
return temp;
}
}

int main() {
const int maxSize = 5;
int A[maxSize];
int top = -1;
int m = 0;
push(A, top, m, maxSize);
push(A, top, m, maxSize);
cout << "Popped value: " << pop(A, top, m) << endl;
cout << "Popped value: " << pop(A, top, m) << endl;
return 0;
}
TWO WAY STACK:
#include <iostream>
using namespace std;
bool isFull(int topLeft, int topRight, int maxSize) {
return topLeft > topRight;
}
bool isEmpty(int topLeft, int topRight) {
return topLeft > topRight;
}
void pushLeft(int A[], int &topLeft, int &topRight, int maxSize) {
if (isFull(topLeft, topRight, maxSize)) {
cout << "Stack is full" << endl;
} else {
int item;
cout << "Enter a value to push from left: ";
cin >> item;
topLeft = topLeft - 1;
A[topLeft] = item;
}
}
void pushRight(int A[], int &topLeft, int &topRight, int maxSize) {
if (isFull(topLeft, topRight, maxSize)) {
cout << "Stack is full" << endl;
} else {
int item;
cout << "Enter a value to push from right: ";
cin >> item;
topRight = topRight + 1;
A[topRight] = item;
}
}
int popLeft(int A[], int &topLeft, int &topRight) {
if (isEmpty(topLeft, topRight)) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = A[topLeft];
topLeft = topLeft + 1;
return temp;
}
}
int popRight(int A[], int &topLeft, int &topRight) {
if (isEmpty(topLeft, topRight)) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = A[topRight];
topRight = topRight - 1;
return temp;
}
}
int main() {
const int maxSize = 10;
int A[maxSize];
int topLeft = maxSize / 2;
int topRight = (maxSize / 2) - 1;
pushLeft(A, topLeft, topRight, maxSize);
pushRight(A, topLeft, topRight, maxSize);
cout << "Popped from left: " << popLeft(A, topLeft, topRight) << endl;
cout << "Popped from right: " << popRight(A, topLeft, topRight) << endl;
return 0;
}
PSEUDO CODE(LINKED LIST)
TOP IS FIXED
struct Node {
int value;
Node* next;
};
void push(Node*& fixedTop, int value) {
if (fixedTop == nullptr) {
fixedTop = new Node{value, nullptr};
} else {
cout << "Stack is full (Top is fixed)" << endl;
}
}

int pop(Node*& fixedTop) {


if (fixedTop == nullptr) {
cout << "Stack is empty" << endl;
return -1;
} else {
return fixedTop->value;
}
}
TOP IS NOT FIXED
struct Node {
int value;
Node* next;
};
void push(Node*& head, int value) {
Node* newNode = new Node{value, head};
head = newNode;
}
int pop(Node*& head) {
if (head == nullptr) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = head->value;
Node* toDelete = head;
head = head->next;
delete toDelete;
return temp;
}
}
TWO WAY STACK
void pushLeft(Node*& headLeft, int value) {
Node* newNode = new Node{value, headLeft};
headLeft = newNode;
}
void pushRight(Node*& headRight, int value) {
Node* newNode = new Node{value, headRight};
headRight = newNode;
}
int popLeft(Node*& headLeft) {
if (headLeft == nullptr) {
cout << "Left stack is empty" << endl;
return -1;
} else {
int temp = headLeft->value;
Node* toDelete = headLeft;
headLeft = headLeft->next;
delete toDelete;
return temp;
}}
int popRight(Node*& headRight) {
if (headRight == nullptr) {
cout << "Right stack is empty" << endl;
return -1;
} else {
int temp = headRight->value;
Node* toDelete = headRight;
headRight = headRight->next;
delete toDelete;
return temp;
}
}
IMPLEMENTATION
#include <iostream>
using namespace std;
struct Node {
int value;
Node* next;
};
void push(Node*& fixedTop, int value) {
if (fixedTop == nullptr) {
fixedTop = new Node{value, nullptr};
} else {
cout << "Stack is full (Top is fixed)" << endl;
}
}
int pop(Node*& fixedTop) {
if (fixedTop == nullptr) {
cout << "Stack is empty" << endl;
return -1;
} else {
return fixedTop->value;
}
}
int main() {
Node* fixedTop = nullptr;
push(fixedTop, 10);
push(fixedTop, 20); // This will fail because top is fixed.
cout << "Popped value: " << pop(fixedTop) << endl;
return 0;
}
TOP IS NOT FIXED
#include <iostream>
using namespace std;
struct Node {
int value;
Node* next;
};
void push(Node*& head, int value) {
Node* newNode = new Node{value, head};
head = newNode;
}
int pop(Node*& head) {
if (head == nullptr) {
cout << "Stack is empty" << endl;
return -1;
} else {
int temp = head->value;
Node* toDelete = head;
head = head->next;
delete toDelete;
return temp;
}
}
int main() {
Node* head = nullptr;
push(head, 10);
push(head, 20);
cout << "Popped value: " << pop(head) << endl;
cout << "Popped value: " << pop(head) << endl;
cout << "Popped value: " << pop(head) << endl; // Empty stack
return 0;
}
TWO WAY STACK
#include <iostream>
using namespace std;
struct Node {
int value;
Node* next;
};
void pushLeft(Node*& headLeft, int value) {
Node* newNode = new Node{value, headLeft};
headLeft = newNode;
}

void pushRight(Node*& headRight, int value) {


Node* newNode = new Node{value, headRight};
headRight = newNode;
}
int popLeft(Node*& headLeft) {
if (headLeft == nullptr) {
cout << "Left stack is empty" << endl;
return -1;
} else {
int temp = headLeft->value;
Node* toDelete = headLeft;
headLeft = headLeft->next;
delete toDelete;
return temp;
}
}
int popRight(Node*& headRight) {
if (headRight == nullptr) {
cout << "Right stack is empty" << endl;
return -1;
} else {
int temp = headRight->value;
Node* toDelete = headRight;
headRight = headRight->next;
delete toDelete;
return temp;
}
}
int main() {
Node* headLeft = nullptr;
Node* headRight = nullptr;
pushLeft(headLeft, 10);
pushRight(headRight, 20);
cout << "Popped from left: " << popLeft(headLeft) << endl;
cout << "Popped from right: " << popRight(headRight) << endl;
return 0;
}

You might also like