Question

1 hode expand (node* head) // fill in code here This problem has you modify a linked lists composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL next pointer 4 The linked lists for this lab store string data. Some of the strings in the linked lists repeat, some do not, but wed like all strings to repeat at least once. Specifically, if a string does not repeat, we want to insert a copy of the string so that there are two of the string in a row. If the string already repeats, we do nothing. Note that a string may re-occur later in a linked list; this does not count as a repetition for our purposes Create a function expand, which takes in a pointer to the head of the linked list, and returns a pointer to the head of a linked list in which all strings repeat following the rules described above The linked lists for this problem use the following class declaration: class node f public: string data; node next; J: Notes and Constraints . The linked list starting at head contains between 1 and 25 nodes, inclusive Examples apple ->NULL return apple ->apple- >NULL 2. apple - >apple->NULL return apple->apple ->NULL 3.green-green->blue ->red->green- NULL return green->green->blue ->blue->red ->red ->green ->green - >NULL

Fill in code on right in c++ and follow all constraints and directions.

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

node *expand( node *head) {

if(head==NULL)

return NULL;

node * curr = head, prev = NULL;

while(curr != null) {

if(curr->next!=NULL && curr->next->data ==curr->data) {

prev = curr->next;
curr = curr->next->next;


}else if(prev != NULL && prev->data==curr->data){
prev = curr;
curr = curr->next;

}else {

node *temp = new node;

temp->data = curr->data;

temp->next = curr->next;

curr->next = temp;

curr = curr->next->next;

}

}

return head;

}

Add a comment
Know the answer?
Add Answer to:
Fill in code on right in c++ and follow all constraints and directions. 1 hode expand...
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
  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...

    CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list...

  • () Given the following structure definition and typedef for a linked list of strings: typedef struct...

    () Given the following structure definition and typedef for a linked list of strings: typedef struct node st node; struct node st { char *word; /* a valid string pointer or NULL */ node *next; /* next node in the list or NULL */ }; Write a C function, free list(), that takes as an argument one of these lists, possibly NULL, and frees all the strings as well as the list itself. Write robust code. void free list(node *list){

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • just a normal linked list Given the following partial code, fill in the blank to complete...

    just a normal linked list Given the following partial code, fill in the blank to complete last node. (don't forget the semicolon) class Node { public Object data = null; public Node next = null; Node head = new Node(); // first Node head.next = new Node; // second Node head.next.next = new Node: // third node head.next next next = new Node: // fourth node

  • Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm ...

    Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm We have seen in class how selection sort algorithm works on arrays data structure. In this lab we will practice how selection sort can be performed on a linked list ADT 1. Convert the following selection sort pseudo-code to perform the sort in ascending order (selectionsort asc function) a. Find the node with the minimum value in the linked list of length rn b. Append...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

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