Question

You are now going to create a LinkedList class, that will work very similarly to the...

You are now going to create a LinkedList class, that will work very similarly to the Stack class seen in the book (and used in the previous exercise). Then write new methods as follows:

add ( LinkedList::Link* l, int n ): will insert in the linked list, after link l, a chain of n new links. Each link will store an integer that will be set to receive, in order, a value starting from 0 until n-1. In the last link of the list always set the next pointer to be NULL in order to mark the end of the list.

print (): this method moves through the entire list printing out each integer value stored in the links. cleanup (): updated cleanup function that will automatically delete all links in the linked list, including the data stored on each link

Stack Class used:

#ifndef STACK_H
#define STACK_H

#include <iostream>

struct Stack {
   struct Link {
       void* data;
       Link* next;
      
       void initialize(void* dat, Link* nxt){
           data = dat;
           next = nxt;
       }
   }* head;
  
   void initialize(){
       head = 0;
   }
  
   void push(void* dat){
       Link* newLink = new Link;
       newLink->initialize(dat, head);
       head = newLink;
   }
  
   void* peek(){
       if (head == 0){
           std::cout << "Stack is empty";
       }
       return head->data;
   }
  
   void* pop(){
       if(head == 0)
           return 0;
      
       void* result = head->data;
       Link* oldHead = head;
       head = head->next;
       delete oldHead;
       return result;
   }
  
   void cleanup(){
       if (head == 0){
           std::cout << "Stack is empty";
       }
       else {
           std::cout << "Stack is not empty";
       }
   }
};
#endif

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
You are now going to create a LinkedList class, that will work very similarly to the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • All the functions are written and work besides removeLinkedList and contains LinkedList. Please fill those out. 무#ifndef #define LINKEDLIST-H LINKEDLISTH define TYPE /*# define TYPE SIZE sizeof (TYP...

    All the functions are written and work besides removeLinkedList and contains LinkedList. Please fill those out. 무#ifndef #define LINKEDLIST-H LINKEDLISTH define TYPE /*# define TYPE SIZE sizeof (TYPE) */ double typedef struct ListStack LinkedList; void initLinkedLǐst(LinkedList *1); void freeLinkedList (LinkedList *1) int İsEmptyLinkedList (LinkedList*1); void pushLinkedList (LinkedList #1, TYPE val); TYPE topLinkedList (LinkedList 1) void popLinkedList (LinkedList *1); /* Bag / void addLinkedList (LinkedList *1, TYPE val) ; int containsLinkedList (LinkedList *1, TYPE val) ; void removeLinkedList (LinkedList l, TYPE...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function 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 member functions to display the list, check if...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // 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...

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
ADVERTISEMENT