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...
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...
Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...
I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...
I need help with this code This is what I need to do: 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...