Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdint.h>
- //#include <appid_sessionstore.c>
- struct hash *hashTable = NULL;
- uint32_t eleCount = 0;
- struct node
- {
- uint32_t ip1, ip2;
- uint16_t p1, p2,sessid;
- //char name[100];
- struct node *next;
- };
- struct hash
- {
- struct node *head;
- uint32_t count;
- };
- uint32_t getIndex(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2)
- {
- return (ip1 + ip2 + p1 + p2) % eleCount;
- // return 1;
- }
- uint32_t isSmaller(uint32_t a, uint32_t b)
- {
- if (a < b)
- return 1;
- return 0;
- //a < b ? return 1 : return 0;
- }
- /*int isLarger(int a, int b)
- {
- a > b ? return 1 : return 0;
- u
- }
- */
- struct node * createhNode(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2, uint16_t sessid)
- {
- struct node *newnode;
- newnode = (struct node *) malloc(sizeof(struct node));
- if (isSmaller(ip1, ip2))
- {
- newnode->ip1 = ip1;
- newnode->ip2 = ip2;
- newnode->p1 = p1;
- newnode->p2 = p2;
- newnode->sessid = sessid;
- }
- else
- {
- newnode->ip1 = ip2;
- newnode->ip2 = ip1;
- newnode->p1 = p2;
- newnode->p2 = p1;
- newnode->sessid = sessid;
- }
- //strcpy(newnode->name, name);
- newnode->next = NULL;
- return newnode;
- }
- int searchInHash(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2)
- {
- uint32_t swap_ip;
- uint16_t swap_port;
- if (!isSmaller(ip1, ip2))
- {
- swap_ip = ip1;
- ip1 = ip2;
- ip2 = swap_ip;
- swap_port = p1;
- p1 = p2;
- p2 = swap_port;
- }
- uint32_t hashIndex = getIndex(ip1, ip2, p1, p2), flag = 0;
- struct node *myNode=NULL;
- myNode = hashTable[hashIndex].head;
- if (!myNode) {
- printf("Search element unavailable in hash table\n");
- return 0;
- }
- while (myNode != NULL) {
- if (myNode->ip1 == ip1 && myNode->ip2 == ip2 &&
- myNode->p1 == p1 && myNode->p2 == p2)
- {
- /* printf("VoterID : %d\n", myNode->key);
- printf("Name : %s\n", myNode->name);
- printf("Age : %d\n", myNode->age);
- */
- printf("Found %u %u %u %u\n", myNode->ip1, myNode->p1, myNode->ip2, myNode->p2);
- flag = 1;
- break;
- }
- myNode = myNode->next;
- }
- if (!flag)
- {
- printf("Search element unavailable in hash table\n");
- return 0;
- }
- return 1;
- }
- int insertToHash(uint32_t ip1, uint32_t ip2, uint16_t p1, uint16_t p2, uint16_t sessid)
- {
- if (searchInHash(ip1, ip2, p1, p2) == 1)
- {
- return 0;
- }
- uint32_t hashIndex = getIndex(ip1, ip2, p1, p2);
- struct node *newnode = createhNode(ip1, ip2, p1, p2,sessid);
- /* head of list for the bucket with index "hashIndex" */
- if (!hashTable[hashIndex].head) {
- hashTable[hashIndex].head = newnode;
- insertion(sessid,newnode); // appid_sessionstore
- hashTable[hashIndex].count = 1;
- return 1;
- }
- /* adding new node to the list */
- newnode->next = (hashTable[hashIndex].head);
- /*
- * update the head of the list and no of
- * nodes in the current bucket
- */
- hashTable[hashIndex].head = newnode;
- insertion(sessid,newnode);
- hashTable[hashIndex].count++;
- return 1;
- }
- void deleteFromHash(int ip1, int ip2, int p1, int p2)
- {
- uint32_t swap_ip;
- uint16_t swap_port;
- if (!isSmaller(ip1, ip2))
- {
- swap_ip = ip1;
- ip1 = ip2;
- ip2 = swap_ip;
- swap_port = p1;
- p1 = p2;
- p2 = swap_port;
- }
- /* find the bucket using hash index */
- uint32_t hashIndex = getIndex(ip1, ip2, p1, p2), flag = 0;
- struct node *temp, *myNode;
- /* get the list head from current bucket */
- myNode = hashTable[hashIndex].head;
- if (!myNode)
- {
- printf("Given data is not present in hash Table!!\n");
- return;
- }
- temp = myNode;
- while (myNode != NULL) {
- /* delete the node with given key */
- if (myNode->ip1 == ip1 && myNode->ip2 == ip2 &&
- myNode->p1 == p1 && myNode->p2 == p2)
- {
- flag = 1;
- deletion(myNode->sessid);
- if (myNode == hashTable[hashIndex].head)
- hashTable[hashIndex].head = myNode->next;
- else
- temp->next = myNode->next;
- hashTable[hashIndex].count--;
- free(myNode);
- break;
- }
- temp = myNode;
- myNode = myNode->next;
- }
- if (flag)
- printf("Data deleted successfully from Hash Table\n");
- else
- printf("Given data is not present in hash Table!!!!\n");
- return;
- }
- void deleteFromHashbySessId(uint16_t sessid)
- {
- struct node *temp = searchHead(sessid);
- deleteFromHash(temp->ip1,temp->ip2,temp->p1,temp->p2);
- }
- void display()
- {
- struct node *myNode;
- uint32_t i;
- for (i = 0; i < eleCount; i++) {
- if (hashTable[i].count == 0)
- continue;
- myNode = hashTable[i].head;
- if (!myNode)
- continue;
- printf("\nData at index %u in Hash Table:\n", i);
- printf("ip1 p1 ip2 p2\n");
- printf("--------------------------------------------\n");
- while (myNode != NULL) {
- printf("%u ", myNode->ip1);
- printf("%u ", myNode->p1);
- printf("%u ", myNode->ip2);
- printf("%u\n", myNode->p2);
- myNode = myNode->next;
- }
- }
- return;
- }
- void initializeHash(uint16_t n)
- {
- if(hashTable==NULL)
- {
- hashTable = (struct hash *) calloc(n, sizeof(struct hash));
- }
- }
- /*int main()
- {
- uint32_t n, ip1, ip2, p1,p2;
- int ch;
- printf("Enter the number of index:");
- scanf("%d", &n);
- eleCount = n;
- create hash table with "n" no of buckets
- hashTable = (struct hash *) calloc(n, sizeof(struct hash));
- while (1)
- {
- printf("\n1. Insertion\t2. Deletion\n");
- printf("3. Searching\t4. Display\n5. Exit\n");
- printf("Enter your choice:");
- scanf("%d", &ch);
- switch (ch)
- {
- case 1:
- printf("Enter ip1:");
- scanf("%u", &ip1);
- getchar();
- printf("Enter p1:");
- scanf("%u", &p1);
- getchar();
- printf("Enter ip2:");
- scanf("%u", &ip2);
- getchar();
- printf("Enter p2:");
- scanf("%u", &p2);
- getchar();
- printf("Name:");
- fgets(name, 100, stdin);
- name[strlen(name) - 1] = '\0';
- printf("Age:");
- scanf("%d", &age);
- inserting new node to hash table
- if (insertToHash(ip1, ip2, p1, p2))
- {
- printf("\nInserted\n");
- }
- else
- {
- printf("\nDid not insert\n");
- }
- break;
- case 2:
- printf("Enter ip1:");
- scanf("%u", &ip1);
- getchar();
- printf("Enter p1:");
- scanf("%u", &p1);
- getchar();
- printf("Enter ip2:");
- scanf("%u", &ip2);
- getchar();
- printf("Enter p2:");
- scanf("%u", &p2);
- delete node with "key" from hash table
- deleteFromHash(ip1, ip2, p1, p2);
- break;
- case 3:
- printf("Enter ip1:");
- scanf("%u", &ip1);
- getchar();
- printf("Enter p1:");
- scanf("%u", &p1);
- getchar();
- printf("Enter ip2:");
- scanf("%u", &ip2);
- getchar();
- printf("Enter p2:");
- scanf("%u", &p2);
- searchInHash(ip1, ip2, p1, p2);
- break;
- case 4:
- display();
- break;
- case 5:
- exit(0);
- default:
- printf("U have entered wrong option!!\n");
- break;
- }
- }
- return 0;
- }*/
Add Comment
Please, Sign In to add comment