Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

starting out with >>> C++ from control structures through objects Chapter 17 Lis

ID: 3722496 • Letter: S

Question

starting out with >>> C++ from control structures through objects

Chapter 17 Lists consider the following code:

struct ListNode { char value; struct ListNode *next; };

ListNode *head;

2. Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member

3. Write a function that inserts a character into the list (pass the character and list to the function.

4. Write a function that takes a character and returns true or false as to whether than character is in the list.

Explanation / Answer

2.

ListNode *temp = head;
while(temp)
{
   cout<<temp->value<<" ";
   temp = temp->next;
}

3.

void insert(ListNode *head , char value)
{
   ListNode *temp = head;
   while(temp)
       temp = temp->next;
  
   ListNode *newNode = (ListNode*)malloc(sizeof(struct ListNode)); // you can also use new operator
   temp->next = newNode;
   newNode -> next = NULL;
}

4.

bool check(ListNode *head , char value)
{
   ListNode *temp = head;
   while(temp)
   {
       if(temp->value == value)
           return true;
       else
           temp = temp->next;
   }  
   return false;
  
}