0

I am having with some problems with a project I am working on. I need to output simple statistics of the "ps -ef" using the line "ps -ef | a.out", a.out being my program.

To store the information I need to use a Hashtable of Linked Lists, without using STL. The Hashtable gets the key by attaining the ASCII value of all the elements in the username string, adding them one by one and dividing by the total (120). This key + value points to a linkedlist of the same name with all the info, which also needs to then be able to print out the total time, etc.

My major problem is with importing the info from input straight into the linked list, and then making the key.

My code is below, and doesn't exactly compile. Can anyone please run through it quickly and tell me what I am doing wrong, I can't get it to work.

Thanks in advance,
Matt.

Note: Sorry, none of the buttons seem to be working for me, or the file uploader!

#include <iostream>

#include <string>

using namespace std;

// Global Constants
const int MAX = 120; // Max users



// Node Data Structure

struct Node{

	string uid;	// Username Identification

	int pid;	// Process User Identification
	int ppid;	// Parent Processer Identification
	int c;		// C

	string sysTime;	// System Time
	string TTY;	// Terminal Process
	int mm, ss;	// Total Running Time: Minutes, Seconds

	string cmd;	// Command Line

	Node *Next;	// Link

	};



// Linked List Data Structure	

class LinkedList{

	private:

		Node *Current, *Head, nodeData;	// Node Pointers
		int totalMins, totalSecs;
		string lngCmd;

	public:

		LinkedList();		   // Constructor

		void Add();		   // Adds To The Linked List
		void printUserName();	   // Prints user name
		void printPid();	   // Prints Parent Identification
		int getTotalMinutes();	   // Returns total amount of minutes for a user
		int getTotalSeconds();     // Returns total amount of seconds for a user
		void checkLongestCmd();	   // Checks if a string is longer than previous
		string returnLongestCmd(); // Returns longest command

		bool isEmpty();		   // Checks if list is empty

		~LinkedList();		   // Destructor

	};

	

LinkedList::LinkedList(){

	Head = NULL;

	}

	

void LinkedList::Add(){

	Node *Temp;

	Temp = new Node;

	Temp->uid = nodeData.uid;

	Temp->pid = nodeData.pid;
	Temp->ppid = nodeData.ppid;
	Temp->c = nodeData.c;
	Temp->sysTime = nodeData.sysTime;
	Temp->TTY = nodeData.TTY;

	Temp->mm = nodeData.mm;
	totalMins += nodeData.mm;	// adds up minutes

	Temp->ss = nodeData.ss;
	totalSecs += nodeData.ss;	// adds up seconds
	Temp->cmd = nodeData.cmd;
	checkLongestCmd();

	Temp->Next = NULL;



	Current = Head;

	while (Current->Next != NULL)

		Current = Current->Next;

	Current->Next = Temp;

}

void LinkedList::printUserName(){
	Node *Temp;
	Temp = new Node;
	cout << Temp->uid;
}

void LinkedList::printPid(){
	Node *Temp;
	Temp = new Node;
	cout << Temp->pid;
}

int LinkedList::getTotalMinutes(){
	return totalMins;
}

int LinkedList::getTotalSeconds(){
	return totalSecs;
}

void LinkedList::checkLongestCmd(){
/* FIX THIS
	Node *Temp;
	Temp = new Node;
	Current = Head;
	while (Current != NULL){
		if (Current->cmd.length() > Temp.Next->cmd.length())
			lngCmd = Current->cmd;
		else
			lngCmd = Next->cmd;
	Current = Temp->Next;
	}
*/
}

string LinkedList::returnLongestCmd(){
	return lngCmd;
}



bool LinkedList::isEmpty(){

	if (Head == NULL)

		return true;

	else

		return false;

}



LinkedList::~LinkedList(){

	Node *Temp;

	

	while(Head != NULL){

		Temp = Head;

		Head = Head->Next;

		delete Temp;

	}

}



// Hash Table Data Structure	

class HashTable{

	public:

	// Variables

	Node *p;

	int asciiVal;
	LinkedList Table[MAX];

	// Functions

	HashTable();

	int generateKey(string userName);	// Generate Key

	~HashTable();

};



HashTable::HashTable(){

	// Initiate Data

	}



int HashTable::generateKey(string userName){

	userName = p->uid;

	for (int a = 0; a != userName.length(); ++a)

		asciiVal += int (userName[a]);



	asciiVal = asciiVal % MAX;



	return asciiVal;

}



HashTable::~HashTable(){

	// Delete Pointers

	}

	

int main(){

	Node *node;

	node = new Node;

	while (!cin.fail()){

		cin >> node->uid;

		cin >> node->pid;
		cin >> node->ppid;
		cin >> node->c;
		cin >> node->sysTime;
		cin >> node->TTY;

		cin >> node->mm;

		cin >> node->ss;

		cin >> node->cmd;

		}


	Table[HashTable.generateKey(node->uid)].Add(node);

	for (int i = 0; i < MAX; i++;){
		Table
		cout << "User " << Table
	}

	

	system ("PAUSE");

	return 0;

	}
1
Contributor
1
Reply
3
Views
9 Years
Discussion Span
Last Post by mattieu
0

Update: I managed to find a PC and upload the file. Its updated and now my problems lie within the add node I believe. If anyone can help, it would be greatly appreciated.

Thanks,
Matt.

Attachments
/*********************************/
/* CSCI212 - Interacting Systems */
/* Matthew Rowles - mjr997 	 */
/* Assignment 1			 */
/* complete.cpp			 */
/*********************************/

#include <iostream>
#include <string>
using namespace std;

// Node Data Structure
struct Node{
	string uid;	// Username Identification
	string pid;	// Process User Identification
	string ppid;	// Parent Processer Identification
	string c;	// C
	string sysTime;	// System Time
	string TTY;	// Terminal Process
	string mm;	// Total Running Time: Minutes, Seconds
	string cmd;	// Command Line
	Node *Next;	// Pointer To The Next Node
	};

// Linked List Data Structure	
class LinkedList{
	private:
		Node *Head;				   // Node Pointer
		int totalMins, totalSecs, totalProcesses;  // Total Amount of Minutes, Seconds
		string lngCmd;		   		   // Longest cmd (eg. /usr/bin/ )
	public:
		LinkedList();		   // Constructor
		void Add(Node nodeData);   // Adds To The Linked List
		string printUserName();	   // Prints user name
		string printPid();	   // Prints Parent Identification
		int getTotalMinutes();	   // Returns total amount of minutes for a user
		int getTotalSeconds();     // Returns total amount of seconds for a user
		int getTotalProcesses();   // Returns total amount of processes
		void checkLongestCmd();	   // Checks if a string is longer than previous
		string returnLongestCmd(); // Returns longest command
		bool isEmpty();		   // Checks if Linked List is empty
		~LinkedList();		   // Destructor
	};

// Constructor
LinkedList::LinkedList(){
	Head = NULL;
	}

// Adds A New Node Of Data Linked List
void LinkedList::Add(Node nodeData){
	Node *Temp;
	Temp = new Node;

	Temp->uid	= nodeData.uid;
	Temp->pid	= nodeData.pid;
	Temp->ppid	= nodeData.ppid;
	Temp->c		= nodeData.c;
	Temp->sysTime 	= nodeData.sysTime;
	Temp->TTY	= nodeData.TTY;
	Temp->mm	= nodeData.mm;
	Temp->cmd	= nodeData.cmd;

	// Statistics Functions
	//totalMins += nodeData.mm;	// Adds Up Minutes
	//totalSecs += nodeData.ss;	// Adds Up Seconds
	totalProcesses ++;
	//checkLongestCmd();		// Checks Longest Command
	
	// Program Output
	cout << endl;
	cout << "User " << nodeData.uid << endl;
	cout << "==========" << endl;
	cout << "User " << nodeData.uid << " has a total of " 
	     << totalProcesses << " processes" << endl;
	cout << "User " << nodeData.uid << " has consumed a total of " 
	     << nodeData.mm << " of CPU time." << endl;
	cout << "The process id with the longest path name is pid " << nodeData.pid
	     << ' ' << nodeData.cmd << '.' << endl;
	cout << endl;

	while (Temp->Next != NULL)
		Temp = Temp->Next;

	Temp->Next = NULL;
}

// Prints user name of certain user
string LinkedList::printUserName(){
	Node *Temp;
	Temp = new Node;
	string userName = Temp->uid;
	return userName;
}

// Prints pid of longest path name for certain user
string LinkedList::printPid(){
	Node Temp;
	string PID = Temp.pid;
	return PID;
}

// Returns total accumulated minutes of certain user
int LinkedList::getTotalMinutes(){
	return totalMins;
}

// Returns total accumulated seconds of certain user
int LinkedList::getTotalSeconds(){
	return totalSecs;
}

// Returns total amount of proccesses of certain user
int LinkedList::getTotalProcesses(){
	return totalProcesses;
}

// Checks and sets the longest path name, based on amount of /'s
void LinkedList::checkLongestCmd(){
	Node *Temp, *Current;
	Temp = new Node;
	Current = new Node;
	Current = Head;
	Temp = Current->Next;
	int slashCount;
	int cmdLength = Current->cmd.length();
	for (int a = 0; a < cmdLength; a++){
		if (a == '/')
			slashCount++;
	}
	while (Current != NULL){
		Current = Current->Next;
		if (Current->cmd.length() > Temp->cmd.length())
			lngCmd = Current->cmd;
		else
			lngCmd = Temp->cmd;
	}
}

// Returns longest path name for certain user
string LinkedList::returnLongestCmd(){
	return lngCmd;
}

// Checks if the list is empty or not
bool LinkedList::isEmpty(){
	if (Head == NULL)
		return true;
	else
		return false;
}

// Destructor
LinkedList::~LinkedList(){
	Node *Temp;
	
	while(Head != NULL){
		Temp = Head;
		Head = Head->Next;
		delete Temp;
	}
}

// Hash Table Data Structure	
class HashTable{
	public:
	Node p;
	LinkedList Table[120];			// Table of Linked Lists, 120 maximum users
	int asciiVal;				// Ascii Value Of User Name
	int generateKey (string userName);	// Generate Key Using Ascii Value
};

// Generates key using the added ascii values of each letter in a user name mod 120
int HashTable::generateKey(string userName){
	userName = p.uid;
	int userNameLength = userName.length();
	for (int a = 0; a != userNameLength; a++)
		asciiVal += int (userName[a]);
	asciiVal = asciiVal % 120;

	return asciiVal;
}

int main(){
	Node node;
	HashTable Hash;
	LinkedList List;
	string userNameTemp = node.uid;
	while (cin.good()){
		cin >> node.uid;
		cin >> node.pid;
		cin >> node.ppid;
		cin >> node.c;
		cin >> node.sysTime;
		cin >> node.TTY;
		cin >> node.mm;
		cin >> node.cmd;
	}
	Hash.Table[Hash.generateKey(userNameTemp)].Add(node);	// Assigns a key to the node

	return 0;
	}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.