Task 2: Implement stack with the help of single linked list. Make sure you follow LIFO pattern and implement push, pop, isempty, isfull and status functions in it. A maximum size of stack needs to be specified in order to implement isfull function.
You are required to submit 3 files, myfriends.h, myfriends.cpp, main.cpp
The code for the above program is given below, i have commented in line for the explanation.
myfriends.cpp
#include <iostream>
using namespace std;
int count=0;
int Max;
struct Node // struct for a node in linked list
{
int data;
struct Node* next;
};
struct Node* ptr=NULL;// pointer to the top of stack
void setSize(int n) // to set maximum size of the
stack
{
Max=n;
}
bool isEmpty()// to check if stack is empty
{
if(ptr == NULL)// if pointer NULL it means the stack is empty
{
return true;
}
return false;
}
void status()
{
struct Node* temp; // temp pointer to traverse the stack
if(isEmpty()) // is stack is empty return empty
{
cout<<"\nThe stack is Empty!\n";
exit(1);
}
else{
temp = ptr; // start with the top of stack
while(temp!=NULL && count!=0)
{
cout<<temp->data<<" --> ";// print the data
temp= temp->next;// move down the stack
}
cout<<"NULL"<<endl;
}
}
bool isFull(int Max)
{
if(count==Max)// if count is equal to maximum size return
true
{
return true;
}
return false;
}
void push(int data)// to push the element in stack
{
struct Node* temp = new Node();// create a new node
if(!isFull(Max))// if stack is not already full
{
temp->data = data;// add data to new node
temp->next = ptr;// next port of this node will point to p of
the stack
ptr = temp;// top will change to new node
count++;// increment the counter to keep track of total node
}
else{// if stack is already full cant add
cout<<"Cant add stack is already full!"<<endl;
cout<<"Status till Know:\n";
status();// print the status till know
exit(1);
}
}
void pop()// to pop the element from the stack
{
struct Node* temp;// pointer to point to removed node to delete
it
if(isEmpty())// if stack is empty cant delete
{
cout<<"\nThe stack is Empty!\n";
exit(1);
}
else{
temp = ptr;// point temp pointer to the top of stack
ptr = ptr->next; // change top pointer to next element of
stack
temp->next = NULL;// delete the next part of the removed
node
free(temp);// free the space of removed node
count--;// decrement counter
}
}
--------------------------------------
myfriends.h
void push(int data);
bool isEmpty();
bool isFull(int size);
void pop();
void status();
struct Node;
void setSize(int n);
-------------------------------------------
main.cpp
#include <iostream>
#include "myfriends.h"
using namespace std;
int main()
{
int n;
cout<<"Maximum size of stack you want?";
cin>>n;
setSize(n);// set size of the stack
push(1);
push(2);
push(3);
push(4);
status();
pop();
pop();
cout<<"After 2 pop are called\n";
status();
return 0;
}
Output:-

Hope it clears your doubt :) and you like it.
Task 2: Implement stack with the help of single linked list. Make sure you follow LIFO...
How do you implement a stack using a singly linked list in Java? Can you make it a simple implementation with just push and pop methods.
Using C
Please comment
» Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...
You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...
ANSWER IN JAVA ASAP 1.Implement a stack on the singly linked list with the operations of Lab Assignment 1. Hint: Using the same Stack class you implemented, change the array to an object of the singly linked list class. The functionality of push and pop is now based on the methods of the linked list class.
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...
I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array. //Array implementation of stacks. import java.util.Arrays; public class ArrayStack implements Stack { //Declare a class constant called DEFAULT_STACK_SIZE with the value 10. private static final int DEFAULT_STACK_SIZE = 10; /* Declare two instance variables: 1. An integer called...
Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...
C++
In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a "next" pointer. Textbook Туре String String String Attribute title author ISBN Textbook* next Library class should have a head node as an attribute to keep the list of the books. Also, following member...
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...
Using Java
You are given a
Node class and a
List class:
public class
Node
{
int data;
Node
next;
}
public class
List
{
Node
first;
}
You are also given a Stack class.
The following functions are available for use:
public class Stack
{
public boolean
isEmpty(){};
public void push(int
n){};
public int
pop(){};}
Write a Java method
snglyRevToStck that pushes the data found
in a linked list t in reverse order into the stack...