哈希表的实现

//linkedlist.h

#ifndef LINKLIST_H
#define LINKLIST_H
#include<iostream>
using namespace std;

struct ListNode;
typedef ListNode *ListNodePtr;

struct ListNode
{
 ListNode()
 {
  memset(this, 0, sizeof(ListNode));
 }
 char uid[10];
 char pid[10];
 char ppid[10];
 char stime[20];
 char tty[10];
 char time[10];
 char path[30];
 int total_process;
 ListNodePtr link;
};

class LinkedList
{
private:
 
public:
 ListNodePtr head;
 LinkedList();
 LinkedList(char*,char*,char*,char*,char*,char*,char*,int total_process=0,int minutes=0,int seconds=0);
 ~LinkedList();
 void InsertLast(ListNode);
 void DestoryList();
 bool isEmpty()const;
 void Display()const;
};

#endif

//linkedlist.cpp

#include"linkedlist.h"
#include"assert.h"
#include<iostream>
#include"assert.h"
using namespace std;

LinkedList::LinkedList()
{
 cout << "test " << endl;
 head = NULL;
}

LinkedList::~LinkedList()
{
 DestoryList();
}

void LinkedList::InsertLast(ListNode newItem)
{
 ListNodePtr tmp;
 tmp = new ListNode;
 strcpy(tmp->uid,newItem.uid);
 strcpy(tmp->pid,newItem.pid);
 strcpy(tmp->ppid,newItem.ppid);
 strcpy(tmp->stime,newItem.stime);
 strcpy(tmp->tty,newItem.tty);
 tmp->link = NULL;
 
 cout<<"@@@"<<endl;
 cout<<tmp->uid<<endl;
 cout<<tmp->pid<<endl;
 cout<<tmp->ppid<<endl;
 cout<<tmp->stime<<endl;
 cout<<tmp->tty<<endl;

 ListNodePtr current;
 cout<<"222"<<endl;
 if(newItem.link==NULL)
 {  //error!!!!!!!!!!
  cout<<"444"<<endl;

  head = tmp;
 }
 else
 {
  cout<<"333"<<endl;
  current = head;
  while(current->link!=NULL)  //find the last node
   current = current->link;
  current->link = tmp;
 }
 cout<<"111"<<endl;
 cout<<current->uid<<endl;

}

void LinkedList::DestoryList()
{
 ListNodePtr tmp;
 while(head!=NULL)
 {
  tmp = head;
  head = head->link;
  delete tmp;
 }
}

bool LinkedList::isEmpty()const
{
 if(head==NULL)
  return true;
 return false;
}

void LinkedList::Display()const
{
 if(head!=NULL)
 {
  ListNodePtr current;
  current = head;
  while(current!=NULL && current->link!=NULL)
  {
   cout<<"User "<<current->uid<<endl;
   cout<<"============"<<endl;
   cout<<endl;

   //cout<<"User "<<current->uid<<"has a total of "<<current->total_process<<" processes"<<endl;
   //cout<<"User "<<current->uid<<"has consumed a total of "<<current->minutes<<" and "<<current->seconds<<" seconds "<<" of CPU time ."<<endl;
   //cout<<"The process id with the longest path name is "<<current->path<<endl;
   current = current->link;
  }
 }
}

 

//table.h

#ifndef TABLE_H_
#define TABLE_H_
#include"linkedlist.h"

typedef LinkedList *LinkedListPtr;
int maxSize = 120;

class Table
{
private:
 LinkedListPtr Hashtable[120];
 int key;
public:
 Table()
 {
  key=0;
  for(int i = 0; i < 120; i++)
  {
   Hashtable[i] = new LinkedList;
   Hashtable[i]->head = NULL;
  }
 }
 int makeHashKey(char *);
 void Insert(ListNode);
 void Display()const;
 bool isEmpty()const;

};

#endif

 

//main.cpp

#include"table.h"
#include"linkedlist.h"
#include<iostream>
using namespace std;

int Table::makeHashKey(char *str)
{
 int sum = 0;
 int length = strlen(str);
 for(int i=0;i<length;i++)
  sum += str[i];
 sum %= maxSize;
 return sum;
}

void Table::Insert(ListNode n)
{
 cout<<"Insert function ------------"<<endl;
 int key = makeHashKey(n.uid);
 if(Hashtable[key]==NULL)
 {
  cout<<"jin"<<endl;
  Hashtable[key]->InsertLast(n);
 
  cout<<"Done"<<endl;
 }
}


void Table::Display()const
{
 for(int i=0;i<maxSize;i++)
  if(Hashtable[i]!=NULL)
  {
   cout<<"@!@"<<endl;
   Hashtable[i]->Display();
  }

}

bool endOfTime(char *t)
{
 if(strlen(t)==3)
  return false;
 return true;
}

int main()
{
 ListNode node;
 Table t1;
 cin>>node.uid>>node.pid>>node.ppid>>node.stime;
/* 
 if(!endOfTime(node.stime))
 {
  cout<<"In the function."<<endl;
  char tmp[10];
  cin>>tmp;
  strcat(node.stime," ");
  strcat(node.stime,tmp);
 }
 */
 cout<<"Out the function."<<endl;
 cin>>node.tty>>node.time>>node.path;
 cout<<"Done"<<endl;
 t1.Insert(node);
 t1.Display();

 return 0;
}

转载于:https://my.oschina.net/u/142173/blog/59811

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值