本文介绍了错误C4700:使用了未初始化的局部变量'currPtr'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
#include <cstring>
#include <fstream>
#include <sstream>
#include<conio.h>
#include <string>
using namespace std;



// Global variables declarations
int MAX_FILE_SIZE = 50;
int MAX_FREQUENCY_LIST = 250;
const int MAX_TABLE_SIZE = 300;
int MAX_QUERY_LIST = 10;


// *************************************************************
// Structures declarations
// *************************************************************
// nodeData structure
// It stores int frequency, int fileNo, and nodeData pointer
// next.
struct nodeData
{
	// Variables declarations
	int frequency;
	int fileNo;
	struct nodeData * next;
};
typedef struct nodeData NodeData;
typedef struct nodeData * NodeDataPtr;

// node structure
// It stores string key, node pointer next, and nodeData pointer
// datanext.
struct node
{
	// Variables declarations
	string key;
	struct node * next;
	struct nodeData * dataNext;
};

typedef struct node Node;
typedef struct node * NodePtr;

// HashTable class
// It stores data into a data dArray containing 300 items.
// It also defines functions: Constructor HashTable(),
// insert(), print() and search() functions.
class HashTable
{
// Private variables and structures declaration
private:
	// Structure declaration struct named data
	struct data
	{
		NodePtr root;
	};

	// Declare dArray[300]
	data dArray[300];
	int numOfData;

// Public functions declaration prototypes
public:
	// Constructor HashTable()
	// HashTable() function set all the NodePtr root to NULL
	HashTable();
	
	// hash() function
	// Parameters list: string key
	// hash() function calculate the hash value index of the
	// input key and return to the caller
	int hash(string key);

	// insert() function
	// Parameters list: string key, int frequency, int fileNo
	// Functions call list: hash() 
	// insert() function calls the hash() function to get the
	// index of key in HashTable to input and store the key,
	// and frequency to the HashTable according to the index.
	void insert(string key, int frequency, int fileNo);

	// print() function
	// print() function prints all the whole HashTable
	void print();

	// search function
	// Parameters list: string key
	// Functions call list: hash()
	// search function calls the hash() function to get the
	// HashTable index and return the NodeDataPtr to the caller
	NodeDataPtr search(string key);
};




// Constructor HashTable()
// HashTable() function set all the NodePtr root to NULL
HashTable::HashTable()
{
	// Set all dArray root = NULL
	for(int i = 0; i < MAX_TABLE_SIZE; i++)
	{
		dArray[i].root = NULL;
	}
}
// End of constructor HashTable()

// Hash() function
// Parameters list: string key
// Hash() function calculate the hash value index of the
// input key and return to the caller
int HashTable::hash(string key)
{	
	// Variables declarations
	unsigned int hashValue;
	int a;
	a=key.length();
	for (int i = 0; i <key.length()>	{
		// Hash function
		hashValue ^= (hashValue << 5) + (hashValue >> 2) + key[i];
	}
	
	return hashValue % MAX_TABLE_SIZE;
}
// End of hash() function

// Insert() function
// Parameters list: string key, int frequency, int fileNo
// Functions call list: hash() 
// Insert() function calls the hash() function to get the
// index of key in HashTable to input and store the key,
// and frequency to the HashTable according to the index.
void HashTable::insert(string key, int frequency, int fileNo)
{
	// Variables declarations
	// Call hash() function and store in int index
	int index = hash(key);
	bool inserted = false;

	// If the hashtable is empty, add the newNode to the root
	if(dArray[index].root == NULL)
	{
		NodePtr newNode = new Node();
		newNode->key = key;
		newNode->next = NULL;

		NodeDataPtr newNodeData = new nodeData();
		newNodeData->frequency = frequency;
		newNodeData->fileNo = fileNo;
		newNodeData->next = NULL;
		
		newNode->dataNext = newNodeData;

		dArray[index].root = newNode;
		dArray[index].root->dataNext = newNodeData;
	}
	// If the hashtable is not empty, add the newNode to the
	// root
	else
	{
		NodePtr currPtr = dArray[index].root;
		NodeDataPtr currDataPtr = currPtr->dataNext;

		while(currPtr != NULL)
		{
			if((key.compare(currPtr->key)) == 0 && (inserted == false))
			{
				NodeDataPtr newNodeData = new nodeData();
					
				newNodeData->frequency = frequency;
				newNodeData->fileNo = fileNo;
				newNodeData->next = currPtr->dataNext;

				currPtr->dataNext = newNodeData;
				inserted = true;
			}
			currPtr = currPtr->next;
		}
	
		if(inserted == false)
		{
			NodePtr newNode = new Node();
			newNode->key = key;
			
			NodeDataPtr newNodeData = new nodeData();
			newNodeData->frequency = frequency;
			newNodeData->fileNo = fileNo;
			newNodeData->next = NULL;
		
			newNode->dataNext = newNodeData;

			newNode->next = dArray[index].root;
			dArray[index].root = newNode;
		}
	}
}
// End of insert() function

// Print() function
// Print() function prints all the whole HashTable
void HashTable::print()
{
	// Variables declarations
	NodePtr currPtr;
	NodeDataPtr currDataPtr = currPtr->dataNext;
	
	// Loop through the whole HashTable
	for(int i = 0; i < MAX_TABLE_SIZE; i++)
	{
		cout << i + 1 << ": ";

		// If dArray[i].root != NULL, print the fileNo,
		// and frequency appear in the document
		if(dArray[i].root != NULL)
		{	
			NodePtr currPtr = dArray[i].root;
			NodeDataPtr currDataPtr = currPtr->dataNext;
			
			while(currPtr != NULL)
			{
				cout << currPtr->key << " ";

				currDataPtr = currPtr->dataNext;

				while(currDataPtr != NULL)
				{
					if(currDataPtr != NULL)
						cout << "@" << currDataPtr->fileNo << " (" << currDataPtr->frequency << ") ";
					currDataPtr = currDataPtr->next;
				}
				
				currPtr = currPtr->next;
			}
		}
		cout << endl;
	}
}
// End of print() function

// Search function
// Parameters list: string key
// Functions call list: hash()
// Search function calls the hash() function to get the
// HashTable index and return the NodeDataPtr to the caller
NodeDataPtr HashTable::search(string key)
{
	// Variables declarations
	// Call hash() function and store in int index
	int index = hash(key);
	NodeDataPtr currNodeDataPtr = NULL;

	// If dArray[index] = NULL
	if(dArray[index].root == NULL) 
	{ 
		return NULL;
	}
	// Else
	else 
	{ 
		NodePtr currPtr = dArray[index].root;

		// Loop through the linked list, return when the key
		// matches the currPtr->key
		while(currPtr != NULL)
		{
			if(key.compare(currPtr->key) == 0)
			{
				currNodeDataPtr = currPtr->dataNext;

				while(currNodeDataPtr != NULL)
				{
					// Use to test if searching the correct fileNo
					cout << "@Doc" << currNodeDataPtr->fileNo << " ";
					return currNodeDataPtr;
					currNodeDataPtr = currNodeDataPtr->next;
				}
				break;
			}
			else
				currPtr = currPtr->next;
		}
	}
	return NULL;
}

推荐答案


这篇关于错误C4700:使用了未初始化的局部变量'currPtr'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 06:36