Docsity

C++ Program to Implement a Linked List and Delete a Node, Assignments of Computer Science

Toggle sidebar
bg1
1. Suppose the function Delete, described below, is implemented. What
will the output be if the input is 10 34 9 0 34 45 10 9 0
2. Write the code for the function.
#include <iostream>
typedef int ComponentType;
struct NodeType; // forward declaration
typedef NodeType * NodePtr;
struct NodeType // completes the declaration
{
ComponentType component;
NodePtr link;
};
int main ()
{
bool Delete( ComponentType item, NodePtr head);
NodePtr head; // used to mark the beginning of a list
NodePtr currPtr; // temp pointer used to create and traverse list
NodePtr newNodePtr; // temp pointer used to create list
ComponentType inputVal; // temp for input
// Create a list reading values from input adding new items to the end of the list
head = new NodeType;
cin >> head->component;
currPtr = head;
cin >>inputVal;
while (inputVal != 0)
{ newNodePtr = new NodeType;
newNodePtr ->component = inputVal;
currPtr->link = newNodePtr;
currPtr = newNodePtr;
cin >> inputVal;
}
currPtr->link = NULL;
// display the list
cout << " Printing the list " << endl;
currPtr = head;
while (currPtr != NULL) {
cout << currPtr->component;
cout << endl;
currPtr = currPtr->link;
}
cin >> inputVal;
while ( inputVal != 0 )
{
if ( Delete( inputVal, head )){
cout << " Printing the list " << endl; //display the list
currPtr = head;
while (currPtr != NULL) {
cout << currPtr->component;
cout << endl;
currPtr = currPtr->link;
}
}
cin>> inputVal;
}
return 0;
}
bool Delete( ComponentType item, NodePtr head)
{
/*
postcondition: if there exists an object of NodeType whose component
field is equal to item then that object is removed from the
list pointed to by head; otherwise, the list is unchanged.
returns: true if there is an object of NodeType whose component files
is equal to item; otherwise false.
*/
}

Partial preview of the text

Download C++ Program to Implement a Linked List and Delete a Node and more Assignments Computer Science in PDF only on Docsity!

1. Suppose the function Delete, described below, is implemented. What

will the output be if the input is 10 34 9 0 34 45 10 9 0

2. Write the code for the function.

#include typedef int ComponentType; struct NodeType; // forward declaration typedef NodeType * NodePtr; struct NodeType // completes the declaration { ComponentType component; NodePtr link; }; int main () { bool Delete( ComponentType item, NodePtr head); NodePtr head; // used to mark the beginning of a list NodePtr currPtr; // temp pointer used to create and traverse list NodePtr newNodePtr; // temp pointer used to create list ComponentType inputVal; // temp for input // Create a list reading values from input adding new items to the end of the list head = new NodeType; cin >> head->component; currPtr = head; cin >>inputVal; while (inputVal != 0) { newNodePtr = new NodeType; newNodePtr ->component = inputVal; currPtr->link = newNodePtr; currPtr = newNodePtr;

cin >> inputVal;

currPtr->link = NULL; // display the list cout << " Printing the list " << endl; currPtr = head; while (currPtr != NULL) { cout << currPtr->component; cout << endl; currPtr = currPtr->link; } cin >> inputVal; while ( inputVal != 0 ) { if ( Delete( inputVal, head )){ cout << " Printing the list " << endl; //display the list currPtr = head; while (currPtr != NULL) { cout << currPtr->component; cout << endl; currPtr = currPtr->link; } } cin>> inputVal; } return 0; } bool Delete( ComponentType item, NodePtr head) {

postcondition: if there exists an object of NodeType whose component

field is equal to item then that object is removed from the

list pointed to by head; otherwise, the list is unchanged.

returns: true if there is an object of NodeType whose component files

is equal to item; otherwise false.