Question

Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

Convert following code to implement linked list C++ language

#include<iostream>
using namespace std;
int top = -1; //globally defining the value of top, as the stack is empty

void push(int stack[], int x, int n)
{
if (top == -1) //if top position is the last of posiition of stack,means stack is full
{
cout << "Stack is full Overflow condition";
}
else
{
top = top + 1; //incrementing top position
stack[top] = x; //inserting element on incremented position
}
}
bool isEmpty()
{
if (top == -1) //stack is empty
return true;
else
return false;
}
void pop(int stack[], int n)
{
if (isEmpty())
{
cout << "Stack is empty. Underflow condition" << endl;
}
else
{
top = top - 1; //decrementing tops position will detach last element from stack
}
}
int size()
{
return top + 1;
}
int topElement()
{
return stack[top];
}
//Now lets implementing these functions on the above stack
int main()
{
int stack[3];
push(stack,5, 3); //pushing element 5 in the stack

cout << "Current size of stack is " << size() << endl;

push(stack, 10, 3);
push(stack, 24, 3);

cout << "Current size of stack is " << size() << endl;

//As now the stack is full, further pushing will show overflow condition
push(stack, 12, 3);

//Accesing the top element
cout << "The current top element in stack is" << topElement() << endl;

//now removing all the elements from stack
for (int i = 0; i < 3; i++)
pop();
cout << "Current size of stack is " << size() << endl;

//as stack is empty, now further popping will show underflow condition
pop();


}


switch from stack to linked list
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

/*
*   C++ program to implement a stack using a linked list
*/
#include <iostream>
using namespace std;
// Node of the linked list
struct node{
    int data;
    node *next;
};
//Function to push
node* push(node *top, int num){
    node *newNode = new node;
    newNode->data = num;
    if(top == NULL)
        newNode->next = NULL;
    else
        newNode->next = top;
    top = newNode;
    return top;
}
//Function to check if linked list is empty
bool isEmpty(node *top){
    if(top == NULL)
        return true;
    return false;
}
//Function to pop
node* pop(node *top){
    if(isEmpty(top)){
        cout << "Stack is empty. UnderFlow condition";
        return top;
    }
    struct node *ptr = top;
    top = ptr->next;
    delete ptr;
    return top;
}
// Function to return size of stack
int size(node *top){
    int count = 0;
    for(node *ptr = top; ptr != NULL; ptr = ptr->next)
        count++;
    return count;
}
// Function to return element at top of the stack
int topElement(node *top){
    // If stack is empty then return -1
    if(isEmpty(top))
        return -1;
    return top->data;
}
int main() {
    node *stack = NULL;
    stack = push(stack, 5); //pushing element 5 in the stack
    cout << "Current size of stack is " << size(stack) << endl;
    stack = push(stack, 10);
    stack = push(stack, 24);
    cout << "Current size of stack is " << size(stack) << endl;
    stack = push(stack, 12);
    //Accesing the top element
    cout << "The current top element in stack is " << topElement(stack) << endl;
    //now removing all the elements from stack
    for (int i = 0; i < 4; i++)
        stack = pop(stack);
    cout << "Current size of stack is " << size(stack) << endl;
    //as stack is empty, now further popping will show underflow condition
    stack = pop(stack);
    return 0;
}

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...

    //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what) : what(what) {} string getWhat() {return what;} private: string what; }; #endif //stack_test_app.cpp #include <iostream> #include <sstream> #include <string> #include "stack.h" using namespace std; /********************************************* * The 'contains' function template goes here * *********************************************/ int main() {    cout << boolalpha;    cout << "--- stack of int" << endl;    Stack<int> si;    cout << "si intially " << si << endl;   ...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • //Look for** to complete this program --C+ please --please add comments // using namespace std; #include...

    //Look for** to complete this program --C+ please --please add comments // using namespace std; #include <iostream> #include <stdlib.h> #include < string> #include "queue.h" //Purpose of the program: ** Algorithm: * int main() /** "A", "B", "C" in the queue //** while loop -- indefinitely { try {//** catches } //* end of loop } |/ II II II II II //File type: ** queue.cpp using namespace std; #include <iostream> #include "queue.h" // constructor queue::queue () } //destructor queue::~queue() queue::queue...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...

    USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class StaticStack { private: int *stack; int capacity; int top; // index of the top element public: StaticStack(int size); // constructor ~StaticStack(); bool isFull(); bool isEmpty(); void push(int); int pop(); }; #include "StaticStack.h" #include <iostream> using namespace std; StaticStack::StaticStack(int size) { stack = new int[size]; // constructing a size sized array capacity = size; top = -1; // empty stack    } StaticStack::~StaticStack() { delete...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT