Question

In: Computer Science

In short, you’re going to implement a linked-list class for storing integers, using a provided main...

In short, you’re going to implement a linked-list class for storing integers, using a provided main program to help you interact and test your work. You’ll want to build the linked-list class function by function, working in “Develop” mode to test out each function you write.

main.cpp is a read only file

linkedlist.h is the file to work on.

main.cpp

#include
#include

#include "linkedlist.h"

using namespace std;


int main()
{
linkedlist LL;
string cmd;
int value, key;

//
// user can enter commands to manipulate the LL:
//
// p w => push w onto the end
// i x y => insert x after y (y must exist in the list)
// r z => remove the first instance of z
// o => output the list
// q => quit
//

//cout << "Enter a command> ";
cin >> cmd;

while (cmd != "q")
{
if (cmd == "p")
{
// push:
cin >> value;

LL.push_back(value);
}
else if (cmd == "i")
{
// insert:
cin >> value;
cin >> key;

LL.insert(value, key);
}
else if (cmd == "r")
{
// remove:
cin >> value;

LL.remove(value);
}
else if (cmd == "o")
{
// output:

LL.output();
}
else
{
cout << "**Invalid command, try p, i, r, o or q" << endl;
cout << endl;
}

//cout << "Enter a command> ";
cin >> cmd;
}

return 0;
}

linkedlist.h

/*linkedlist.h*/

#pragma once

#include <iostream>

using namespace std;


class linkedlist
{
private:
struct NODE
{
int Data;
struct NODE* Next;
};

struct NODE* Head; // first node in list (or nullptr)
struct NODE* Tail; // last node in list (or nullptr)
int Size; // # of elements (i.e. nodes) in list

public:
//
// default constructor
//
// Creates an empty list.
//
linkedlist()
{
Head = nullptr;
Tail = nullptr;
Size = 0;
}

//
// size
//
// Returns the # of elements in the list.
//
int size()
{
return Size;
}

//
// push_back
//
// Pushes value onto the end of the list.
//
void push_back(int value)
{
struct NODE* newNode = new struct NODE();
newNode->Data = value;
newNode->Next = nullptr;

//
// TODO: a new node containing the value was created
// above. Add this new node to the end of the list.
//
  
//
// HINT #2: don't forget to increment size
//
}

//
// insert
//
// Inserts the given value in the list *after* the key. If
// the key cannot be found, nothing happens; if the key occurs
// multiple times, value will be inserted after the first
// instance.
//
void insert(int value, int key)
{
// allocate a new node to hold the value:
struct NODE* newNode = new struct NODE();
newNode->Data = value;
newNode->Next = nullptr;

//
// TODO: a new node containing the value was created
// above. Insert this new node after the node containing
// the given key (assume the key appears only once).
// If the key cannot be found (or the list is empty), do
// nothing and just return (or delete newNode and return).
//
t.
//
// HINT #2: don't forget to increment size
//
}

//
// remove
//
// Removes the first instance of value from the list; if
// the value cannot be found, nothing happens.
//
void remove(int value)
{
//
// TODO: remove the first node that contains value; if value
// is not found, do nothing. You'll need to search the list
// for the value, and then unlink that node from the list.
// Don't worry about freeing the memory, you can ignore that
// for now (or use delete ptrToNode; if you want to free).
//

//
// HINT #2: don't forget to decrement size
//
}

//
// output
//
// Outputs the size, followed by the elements one by one on
// the same line.
//
void output()
{
cout << "Size: " << Size << endl;
cout << "Elements: ";

//
// TODO: output elements with a space after each one
// (including a space after last element). Output all
// on the same line, save the end-of-line for after
// the traversal loop is over.
//
.
//

cout << endl;
}

};

Solutions

Expert Solution

Screenshot

Program

linkedlist.h

/*linkedlist.h*/
#pragma once
#include <iostream>
using namespace std;
class linkedlist{
private:
   struct NODE{
       int Data;
       struct NODE* Next;
   };
   struct NODE* Head; // first node in list (or nullptr)
   struct NODE* Tail; // last node in list (or nullptr)
   int Size; // # of elements (i.e. nodes) in list
public:
   //
   // default constructor
   //
   // Creates an empty list.
   //
   linkedlist(){
       Head = nullptr;
       Tail = nullptr;
       Size = 0;
   }
   //
   // size
   //
   // Returns the # of elements in the list.
   //
   int size(){
       return Size;
   }
   //
   // push_back
   //
   // Pushes value onto the end of the list.
   //
   void push_back(int value){
       struct NODE* newNode = new struct NODE();
       newNode->Data = value;
       newNode->Next = nullptr;
       //
       // TODO: a new node containing the value was created
       // above. Add this new node to the end of the list.
       //
       if (Head == nullptr) {
           Head = newNode;
           Tail = newNode;
       }
       else
       {
           Tail->Next = newNode;
           Tail = Tail->Next;
       }
       //
       // HINT #2: don't forget to increment size
       //
       Size++;
   }

   //
   // insert
   //
   // Inserts the given value in the list *after* the key. If
   // the key cannot be found, nothing happens; if the key occurs
   // multiple times, value will be inserted after the first
   // instance.
   //
   void insert(int value, int key)
   {
       // allocate a new node to hold the value:
       struct NODE* newNode = new struct NODE();
       newNode->Data = value;
       newNode->Next = nullptr;

       //
       // TODO: a new node containing the value was created
       // above. Insert this new node after the node containing
       // the given key (assume the key appears only once).
       // If the key cannot be found (or the list is empty), do
       // nothing and just return (or delete newNode and return).
       //
       struct NODE* current =Head;
       struct NODE* next = current->Next;
       bool flag = false;
       while(current)
       {
           if (current->Data==key) {
               flag = true;
               break;
           }
           current = current->Next;
           next = current->Next;
       }
       if (flag == true) {
           current->Next = newNode;
           newNode->Next =next;

             Size++;
       }
   }

   //
   // remove
   //
   // Removes the first instance of value from the list; if
   // the value cannot be found, nothing happens.
   //
   void remove(int value)
   {
       //
       // TODO: remove the first node that contains value; if value
       // is not found, do nothing. You'll need to search the list
       // for the value, and then unlink that node from the list.
       // Don't worry about freeing the memory, you can ignore that
       // for now (or use delete ptrToNode; if you want to free).
       //
       struct NODE* current =Head;
       struct NODE* previous =Head;
       bool flag = false;
       while(current)
       {
           if (current->Data == value) {
               flag = true;
               break;
           }
           previous = current;
           current = current->Next;
       }
       if (flag == true) {
           previous->Next = current->Next;
           Size--;
       }
      
       //
       // HINT #2: don't forget to decrement size
       //
   }

   //
   // output
   //
   // Outputs the size, followed by the elements one by one on
   // the same line.
   //
   void output()
   {
       cout << "Size: " << Size << endl;
       cout << "Elements: ";

       //
       // TODO: output elements with a space after each one
       // (including a space after last element). Output all
       // on the same line, save the end-of-line for after
       // the traversal loop is over.
       //
       struct NODE* temp = Head;
       while (temp)
       {
           cout << temp->Data << "\t";
           temp = temp->Next;
       }
           //

           cout << endl;
   }

};

main.cpp

#include<iostream>
#include<string>

#include "linkedlist.h"
using namespace std;
int main(){
   linkedlist LL;
   string cmd;
   int value, key;
   //
   // user can enter commands to manipulate the LL:
   //
   // p w => push w onto the end
   // i x y => insert x after y (y must exist in the list)
   // r z => remove the first instance of z
   // o => output the list
   // q => quit
   //
   //cout << "Enter a command> ";
   cin >> cmd;
   while (cmd != "q")
   {
       if (cmd == "p")
       {
           // push:
           cin >> value;

           LL.push_back(value);
       }
       else if (cmd == "i")
       {
           // insert:
           cin >> value;
           cin >> key;

           LL.insert(value, key);
       }
       else if (cmd == "r")
       {
           // remove:
           cin >> value;

           LL.remove(value);
       }
       else if (cmd == "o")
       {
           // output:

           LL.output();
       }
       else
       {
           cout << "**Invalid command, try p, i, r, o or q" << endl;
           cout << endl;
       }

       //cout << "Enter a command> ";
       cin >> cmd;
   }
   return 0;
}

--------------------------------------------------------

Output

p 2
p 5
p 6
p 8
o
Size: 4
Elements: 2     5       6       8
i 7 6
o
Size: 5
Elements: 2     5       6       7       8
r 7
o
Size: 4
Elements: 2     5       6       8
q


Related Solutions

//LinkNode is a class for storing a single node of a linked list storing integer values....
//LinkNode is a class for storing a single node of a linked list storing integer values. It has two public data fields for the data and the link to //the next node in the list and has three constructors: public class LinkNode { public int data;       public LinkNode next; // post: constructs a node with data 0 and null link public ListNode() {      this(0, null); } // post: constructs a node with given data and null link public LinkNode (int...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
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...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
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)
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
(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...
A polynomial can be represented using linked list, storing coefficient and exponent of each component of...
A polynomial can be represented using linked list, storing coefficient and exponent of each component of polynomial in a node of it. Write a class in C++ to implement polynomials in three variables x, y and z (a polynomial may also have a constant term). Your class should implement methods for following operations. • Add two polynomials. • Multiply two polynomials. Use a two way header list for implementation. Your program should print the coefficient and exponent of each component...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT