Advertisement
Nahid8195

Symbol Management Table in C

Dec 9th, 2022
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.52 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. int size=0;
  4. void Insert();
  5. void Display();
  6. void Delete();
  7. int Search(char lab[]);
  8. void Modify();
  9. struct SymbTab
  10. {
  11.     char label[10],symbol[10];
  12.     int addr;
  13.     struct SymbTab *next;
  14. };
  15. struct SymbTab *first,*last;
  16. int main()
  17. {
  18.     int op,y;
  19.     char la[10];
  20.     do
  21.     {
  22.         printf("\n\tSYMBOL TABLE IMPLEMENTATION\n");
  23.         printf("\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.MODIFY\n\t6.END\n");
  24.         printf("\n\tEnter your option : ");
  25.         scanf("%d",&op);
  26.         switch(op)
  27.         {
  28.         case 1:
  29.             Insert();
  30.             break;
  31.         case 2:
  32.             Display();
  33.             break;
  34.         case 3:
  35.             Delete();
  36.             break;
  37.         case 4:
  38.             printf("\n\tEnter the label to be searched : ");
  39.             scanf("%s",la);
  40.             y=Search(la);
  41.             printf("\n\tSearch Result:");
  42.             if(y==1)
  43.                 printf("\n\tThe label is present in the symbol table\n");
  44.             else
  45.                 printf("\n\tThe label is not present in the symbol table\n");
  46.             break;
  47.         case 5:
  48.             Modify();
  49.             break;
  50.         case 6:
  51.             exit(0);
  52.         }
  53.     }
  54.     while(op<6);
  55.     getchar();
  56.     return 0;
  57. }
  58. void Insert()
  59. {
  60.     int n;
  61.     char l[10];
  62.     printf("\n\tEnter the label : ");
  63.     scanf("%s",l);
  64.     n=Search(l);
  65.     if(n==1)
  66.         printf("\n\tThe label exists already in the symbol table\n\tDuplicate can’t be inserted");
  67.     else
  68.     {
  69.         struct SymbTab *p = (struct SymbTab*) malloc(sizeof(struct SymbTab));
  70.         strcpy(p->label,l);
  71.         printf("\n\tEnter the symbol : ");
  72.         scanf("%s",p->symbol);
  73.         printf("\n\tEnter the address : ");
  74.         scanf("%d",&p->addr);
  75.         p->next=NULL;
  76.         if(size==0)
  77.         {
  78.             first=p;
  79.             last=p;
  80.         }
  81.         else
  82.         {
  83.             last->next=p;
  84.             last=p;
  85.         }
  86.         size++;
  87.     }
  88.     printf("\n\tLabel inserted\n");
  89. }
  90. void Display()
  91. {
  92.     int i;
  93.     struct SymbTab *p;
  94.     p=first;
  95.     printf("\n\tLABEL\t\tSYMBOL\t\tADDRESS\n");
  96.     for(i=0; i<size; i++)
  97.     {
  98.         printf("\t%s\t\t%s\t\t%d\n",p->label,p->symbol,p->addr);
  99.         p=p->next;
  100.     }
  101. }
  102. int Search(char lab[])
  103. {
  104.     int i,flag=0;
  105.     struct SymbTab *p;
  106.     p=first;
  107.     for(i=0; i<size; i++)
  108.     {
  109.         if(strcmp(p->label,lab)==0)
  110.             flag=1;
  111.         p=p->next;
  112.     }
  113.     return flag;
  114. }
  115. void Modify()
  116. {
  117.     char l[10],nl[10];
  118.     int add,choice,i,s;
  119.     struct SymbTab *p;
  120.     p=first;
  121.     printf("\n\tWhat do you want to modify?\n");
  122.     printf("\n\t1.Only the label\n\t2.Only the address\n\t3.Both the label and address\n");
  123.     printf("\tEnter your choice : ");
  124.     scanf("%d",&choice);
  125.     switch(choice)
  126.     {
  127.     case 1:
  128.         printf("\n\tEnter the old label : ");
  129.         scanf("%s",l);
  130.         s=Search(l);
  131.         if(s==0)
  132.             printf("\n\tLabel not found\n");
  133.         else
  134.         {
  135.             printf("\n\tEnter the new label : ");
  136.             scanf("%s",nl);
  137.             for(i=0; i<size; i++)
  138.             {
  139.                 if(strcmp(p->label,l)==0)
  140.                     strcpy(p->label,nl);
  141.                 p=p->next;
  142.             }
  143.             printf("\n\tAfter Modification:\n");
  144.             Display();
  145.         }
  146.         break;
  147.     case 2:
  148.         printf("\n\tEnter the label where the address is to be modified : ");
  149.         scanf("%s",l);
  150.         s=Search(l);
  151.         if(s==0)
  152.             printf("\n\tLabel not found\n");
  153.         else
  154.         {
  155.             printf("\n\tEnter the new address : ");
  156.             scanf("%d",&add);
  157.             for(i=0; i<size; i++)
  158.             {
  159.                 if(strcmp(p->label,l)==0)
  160.                     p->addr=add;
  161.                 p=p->next;
  162.             }
  163.             printf("\n\tAfter Modification:\n");
  164.             Display();
  165.         }
  166.         break;
  167.     case 3:
  168.         printf("\n\tEnter the old label : ");
  169.         scanf("%s",l);
  170.         s=Search(l);
  171.         if(s==0)
  172.             printf("\n\tLabel not found\n");
  173.         else
  174.         {
  175.             printf("\n\tEnter the new label : ");
  176.             scanf("%s",nl);
  177.             printf("\n\tEnter the new address : ");
  178.             scanf("%d",&add);
  179.             for(i=0; i<size; i++)
  180.             {
  181.                 if(strcmp(p->label,l)==0)
  182.                 {
  183.                     strcpy(p->label,nl);
  184.                     p->addr=add;
  185.                 }
  186.                 p=p->next;
  187.             }
  188.             printf("\n\tAfter Modification:\n");
  189.             Display();
  190.         }
  191.         break;
  192.     }
  193. }
  194. void Delete()
  195. {
  196.     int a;
  197.     char l[10];
  198.     struct SymbTab *p,*q;
  199.     p=first;
  200.     printf("\n\tEnter the label to be deleted : ");
  201.     scanf("%s",l);
  202.     a=Search(l);
  203.     if(a==0)
  204.         printf("\n\tLabel not found\n");
  205.     else
  206.     {
  207.         if(strcmp(first->label,l)==0)
  208.             first=first->next;
  209.         else if(strcmp(last->label,l)==0)
  210.         {
  211.             q=p->next;
  212.             while(strcmp(q->label,l)!=0)
  213.             {
  214.                 p=p->next;
  215.                 q=q->next;
  216.             }
  217.             p->next=NULL;
  218.             last=p;
  219.         }
  220.         else
  221.         {
  222.             q=p->next;
  223.             while(strcmp(q->label,l)!=0)
  224.             {
  225.                 p=p->next;
  226.                 q=q->next;
  227.             }
  228.             p->next=q->next;
  229.         }
  230.         size--;
  231.         printf("\n\tAfter Deletion:\n");
  232.         Display();
  233.     }
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement