Advertisement
backstreetimrul

Symbol Table

Jul 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. #include <iostream>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. struct Node
  6. {
  7.     char *name;
  8.     char *classtype;
  9.     Node *next;
  10. };
  11. ///
  12. //Declare starting (Head) node
  13. struct Node *head=NULL;
  14. //Insert node at start
  15. void insertNode(char *n, char *c)
  16. {
  17.     int flag=0;
  18.     struct Node *temp=head;
  19.     while(temp!=NULL)
  20.     {
  21.         if(strcmp( temp->name, n )==0)
  22.         {
  23.             flag=1;
  24.         }
  25.         temp = temp->next;
  26.     }
  27.     if(flag==0)
  28.     {
  29.         struct Node *newNode=new Node;
  30.         newNode->name=n;
  31.         newNode->classtype=c; //
  32.         newNode->next=head;
  33.         head=newNode;
  34.     }
  35.     else
  36.     {
  37.         cout<<"This entry already exist"<<endl;
  38.     }
  39. }
  40. //Update
  41. void update(char *n, char *c, char *cNew)
  42. {
  43.     struct Node *temp=head;
  44.     while(temp!=NULL)
  45.     {
  46.         if(!strcmp(temp->name,n) && !strcmp(temp->classtype,c))
  47.         {
  48.             temp->classtype=cNew;
  49.         }
  50.         temp = temp->next;
  51.     }
  52. }
  53. //delete node from start
  54. void delete_position(int pos)
  55. {
  56.     pos=pos+1;
  57.     int i;
  58.     //beginning
  59.     if(pos==1)
  60.     {
  61.         Node *temp=new Node;
  62.         temp=head;
  63.         head=head->next;
  64.         delete temp;
  65.     }
  66.     //middle or last
  67.     else
  68.     {
  69.         Node *current=new Node;
  70.         Node *previous=new Node;
  71.         current=head;
  72.         for(i=1;i<pos;i++)
  73.         {
  74.             previous=current;
  75.             current=current->next;
  76.         }
  77.         previous->next=current->next;
  78.     }
  79. }
  80.  
  81. //Search
  82. void Search(char *n, char *c)
  83. {
  84.     int cnt=0;
  85.     int flag1=0;
  86.     struct Node *temp=head;
  87.     while(temp!=NULL)
  88.     {
  89.         cnt++;
  90.         if(!strcmp(temp->name,n) && !strcmp(temp->classtype,c))
  91.         {
  92.             flag1=1;
  93.             //return true;
  94.             cout<<"Found at position: "<<cnt<<" --->";
  95.             cout<<temp->name<<":"<<temp->classtype<<endl;
  96.             break;
  97.         }
  98.         temp = temp->next;
  99.     }
  100.     if(flag1==0)
  101.     {
  102.         cout<<"Not Found"<<endl;
  103.     }
  104. }
  105. //Traverse/ display all nodes (print items)
  106. void display()
  107. {
  108.     if(head==NULL)
  109.     {
  110.         cout<<"List is empty!"<<endl;
  111.         return;
  112.     }
  113.     struct Node *temp=head;
  114.     cout<<endl<<endl<<endl<<"--------------------------Symbolic Table----------------------------"<<endl;
  115.     cout<<"name : class type"<<endl;
  116.     cout<<"-----------------"<<endl;
  117.     int cnt=0;
  118.     while(temp!=NULL)
  119.     {
  120.         cnt++;
  121.         cout<<cnt<<"- "<<temp->name<<" : "<<temp->classtype<<endl;
  122.         temp=temp->next;
  123.     }
  124.     cout<<endl;
  125.     cout<<"-------------------------------------------------------------------------"<<endl;
  126. }
  127.  
  128.  
  129.  
  130. int showMenu()
  131. {
  132.     system("color F");
  133.     cout << "\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 Welcome to the Main Menu \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\n";
  134.     string message = "1: Insert (name, class type)\n"
  135.                      "2: Update (name, class type, new class type)\n"
  136.                      "3: Search (name, class type)\n"
  137.                      "4: Delete (name, class type)\n"
  138.                      "5: View Table\n";
  139.     cout << message << "\n";
  140.  
  141.  
  142.     cout << "User Choice: ";
  143.     int select;
  144.     //scanf("%d", &choice);
  145.     cin>>select;
  146.  
  147.     return select;
  148. }
  149.  
  150. int main()
  151. {
  152.     int select = showMenu();
  153.     while(1)
  154.     {
  155.         char *name = new char[100];
  156.         char *classtype = new char[100];
  157.         char *newClasstype = new char[100];
  158.  
  159.         switch(select)
  160.         {
  161.         case 1:
  162.             {
  163.                 cout <<"Insert Operation:\n";
  164.                 cin>>name>>classtype;
  165.  
  166.                 if(name[strlen(name)-1]==',')
  167.                     name[strlen(name)-1]='\0';
  168.  
  169.                 insertNode(name,classtype);
  170.             }
  171.             break;
  172.         case 2:
  173.             {
  174.                 cout<<"Update Operation:"<<endl;
  175.                 cin>>name>>classtype>>newClasstype;
  176.  
  177.                 if(name[strlen(name)-1]==',')
  178.                     name[strlen(name)-1]='\0';
  179.                 if(classtype[strlen(classtype)-1]==',')
  180.                     classtype[strlen(classtype)-1]='\0';
  181.  
  182.                 update(name,classtype,newClasstype);
  183.             }
  184.             break;
  185.         case 3:
  186.             {
  187.                 cout<<"Search Operation:"<<endl;
  188.                 cin>>name>>classtype;
  189.  
  190.                 if(name[strlen(name)-1]==',')
  191.                     name[strlen(name)-1]='\0';
  192.  
  193.                 Search(name,classtype);
  194.             }
  195.             break;
  196.         case 4:
  197.             {
  198.                 cout<<"Delete Operation:"<<endl;
  199.                 cin>>name>>classtype;
  200.  
  201.                 if(name[strlen(name)-1]==',')
  202.                     name[strlen(name)-1]='\0';
  203.  
  204.                 int pos=-1;
  205.                 int flag=0;
  206.                 struct Node *temp=head;
  207.                 while(temp!=NULL)
  208.                 {
  209.                     pos++;
  210.                     if(!strcmp(temp->name,name) && !strcmp(temp->classtype,classtype))
  211.                     {
  212.                         //return true;
  213.                         flag=1;
  214.                         break;
  215.                     }
  216.                     temp = temp->next;
  217.                 }
  218.                 if(flag==0)
  219.                 {
  220.                     cout<<"Data you have wanted to delete is not in the List"<<endl;
  221.                 }
  222.                 if(flag==1)
  223.                 {
  224.                     delete_position(pos);
  225.                 }
  226.             }
  227.             break;
  228.         case 5:
  229.             {
  230.                 cout<<"Display Operation:"<<endl;
  231.                 display();
  232.             }
  233.             break;
  234.         default:
  235.             {
  236.                 cout<<"Input Correctly among 1 to 5"<<endl;
  237.                 break;
  238.             }
  239.         }
  240.         cout<<"\n\npress 'y' to go to main menu again: ";
  241.         char yes;
  242.         cin>>yes;
  243.         if(yes=='y')
  244.         {
  245.             system("cls");
  246.             select = showMenu();
  247.         }
  248.     }
  249.  
  250.     return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement