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++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
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 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:...
How do I make this code not include negative numbers in the total or average if...
How do I make this code not include negative numbers in the total or average if they are entered? (C++) For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only. ----- #include using namespace std; int main() {    int x, total = 0, count = 0;       cout << "Type in first value ";   ...
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:...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....