![]() |
#2
rjsp2016-10-10 08:13
|
struct listsNode *pos ;
pos = find(user, hash) ;
这里find函数返回值也是struct listsNode *类型的为什么提示说
不能将struct Node* 的值赋给struct Node *实体。

#include <iostream>
#include <stdlib.h>
#include<malloc.h>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
struct userNode
{
string account ;//账号
string password ;//密码
char order;//命令符,判断是登陆还是申请
};
struct hashHead
{
int tableSize;
struct listNode *theLists;
};
struct listNode
{
struct userNode userMessage;
struct listNode *next;
};
struct hashHead* initHashTable( int tableSize )
{
tableSize = nextPrime( tableSize ) ;
struct hashHead* hash;
hash = (struct hashHead* )malloc( sizeof( struct hashHead ) ) ;
hash->tableSize = tableSize ;
hash->theLists = (struct listNode*) malloc(tableSize* sizeof( struct listNode ) ) ;
for ( int i = 0; i < tableSize; ++i )
{
hash->theLists[i].next = NULL ;
hash->theLists[i].userMessage.account = "0";//账号密码全部默认为0
hash->theLists[i].userMessage.password = "0";
hash->theLists[i].userMessage.order = '0';
}
return hash;
}
int nextPrime( int size )
{
int i;
if(size % 2 == 0 ) ++size;
for ( ; ; size += 2 )
{
for (i = 3; i*i < size; ++i)//判断size是否为素数
if (size % i == 0) break ;
if (i*i > size) return size ;
}
}
void checkAndOutPut( struct userNode *user , struct hashHead* hash )
{
//将账号后五位数字作为有效数字,用于计算散列址
int address ;
//number = atoi( (user->account+5) ) ;
address= atoi( user->account.c_str() )% hash->tableSize ;//未截取
struct listsNode *pos ;
pos = find(user, hash) ;//出错了??
if ( pos )//找到后核对用户信息
{
if (user->password == find(user, hash)->userMessage.password)
cout << "密码正确" << "登录中" << endl;
else
cout << "密码错误"<<endl;
}
else//pos=NULL时未找到,此时插入改节点
{
insertList( user, hash , address ) ;
cout << "建立改账号" << endl ;
}
}
void insertList(struct userNode *user, struct hashHead* hash, int address)
{
struct listNode *headPos;
headPos = &hash->theLists[address];
struct listNode *insertNode;
insertNode = (struct listNode*)malloc( sizeof( struct listNode ) ) ;
insertNode->userMessage = *user ; //将user内容传给改节点
insertNode->next = headPos ;//头插
headPos = insertNode ;
}
struct listNode * find(struct userNode *user, struct hashHead* hash )
{
int address;
//计算散列址
address = atoi( user->account.c_str() ) % hash->tableSize;
struct listNode *pos ;
pos = &hash->theLists[address] ;
while ( !pos->next && pos->userMessage.account != user->account )//存在下一个节点且不为key
pos = pos->next ;
return pos ;
}
int main()
{
int N ;
scanf("%d", &N) ;
struct hashHead* hash;
hash = initHashTable(10000) ;
for (int i = 1; i <= N; ++i)
{
userNode *user ;//传引用去掉*
user = (struct userNode*)malloc(sizeof(struct userNode)) ;
scanf("%c %s %s", &user->order, &user->account,&user->password ) ;
//查找,并输出账号信息,存在,不存在等
checkAndOutPut( user ) ;
}
return 0 ;
}
#include <stdlib.h>
#include<malloc.h>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
struct userNode
{
string account ;//账号
string password ;//密码
char order;//命令符,判断是登陆还是申请
};
struct hashHead
{
int tableSize;
struct listNode *theLists;
};
struct listNode
{
struct userNode userMessage;
struct listNode *next;
};
struct hashHead* initHashTable( int tableSize )
{
tableSize = nextPrime( tableSize ) ;
struct hashHead* hash;
hash = (struct hashHead* )malloc( sizeof( struct hashHead ) ) ;
hash->tableSize = tableSize ;
hash->theLists = (struct listNode*) malloc(tableSize* sizeof( struct listNode ) ) ;
for ( int i = 0; i < tableSize; ++i )
{
hash->theLists[i].next = NULL ;
hash->theLists[i].userMessage.account = "0";//账号密码全部默认为0
hash->theLists[i].userMessage.password = "0";
hash->theLists[i].userMessage.order = '0';
}
return hash;
}
int nextPrime( int size )
{
int i;
if(size % 2 == 0 ) ++size;
for ( ; ; size += 2 )
{
for (i = 3; i*i < size; ++i)//判断size是否为素数
if (size % i == 0) break ;
if (i*i > size) return size ;
}
}
void checkAndOutPut( struct userNode *user , struct hashHead* hash )
{
//将账号后五位数字作为有效数字,用于计算散列址
int address ;
//number = atoi( (user->account+5) ) ;
address= atoi( user->account.c_str() )% hash->tableSize ;//未截取
struct listsNode *pos ;
pos = find(user, hash) ;//出错了??
if ( pos )//找到后核对用户信息
{
if (user->password == find(user, hash)->userMessage.password)
cout << "密码正确" << "登录中" << endl;
else
cout << "密码错误"<<endl;
}
else//pos=NULL时未找到,此时插入改节点
{
insertList( user, hash , address ) ;
cout << "建立改账号" << endl ;
}
}
void insertList(struct userNode *user, struct hashHead* hash, int address)
{
struct listNode *headPos;
headPos = &hash->theLists[address];
struct listNode *insertNode;
insertNode = (struct listNode*)malloc( sizeof( struct listNode ) ) ;
insertNode->userMessage = *user ; //将user内容传给改节点
insertNode->next = headPos ;//头插
headPos = insertNode ;
}
struct listNode * find(struct userNode *user, struct hashHead* hash )
{
int address;
//计算散列址
address = atoi( user->account.c_str() ) % hash->tableSize;
struct listNode *pos ;
pos = &hash->theLists[address] ;
while ( !pos->next && pos->userMessage.account != user->account )//存在下一个节点且不为key
pos = pos->next ;
return pos ;
}
int main()
{
int N ;
scanf("%d", &N) ;
struct hashHead* hash;
hash = initHashTable(10000) ;
for (int i = 1; i <= N; ++i)
{
userNode *user ;//传引用去掉*
user = (struct userNode*)malloc(sizeof(struct userNode)) ;
scanf("%c %s %s", &user->order, &user->account,&user->password ) ;
//查找,并输出账号信息,存在,不存在等
checkAndOutPut( user ) ;
}
return 0 ;
}