Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include <stdbool.h>
- #define MAX 61
- #define input_length 500
- struct SymbolInfo{
- char symbol[input_length];
- char type[input_length];
- struct SymbolInfo* next;
- };
- struct SymbolInfo* SymbolTable[MAX];
- struct SymbolInfo* temp;
- void reset_table(){
- for(int i = 0; i < MAX; i++)
- SymbolTable[i] = NULL;
- }
- void print_message(){
- printf("\nPlease Choose option from below:");
- printf("\n1.Insert 2.Search 3.Delete 4.Show 0.EXIT");
- }
- int getHashKey(char const str[]){
- int length = strlen(str), sum = 0;
- for(int i = 0; i < length; i++)
- sum += str[i];
- return (sum % MAX);
- }
- void Insert(char const symbol[], char const type[]){
- int hashKey = getHashKey(symbol);
- temp = (struct SymbolInfo*) malloc(sizeof(struct SymbolInfo));
- strcpy(temp->symbol, symbol);
- strcpy(temp->type, type);
- temp->next = SymbolTable[hashKey];
- SymbolTable[hashKey] = temp;
- }
- struct SymbolInfo* Search(char const symbol[], char const type[]){
- int hashKey = getHashKey(symbol);
- temp = SymbolTable[hashKey];
- while(temp != NULL && (strcmp(temp->symbol, symbol) || strcmp(temp->type, type)))
- temp = temp->next;
- return temp;
- }
- bool Delete(char const symbol[], char const type[]){
- int hashKey = getHashKey(symbol);
- if(SymbolTable[hashKey] != NULL){
- temp = SymbolTable[hashKey];
- if(strcmp(temp->symbol, symbol)==0 && strcmp(temp->type, type)==0){
- SymbolTable[hashKey] = temp->next;
- free(temp);
- }
- else{
- while((temp->next != NULL) && (strcmp(temp->next->symbol, symbol) || strcmp(temp->next->type, type)))
- temp = temp->next;
- if((temp->next != NULL) && (strcmp(temp->next->symbol, symbol) == 0 && strcmp(temp->next->type, type) == 0)){
- struct SymbolInfo* temporary = temp->next;
- temp->next = temp->next->next;
- free(temporary);
- }
- else if((temp->next == NULL) && strcmp(temp->symbol, symbol) == 0 && strcmp(temp->type, type) == 0)
- free(temp);
- else return 0;
- }
- }
- else return 0;
- return 1;
- }
- void Show(){
- printf(" ______________________________________\n");
- for(int i = 0; i < MAX; i++){
- if(SymbolTable[i] != NULL){
- printf(" SymbolTable[%d] = ", i);
- temp = SymbolTable[i];
- while(temp->next != NULL){
- printf("(%s, %s)->", temp->symbol, temp->type);
- temp = temp->next;
- }
- printf("(%s, %s)\n", temp->symbol, temp->type);
- }
- }
- printf(" ______________________________________\n");
- }
- int main(){
- int number_of_element=0;
- print_message();
- int op;
- scanf("%d", &op);
- while(1){
- if(op == 0) return 0;
- else if(op == 4){
- if(number_of_element){
- printf("\nTotal number of token in SymbolTable: %d\n", number_of_element);
- Show();
- }
- else printf("SymbolTable is empty!!\n");
- }
- else{
- char symbol[input_length], type[input_length];
- printf("Input Symbol: \n");
- scanf("%s", symbol);
- printf("Input Type: \n");
- scanf("%s", type);
- if(op == 1){
- if(Search(symbol, type) == NULL){
- Insert(symbol, type);
- printf("Succesfully inserted token <%s, %s> in the SymbolTable\n",
- symbol, type);
- number_of_element++;
- }
- else printf("Token <%s, %s> already exists in SymbolTable", symbol, type);
- }
- else if(op == 2){
- if(Search(symbol, type) != NULL)
- printf("Token <%s, %s> exists in the SymbolTable\n", symbol, type);
- else printf("Token <%s, %s> does not exist in the SymbolTable\n", symbol, type);
- }
- else if(op == 3){
- if(Delete(symbol, type)){
- printf("Succesfully deleted token <%s, %s> from the SymbolTable\n",
- symbol, type);
- number_of_element--;
- }
- else printf("Not possible to delete, token <%s, %s> does not exist in the SymbolTable\n", symbol, type);
- }
- else printf("Wrong Keyword\n");
- }
- print_message();
- scanf("%d", &op);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement