Stacks
There are two main operations associated with stacks;
1) putting things on the stack which is referred to as push,
2) taking things from the stack which is referred to as pop.
We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of the word one-by-one and as you read them will push them onto a stack. Once all letters are pushed onto the stack, then pop them back one-by-one. This will produce the letters of the word in reverse order.
Give the definition of the member function push of the class Stack.
Given the definition of the copy constructor for the class Stack.
Write a program that implements a stack. Your program will ask users to input a word letter-by-letter and then displays the word backward. Please note that you are working with letters to build the stack, thus when you read the word, you will push the letters onto the stack and when you write them, you will pop those letters one-by-one.
C++ Program:
/* C++ Program that implements Stack using Linked List */
#include <iostream>
using namespace std;
//class definition
class Node
{
private:
//Data variables
char data;
Node *head;
Node *next;
public:
//Constructor
Node()
{
data = 0;
next = NULL;
head = NULL;
}
//Push
operation
void push(char ch)
{
Node *temp, *current;
//If list is empty
if(head == NULL)
{
//Creating a new node and naming it as head
head = new Node();
head->data = ch;
head->next = NULL;
}
else
{
temp = head;
//Moving till end of list
while(temp->next!=NULL)
temp = temp->next;
//Adding new node at the end of the list
current = new Node();
current->data = ch;
current->next = NULL;
temp->next = current;
}
}
//Function that
removes an element from list
char pop()
{
char popped;
Node *temp, *prev;
temp = head;
//Only one element
if(temp == NULL)
{
//Updating pointers
head = NULL;
delete head;
}
//More than one node
else
{
//Iterate till end of list
while(temp->next != NULL)
{
//Holding reference of previous node
prev = temp;
//Moving to next node
temp = temp->next;
}
//Data that is going to be popped
popped = temp->data;
//Updating pointers
prev->next = NULL;
//Deleting node
if(temp != NULL)
delete temp;
}
return popped;
}
};
//Main function
int main()
{
char ch = ' ';
int cnt = 0;
Node stackObj;
//Prompting user
cout << "\n Enter word character by
character (Type : to mark end of string): ";
//Reading character
cin >> ch;
//Reading character by character
while(ch != ':')
{
//Pushing elements to
stack
stackObj.push(ch);
cnt++;
//Reading
character
cin >> ch;
}
cout << "\n In reverse order: ";
//Popping elements from stack
while(cnt > 0)
{
//Printing popped
element
cout <<
stackObj.pop();
//Decrementing
count
cnt --;
}
cout << endl << endl;
return 0;
}
__________________________________________________________________________________________________
Sample Output:
Stacks There are two main operations associated with stacks; 1) putting things on the stack which...
ae two stacks, a number stack and an operator stack, to evaluate the tollowing algebraic expression (6+2*8/(16-4*3))/2 Read the items in the expression one at a time from left to right. Show the number stack and the operator stack after each item in the expression has been read and processed. If processing an item involves evaluating the top, show the two stacks af and then show the two stacks again after a "(” , if any, is popped off or...
ii. (30) A Stack is a container commonly used to provide a way to keep organized so that the last one pushed' onto the top of the stack is thth one 'popped (removed)- so stacks are called "last-in-first-out". on top of the stack is visible, but after removing the top item, t pushed item is visible, since it is back on top. The following Python co is a recursive definition of the class Stack. Read the code, and answer te...
Define a class DoubleStack which implements two stacks of objects of type Object using a single shared array, so that the push and pop operations specify which of the two stacks is involved in the operation (as described below). (a) Specify necessary instance variables and write a constructor “DoubleStack(int n)” that takes integer n and creates an empty DoubleStack object with the shared array with room for n elements (2 pt); b) Write methods "boolean push(int i, Object o)" and...
Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...
Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...
Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...
Suppose we execute the following stack operations on a stack of ints. push(1); pop(); // #1 push(10); pop(); // #2 push(7); push(4); push(3); pop(); // #3 push(5); pop(); //#4 Write the final state of the stack, and for each pop() operation, write the value that will be popped off the stack (pops are numbered so you can refer to them).
Backtracking is a computing algorithm using stack to “remember” user-generated events when using a program. A user event may be “pressing the Enter key on keyboard” or “clicking a mouse button”. Stack is a data structure with the Last-In-First-Out property (LIFO). If we push the aforesaid user events into a stack, a computer program using that data structure can “rewind” user events by popping them out of stack one at a time. This backtracking feature is available as Edit ->...
For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks and wanted to but merge them into one final sorted stack! what is the time complexity then? code / Java program to merge to unsorted stacks // into a third stack in sorted way. import java.io.*; import java.util.*; public class GFG { // This is the temporary stack static Stack<Integer> res = new Stack<Integer>(); static Stack<Integer> tmpStack = new Stack<Integer>(); //...
Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...