473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

program to store students information using linear hashing with linked list in c++

please help me to complete the below written c++ program code. i have witten the code to insert student's information(case:1) and display it using linked list(case:5). Help me to write the code for case:2 -to count the no. of collisions in the hash table, case:3 -to show the values of hash table, case:4 -to search a value using the hash table. use roll no of the student to be stored in hash table using double linked list.

#include <iostream.h>
#include<conio.h>

struct node
{ char name[20]; // Name of up to 20 letters
int roll;
char branch[20];
node *nxt;// Pointer to next node
};

node *start_ptr = NULL;
node *current; // Used to move along the list
int option = 0;

void insert()
{ node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with data
temp = new node;
cout << "Please enter the name : ";
cin >> temp->name;
cout << "Please enter the roll : ";
cin >> temp->roll;
cout << "Please enter the branch : ";
cin >> temp->branch;
temp->nxt = NULL;

// Set up link to this node
if (start_ptr == NULL)
{ start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{ temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}

void display()
{ node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout<<"\nROLL\tNAME\tBRANCH\n";
cout <<temp->name <<"\t" << temp->roll <<"\t"<< temp->branch<<"\n";
temp = temp->nxt;

}
}
}
int main()
{
int c=0;
clrscr();
do
{
cout<<"\n****************PROJECT****************** ";
cout<<"\n0 : exit";
cout<<"\n1 : Insert into the table";
cout<<"\n2 : count no. of collisions";
cout<<"\n3 : show values";
cout<<"\n4 : search a value";
cout<<"\n5 : show all records";
cout<<"\nenter your choice";
cin>>c;


switch(c)
{
case 0:
cout<<"\n0";
break;

case 1:
cout<<"\nenter students info"<<endl;
insert();
break;

case 2:
cout<<"\n2";
break;

case 3:
cout<<"\n3";
break;

case 4:
cout<<"\n4";
break;

case 5:
cout<<"\nstudent's info:";
display();
break;

default:
cout<<"\nWRONG CHOICE";
}
} while(c!=0);


return 0;

}
Aug 20 '10 #1
1 6692
weaknessforcats
9,208 Expert Mod 8TB
It looks like you are after a thing called a chain-link table. Here you hash a a key to get a value.

Then you create a node for a linked list and out the key in the node. Then put the address of the node in the array.
Let's say you allow 100 values. The address of a node whose key has a hash value of 34 would go in array[34].

You start by initializing the array elements to 0. You can assign a node address to an array element if the element is currently 0. Otherwise, you have a collision.

On collisions you can a) report the collision an do nothing or b) add the address of the new node as the "next" node in a linked list. So if you hash 34 again, you go to array[34] to get the first node and add the new node and the "next" node for the linked list starting a array[34].

There is a good explanation for this in Algorithms in C++ by Robert Sedgewick.

You logic should look like:

1) hash the key.
2) create a node with the key
3) if the hash array element for the has value is 0 then assing the address of the node to the4 array element corresponding to the hash value. Otherwise, add the new node as the "next" node for that array element.
Aug 22 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
59
by: Anando | last post by:
Hi, I have a linear singly linked list of 100 elements. I would like to prune it such that only user specified elements (say only the elements 1, 13, 78 and 100 of the original list) survive...
2
by: hakimks | last post by:
You are provided with a sample C programs: calc.c, which implements a reverse polish notation calculator. Study it carefully. This program uses a stack (of course!) but the stack implementation is...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
1
by: inboxforsumit | last post by:
Hello friends... I have to submit an asginment on the following topic. Write a program to scan a text file for unique words and store these words alphabetically in a linked list. You have to take...
0
by: brankec | last post by:
I'm trying to write C program that reads data from dbf fajl. This is my first program with linked list. When I delete line1 and line2 program works, but then I dont have linked list. And when I use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.