Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <cstring>
- const int NAME_SIZE=16;
- const int ID_SIZE=10;
- struct Student
- {
- char fname[NAME_SIZE];
- char lname[NAME_SIZE];
- char id[ID_SIZE];
- double grade1;
- double grade2;
- double grade3;
- };
- struct Node
- {
- char fname[NAME_SIZE];
- char lname[NAME_SIZE];
- char id[ID_SIZE];
- Node* next;
- };
- Node* buildList(void);
- Node* insertNode(Node*,Node*);
- void searchNode(Node*,char*);
- Node* deleteNode(Node*,char*);
- Node* addNode(Node*,char*);
- void displayList(Node*);
- Node* cleanupList(Node*);
- Node* searchnode2(Node*, char*);
- int main()
- {
- int x;
- char id[ID_SIZE];
- Node* head=(Node*)0;
- head=buildList();
- while(x!=5)
- {
- std::cout<<"1.Display all keys\n"
- <<"2.Display specified records\n"
- <<"3.Delete a specified record\n"
- <<"4.Add a specified record\n"
- <<"5.Quit\n"<<std::endl;
- std::cin>>x;
- if(x==1)
- {
- std::cout<<"Here is your list:"<<std::endl;
- displayList(head);
- }
- if(x==2)
- {
- std::cout<<"\nEnter an id to search for ===> "<<std::flush;
- std::cin>>id;
- searchNode(head,id);
- }
- if(x==3)
- {
- std::cout<<"\nEnter an id to delete ===> "<<std::flush;
- std::cin>>id;
- head=deleteNode(head,id);
- std::cout<<"\nHere is your list after the delete:"<<std::endl;
- displayList(head);
- }
- if(x==4)
- {
- std::cout<<"\nEnter an id to add ===> "<<std::flush;
- std::cin>>id;
- head=addNode(head,id);
- std::cout<<"\nHere is your list after the add:"<<std::endl;
- displayList(head);
- }
- }
- head=cleanupList(head);
- displayList(head);
- return(0);
- }
- Node* buildList(void)
- {
- Node* head=(Node*)0;
- Node* newNode=(Node*)0;
- Student person;
- std::ifstream infile;
- infile.open("students.txt",std::ios::in);
- if(!infile)
- {
- std::cerr<<"\n\nFile not opened...\n"<<std::endl;
- exit(-1);
- }
- infile>>person.fname
- >>person.lname
- >>person.id
- >>person.grade1
- >>person.grade2
- >>person.grade3;
- for(;!infile.eof();)
- {
- newNode=new Node;
- strncpy(newNode->fname,person.fname,NAME_SIZE);
- newNode->fname[NAME_SIZE-1]='\0';
- strncpy(newNode->lname,person.lname,NAME_SIZE);
- newNode->lname[NAME_SIZE-1]='\0';
- strncpy(newNode->id,person.id,ID_SIZE);
- newNode->id[ID_SIZE-1]='\0';
- newNode->next=(Node*)0;
- head=insertNode(head,newNode);
- infile>>person.fname
- >>person.lname
- >>person.id
- >>person.grade1
- >>person.grade2
- >>person.grade3;
- }
- infile.close();
- return(head);
- }
- Node* insertNode(Node* head,Node* newNode)
- {
- Node* current=head;
- Node* prior=NULL;
- while((current)&&(strncmp(current->id,newNode->id,ID_SIZE)<0))
- {
- prior=current;
- current=current->next;
- }
- newNode->next=current;
- if(!prior)
- head=newNode;
- else
- prior->next=newNode;
- return(head);
- }
- void searchNode(Node* head,char* id)
- {
- Node* current=head;
- int found=0;
- while((current)&&(!found))
- {
- if(strncmp(id,current->id,ID_SIZE)==0)
- found=1;
- else
- current=current->next;
- }
- if(found)
- {
- std::cout<<std::endl
- <<current->fname<<"\t"
- <<current->lname<<"\t"
- <<current->id<<std::endl;
- }
- else
- std::cout<<"\nRecord not in the list..."<<std::endl;
- }
- Node* searchNode2(Node* head, char*id)
- {
- Node* current=head;
- int found=0;
- while((current)&&(!found))
- {
- if(strncmp(id,current->id,ID_SIZE)==0)
- found=0;
- else
- current=current->next;
- }
- return(current);
- }
- Node* deleteNode(Node* head,char* id)
- {
- Node* current=head;
- Node* prior=(Node*)0;
- int found=0;
- while((current)&&(!found))
- {
- if(strncmp(id,current->id,ID_SIZE)==0)
- found=1;
- else
- {
- prior=current;
- current=current->next;
- }
- }
- if(found)
- {
- if(prior)
- prior->next=current->next;
- else
- head=current->next;
- delete current;
- }
- else
- std::cout<<"\nNo one by that name to delete..."<<std::endl;
- return(head);
- }
- Node* addNode(Node* head,char* id)
- {
- Node* newNode=(Node*)0;
- Student person;
- Node* newNode= new Node;
- if(searchNode2(head, id))
- {
- newNode= new Node;
- strncpy(newNode->id.id,ID_SIZE);
- std::cout<<"\nEnter first name ===> "<<std::flush;
- std::cin>>person.fname;
- std::cout<<"Enter last name ===> "<<std::flush;
- std::cin>>person.lname;
- std::cout<<"Enter id ===> "<<std::flush;
- std::cin>>person.id;
- strncpy(newNode->fname,person.fname,NAME_SIZE);
- newNode->fname[NAME_SIZE-1]='\0';
- strncpy(newNode->lname,person.lname,NAME_SIZE);
- newNode->lname[NAME_SIZE-1]='\0';
- strncpy(newNode->id,person.id,ID_SIZE);
- newNode->id[ID_SIZE-1]='\0';
- newNode->next=(Node*)0;
- head=insertNode(head, newNode);
- }
- else
- {
- std::cout<<"id already in list"<<std::endl;
- }
- return(head);
- }
- void displayList(Node* head)
- {
- Node* current=head;
- if(!head)
- std::cout<<"error"<<std::flush;
- while(current)
- {
- std::cout<<std::endl
- <<current->fname<<"\t"
- <<current->lname<<"\t"
- <<current->id<<std::endl;
- current=current->next;
- }
- }
- Node* cleanupList(Node* head)
- {
- Node* current=head;
- while(head)
- {
- current=head;
- head=current->next;
- delete current;
- }
- return(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement