Question
I am attempting to create a int FetchAddr (char *s) function in symtable.c file that looks at the list and find the match address of
I am attempting to create a int FetchAddr (char *s) function in symtable.c file that looks at the list and find the match address of a defined variable. Let me know if there is needed clarification on what I am asking. Here's my code for symtable.c so far:
#include /* #include */ #include #include #include #include "symtable.h"
int size=0;
/* commented out main method void main() { int op,y,add; char sym [1000];
do { printf(" \tSYMBOL TABLE IMPLEMENTATION "); printf(" \t1.INSERT \t2.DISPLAY \t3.DELETE \t4.SEARCH \t5.END "); printf(" \tEnter your option : "); scanf("%d",&op);
switch(op) { case 1: printf(" \tEnter symbol for insertion : "); scanf("%s", sym); printf(" \tEnter address for insertion : "); scanf("%d", &add); Insert(sym, add); break;
case 2: Display(); break;
case 3: printf(" \tEnter symbol to be removed : "); scanf("%s", sym); Delete(sym); break;
case 4: printf(" \tEnter the symbol to be searched : ");
scanf("%s", sym); y=Search(sym); printf(" \tSearch Result:");
if(y==1) printf(" \tThe symbol is present in the symbol table "); else printf(" \tThe symbol is not present in the symbol table "); break;
case 5: exit(0); } }
while(op < 5); } end of main */
/* Inserts an entry to the symbol table, if the symbol is not a duplicate of itself */ void Insert(char *symb, int address) { int n; n=Search(symb);
if(n==1) printf(" \tThe symbol exists already in the symbol table \tDuplicate can't be inserted"); else { struct SymbTab *p; p=malloc(sizeof(struct SymbTab)); p->sym = strdup(symb); p->addr = address; p->next=NULL;
if(size==0) { first=p; last=p; } /* case: not first entry */ else {
last->next=p; last=p; } /* increments size */ size++; } printf(" \tSymbol inserted "); } /* end of Insert method */
/* prints all contents of symbol table */ void Display() { int i; struct SymbTab *p; p=first; printf(" \tSYMBOL\t\tADDRESS ");
/*for each entry in linked list, prints out both the symbol and adddress input by the user */ for(i=0;i { printf("\t%s\t\t%d ",p->sym,p->addr); p=p->next; } }/* end Display method */
/* Parameter : (char *s) where it points to the string representing SymTab symbol */ /* search the symbol table for any kind of symbol and returns 1 if found, 0 if not found */ int Search(char *s) { int i,flag=0; struct SymbTab *p; p=first;
/* iterates through the linked list while searching for specified symbol*/ for(i=0;i { if(strcmp(p->sym,s)==0) flag=1; p=p->next; } return flag; } /* end Search method */
/* searches symbol table for the symbol that the user types in and removes it if found * returns string with specified sumbol removed */ void Delete(char *s) { int a; struct SymbTab *p,*q; q = NULL; p=first;
/* check to see if symbol is in symbol table */ a=Search(s);
if(a==0) printf(" \tSymbol not found "); else { while (strcmp(p->sym, s) != 0) { q = p; p = p->next; } /* end while loop */
/* lecated at head of the list */ if(q == NULL) { first = p->next; } /* end if */
/*located at tail of the list */ else if (p == last) { last = q; last->next = NULL; } /* end else if */
/* located in the middle of the list */ else { q->next = p->next; } /* end else */
size--; printf(" \tAfter Deletion: "); Display(); } /* end outer else block */ } /* end delete method */
/*FetchAddr function that will return an address with defined variable*/
//Prototype:
int FetchAddr(char *s) {
} */end FetchAddr method*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started