Question

In: Computer Science

C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...

C++ coding functions

Implement the following class using linked lists. Creating a simple linked list class to use is probably your
first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation

A templated stack class that stores type T with the following public functions:
- void Push(T t) - adds t to the top of the stack
- T Pop() - asserts that the stack is not empty then removes and returns the item at the top of the stack.
- T Peek() - asserts that the queue is not empty then returns the item at the top of the stack without removing it.

-  unsigned int Size() - returns the number of items currently in the stack

Solutions

Expert Solution

#include<iostream>
using namespace std;

int c=0;

template<class T>

class node
{
public:
T data;
node<T>* next;
};

template <class T>
class Stack
{
private:
node<T>* top;
public:
Stack() {
top = NULL;
}
void push(const T & val) {
if (top == NULL) {
top = new node<T>;
top->next = NULL;
top->data = val;
}
else {
node<T>* temp = new node<T>;
temp->data = val;
temp->next = top;
top = temp;
}c++;
}
void pop()
{
if (top == NULL) {
cout << "Stack is empty" << endl;
return;
}
else {
cout << top->data << " is popped" << endl;
node<T>* temp = top;
top = top->next;
delete temp;
}
c--;
}
void print()
{
node<T>* temp = top;
if (top == NULL)
{
cout << "Stack is empty" << endl;
return;
}
else
{
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
}

int getsize()
{
return c;
}
};

int main()
{
Stack<int> s;
int choice;

while (1)
{
cout << "\n1. Push 2. Pop 3. Print 4.getsize 5. Quit" << endl;
cout<<" \nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nEnter a number to push in Stack?";
int n;
cin >> n;
s.push(n);
break;
case 2:
s.pop();
break;
case 3:
s.print();
break;
case 4:cout<<"size="<<s.getsize();
break;
case 5:
cout << "Quitting program........." << endl;
return 1;
default:
cout << "Invalid choice!!" << endl;
break;
}
}
return 0;
}

output


Related Solutions

C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two...
C++ Using an appropriate definition of ListNode, design a simple linked list class with only two member functions and a default constructor: void add(double x); boolean isMember(double x); LinkedList( ); The add function adds a new node containing x to the front (head) of the list, while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership....
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
C++ Need to add the following functions to my templatized class linked list. (main is already...
C++ Need to add the following functions to my templatized class linked list. (main is already set to accommodate the functions below) --void destroy_list () deletes each node in the list, and resets the header to nullptr --bool search_list (key value) searches the list for a node with the given key. Returns true if found, false if not. --bool delete_node (key value) deletes the node which contains the given key. If there is more than one node with the same...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
C coding • Implement, using structures and functions as appropriate, a program which requires you to...
C coding • Implement, using structures and functions as appropriate, a program which requires you to enter a number of points in 3 dimensions. The points will have a name (one alphanumeric character) and three coordinates x, y, and z. Find and implement a suitable way to stop the input loop. The program, through an appropriate distance function, should identify the two points which are the furthest apart. Another function should calculate the centre of gravity of the point cloud...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT