//linkedlist.hnode
#ifndef LINKLIST_H
#define LINKLIST_H
#include<iostream>
using namespace std;ios
struct ListNode;
typedef ListNode *ListNodePtr;ui
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;
};this
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;
};spa
#endifci
//linkedlist.cppit
#include"linkedlist.h"
#include"assert.h"
#include<iostream>
#include"assert.h"
using namespace std;io
LinkedList::LinkedList()
{
cout << "test " << endl;
head = NULL;
}table
LinkedList::~LinkedList()
{
DestoryList();
}ast
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; }