Implement a stack using a linked list
Example 1:
Push(1): true: isEmpty(): false: Peek() : 1: Size() : 1
Peek(): 1: isEmpty(): false: Pop() : 1: Size() : 0
Underflow!! Underflow!! Peek(): -2147483648: isEmpty(): true: Pop() : -2147483648: Size() : 0
Push(1): true: isEmpty(): false: Peek() : 1: Size() : 1
Push(2): true: isEmpty(): false: Peek() : 2: Size() : 2
Push(3): true: isEmpty(): false: Peek() : 3: Size() : 3
Overflow!! Push(4): false: isEmpty(): false: Peek() : 3: Size() : 3
Approach:
Java
public class StackImlList {public static void main(String[] args) {// create stack with sizeStackList s = new StackList (3);// push 1 elementSystem.out.println("Push(1): " + s.push(1) +": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// pop elementSystem.out.println("Peek(): " + s.peek()+ ": isEmpty(): " + s.isEmpty() + ": Pop() : " + s.pop()+ ": Size() : " + s.size());// pop elementSystem.out.println("Peek(): " + s.peek()+ ": isEmpty(): " + s.isEmpty() + ": Pop() : " + s.pop()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(1): " + s.push(1)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(2): " + s.push(2)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(3): " + s.push(3)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(4): " + s.push(4)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());}}class StackList {private int DEFAULT_CAPACITY = 10;private ListNode list = null;private int TOP = -1;public StackList(int maxSize) {this.DEFAULT_CAPACITY = maxSize;this.TOP = -1;}public StackList() {this.TOP = -1;}boolean push(int number) {if (!isFull()) {TOP++;if (TOP == 0)list = new ListNode(number);elselist.next = new ListNode(number);return true;} else {System.out.print("Overflow!! ");return false;}}int pop() {if (!this.isEmpty()) {TOP--;return popAndDeletionAtEnd();} else {System.out.print("Underflow!! ");return -1;}}private int popAndDeletionAtEnd() {int val = 0;ListNode tmp = list;// If list is blank or single nodeif (list == null) {return -1;}if (list.next == null) {val = list.val;list = null;return val;}val = tmp.next.val;// Iterate till tmp.next->next is nullwhile (tmp.next.next != null) {val = tmp.val;tmp = tmp.next;}tmp.next = null;return val;}private int popAtEnd() {int val = 0;ListNode tmp = list;// If list is blank or single nodeif (list == null) {return -1;}// Iterate till tmp is nullListNode prev = null;while (tmp != null) {prev = tmp;tmp = tmp.next;}val = prev.val;return val;}int peek() {if (!this.isEmpty()) {return popAtEnd();} else {System.out.print("Underflow!! ");return -1;}}public int size() {return TOP + 1;}boolean isEmpty() {return TOP == -1;}boolean isFull() {return TOP == DEFAULT_CAPACITY - 1;}}class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}
C++
#include <bits/stdc++.h>using namespace std;class ListNode {public:int val;ListNode* next;ListNode() {}ListNode(int val) {this->val = val;this->next=NULL;}ListNode(int val, ListNode *next) {this->val = val;this->next = next;}};class StackList {int DEFAULT_CAPACITY = 10;ListNode* list = NULL;int TOP = -1;public:StackList(int maxSize) {this->DEFAULT_CAPACITY = maxSize;this->TOP = -1;}StackList() {this->TOP = -1;}void push(int number) {if (!isFull()) {TOP++;if (TOP == 0)list = new ListNode(number);else{list->next = new ListNode(number);}} else {cout<<"Overflow!! ";}}void popAndDeletionAtEnd() {int val = 0;ListNode *tmp = list;// If list is blank or single nodeif (list == nullptr) {return ;}if (list->next == nullptr) {val = list->val;list = nullptr;return ;}val = tmp->next->val;// Iterate till tmp.next->next is nullwhile (tmp->next->next != nullptr) {val = tmp->val;tmp = tmp->next;}tmp->next = nullptr;}void pop() {if (!this->isEmpty()) {TOP--;popAndDeletionAtEnd();} else {cout<<"Underflow!! ";}}int popAtEnd() {int val = 0;ListNode *tmp = list;// If list is blank or single nodeif (list ==NULL) {return -1;}// Iterate till tmp is nullListNode *prev = NULL;while (tmp !=NULL) {prev = tmp;tmp = tmp->next;}val = prev->val;return val;}int peek() {if (!this->isEmpty()) {return popAtEnd();} else {cout<<"Underflow!! ";return -1;}}int size() {return TOP + 1;}bool isEmpty() {return TOP == -1;}bool isFull() {return TOP == DEFAULT_CAPACITY - 1;}};int main(){// create stack with sizeStackList s(3);// push 1 elementcout<<"Push(1): ";s.push(1);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// pop elementcout<<"Peek(): "<<s.peek();cout<<": isEmpty(): "<<s.isEmpty();cout<<": Pop() : ";s.pop();cout<<": Size() : "<<s.size()<<"\n";// pop elementcout<<" Peek(): "<<s.peek();cout<<": isEmpty(): "<<s.isEmpty();cout<<": Pop() : ";s.pop();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<" Push(1): ";s.push(1);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<"Push(2): ";s.push(2);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<"Push(3): ";s.push(3);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<" Push(4): ";s.push(4);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";return 0;}
No comments:
Post a Comment