SRL Archive - General

C++ Linked list(class and linking using structs) help.


mcbain

2013-01-17 02:37:06

well, this may not be the best forum to put this on, but this community is very helpful and I'm sure some of you fellows have some C++ knowledge :)

So basically, i have an assignment that is to make a linked list simulating a printer spool. I have to have a class and link the list using structures. Now, I get quite confused when structures are involved because they have more than one item that can be sent to the list.. I don't know why its confusing to me.. but it is :p

Header file code:



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


struct node
{
string name;
string priority;
node* next;
};


class MyPrintSpool
{
private:
int sizeoflist;
public:
node* head;

MyPrintSpool();
int countitems();
void send(string b, string a);
void sendpri(char arr[],string a);
void show();
void remove();
void removefile(string);
node *position(int p);
void print();
int size();
void printpri();
void priority();
};

and my cpp file code:



#include "Printspool.h"

MyPrintSpool::MyPrintSpool()
{
sizeoflist = 0;
head = NULL;
}

int MyPrintSpool::size()
{
return sizeoflist;
}
void MyPrintSpool::send(string pri,string com)
{
node *newnode;
node *currptr = head;
node *prevptr = NULL;

while(currptr != NULL)
{
prevptr = currptr;//going through the list until currptr equals
currptr = currptr->next;//Null, if it does then ill need to insert
} //the file name and priority there.
newnode = new node;//Obviously this is wrong, but i just can't
newnode->name = com;//think of another way.
newnode->priority = pri;
if (prevptr == NULL)
head=newnode;
else
prevptr->next = newnode;
}

void MyPrintSpool::print()
{
node* currPtr = head;


while(currPtr != NULL)
{
cout << currPtr->name << endl;
cout << currPtr->priority << endl;
currPtr = currPtr->next;
}

}

So basically, my program crashes once i send the string arguments (file name and a priority) to the class member function send. I just cant seem to figure it out.. i need the class member function to make a new "node"(my structure) and send it a file name, and priority(1-5) For this function im just making the priority a default of five to test things out. But this does not work.
if you need to see my main file just let me know and ill be happy to put this up as well.

com is the file name, and pri is the priority.

Anyone that could help me out please done hesitate too :)

Recursive

2013-01-17 03:27:21

You forgot to add a destructor:


MyPrintSpool::~MyPrintSpool()
{
cout <<"Spool destroyed!\n";
}

You may have to post your main code or finish implementing all the parts to your printspool.cpp file.

Echo_

2013-01-17 13:44:08

printerspool.h

#ifndef PRINTERSPOOL_H_
#define PRINTERSPOOL_H_

#include <iostream>
#include <string>

struct Node {
std::string name;
std::string priority;
Node *next;
};

class PrinterSpool {
int size;
Node *head;
Node **iterator;
public:
PrinterSpool();
~PrinterSpool();
void send(const std::string &, const std::string &);
void print();
};

#endif

printerspool.cpp
#include "printerspool.h"

PrinterSpool::PrinterSpool() {
size = 0;
head = NULL;
}

PrinterSpool::~PrinterSpool() {
iterator = &head;
Node *temp;
while (*iterator != NULL) {
temp = (*iterator)->next;
delete *iterator;
iterator = &temp;
}
}

void PrinterSpool::send(const std::string &name, const std::string &priority) {
size++;
iterator = &head;
while (*iterator != NULL)
iterator = &(*iterator)->next;

*iterator = new Node;
(*iterator)->name = name;
(*iterator)->priority = priority;
(*iterator)->next = NULL;
}

void PrinterSpool::print() {
iterator = &head;
while (*iterator != NULL) {
std::cout << (*iterator)->name << std::endl;
std::cout << (*iterator)->priority << std::endl;
iterator = &(*iterator)->next;
}
}

mcbain

2013-01-17 17:45:43

From: Echo_
printerspool.h

#ifndef PRINTERSPOOL_H_
#define PRINTERSPOOL_H_

#include <iostream>
#include <string>

struct Node {
std::string name;
std::string priority;
Node *next;
};

class PrinterSpool {
int size;
Node *head;
Node **iterator;
public:
PrinterSpool();
~PrinterSpool();
void send(const std::string &, const std::string &);
void print();
};

#endif

printerspool.cpp
#include "printerspool.h"

PrinterSpool::PrinterSpool() {
size = 0;
head = NULL;
}

PrinterSpool::~PrinterSpool() {
iterator = &head;
Node *temp;
while (*iterator != NULL) {
temp = (*iterator)->next;
delete *iterator;
iterator = &temp;
}
}

void PrinterSpool::send(const std::string &name, const std::string &priority) {
size++;
iterator = &head;
while (*iterator != NULL)
iterator = &(*iterator)->next;

*iterator = new Node;
(*iterator)->name = name;
(*iterator)->priority = priority;
(*iterator)->next = NULL;
}

void PrinterSpool::print() {
iterator = &head;
while (*iterator != NULL) {
std::cout << (*iterator)->name << std::endl;
std::cout << (*iterator)->priority << std::endl;
iterator = &(*iterator)->next;
}
}


Genius, And the double pointer stops me from having to use the previous pointer and current pointer, thanks so much! :D