Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4022166
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:26:30+00:00 2026-05-20T10:26:30+00:00

#include <iostream> using namespace std; struct list { char name[20]; int age; double height;

  • 0
#include <iostream>

using namespace std;

struct list
{
  char name[20];
  int age;
  double height;
  list *next;
};

list *first = NULL, *current;
int optn = 0;

void currentfor()
{
  if (current->next == NULL)
    cout << "List has ended!" << endl;
  else
    current = current->next;
}

void currentbac()
{
  if (current == first)
    cout << "This is the beginning of the list." << endl;
  else
    {
      list *previous;
      previous = first;
      while (previous->next != current)
        {
          previous = previous->next;
        }
      current = previous;
    }
}

void addbeginning()
{
  list *newlist;

  newlist = new list;
  cout << "Enter your name:" ;
  cin >> newlist->name;
  cout << "Enter your age:" ;
  cin >> newlist->age;
  cout << "Enter your height:" ;
  cin >> newlist->height;
  newlist->next=first;
  first=newlist;
}

void addending()
{
  list *newlist, *newlist2;

  newlist = new list;
  cout << "Enter your name: ";
  cin >> newlist->name;
  cout << "Enter your age : ";
  cin >> newlist->age;
  cout << "Enter your height : ";
  cin >> newlist->height;
  newlist->next = NULL;
  if (first == NULL)
    {
      first = newlist;
      current=first;
    }
  else
    {
      newlist2 = first;
      while (newlist2->next != NULL)
        {
          newlist2 = newlist2->next;
        }
      newlist2->next = newlist;
    }
}

void addmiddle()
{
  if ( current->next=NULL)
    addending();
  else
    {
      list *newlist;
      newlist=new list;
      cout << "Enter your name:" ;
      cin >> newlist->name;
      cout << "Enter your age:" ;
      cin >> newlist->age;
      cout << "Enter your height:" ;
      cin >> newlist->height;
      newlist->next=current->next;
      current->next=newlist;
    }
}

void deletebegin()
{
  list *newlist;
  newlist = first;
  first = first->next;
  delete newlist;
}

void deletemiddle()
{
  if ( current->next=NULL)
    cout<<"There is no one after this.";
  else
    {
      list *newlist;
      newlist=current;
      current=current->next;
      delete newlist;
    }
}

void deleteend()
{
  list *newlist, *newlist2;

  if (first == NULL)
    cout << "End of list" << endl;
  else
    {
      newlist = first;
      if (newlist->next == NULL)
        {
          delete newlist;
          first = NULL;
        }
      else
        {
          while (newlist->next != NULL)
            {
              newlist2 = newlist;
              newlist = newlist->next;
            }
          delete newlist;
          newlist2->next = NULL;
        }
    }
}

void display()
{
  list *newlist;

  newlist = first;
  cout << endl;
  do
    {
      if (newlist == NULL)
        cout << "End of List" << endl;
      else
        {
          cout << "Name is: " << newlist->name << " ";
          cout << "Age is: " << newlist->age << " ";
          cout << "Height is: " << newlist->height;
          cout<<" <-- Current position ";
          cout<< endl;
          newlist = newlist->next;
        }
    }
  while (newlist!=NULL);
  cout << "End of list" << endl;
}


int main()
{
  first = NULL;
  do
    {
      display();
      cout << endl;
      cout << "Choose an option: " << endl;
      cout << "1. Move the current position forward once." << endl;
      cout << "2. Move the current position backwards once." << endl;
      cout << "3. Add a member at the beginning of the list." << endl;
      cout << "4. Add a member at the current position of the list." << endl;
      cout << "5. Add a member at the ending of the list." << endl;
      cout << "6. Delete the first member from the list." << endl;
      cout << "7. Delete the member at current position from the list." << endl;
      cout << "8. Delete the last member from the list." << endl;
      cout << "9. End program." << endl;
      cout << endl << " >> " ;
      cin >> optn;
      switch (optn)
        {
        case 1 : currentfor();
          break;
        case 2 : currentbac();
          break;
        case 3 : addbeginning();
          break;
        case 4 : addmiddle();
          break;
        case 5 : addending();
          break;
        case 6 : deletebegin();
          break;
        case 7 : deletemiddle();
          break;
        case 8 : deleteend();
          break;
        }
    }
  while (optn!= 9);
}

I execute this program and number 3 works.But the others seem to crash my program 🙁
can someone please show me the correct coding to this?
Im making a linked list with nodes filled with name height and age and it should work for any user.
And I m supposed to display the entire list right?Or dont linked lists appear that way ?
can some good soul please help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:26:31+00:00Added an answer on May 20, 2026 at 10:26 am

    I corrected your program and indicated all errors you should pay attention to in the comments. Please take a good look at them and try to see what you did wrong, so you can learn from this.

    #include <iostream>
    
    using namespace std;
    
    struct list {
       char name[20];
       int age;
       double height;
       list *next;
    };
    
    list *first = NULL, *current = NULL; // Init current to NULL so you can test whether current is set at all
    
    int optn = 0;
    
    void currentfor() {
       if(current == NULL) {
          cout << "You don't have any members yet!" << endl;
       } else {
          if (current->next == NULL)
             cout << "This is the end of the list." << endl;
          else
             current = current->next;
       }
    }
    
    void currentbac() {
       if (current == first)
          cout << "This is the beginning of the list." << endl;
       else {
          list *previous;
          previous = first;
          while (previous->next != current) {
             previous = previous->next;
          }
          current = previous;
       }
    }
    
    void addbeginning() {
       list *newlist;
    
       newlist = new list;
       cout << "Enter your name:" ;
       cin >> newlist->name;
       cout << "Enter your age:" ;
       cin >> newlist->age;
       cout << "Enter your height:" ;
       cin >> newlist->height;
       newlist->next = first;
       first = newlist;
    
       if(current == NULL) // Set the current pointer to first, because this is the first element you add
          current = first;
    }
    
    void addending() {  
       list *newlist, *newlist2;
    
       newlist = new list;
       cout << "Enter your name: ";
       cin >> newlist->name;
       cout << "Enter your age : ";
       cin >> newlist->age;
       cout << "Enter your height : ";
       cin >> newlist->height;
       newlist->next = NULL;
    
       if (first == NULL) {
          first = newlist;
          current=first;
       } else {
          newlist2 = first;
          while (newlist2->next != NULL) {
             newlist2 = newlist2->next;
          }
          newlist2->next = newlist;
       }
    }
    void addmiddle() {
       if (current->next == NULL) // You were assigning here. Use == instead of = or you will assign NULL to
                                  // current->next! Which is incorrect.
          addending();
       else {
          list *newlist;
          newlist = new list;
          cout << "Enter your name:" ;
          cin >> newlist->name;
          cout << "Enter your age:" ;
          cin >> newlist->age;
          cout << "Enter your height:" ;
          cin >> newlist->height;
    
          newlist->next = current->next;
          current->next = newlist;
       }
    }
    
    void deletebegin() {
       list *newlist;
       newlist = first;
       first = first->next;
    
       // You need to update the current pointer first
       if(newlist == current) {
          current = current->next;
       }
    
       delete newlist;
    }
    
    void deletemiddle() {
       list *newlist;
       newlist = first;
    
       // If we delete the first element
       if(current == first) {
          list *deleteMe = first;
          first = first->next;
    
          delete deleteMe;
          current = current->next;
       } else { // Otherwise
          // Search until newlist->next == current
          // Als test for newlist != NULL or you will try to get a next value from NULL -> crash!
          while(newlist != NULL && newlist->next != current)
             newlist = newlist->next;
    
          if(newlist != NULL) {
             delete newlist->next;
             newlist->next = current->next; // Also update the next from the previous node in the list! Or it will not disappear when displaying
    
             if (current->next == NULL) // You did it again here. Use == for comparing values instead of =
                current = first; // It doesn't mean that you don't have to delete if
             // you don't have a current->next. If you don't have a current->next,
             // just set it to first. The element does need to be deleted.
             else
                current = current->next;
          }
       }
    }
    
    void deleteend() {
       list *newlist, *newlist2;
    
       if (first == NULL)
          cout << "End of list" << endl;
       else {
          newlist = first;
    
          if (newlist->next == NULL) {
             delete newlist;
             first = NULL;
             current = NULL; // Current should also be null
          } else {
             while (newlist->next != NULL) {
                newlist2 = newlist;
                newlist = newlist->next;
             }
    
             delete newlist;
             newlist2->next = NULL;
             current = newlist2; // You forgot to update the current pointer.
          }
       }
    }
    
    void display() { 
       list *newlist;
    
       newlist = first;
       cout << endl;
    
       do {
          if (newlist == NULL)
             cout << "End of List" << endl;
          else
             {
                cout << "Name is: " << newlist->name << " ";
                cout << "Age is: " << newlist->age << " ";
                cout << "Height is: " << newlist->height;
    
                if(current == newlist) // You need to check whether you really are at the current position
                   cout<<" <-- Current position ";
    
                cout<< endl;
                newlist = newlist->next;
             }
       }
       while(newlist!=NULL);
    
       if(newlist != NULL) // What if the newList was initially NULL? You will print twice.
          cout << "End of list" << endl;
    }
    
    
    int main(void) {
       first=NULL;
    
       do {
             display();
             cout << endl;
             cout << "Choose an option: " << endl;
             cout << "1. Move the current position forward once." << endl;
             cout << "2. Move the current position backwards once." << endl;
             cout << "3. Add a member at the beginning of the list." << endl;
             cout << "4. Add a member at the current position of the list." << endl;
             cout << "5. Add a member at the ending of the list." << endl;
             cout << "6. Delete the first member from the list." << endl;
             cout << "7. Delete the member at current position from the list." << endl;
             cout << "8. Delete the last member from the list." << endl;
             cout << "9. End program." << endl;
             cout << endl << " >> " ;
             cin >> optn;
             switch (optn) {
                case 1 : currentfor();
                   break;
                case 2 : currentbac();
                   break;
                case 3 : addbeginning();
                   break;
                case 4 : addmiddle();
                   break;
                case 5 : addending();
                   break;
                case 6 : deletebegin();
                   break;
                case 7 : deletemiddle();
                   break;
                case 8 : deleteend();
                   break;
                }
          }
       while (optn!= 9);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { struct list { string name; int
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct node { int v; node* next; node (int
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include<iostream> using namespace std; struct sample { int data[3][2]; }; struct sample* function() {
#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> #include <queue> using namespace std; struct Call { Call( int callNum, long
#include <iostream> #include <vector> using namespace std; struct s_Astruct { vector <int> z; };
#include <iostream> using namespace std; struct Node { int item; // storage for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.