Question

my code has several functions; delete and backward functions are not working, rewrite the code for...

my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main:

#include<iostream>
#include<cassert>
using namespace std;

struct nodeType
{
   int info;
   nodeType *link;
};

class linkedList
{
public:


   void initializeList();

   bool isEmptyList();

   void print();

   int length();

   void destroyList();

   void insertFirst(int newItem);

   void insertLast(int newItem);

   int front();

   linkedList();

   void copyList(const linkedList otherList);

   void insertNewValue(int value);

   void deleteItem(int deleteItem);


   void Backward(int num);

private:

   int count;
   nodeType *first;
   nodeType *last;
};

int linkedList::front()
{
   assert(first != nullptr);
   return last->info;
}
bool linkedList::isEmptyList()
{
   return (first == nullptr);
}

void linkedList::insertFirst(int newItem)
{
   nodeType *newNode;

   newNode = new nodeType;

   newNode->info = newItem;
   newNode->link = first;
   first = newNode;

   count++;

   if (last == nullptr)
       last = newNode;
}

void linkedList::insertLast(int newItem)
{
   nodeType *newNode;

   newNode = new nodeType;
   newNode->info = newItem;
   newNode->link = nullptr;
   if (first == nullptr)
   {
       first = newNode;
       last = newNode;
       count++;
   }
   else
   {
       last->link = newNode;
       last = newNode;
       count++;
   }
}

linkedList::linkedList()
{
   first = nullptr;
   last = nullptr;
   count = 0;
}

void linkedList::destroyList()
{
   nodeType *temp;
   while (first != nullptr)
   {
       temp = first;
       first = first->link;
       delete temp;
   }
   last = nullptr;
   count = 0;
}


void linkedList::initializeList()
{
   destroyList();
}


void linkedList::print()
{
   nodeType *current;

   current = first;
   while (current != nullptr)
   {
       cout << current->info << " ";
       current = current->link;
   }
}

int linkedList::length()
{
   return count;
}

void linkedList::insertNewValue(int value)
{
   nodeType *curr = first; // set curr to first node of the list
   bool found = false; // set found to false
                       // loop over the list
   while (curr != nullptr)
   {
       if (curr->info == value) // value found, set found to true and exit the loop
       {
           found = true;
           break;
       }

       curr = curr->link;
   }

   if (!found) // value not found in list, insert value at end
       insertLast(value);
   else // value found in list, hence not inserted
       cout << "ERROR: " << value << " already inserted in the list. " << endl;

}

void linkedList::deleteItem(int deleteItem)
{
   nodeType *p, *q;
   q = nullptr;
   p = first;
   while (p != nullptr)
   {
       // p->info as we are accessing data and not the pointer
       if (p->info == deleteItem)
       {
           q = p;
           p->link = p->link->link;
           delete q;
           return;
       }
       p = p->link;
   }
}

void linkedList::copyList(const linkedList otherList)
{
   nodeType *newNode, *current, *fin;
   if (first != nullptr)
       initializeList();
   if (otherList.first == nullptr)
       first = nullptr;
   else
   {

       current = otherList.first;
       first = new nodeType;
       first->info = current->info;
       first->link = nullptr;
       last = first;
       current = current->link;

       while (current != nullptr)
       {
           // cout<<"here\n";
           nodeType *p = new nodeType();
           count++;
           p->info = current->info;
           p->link = nullptr;
           last->link = p;
           last = last->link;
           current = current->link;
       }
   }
   return;

}

void linkedList::Backward(int num)
{

   // Code changes
   // Addind node to the tail
   nodeType *p = new nodeType();
   count++;
   p->info = num;
   p->link = nullptr;
   if (first == nullptr)
   {
       first = last = p;
       return;
   }
   last->link = p;
   last = last->link;

}


int main()
{

   linkedList List;
   linkedList listForward;
   int num = -1, x;

   cout << "Enter numbers ending with -999" << endl;
   cin >> num;

   while (num != -999)
   {
       List.Backward(num);
       listForward.insertFirst(num);

       cin >> num;
   }
   cout << endl;

   cout << "The list in backward" << endl;
   List.print();

   cout << endl;
   cout << "The list in forward" << endl;
   listForward.print();

   cout << endl;

   cout << "Length of the list: " << List.length() << endl;

   cout << "Enter a number to add in the list " << endl;
   cin >> x;
   List.insertNewValue(x);
   cout << "The list after adding the number " << endl;
   List.print();
   cout << endl;

   cout << "The length of the list after adding a number is " << List.length() << endl;

   int el;
   cout << "Enter the value to be deleted in the list: " << endl;
   cin >> el;
   List.deleteItem(el);

   List.print();
   cout << endl;

   cout << "Enter new value to be added in the list: " << endl;
   int k;
   cin >> k;
   List.insertNewValue(k);


   List.print();
   cout << endl;

   cout << endl;
   cout << endl << "Creating a copy of the linked list" << endl;
   linkedList newList;
   newList.copyList(List);
   cout << "Printing the new linked list\n";
   newList.print();


   List.destroyList();
   cout << endl;
   cout << "After destroying the list, the length is " << List.length() << endl;

   system("pause");
   return 0;
}

Homework Answers

Answer #1

void linkedList::deleteItem(int deleteItem)
{

   nodeType *p, *q;
   q = nullptr;
   p = first;

if (p==nullptr)

{

cout<<"linked list not initialized";

return ;

}
   while (p != nullptr)
   {
       // p->info as we are accessing data and not the pointer
       if (p->info == deleteItem)
       {
           q = p;
           p->link = p->link->link;
           delete q;
           return;
       }
       p = p->link;
   }

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using linked lists. Currently I am able to input processes and store / display them properly. The issue begins somewhere after I have displayed the processlist (I get a segmentation fault (core dumped) or the code doesnt seem to run). I have marked the location of where I think the issue begins with a comment. Please fix the code so that it is working properly....
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs...
in c++ Each of the member functions in WrongCode.cpp has errors in the way it performs a linked list operation. Find as many mistakes as you can //--------------------one void NumberList::appendNode(double num) { ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new listNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node. if (!head) head = newNode; else // Otherwise, insert newNode. { // Find the last...
Utilize the code from last week Add a default, full, and copy constructor. Also add a...
Utilize the code from last week Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects. #include <iostream> #include <string> #include <iomanip> using namespace std; class VideoGame { private:     string name;     int highScore;     int numOfPlays; public:     VideoGame() {        ...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...