Hi I am trying to get this program to work to insert a node into the list and move the list
down so the node will fit. I get the node to be inserted into the right place but the list
is copied as such.
if 1. joe
2. linda
3. harvey
4. stacey
5. klaus
if i insert a node into 2 the list will look like this
1.joe
2. node inserted
3. linda
4. linda
5. linda
6. linda
i need the list to be like the below after node is inserted.
1. joe
2. inserted list
3.linda
4.harvey
5. stacey
6. klaus
Can you please help me figure this out for the doinsert function.
and the dodelete function deletes the node that the user
specifies.
#include<iostream>
#include<conio.h> //for clearscreen
#include<stdlib.h> //for clearscreen
#include<string.h> //allows string processing
#include<fstream> //for files in c++
#include<assert.h> //used to detect file errors
using namespace std;
struct Listnode
{
char firstname[30];
char lastname[30];
int age;
};
struct Listtype
{
Listnode* person[30];
int size;
};
//prototypes
Listtype initialize(Listtype &list);
void fillList(Listtype & list);
void printList(Listtype & list);
void doInsert(Listtype &list);
void doDelete(Listtype &list);
int menu();
void main(){
Listtype List;
int choice;
List=initialize(List);
do{
system("cls"); //clears screen
choice=menu();
switch(choice)
{
case 1: fillList(List);break;
case 2: printList(List);break;
case 3: doInsert(List);break;
case 4: doDelete(List);break;
case 5: break;
default: cout<<"\nINVALID...1-5 ONLY!\n";
cin.ignore();
cin.ignore();
}
}while(choice !=5);
}
int menu()
{
int choice;
cout<<"\nMENU FOR LAB1\n";
cout<<"1.Fill the list from file data.txt\n"
<<"2.Print the list (include size of the list)\n"
<<"3.Insert a record into the list\n"
<<"4.Delete a record from the list\n"
<<"5.Quit"<<endl<<endl<<endl;
cout<<"ENTER YOUR CHOICE_____\n";
cin >>choice;
return choice;
}
Listtype initialize(Listtype & list)
//precondition: List is undefined
//postconditon: List is initialized to NULL pointers
{
for(int i=0; i<30; i++)
{
list.person[i]=NULL;
}
list.size=0;
return list;
}
void fillList(Listtype & list)
//precondition: List has been initialized to NULL pointers
//postcondition: List contains the firstname, lastname, age of people in file data.txt; list.size is the number of people in the file
{
char firstname[30];
char lastname[30];
int age=0,i=0;
ifstream infile("data.txt",ios::in);
assert(!infile.fail());
while(infile>>firstname>>lastname>>age)
{
//infile>>lastname;
//infile>>age;
list.person[i]=new Listnode;
strcpy(list.person[i]->firstname,firstname);
strcpy(list.person[i]->lastname,lastname);
list.person[i]->age=age;
i++;
}
list.size=i;
}
void printList(Listtype &list)
//precondition: list contains valid data
//postcondition: the contents and size of the list are output to screen
{
system("cls");
int i;
for(i=0; i<list.size; i++)
cout<<i+1<<" "<<list.person[i]->firstname<<" "<<
list.person[i]->lastname<<" "<<list.person[i]->age<<endl;
cout<<"\nSize of list="<<list.size<<endl;
cin.ignore();
cin.ignore();
return;
}
void doInsert(Listtype &list)
//precondition: list has only the list from file data.txt
//postcondtion: list is filled with user data and appended by adding a new list at a position specified by user
{
system("cls");
char firstname[30];
char lastname[30];
int age=0;
int pos=0;
cout<<"input first\n";
cin>>firstname;
cout<<"last\n";
cin>>lastname;
cout<<"age\n";
cin>>age;
cout<<"pos\n";
cin>>pos;
list.size++;
int rev=pos-1;
for(pos; pos<list.size; pos++ )
{
list.person[pos+1]=list.person[pos];
}
for(pos; pos!=NULL; pos-- )
{
list.person[pos-1]=list.person[pos];
}
list.person[rev]=new Listnode;
strcpy(list.person[rev]->firstname,firstname);
strcpy(list.person[rev]->lastname,lastname);
list.person[rev]->age=age;
cin.ignore();
cin.ignore();
}
void doDelete (Listtype &list)
//precondition:list has data from the user and appended data
//postcondition:a list can be removed by specifing lastname, firstname
{
char lastname[30], firstname[30];
int i=0,remove;
cout << "Enter the name of the record to delete: ";
cin >> lastname >> firstname;
for (i; i<list.size; i++)
{
if (strcmp(lastname, list.person[i]->lastname)!=0 && strcmp(firstname, list.person[i]->firstname)!=0)
{
remove = i;
cout << "position remove is executed" << endl;
for (i; i<list.size; i++)
{
list.person[i]=list.person[i+1];
}
break;
}
else if (i=list.size)
{
cout << "NAME NOT IN LIST" << endl;
cout << "Hit return to continue" << endl;
}
}
delete list.person[remove];
list.size--;
}