Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <cstring>
  5.  
  6. const int NAME_SIZE=16;
  7. const int ID_SIZE=10;
  8.  
  9. struct Student
  10. {
  11. char fname[NAME_SIZE];
  12. char lname[NAME_SIZE];
  13. char id[ID_SIZE];
  14. double grade1;
  15. double grade2;
  16. double grade3;
  17. };
  18.  
  19. struct Node
  20. {
  21. char fname[NAME_SIZE];
  22. char lname[NAME_SIZE];
  23. char id[ID_SIZE];
  24. Node* next;
  25. };
  26.  
  27. Node* buildList(void);
  28. Node* insertNode(Node*,Node*);
  29. void searchNode(Node*,char*);
  30. Node* deleteNode(Node*,char*);
  31. Node* addNode(Node*,char*);
  32. void displayList(Node*);
  33. Node* cleanupList(Node*);
  34. Node* searchnode2(Node*, char*);
  35.  
  36. int main()
  37. {
  38.   int x;
  39.   char id[ID_SIZE];
  40.   Node* head=(Node*)0;
  41.   head=buildList();
  42.   while(x!=5)
  43.   {
  44.     std::cout<<"1.Display all keys\n"
  45.              <<"2.Display specified records\n"
  46.              <<"3.Delete a specified record\n"
  47.              <<"4.Add a specified record\n"
  48.              <<"5.Quit\n"<<std::endl;
  49.     std::cin>>x;
  50.     if(x==1)
  51.     {
  52.       std::cout<<"Here is your list:"<<std::endl;
  53.       displayList(head);
  54.     }
  55.     if(x==2)
  56.     {
  57.       std::cout<<"\nEnter an id to search for ===> "<<std::flush;
  58.       std::cin>>id;
  59.       searchNode(head,id);
  60.     }
  61.     if(x==3)
  62.     {
  63.       std::cout<<"\nEnter an id to delete ===> "<<std::flush;
  64.       std::cin>>id;
  65.       head=deleteNode(head,id);
  66.       std::cout<<"\nHere is your list after the delete:"<<std::endl;
  67.       displayList(head);
  68.     }
  69.     if(x==4)
  70.     {
  71.       std::cout<<"\nEnter an id to add ===> "<<std::flush;
  72.       std::cin>>id;
  73.       head=addNode(head,id);
  74.       std::cout<<"\nHere is your list after the add:"<<std::endl;
  75.       displayList(head);
  76.     }
  77.   }
  78. head=cleanupList(head);
  79.  
  80. displayList(head);
  81. return(0);
  82. }
  83.  
  84. Node* buildList(void)
  85. {
  86.   Node* head=(Node*)0;
  87.   Node* newNode=(Node*)0;
  88.   Student person;
  89.   std::ifstream infile;
  90.   infile.open("students.txt",std::ios::in);
  91.   if(!infile)
  92.   {
  93.     std::cerr<<"\n\nFile not opened...\n"<<std::endl;
  94.     exit(-1);
  95.   }
  96.   infile>>person.fname
  97.         >>person.lname
  98.         >>person.id
  99.         >>person.grade1
  100.         >>person.grade2
  101.         >>person.grade3;
  102.   for(;!infile.eof();)
  103.     {
  104.       newNode=new Node;
  105.       strncpy(newNode->fname,person.fname,NAME_SIZE);
  106.       newNode->fname[NAME_SIZE-1]='\0';
  107.       strncpy(newNode->lname,person.lname,NAME_SIZE);
  108.       newNode->lname[NAME_SIZE-1]='\0';
  109.       strncpy(newNode->id,person.id,ID_SIZE);
  110.       newNode->id[ID_SIZE-1]='\0';
  111.       newNode->next=(Node*)0;
  112.       head=insertNode(head,newNode);
  113.       infile>>person.fname
  114.             >>person.lname
  115.             >>person.id
  116.             >>person.grade1
  117.             >>person.grade2
  118.             >>person.grade3;
  119.     }
  120. infile.close();
  121. return(head);
  122. }
  123.  
  124. Node* insertNode(Node* head,Node* newNode)
  125. {
  126.   Node* current=head;
  127.   Node* prior=NULL;
  128.   while((current)&&(strncmp(current->id,newNode->id,ID_SIZE)<0))
  129.     {
  130.     prior=current;
  131.     current=current->next;
  132.     }
  133.   newNode->next=current;
  134.   if(!prior)
  135.     head=newNode;
  136.   else
  137.     prior->next=newNode;
  138. return(head);
  139. }
  140.  
  141. void searchNode(Node* head,char* id)
  142. {
  143.   Node* current=head;
  144.   int found=0;
  145.   while((current)&&(!found))
  146.   {
  147.     if(strncmp(id,current->id,ID_SIZE)==0)
  148.       found=1;
  149.     else
  150.       current=current->next;
  151.   }
  152.   if(found)
  153.   {
  154.     std::cout<<std::endl
  155.              <<current->fname<<"\t"
  156.              <<current->lname<<"\t"
  157.              <<current->id<<std::endl;
  158.   }
  159.   else
  160.     std::cout<<"\nRecord not in the list..."<<std::endl;
  161. }
  162.  
  163. Node* searchNode2(Node* head, char*id)
  164. {
  165.   Node* current=head;
  166.   int found=0;
  167.   while((current)&&(!found))
  168.   {
  169.     if(strncmp(id,current->id,ID_SIZE)==0)
  170.       found=0;
  171.     else
  172.       current=current->next;
  173.   }
  174. return(current);
  175. }
  176.  
  177. Node* deleteNode(Node* head,char* id)
  178. {
  179.   Node* current=head;
  180.   Node* prior=(Node*)0;
  181.   int found=0;
  182.   while((current)&&(!found))
  183.   {
  184.     if(strncmp(id,current->id,ID_SIZE)==0)
  185.       found=1;
  186.     else
  187.     {
  188.       prior=current;
  189.       current=current->next;
  190.     }
  191.   }
  192.   if(found)
  193.   {
  194.     if(prior)
  195.       prior->next=current->next;
  196.     else
  197.       head=current->next;
  198.     delete current;
  199.   }
  200.   else
  201.     std::cout<<"\nNo one by that name to delete..."<<std::endl;
  202. return(head);
  203. }
  204.  
  205. Node* addNode(Node* head,char* id)
  206. {
  207.   Node* newNode=(Node*)0;
  208.   Student person;
  209.   Node* newNode= new Node;
  210.   if(searchNode2(head, id))
  211.   {
  212.     newNode= new Node;
  213.     strncpy(newNode->id.id,ID_SIZE);
  214.     std::cout<<"\nEnter first name ===> "<<std::flush;
  215.     std::cin>>person.fname;
  216.     std::cout<<"Enter last name ===> "<<std::flush;
  217.     std::cin>>person.lname;
  218.     std::cout<<"Enter id  ===> "<<std::flush;
  219.     std::cin>>person.id;
  220.  
  221.     strncpy(newNode->fname,person.fname,NAME_SIZE);
  222.     newNode->fname[NAME_SIZE-1]='\0';
  223.     strncpy(newNode->lname,person.lname,NAME_SIZE);
  224.     newNode->lname[NAME_SIZE-1]='\0';
  225.     strncpy(newNode->id,person.id,ID_SIZE);
  226.     newNode->id[ID_SIZE-1]='\0';
  227.     newNode->next=(Node*)0;
  228.     head=insertNode(head, newNode);
  229.   }    
  230.   else
  231.   {
  232.     std::cout<<"id already in list"<<std::endl;
  233.   }
  234. return(head);
  235. }
  236.  
  237. void displayList(Node* head)
  238. {
  239.   Node* current=head;
  240.   if(!head)
  241.   std::cout<<"error"<<std::flush;
  242.   while(current)
  243.   {
  244.     std::cout<<std::endl
  245.              <<current->fname<<"\t"
  246.              <<current->lname<<"\t"
  247.              <<current->id<<std::endl;
  248.     current=current->next;
  249.   }
  250. }
  251.  
  252. Node* cleanupList(Node* head)
  253. {
  254.   Node* current=head;
  255.   while(head)
  256.   {
  257.     current=head;
  258.     head=current->next;
  259.     delete current;
  260.   }
  261. return(head);
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement