0

I need to modify the following program to store person objects instead of integers. Include a function to input person data from the keyboard in the person class (similar to the readRecord() function). Also add a non class function to display the list contents.

Include a separate search function (search on the name of the person) which returns the position in the list of the item if found

Setup a menu to access the different functions such as:
1 Build a list
2 Search list
3 Add to list
4 Delete from list
5 Display list

This is the code:

#include <iostream>
using namespace std;
// Linked list using structures
struct nodeType {
    int info;
    nodeType *link;
};
nodeType* buildListForward()
{
  nodeType *first,*newNode, *last;
  int num;
  cout<<"enter numbers";
  cin >>num;
  first=NULL;
  while(num!=-999)
  {
    newNode = new nodeType;     // create new Node
    newNode->info =num;
    newNode->link = NULL;
    if(first==NULL)             // special case empty list
    {
    first = newNode;
    last= newNode;
    }
    else
    {
      last->link = newNode;
      last = newNode;
    }
        cin>>num;
   }
   return first;
}
nodeType* buildListBackward()
{
  nodeType *first,*newNode;
  int num;
  cout<<"enter numbers";
  cin >>num;
  first=NULL;
  while(num!=-999)
  {
    newNode = new nodeType;     // creat new node
    newNode->info =num;
    newNode->link = first;

    first = newNode;            // keep adding at beginning

        cin>>num;
   }
   return first;
}
nodeType* deleteNode(nodeType *head,int delItem)
{
  nodeType *current,*prev;
  bool found=false;
  current = head;
  prev = head;
  while(current!=NULL && !found) {          // search loop
    if(current->info==delItem) found = true;
    else {
          prev = current;
          current = current->link;
     }
  }
  if(found) {
    if (current==head) head = current->link;// special case delete head
    else
     prev->link = current->link;
    delete current;
  }
  return head;
}
nodeType* insert(nodeType *head, int item)
{
  nodeType *current,*prev,*newNode;
  bool found=false;
  newNode = new nodeType;
  newNode->info = item;
  newNode->link = NULL;
  current = head;
  prev = head;
  while(current!=NULL && current->info<item) {  // search loop

          prev = current;
          current = current->link;

  }

    if (current==head) {            // special case add at head
        newNode->link = current;
        head = newNode;
    }
    else {
     prev->link = newNode;
     newNode->link = current;
    }

  return head;
}
int main() {
    nodeType *head, *current;


    system("pause");
    return 0;
}

Any help will be greatly appreciated

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
0

Now that we know what you want to do, how about showing us what you have done, and tell us what problems you are having or what you don't understand. No one here will do that problem for you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.