Docsity

C Programming: Creating and Manipulating a Linked List, Study notes of Computer Science

Toggle sidebar
bg1
Suppose input is 63,-15, 99, 45, 545. Draw a diagram that shows the list after the
statements below are executed.
typedef int ComponentType;
struct NodeType; // forward declaration
typedef NodeType * NodePtr;
struct NodeType // complete the declaration
{
ComponentType component;
NodePtr link;
};
NodePtr head;
NodePtr currPtr;
NodePtr newNodePtr;
ComponentType inputVal;
head = new NodeType;
cin >> head->component;
currPtr = head;
cin >>inputVal;
while (cin)
{ newNodePtr = new NodeType;
newNodePtr ->component = inputVal;
currPtr -> link = newNodePtr;
currPtr = newNodePtr;
cin >> inputVal;
}
currPtr->link = NULL;
Write the statements necessary to add an item whose component field is 123 to the
end of the list.
Given the changes we’ve made to the list, write the statements necessary to add an
item whose component field is 42 after the item whose component field is 99.
Given the changes we’ve made to the list, write the statements necessary to add an
item whose component field is 24 before the item whose component field is 42.
Given the changes we’ve made to the list, write the statements necessary to find the
average of the values in the component field of all the items on the list.
Given the changes we’ve made to the list, write the statements necessary to delete
the first item in the list whose component field is 42.

Partial preview of the text

Download C Programming: Creating and Manipulating a Linked List and more Study notes Computer Science in PDF only on Docsity!

Suppose input is 63,-15, 99, 45, 545. Draw a diagram that shows the list after the statements below are executed. typedef int ComponentType; struct NodeType; // forward declaration typedef NodeType * NodePtr; struct NodeType // complete the declaration { ComponentType component; NodePtr link; }; NodePtr head; NodePtr currPtr; NodePtr newNodePtr; ComponentType inputVal; head = new NodeType; cin >> head->component; currPtr = head; cin >>inputVal; while (cin) { newNodePtr = new NodeType; newNodePtr ->component = inputVal; currPtr -> link = newNodePtr; currPtr = newNodePtr; cin >> inputVal; } currPtr->link = NULL; Write the statements necessary to add an item whose component field is 123 to the end of the list. Given the changes we’ve made to the list, write the statements necessary to add an item whose component field is 42 after the item whose component field is 99. Given the changes we’ve made to the list, write the statements necessary to add an item whose component field is 24 before the item whose component field is 42. Given the changes we’ve made to the list, write the statements necessary to find the average of the values in the component field of all the items on the list. Given the changes we’ve made to the list, write the statements necessary to delete the first item in the list whose component field is 42.