Question

The source code I have is what I'm trying to fix for the assignment at the...

The source code I have is what I'm trying to fix for the assignment at the bottom.

Source code:

//Group 1 - Jodie Butterworth, Brandon Kidd, Matt Heckler
//11-21-19
//CSC 201

// Exercise to practice the basic manipulations of a linked list
// Note: Uses nullptr, so need to make sure your compiler is set to use C++11
// In Code::Blocks this is under Settings==>Compiler
#include <iostream>

using namespace std;

// Structure to contain data for a node (Could be defined in a header file)
// Note: Not using a typedef in this definition
struct NodeType
{
int component;
NodeType *next;
};

// Prints out all elements in the list
// Pre: currPtr must point to a valid list, or nullptr
// Post: Print all elements in list, if any
void PrintList(NodeType *currPtr);

// Create and return a new node.
// Pre: None
// Post: Returns a pointer to NodeType with component set to -1 and next set to nullptr
NodeType *CreateNode();

// Add a given node to a list in a sorted position.
// Pre: listPtr set to a valid list, or nullptr
// newNodePtr assigned to a valid node with valid component value
// Post: listPtr is updated with nodePtr included in the sorted position
void AddNode(NodeType *&listPtr, NodeType *newNodePtr);

// Find a value and remove that node from a list.
// Pre: listPtr set to a valid list, or nullptr
// delValue set to the value of a node to be deleted from the list
// Post: listPtr is updated with the first node with delValue, if it exists, deleted from the list
void DeleteNode(NodeType *&listPtr, int delValue);

int main()
{
NodeType *headPtr; // Always points to the first node in the list
NodeType *lastPtr; // Always points to the last node in the list
NodeType *currPtr; // Working pointer. Points to whatever node it needs to
NodeType *newNodePtr; // Points to a newly created node

headPtr = nullptr; // If you're getting an error on this line, read the comment at the top

newNodePtr = CreateNode(); // Call CreateNode to get a new node

headPtr = newNodePtr; // As it is the only node, set headPtr to newNodePtr

PrintList(headPtr); // Print full list
}

void PrintList(NodeType *currPtr){

if (currPtr == nullptr)
cout << "Empty list!\n";

// Loop until we've reached the end of the list (Or it's an empty list)
while (currPtr != nullptr) {
cout << currPtr->component << " "; // Print value of current node
currPtr = currPtr->next; // Advance currPtr to point at next node
}

cout << endl; // Keep list on one line
}

// Create and return a new node.
// Pre: None
// Post: Returns a pointer to NodeType with component set to -1 and next set to nullptr
NodeType *CreateNode(){
NodeType *newNodePtr; // Pointer for new node
int newValue; // Value to be assigned to new node

// 1 - Allocate space for new node
newNodePtr = new NodeType;

// 2 - Assign values to node
cout << "Enter value for new node: "; // Alternatively, this value could be passed as a parameter
cin >> newValue;
newNodePtr->component = newValue; // Set component value
newNodePtr->next = nullptr; // Set next to nullptr for safety/consistency

// 3 - Return node
return newNodePtr;
}

// Add a given node to a list in a sorted position.
// Pre: listPtr set to a valid list, or nullptr
// newNodePtr assigned to a valid node with valid component value
// Post: listPtr is updated with nodePtr included in the sorted position
void AddNode(NodeType *&listPtr, NodeType *newNodePtr){
NodeType *currPtr = listPtr; // Points to current node in list
NodeType *prevPtr = currPtr-1; // Points to node before the currPtr
NodeType *lastPtr = nullptr; //Points to the last node

//find the end of the list for lastPtr
while (currPtr->component){
lastPtr = currPtr;
}

while ((newNodePtr->component > currPtr->component) && (currPtr != nullptr)){
// Traverse through with currPtr pointing to current and prevPtr trailing
// Stop when currPtr reaches insertion point for new node
prevPtr = currPtr;
currPtr = currPtr->next;

}
//use component value to find place in the list
if (currPtr->component == 0){
newNodePtr = listPtr;
}
else if (currPtr->component < 0){
newNodePtr->component = currPtr->component-1;
}
else if (currPtr->component == lastPtr->component){
newNodePtr->component = lastPtr->component+1;
}
else{
currPtr->component = prevPtr->component+1;
}
// 2 - Insert node
// If First node in list, then newNodePtr IS the list

// If inserting at beginning of list, add newNodePtr BEFORE listPtr

// If inserting at end of list, add newNodePtr at end

// Otherwise, must be in the middle of the list. Use prevPtr to insert currPtr

}

// Find a value and remove that node from a list.
// Pre: listPtr set to a valid list, or nullptr
// delValue set to the value of a node to be deleted from the list
// Post: listPtr is updated with the first node with delValue, if it exists, deleted from the list
void DeleteNode(NodeType *&listPtr, int delValue){
NodeType *currPtr = listPtr; // Points to current node in list
NodeType *prevPtr = nullptr; // Points to node before the currPtr

// 1 - Find node to delete (currPtr->component == delValue)
// Traverse through with currPtr pointing to current and prevPtr trailing
// Stop when currPtr reaches node to delete or end of list

// If node is found,
// 2 - Remove node from list
// If first node, update listPtr to remove first node from list

// If last node, update prevPtr to end of list

// Otherwise, in middle of list, so use prevPtr->next to skip over currPtr

// 3 - Deallocate memory used by deleted node

// Else
// cout << "Value not found!\n";
}

Assignment details:

Continuing from Part 1...

  1. CreateNode: Create and return a new node.
  2. AddNode: Add a given node to a list in a sorted position.
  3. DeleteNode: Find a value and remove that node from a list.
  • Write the code for the DeleteNode function.
  • Provide some test calls in your main function.

Submit: Commented source code with your DeleteNode function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am answering in two different parts  

functions insert, delete , display on ordered linked list

#include<iostream>

using namespace std;

struct node

{

          int info;

          node* next;

};

class linkedtype

{

          node* head;

public:

          linkedtype()

          {

                   head = NULL;

          }

          void creatnewnode(int);

          void deletion();

          void display();

};

void linkedtype::deletion()

{

          int del;

          node *temp, *temp1;

          cout << "\nEnter the value of node to be deleted : ";

          cin >> del;

          if (head == NULL)

                   cout << "\nUnderflow";

          else

                   if (head->info == del)

                   {

                             temp1 = head;

                             head = temp1->next;

                             delete temp1;

                   }

                   else

                   {

                             for (temp = head; (temp->next->info != del) && (temp->next->next != NULL);temp = temp->next);

                             if (temp->next->info == del)

                             {

                                      temp1 = temp->next;

                                      temp->next = temp1->next;

                                      delete temp1;

                             }

                   }

}

void linkedtype :: display()

{

          node* temp;

          if (head == NULL)

                   cout << "\nLinked List is empty";

          else

          {

                   cout << "\nLinked List : ";

                   for (temp = head; temp != NULL;temp = temp->next)

                   {

                             cout << temp->info << "\t";

                   }

          }

}

void linkedtype::creatnewnode(int add)

{

          node* nptr;

          nptr = new node;

          nptr->info = add;

          nptr->next = NULL;

          node* temp;

          if (head == NULL)

                   head = nptr;

          else if (head->info > nptr->info)

          {

                   nptr->next = head;

                   head = nptr;

          }

          else

          {

                   for (temp = head;temp->next != NULL && temp->next->info < (nptr->info);temp = temp->next)

                   {

                   }

                   if (temp->next == NULL)

                   {

                             temp->next = nptr;

                   }

                   else

                   {

                             nptr->next = temp->next;

                             temp->next = nptr;

                   }

          }

}

int main()

{

          linkedtype l, l1, l2;

          int choice, add, n;

          node* nptr;

          char ch = 'Y';

          cout << "\nMAIN MENU ";

          cout << "\n1.Add at end"<<"\n2.Deletion"<<"\n3.Display";

          do

          {

                   cout << "\nEnter your choice: ";

                   cin >> choice;

                   switch (choice)

                   {

                   case 1:

                             cout << "\nEnter the info part to be inserted: ";

                             cin >> add;

                             l.creatnewnode(add);

                             l.display();

                             break;

                   case 2:

                             l.deletion();

                             l.display();

                             break;

                  

                   case 3:

                             l.display();

                             break;

                   default :

                             cout << "\nWRONG CHOICE";

                   }

                   cout << "\nDo you want to continue? ";

                   cin >> ch;

          }

          while (ch == 'y' || ch == 'Y');

          return 0;

}

2.operations search ,insertion, deletion, and after ,and add before on the linked list

#include<iostream>

using namespace std;

struct node

{

          int info;

          struct node *next;

}*last;

class cllist

{

public:

          void search(int ele);

          void add_end(int value);

          void add_begin(int value);

          void add_after(int value, int position);

          void deletion(int value);

          void display();

          cllist()

          {

                   last = NULL;

          }

};

int main()

{

          int choice, element, position, ele;

          char ch;

          cllist cl;

          do

          {

                   cout << "\nMain Menu";

                   cout << "\n1.Add at end";

                   cout << "\n2.Add at beginning";

                   cout << "\n3.Add after";

                   cout << "\n4.Delete";

                   cout << "\n5.search";

                   cout << "\n6.display";

                   cout << "\n.Enter your choice : ";

                   cin >> choice;

                   switch (choice)

                   {

                   case 1:

                             cout << "\nEnter the element: ";

                             cin >> element;

                             cl.add_end(element);

                             cout << endl;

                             cl.display();

                             break;

                   case 2:

                             cout << "\nEnter the element: ";

                             cin >> element;

                             cl.add_begin(element);

                             cout << endl;

                             cl.display();

                             break;

                   case 3:

                             cout << "\nEnter the element: ";

                             cin >> element;

                             cout << "\nInsert element after position: ";

                             cin >> position;

                             cl.add_after(element, position);

                             cout << endl;

                             cl.display();

                             break;

                   case 4:

                             if (last == NULL)

                             {

                                      cout << "\nList is empty, nothing to delete";

                                      break;

                             }

                             cout << "\nEnter the element for deletion: ";

                             cin >> element;

                             cl.deletion(element);

                             cout << endl;

                             break;

                   case 5:

                             cout << "Enter the element to be searched ";

                             cin >> ele;

                             cl.search(ele);

                             break;

                                    case 6:

                             cl.display();

                             break;

                   }

                   cout << "\n Do you want to continue?";

                   cin >> ch;

          }

          while (ch == 'y' || ch == 'Y');

}

void cllist::add_end(int value)

{

          struct node *temp;

          temp = new(struct node);

          temp->info = value;

          if (last == NULL)

          {

                   last = temp;

                   temp->next = last;

          }

          else

          {

                   temp->next = last->next;

                   last->next = temp;

                   last = temp;

          }

}

void cllist:: search(int ele)

{

          int flag = 0;

          int count = 0;

          struct node *t;

          t = last->next;

          while (t->next != last)

          {

                   ++count;

                   if (t->info == ele)

                   {

                             flag = 1;

                             cout << "Element found at position " << count;

                             break;

                   }

                   t = t->next;

          }

          if (flag == 0)

                   cout << "Element not found ";

}

void cllist::add_begin(int value)

{

          if (last == NULL)

          {

                   cout << "Empty" << endl;

                   return;

          }

          struct node *temp;

          temp = new(struct node);

          temp->info = value;

          temp->next = last->next;

          last->next = temp;

}

void cllist::add_after(int value, int pos)

{

          if (last == NULL)

          {

                   cout << "Empty" << endl;

                   return;

          }

          struct node *temp, *s;

          s = last->next;

          for (int i = 0;i < pos - 1;i++)

          {

                   s = s->next;

                   if (s == last->next)

                   {

                             cout << "There are less than ";

                             cout << pos << " in the list" << endl;

                             return;

                   }

          }

          temp = new(struct node);

          temp->next = s->next;

          temp->info = value;

          s->next = temp;

          if (s == last)

          {

                   last = temp;

          }

}

void cllist::deletion(int value)

{

          struct node *temp, *s;

          s = last->next;

          if (last->next == last && last->info == value)

          {

                   temp = last;

                   last = NULL;

                   free(temp);

                   return;

          }

          if (s->info == value)

          {

                   temp = s;

                   last->next = s->next;

                   free(temp);

                    return;

          }

          while (s->next != last)

          {

                   if (s->next->info == value)

                   {

                             temp = s->next;

                             s->next = temp->next;

                             free(temp);

                             cout << "Element " << value;

                             cout << " deleted from the list" << endl;

                             return;

                   }

                   s = s->next;

          }

          if (s->next->info == value)

          {

                   temp = s->next;

                   s->next = last->next;

                   free(temp);

                   last = s;

                   return;

          }

          cout << "Element " << value << " not found in the list" << endl;

}

void cllist::display()

{

          struct node *s;

          if (last == NULL)

          {

                   cout << "List is empty, nothing to display" << endl;

                   return;

          }

          s = last->next;

          cout << "Circular Linked List: " << endl;

          while (s != last)

          {

                   cout << s->info << " -> ";

                   s = s->next;

          }

          cout << s->info << endl;

}

thank you

Add a comment
Know the answer?
Add Answer to:
The source code I have is what I'm trying to fix for the assignment at the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • Trying to figure out what needs to be in the headers.h file. I have written the...

    Trying to figure out what needs to be in the headers.h file. I have written the print function. Qualities in header file main() needs insertionSort() and mergeSortWrapper() Both insertionSort() and mergeSortWrapper() need print(). Please copy-and-paste the following files (0 Points): insertionSort.c /*--------------------------------------------------------------------------* *---- ----* *---- insertionSort.c ----* *---- ----* *---- This file defines a function that implements insertion ----* *---- sort on a linked-list of integers. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* *----...

  • 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;...

  • the source code I have is what i'm supposed to fix for the assignment at the...

    the source code I have is what i'm supposed to fix for the assignment at the bottom. cars.cpp source code: /* Program Cars.cpp reads records from file and writes them back to another file with the price member increased by 10%. */ #include <fstream> #include <iostream> #include <string> #include "car.h" // car.h header file is included here using namespace std; /* Reads data from a file and using it to populate a Car struct and then return that Car struct...

  • In this assignment, you will use a Hashtable to determine the common elements in all the...

    In this assignment, you will use a Hashtable to determine the common elements in all the lists. If an element appears more than once in one or more lists, the algorithm should capture the instances the element is present in all the lists. You are given a code that implements a Hashtable as an array of linked lists. You are also given a main function that will create an array of Lists using the input variable values that you enter....

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