0% found this document useful (0 votes)
6 views2 pages

Hashing and Hash Tables

The document outlines the implementation of a hash table for storing student records, including functions for creating the table, adding new records, finding records, and deleting records. It defines a hash function that uses the modulus operator to determine the index for a given student ID. The procedures handle collisions through linear probing and manage the state of the hash table effectively.

Uploaded by

Mazen Zaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Hashing and Hash Tables

The document outlines the implementation of a hash table for storing student records, including functions for creating the table, adding new records, finding records, and deleting records. It defines a hash function that uses the modulus operator to determine the index for a given student ID. The procedures handle collisions through linear probing and manage the state of the hash table effectively.

Uploaded by

Mazen Zaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Hashing and Hash Tables

Create a HashTable
Hash Function
Add New Record To Hash Table
Find a record in the hash table
Deleting a record

FUNCTION Hash(Key:INTEGER)
DECLARE hashval : INTEGER
hashVal ß Key MOD 15
RETURN hashVal
END FUNCTION

TYPE STUDENT
DECLARE Student Id : INTEGER
DECLARE Student Gpa : REAL
END TYPE

DECLARE Class : ARRAY [0:14] OF STUDENT

FOR x ß 0 TO 14
Class[x].Id ß -1
Class[x].Gpaß0.0
NEXT
END FOR

DECLARE Free : INTEGER


Free ß 15

PROCEDURE AddToTable(NewId : INTEGER, NewGpa:REAL)


DECLARE Index : INTEGER
IF Free>0
IndexßHash(NewId)
WHILE Class[Index].ID<>-1
IndexßIndex+1
IF Index>14
Indexß0
END IF
END WHILE
Class[Index].Id ß NewId
Class[Index].Gpa ß NewGpa
FreeßFree-1
ELSE
OUTPUT “No more space”
END IF
END PROCEDURE
PROCEDURE Find(FindId : INTEGER)
DECLARE Index,Start : INTEGER
DECLARE Wrapped : BOOLEAN
WrappedßFALSE
IF Free<15
IndexßHash(FindId)
StartßIndex
WHILE Wrapped=FALSE AND Class[Index].ID <>FindId
IndexßIndex+1
IF Index>14
Indexß0
END IF
IF Index=Start
WrappedßTRUE
END IF
END WHILE
IF Wrapped = TRUE
OUTPUT “Not Found”

ELSE
OUTPUT “The Gpa of student is”, Class[Index].Gpa
END IF
ELSE
OUTPUT “There are not elements in the array”
END IF
END PROCEDURE

PROCEDURE Delete(FindId : INTEGER)


DECLARE Index,Start : INTEGER
DECLARE Wrapped : BOOLEAN
WrappedßFALSE
IF Free<15
IndexßHash(FindId)
StartßIndex
WHILE Wrapped=FALSE AND Class[Index].ID <>FindId
IndexßIndex+1
IF Index>14
Indexß0
END IF
IF Index=Start
WrappedßTRUE
END IF
END WHILE
IF Wrapped = TRUE
OUTPUT “Not Found”
RETURN -1
ELSE
Class[Index].ID ß -1
Class[Index].Gpa ß0.0
END IF
ELSE
OUTPUT “There are no elements in the array”
END IF
END PROCEDURE

You might also like