Question

C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST

A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function)

#include "pch.h"
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
#define MAX 512

struct node {
   char c[MAX+1];
   struct node *next;
};
node *str = NULL;

node *addOnto(struct node *list, char *a) {
   node * current = new node;
   if (current == NULL) {
       cout << "Error: cannot add to list";
       return 0;
   }
   strcpy_s(current->c, a);
   current->next = list;
   return current;
}

void print() {
   node *current;
   for (current = str; current != NULL; current = current->next) {
       cout << current->c << " ";
   }
}
void reverse_word(char *a) {
   reverse(a.begin(), a.end());
}
void reverse(struct node * sum) {
   node * current = sum;

   while (current != NULL) {
       reverse_word(current->c);
       current = current->next;
   }
}
int find() {

}

int main()
{
   char word[MAX];
   char *p;
  
   cout << "Enter a word: ";
   cin >> word;
   p = &word[0];

   str = addOnto(str, p);
   print();

   return 0;
}

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

#include "pch.h"
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
#define MAX 512

struct node {
   char c[MAX+1];
   struct node *next;
};
node *str = NULL;

node *addOnto(struct node *list, char *a) {
   node * current = new node;
   if (current == NULL) {
       cout << "Error: cannot add to list";
       return 0;
   }
   strcpy_s(current->c, a);
   current->next = list;
   return current;
}

void print() {
   node *current;
   for (current = str; current != NULL; current = current->next) {
       cout << current->c << " ";
   }
}
void reverse_word(char *a) {
   reverse(a.begin(), a.end());
}
void reverse(struct node * sum) {
   node * current = sum;

   if(current == NULL)

        return;

else{
       reverse(current->next);
       cout<<current->c;
   }
}
int find() {

}

int main()
{
   char word[MAX];
   char *p;
  
   cout << "Enter a word: ";
   cin >> word;
   p = &word[0];

   str = addOnto(str, p);
   print();

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...
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
  • 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...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() βž” Constructor: Initialize any member variables of the class to default ~MovieTree() βž” Destructor: Free all memory that was allocated void printMovieInventory() βž” Print every movie in the data structure in...

  • C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove...

    C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. (i.e. n is greater than 0) Follow up: Could you do this in one pass? Hint: Maintain two pointers and update one with...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • () 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){

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