Results 1 to 4 of 4

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

  1. #1
    Join Date
    Dec 2012
    Posts
    115
    Mentioned
    0 Post(s)
    Quoted
    25 Post(s)

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

    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

    Header file code:

    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:

    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
    Last edited by mcbain; 01-17-2013 at 07:40 AM.

  2. #2
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    You forgot to add a destructor:

    C++ Code:
    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.

  3. #3
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    printerspool.h

    C++ Code:
    #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
    C++ Code:
    #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;
        }
    }
    Last edited by Echo_; 01-17-2013 at 10:13 PM.

  4. #4
    Join Date
    Dec 2012
    Posts
    115
    Mentioned
    0 Post(s)
    Quoted
    25 Post(s)

    Default

    Quote Originally Posted by Echo_ View Post
    printerspool.h

    C++ Code:
    #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
    C++ Code:
    #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!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •