< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

  1. Stack
  2. What's Stack & Implementation?

Stack using LL

Stack.h
#include "Singly LL.h"
#pragma once 

template<typename T>class Stack{
    SinglyLL<T> list;
public:
    bool isEmpty();
    bool isFull();
    void Push(const T val);
    bool Pop(T& val);
    T top();
};

template<typename T> T Stack<T> :: top() {
    SinglyLL<T> cur(list);
    T temp;
    cur.removeAtHead(temp);
    return temp;
}

template<typename T>bool Stack<T> :: isEmpty() {
    return list.Empty();
} 

template<typename T>bool Stack<T> :: isFull() {
    return false;
}

template<typename T>void Stack<T> :: Push (const T val) {
    list.insertAtHead(val);   
}

template<typename T>bool Stack<T> :: Pop (T& val) {
    if(isEmpty()) return false;
    
    list.removeAtHead(val);
    return true;
}
Singly LL.h
#include <iostream>
#pragma once
using namespace std;

template<typename T>class Node{
public:
	T data;
	Node* next;
	explicit Node(const T d) : data(d), next(nullptr) {}
};

template<typename T>class SinglyLL{
	Node<T>* head ;
	int elements;
public:
	SinglyLL(): elements(0), head(nullptr) {};

	SinglyLL(const SinglyLL<T>& list);
	
	void insert(const T val);
	
	void insertAtHead(const T val);
	
	bool remove(const T val);
	
	void removeAtHead(T& headData);

    bool Empty() { return elements == 0; }
};

template<typename T>SinglyLL<T> :: SinglyLL(const SinglyLL<T>& list) {
	Node<T>* listHead = list.head;
	Node<T>* newNode = new Node<T>(listHead -> data);
	Node<T>* cur = head = newNode;
	listHead = listHead -> next;

	while (listHead != nullptr) {
		Node<T>* temp = new Node<T>(listHead -> data);
		listHead = listHead -> next;
		cur -> next = temp;
		cur = temp;
	}
}

// Insert At Any Position
template<typename T>void SinglyLL<T> :: insert(const T val) {
	if(elements == 0) {
		Node<T>* newNode = new Node<T>(val);
		head = newNode;
		elements++;
		return;
	}
	
	Node<T>* newNode = new Node<T>(val);
	Node<T>* cur = head;
	
	while (cur -> next != nullptr) {
		cur = cur -> next;
	}
	cur -> next = newNode;
	elements++;
}

// Insert At Head
template<typename T>void SinglyLL<T> :: insertAtHead(const T val) {
	if(elements == 0) {
		Node<T>* newNode = new Node<T>(val);
		head = newNode;
		elements++;
		return;
	}
	
	Node<T>* newNode = new Node<T>(val);
	newNode -> next = head;
	head = newNode;
	elements++;
}

// Remove Head
template<typename T>void SinglyLL<T> :: removeAtHead(T& headData) {
	if(elements == 0) return ;

	if(elements == 1) {
		headData = head -> data;
		delete head;
		elements--;
		return ;
	}

	Node<T>* cur = head;
	headData = cur -> data;
	head = head -> next;
	delete cur;
	elements--;
	return;
}
main.cpp
#include "Stack.h"

int main()
{

    Stack<int> s1;
    s1.Push(1);
    s1.Push(2);
    s1.Push(3);
    s1.Push(4);

    cout << "Stack Top Element: " << s1.top() << endl;

    cout << "Stack Elements: ";
    while (!s1.isEmpty())
    {
        int temp;
        s1.Pop(temp);
        cout << temp << " ";
    }
    cout << endl;
}
PreviousStack Using ArrayNextProblems

Last updated 10 months ago

Was this helpful?

📚