Displaying the Consolidated Lists

Asked 2 years ago, Updated 2 years ago, 30 views

I'm studying the consolidated list, but it doesn't work well.
If anyone understands, please let me know what's wrong.

The purpose of Activity::append is to create the first node in the concatenated list if the head is NULL, and then generate a node if there is a node first.

However, when I ran and displayed the concatenation list, only the last node I entered was displayed.

If there is anything that needs to be improved, please let me know.

#include<iostream>
# include <cctype>
# include <cstring>
using namespace std;

constint SIZE=20;
const int SIZE2 = 130;
const int SIZE 3 = 100;

structure node
{
  char name [SIZE];
  char plain [SIZE2];
  char reason [SIZE3];
  int times;
  node*next;
};

class activity
{
  public:
    Activity();
    ~Activity();
    void read();
    void append(char name[], char expand[], char reason[], int times);
    void display();
  
  private:
    node*head;
};

Activity:: Activity()
{
  head = NULL;
}

Activity:: ~ Activity()
{
  while(head!=NULL)
  {
    node*temp;
    temp=head->next;
    delete head;
    head = temp;
  }

  delete head;
  
}


voidActivity::append(char name[], char expand[], char reason[], int times)
{
    if(NULL==head)  
    {  
    cout<<endl;
    head = new node;
    strcpy(head->name,name);
    strcpy(head->explain,explain);
    strcpy(head->reason,reason); 
    head->times=times;  
    head->next = NULL;
    }
    else
    {
      node*current=head;
      while (current->next!=NULL)
      {
        current=current->next;
      }
      current->next=new node;
      current=current->next;
      strcpy(head->name,name);
      strcpy(head->explain,explain);
      strcpy(head->reason,reason); 
      head->times=times;  
      head->next = NULL;
      current ->next = NULL;
    }

}

void Activity::display()
{
  if(head==NULL)
  return;
  node*current=head;
  while (current!=NULL)
  {
    cout<current->name<endl;
    cout<current->explain<endl;
    cout<current->reason<endl;
    cout<current->times<endl;
    cout<<endl;
    current=current->next;
  }
}

int main()
{
  Activity my_activity;
  char response='y';
  do
  {
    char name [SIZE];
    char plain [SIZE2];
    char reason [SIZE3];
    int times;
    cout<<"Please enter name of activity"<<endl;
    cin.get(name, SIZE, '\n'); cin.ignore(100, '\n');
    cout<<"Please expand detail of it"<<endl;
    cin.get(explain, SIZE2, '\n'); cin.ignore(100, '\n');
    cout<<"please explain why do you want to do"<<endl;
    cin.get(reason, SIZE3, '\n'); cin.ignore(100, '\n');
    cout<<"How many times does it take?"<<endl;
    cin>>times;cin.ignore(100, '\n');

    my_activity.append(name,explain,reason,times);

    cout<<"Do you want to continue to append?"<<endl;
    cin>>response;cin.ignore(100, '\n');
  }while(response=='y');
  my_activity.display();
}

c++

2022-09-30 18:13

1 Answers

"It doesn't work" only tells you that the actual behavior of the program is different from what you expect.Oylas, even if your readers want it to be like this, it doesn't actually work out as you expect it to be: it happens often, and then the answers are wasted.

So, in Oira's own interpretation
Expectations: I want all the elements I entered to be displayed in the order I entered them.
Reality: Only the last element is displayed
Assume that (if you don't meet your wishes, wait for someone else's answer)

The display routine Activity::display() appears to be correct.The wrong one is probably Activity::append(), so how to fix it is completely different depending on what is right, so it's a bit of a complaint.

Activity::append() Delusional about the expected behavior

  • By name, head might want to start the consolidated list
  • Create a leading element if there is no consolidated list (updated head)
  • Add one element to the end of the consolidated list if there is a consolidated list (head must not be updated)

If head represents (want to) the "first element", you can only update it when you create the first element, and if you already have one, you must not rewrite the head itself.I think it would be good to correct that (I will not give you an example of the correction right now, so I would appreciate it if you could give me a self-answer if you can correct it well)

The lack of answers is not cool for SO, so I will present a code sample.Although the front and back are abbreviated, of course it is the contents of Activity::append().

Level 1 Answer: Do not update head if you already have a list

implement honestly
 node*self=new node; // strcpy, etc. omitted
self->next=NULL;// There is no next, which is basically the end.
if(head==NULL) {// There is no list, that is, the one I made this time is the first one.
    head = self; // Head and exit
    return;
}
// If you already have a list, add the one you made this time to the end.
// You must not update the head at this time.
node*p=head;
for(;p->next!=NULL;p=p->next);// Look for the end
// Now, p should be a valid last node.
p->next=self;//self->next is already NULL, so that's all.

Level 2 answers: Somehow if is not cool, so I want to remove it

//Previous: I don't dare to explain what I'm doing, so please read it.
node**p=&head;
for(;*p!=NULL;p=&(*p)->next);
*p=self;

Level 3 answers: These "additional actions" should not be a member of Activity or perhaps even node, so you might want to create another list class.


2022-09-30 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.