Thread: A few things I don't understand

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    A few things I don't understand

    The following is the code for a linked list that I am supposed to build on for my project (no main(), just the function definitons) -the header file for the process_list class is not included:
    Code:
    #include <iostream>
    #include <stdarg.h>
    #include "scheduling.h"
    using namespace std;
    
    typedef unsigned char boolean;
    typedef int* int_ptr;
    
    // default constructor for head of list
    process_list::process_list() {
      m_curr = this;
      m_prev = NULL;
      m_next = NULL;
    }
    
    //constructor for extra nodes
    process_list::process_list(process_list* prev) 
    {
      m_prev = prev;
      m_next = NULL;
    }
    
    //inserts a new process with data given
    void process_list::insert(int temp_pid, int temp_tarq, int temp_prio, int temp_tncpu, int_ptr temp_cpu_burst_lenth, int_ptr temp_IO_burst_lenth) 
    {
      while (m_curr->get_next() != NULL)
        m_curr = m_curr->get_next();
      m_curr->set_data(temp_pid, temp_tarq, temp_prio, temp_tncpu, temp_cpu_burst_lenth, temp_IO_burst_lenth);
      m_curr->set_next(new process_list(m_curr));
    }
    
    //Fetch PID functions
    int process_list::get_pidE1() 
    {
      return pid;
    }
    
    int process_list::get_pid() 
    {
      return m_curr->get_pidE1();
    }
    
    
    //Fetch tarq functions
    int process_list::get_tarqE1() 
    {
      return tarq;
    }
    int process_list::get_tarq() 
    {
      return m_curr->get_tarqE1();
    }
    
    
    //Fetch Prio functions
    int process_list::get_prioE1() 
    {
      return prio;
    }
    int process_list::get_prio() 
    {
      return m_curr->get_prioE1();
    }
    
    
    //Fetch tncpu functions
    int process_list::get_tncpuE1() 
    {
      return tncpu;
    }
    int process_list::get_tncpu() 
    {
      return m_curr->get_tncpuE1();
    }
    
    
    //Fetch cpu_burst_lenth functions
    int_ptr process_list::get_cpu_burst_lenthE1() 
    {
      return cpu_burst_lenth;
    }
    int_ptr process_list::get_cpu_burst_lenth() 
    {
      return m_curr->get_cpu_burst_lenthE1();
    }
    
    
    //Fetch IO_burst_lenth functions
    int_ptr process_list::get_IO_burst_lenthE1() 
    {
      return IO_burst_lenth;
    }
    int_ptr process_list::get_IO_burst_lenth() 
    {
      return m_curr->get_IO_burst_lenthE1();
    }
    
    
    // set process information function
    void process_list::set_data(int temp_pid, int temp_tarq, int temp_prio, int temp_tncpu, int_ptr temp_cpu_burst_lenth, int_ptr temp_IO_burst_lenth) 
    {
        pid = temp_pid;		
    	tarq = temp_tarq;	
    	prio = temp_prio;	
    	tncpu = temp_tncpu;	
    	cpu_burst_lenth = temp_cpu_burst_lenth;
    	IO_burst_lenth = temp_IO_burst_lenth;
    }
    
    //returns a pointer to the previous process
    process_list* process_list::get_prev() 
    {
      return m_prev;
    }
    
    //returns a pointer to the next process
    process_list* process_list::get_next() 
    {
      return m_next;
    }
    
    
    //add a new process to the list
    void process_list::set_next(process_list* process) 
    {
      m_next = process;
    }
    
    
    //sets m_curr pointer back to the start of the list
    void process_list::reset()
     {
      m_curr = this;
    }
    
    //moves m_curr to the next process in the list
    void process_list::next() 
    {
      m_curr = m_curr->get_next();
    }
    
    //moved m_curr to the previous process in the list
    void process_list::prev() 
    {
      m_curr = m_curr->get_prev();
    }
    
    //check for end
    boolean process_list::is_end() 
    {
      return m_curr->get_next() == NULL ? 1 : 0;
    }
    
    //check for biginning
    boolean process_list::is_beginning() 
    {
      return m_curr == NULL ? 1 : 0;
    }
    
    //check if any entries have been added to the list
    boolean process_list::is_empty() 
    {
      return m_curr->get_next() == m_curr->get_prev() ? 1 : 0;
    }
    Now my questions:
    1) In the default constructor, what does m_curr = this; do?
    2) Why doesn't the constructor for extra nodes have a m_curr field?
    3) What's the difference between a function, say get_tarq(), and the one with the same name but with an E1 at the end (get_tarqE1())?
    4) Which function of stdarg.h is used in this code?
    5) Is this an efficient code? What do you think of the style of coding?

    The above questions arose because I didn't write the code. Any help is greatly appreciated.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1) this is the current object of the class.
    3) The name.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Which function of stdarg.h is used in this code?
    Comment this header, compile, read the warnings listing all functions not having a prototype
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    ok, thanks. Is it possible to replace the following 2 func's:
    Code:
    //Fetch PID functions
    int process_list::get_pidE1() 
    {
      return pid;
    }
    
    int process_list::get_pid() 
    {
      return m_curr->get_pidE1();
    }
    with:
    Code:
    //Fetch PID functions
    int process_list::get_pid() 
    {
      return m_curr->pid;
    }
    ?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Right now it is. However, if in the future your design changes and instead of storing pid in the list object you calculate it on the fly, your first code only requires a change to get_pidE1() where your second code requires a change to get_pid and any other code that you removed get_pidE1() from.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me to understand this method
    By -EquinoX- in forum C Programming
    Replies: 2
    Last Post: 05-02-2008, 12:15 AM
  2. 2 things at once?
    By bradszy in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2008, 01:19 PM
  3. Help me understand copying better
    By sh3rpa in forum C++ Programming
    Replies: 20
    Last Post: 11-12-2007, 09:34 PM
  4. clearing things up
    By webturtle0 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-28-2002, 03:59 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM