I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question.
class Stack {
private:
// Desc: Nodes for a singly-linked list
class StackNode {
public:
int data;
StackNode * next;
};
// Desc: head = ptr to the first StackNode (NULL if empty)
// tail = ptr to the last StackNode (NULL if empty)
StackNode * head;
StackNode * tail; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!please tell me
how to use this
public:
// Desc: Constructor
// Post: Stack is empty
Stack();
// Desc: Destructor
// Post: deallocate memory and do other cleanup for the class
object and its class members when the object is destroyed
~Stack();
// Desc: Insert element x to the top of the stack.
// Post: add items at the tail of the linked list
void push(int x);
// Desc: Remove and return element at the top of the stack.
// Pre: stack is not empty
// Post: remove items from the tail of the list
int pop();
// Desc: Return the topmost element of the stack.
// Pre: stack is not empty
// Post:
int peek() const;
// Desc: describe if the stack is empty
// Post: return true if empty
bool isEmpty() const;
};
/*Referring to the proposed implementation , could you please analyze the total running time required to push n items to the Stack. Next, analyze the total running time required to pop those n items from the Stack. A detailed analysis is expected, it will be helpful to my learning,:)*/
ans............................................
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void show();
};
void stack::push()
{
int value;
struct node *ptr;
cout<<" PUSH Operationn";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<" New item is inserted to the stack!!!";
}
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<" The stack is empty!!!";
}
temp=top;
top=top->next;
cout<<" POP Operation........ Poped value is
"<<temp->data;
delete temp;
}
void stack::show()
{
struct node *ptr1=top;
cout<<" The stack is ";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL ";
}
int main()
{
stack s;
int choice;
while(1)
{
cout<<"
-----------------------------------------------------------";
cout<<" STACK USING LINKED LIST ";
cout<<"1:PUSH 2:POP 3:DISPLAY STACK 4:EXIT";
cout<<" Enter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
return 0;
break;
default:
cout<<" Please enter correct choice(1-4)!!";
break;
}
}
return 0;
}
I need help with a c++ code, i am new learner on stack, please help me...
Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...
Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...
template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...
can some help me why it is erroring. I cannot find the mistake in the code. It is to implment a circular double linked list. please and thank you ////////////////////////////////////////////////////////////////////////// HEADER FILE //////////////////////////////////////////////////////////////////////// #pragma once #include <cstdlib> template <typename T> class Node { public: T val; Node<T> *next; Node(T v) { val = v; next = NULL; } }; /////////////////////////////////////////////////////////////////////////////////// STACK.CPP FILE /////////////////////////////////////////////////////////////////////////////////// #include "Stack.h" template <typename T> class Stack {...
// thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...
My question is pretty simple, I just want to know how to call my operator== function in Stack.cpp using a list function. Here is what I meant. This is my Stack.h file: class Stack { public: /**constructors and destructors*/ Stack(); Stack(const Stack &S); ~Stack(); void pop(); void push(int data); bool operator==(const Stack &S); [ .......] private: List<int> stack; }; Here is my List.h file: template<class listitem> class List { private: struct...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
// Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...
Please Answer this question using the language of C++.
I provide you with the picture of figure 18_02. Thank you.
I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include 7 template 8 class Stack ( 9 public: 10 I const T& top) 12 13 l/ return the top element of the Stack return stack.frontO; // push an element onto the Stack void push(const T& pushValue) 15 stack push front (pushValue); 17...