Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node{
- int data;
- struct node* next;
- };
- struct node* newNode(int item){
- struct node* newnode;
- newnode = (struct node*)malloc(sizeof(struct node));
- newnode->data=item;
- newnode->next=NULL;
- return newnode;
- }
- void insertAtEnd(struct node* head,int item){
- struct node* newnode;
- newnode = (struct node*)malloc(sizeof(struct node));
- newnode->data=item;
- newnode->next=NULL;
- if(head!=NULL){
- struct node* temp=head;
- while(temp->next!=NULL){
- temp=temp->next;
- }
- temp->next=newnode;
- }
- }
- void display(struct node* head){
- if(head==NULL){
- return;
- }
- struct node* current=head;
- while(current->next!=NULL){
- printf("%d, ",current->data);
- current=current->next;
- }
- printf("%d",current->data);
- }
- void closedHashing(){
- int hashTable[10];
- int index,ele,count,arg;
- arg=1;
- for(int i=0;i<10;i++){
- hashTable[i]=-1;
- }
- while(arg==1){
- printf("Enter element to be inserted:");
- scanf("%d",&ele);
- index = ele%10;
- if(hashTable[index]==-1)
- hashTable[index]=ele;
- else{
- count=0;
- while(count < 9){
- if(index==9)
- index=-1;
- if(hashTable[++index]==-1){
- hashTable[index]=ele;
- break;
- }
- else{
- count++;
- }
- }
- if(count>=9)
- printf("\nNo space in table\n");
- }
- printf("\nHash Table\n");
- for(int i=0;i<10;i++){
- if(hashTable[i]==-1){
- printf("%d\t-\n",i);
- }
- else
- printf("%d\t%d\n",i,hashTable[i]);
- }
- printf("Do you want to continue the process:'1' for yes and '0' for no.\n");
- scanf("%d",&arg);
- }
- }
- void openHashing(){
- struct node* hashTable[10];
- int index,ele,arg;
- arg=1;
- for(int i=0;i<10;i++){
- hashTable[i]=NULL;
- }
- while(arg==1){
- printf("Enter element to be inserted:");
- scanf("%d",&ele);
- index = ele%10;
- if(hashTable[index]==NULL)
- hashTable[index]=newNode(ele);
- else{
- insertAtEnd(hashTable[index],ele);
- }
- printf("\nHash Table\n");
- for(int i=0;i<10;i++){
- if(hashTable[i]==NULL){
- printf("%d\t-\n",i);
- }
- else{
- printf("%d\t",i);
- display(hashTable[i]);
- printf("\n");
- }
- }
- printf("Do you want to continue the process:'1' for yes and '0' for no.\n");
- scanf("%d",&arg);
- }
- }
- int main(){
- int ch;
- while(1){
- printf("*******");
- printf("\n1. Closed Hashing\n2. Open Hashing\n0. Exit\n");
- printf("*******\n");
- scanf("%d",&ch);
- switch(ch){
- case 1:
- closedHashing();
- break;
- case 2:
- openHashing();
- break;
- case 0:
- exit(0);
- default:
- printf("Invalid argument!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement