Home > Software design >  How to sort nodes from a stack and put into a queue
How to sort nodes from a stack and put into a queue

Time:01-23

im trying to implement both queue and stack in my mini project. Customer will enter nodes into stack and the seller will sort the nodes from stack and put them into queue. I want to sort nodes from a stack based on college name, where all nodes with college = "kdoj" will get into the queue first then followed by all nodes with college = "kdse". The problem is i have tried doing the sorting with 2 (for loop) and i dont know why only 2 nodes get into the queue when i have more than 2 nodes in the stack. i am not sure where is the problem and been stuck here for a while :(.

This is the input example:

name : david
ic num: 123
phone num: 123
college: kdse
quantity: 1
size: m

name : jojo
ic num: 123
phone num: 123
college: kdse
quantity: 1
size: m

name : zoro
ic num: 123
phone num: 123
college: kdoj
quantity: 1
size: m

This is the runnable code. The problem will occur when you sort the list in seller menu after entering the data.



#include <iostream>
#include <cstring>
#include<iomanip>
#include <conio.h>
#include<string>

using namespace std;

//nodes
struct customerNode {
    string name, id, college;
    int phoneNum,quantity;
    char size;
    public:
    customerNode* next;
    customerNode(string name, string id, int phoneNum,string college, int quantity, char size)
    {
        name = name;
        id = id;
        phoneNum = phoneNum;
        college = college;
        quantity = quantity;
        size = size;
        next = NULL;
    }
};

//class customer
class Customer{
    public:
    customerNode *head = NULL;
    bool isEmpty(){
        return(head == NULL);
    }
    void pushStack(string name, string id, int phoneNum, string college, int quantity, char size){
        customerNode *newnode = new customerNode(name,id,phoneNum,college,quantity,size);
        
        newnode->name = name;
        newnode->id= id;
        newnode->phoneNum = phoneNum;
        newnode->college = college;
        newnode->quantity = quantity;
        newnode->size = size;
        
        if(isEmpty()){
            newnode->next = head;
            head = newnode;
            cout<<endl<<"created new stack."<<endl;
        }
        else{
            newnode->next = head;
            head = newnode;
            cout<<endl<<"added node into a stack."<<endl;
        }
    }
    
    //pop out node from stack
    void popStack(){
        customerNode *temp;
        
        temp = head; //temp variable
        head = temp->next;
        temp->next = NULL;
            cout<<endl<<"Order has been deleted!";
    }
    
    //push node into stack
    void enterOrder(){
         int phoneNum, quantity;
        char size;
        string name, id, college;
                cout<<endl<<"\tADDING YOUR ORDER.\n";
                cout<<"\tName: ";
                cin.ignore();
                getline(cin,name);
                cout<<"\tIC Num: ";
                cin>>id;
                cout<<"\tPhone Number: ";
                cin>>phoneNum;
                cout<<"\tCollege name: ";
                cin>>college;
                cout<<"\tQuantity: ";
                cin>>quantity;
                cout<<"\tSize (S/M/L/XL): ";
                cin>>size;
        pushStack(name, id, phoneNum,college, quantity, size);
        cin.get();
    }
    
    //see order and trigger popout funct
    void deleteorder(){
        char choice;
        cout<<"YOUR ODER."<<endl<<
        setw(10)<<left<<"ID"
        <<setw(20)<<left<<"NAME"
        <<setw(10)<<left<<"CONTACT"
        <<setw(20)<<left<<"COLLEGE"
        <<setw(10)<<left<<"QUANTITY"
        <<setw(5)<<left<<"SIZE";
        stackTop();
        cout<<endl;
        cout<<endl<<"Confirm to delete?: ";
        cin>>choice;
        if(choice == 'y' || choice == 'Y'){
            popStack();
        }
    }
    
    //display node at the top
    void stackTop(){
        customerNode *check;
        if(!isEmpty()){
        check = head;
        cout<<endl<<
        setw(10)<<left<<check->id
        <<setw(20)<<left<<check->name
        <<setw(10)<<left<<check->phoneNum
        <<setw(20)<<left<<check->college
        <<setw(10)<<left<<check->quantity
        <<setw(5)<<left<<check->size;
        }
        else{
            cout<<endl<<"Stack is an underflow.";
            getch();
        }
    }
    
    int getNumberNodes(){
        customerNode  *check;
        check = head;
        int counter = 0;
        while(check!= NULL){
            counter  ;
            check = check->next;
        }
        return counter;
    }
    
    
};

//Customer main menu    
void customerMenu(Customer &c){
    int choice;
    do{cout    
    <<endl<<"CUSTOMER MENU"
    <<endl<<"Please choose your task."
    <<endl<<"1. Insert Order"
    <<endl<<"2. Delete Order"
    <<endl<<"3. Check Order"
    <<endl<<"4. Exit"
    <<endl<<"Your Choice: ";
    cin>>choice;

        //customer task
        switch(choice){
            case 1: c.enterOrder();
            break; 
            case 2: c.deleteorder();
            break;
            case 3:
                    cout<<"\nYOUR ODER."<<endl<<
                    setw(10)<<left<<"ID"
                    <<setw(20)<<left<<"NAME"
                    <<setw(10)<<left<<"CONTACT"
                    <<setw(20)<<left<<"COLLEGE"
                    <<setw(10)<<left<<"QUANTITY"
                    <<setw(5)<<left<<"SIZE";
                    c.stackTop();
                    cout<<endl;
            break;
        }
    }while(choice != 4);
    
};




//SELLER CLASS
class Seller{
    string id;
    string password;
    
    public:
    customerNode *backPtr, *frontPtr;
    
    //constructor seller
    Seller(){
        id = "12345";
        password = "12345";
        backPtr = NULL;
        frontPtr = NULL;
    }
    
    //check queue empty
    bool queueEmpty(){
        return (backPtr == NULL && frontPtr == NULL);
    }
    

    
    //display all nodes in stack
    void getStack(Customer &c){
        customerNode *check;
        if(!c.isEmpty()){
        check = c.head;
        while(check!=NULL){
        cout<<endl<<
        setw(10)<<left<<check->id
        <<setw(20)<<left<<check->name
        <<setw(10)<<left<<check->phoneNum
        <<setw(20)<<left<<check->college
        <<setw(10)<<left<<check->quantity
        <<setw(5)<<left<<check->size;
        check=check->next;
        }
        }
        else{
            cout<<endl<<"Stack is an underflow.";
            getch();
        }
    }
    
    
    //will trigger getStack() 
    void displayStack(Customer &c){
        cout<<"\nCUSTOMER FULL ORDERLIST."<<endl<<
        setw(10)<<left<<"ID"
        <<setw(20)<<left<<"NAME"
        <<setw(10)<<left<<"CONTACT"
        <<setw(20)<<left<<"COLLEGE"
        <<setw(10)<<left<<"QUANTITY"
        <<setw(5)<<left<<"SIZE";
        getStack(c);
        cout<<endl;
    }


    //display all nodes in queue
    void getQueue(){
        customerNode *check;
        if(!queueEmpty()){
            check = frontPtr;
                while(check!=NULL){
                cout<<endl<<
                setw(10)<<left<<check->id
                <<setw(20)<<left<<check->name
                <<setw(10)<<left<<check->phoneNum
                <<setw(20)<<left<<check->college
                <<setw(10)<<left<<check->quantity
                <<setw(5)<<left<<check->size;
                check=check->next;
                }
        }
        else{
            cout<<endl<<"Stack is an underflow.";
            getch();
        }
    }
    
    //display node in queue 
    void displayQueue(){
        cout<<"\nSORTED CUSTOMER FULL ORDERLIST."<<endl<<
        setw(10)<<left<<"ID"
        <<setw(20)<<left<<"NAME"
        <<setw(10)<<left<<"CONTACT"
        <<setw(20)<<left<<"COLLEGE"
        <<setw(10)<<left<<"QUANTITY"
        <<setw(5)<<left<<"SIZE";
        getQueue();
        cout<<endl;
    }
    
    //insert queuee
    void insertQueue(customerNode *newNode){
        if(!queueEmpty()){
            newNode->next = NULL;
            backPtr->next = newNode;
            backPtr = newNode;
            cout<<endl<<"inserted node into queue."<<endl;
        }
        else{
            frontPtr = newNode;
            backPtr = newNode;
        }
    }
    
    
    
    void sortOrder(Customer &c){
            for(customerNode *node = c.head; node!=NULL; node = node->next){
                if(node->college == "kdoj"){
                    cout<<endl<<node->college;
                    insertQueue(node);
                }
            }

            for(customerNode *node = c.head; node!=NULL; node = node->next){
                if(node->college == "kdse"){
                    cout<<endl<<node->college;
                    insertQueue(node);
                }
            }
            
        displayQueue();
    }
};

//seller main menu
void sellerMenu(Customer &c, Seller &s){
    int choice;
    do{cout    
    <<endl<<"SELLER MENU"
    <<endl<<"Please choose your task."
    <<endl<<"1. Display OrderList"
    <<endl<<"2. Sort OrderList"
    <<endl<<"4. Exit"
    <<endl<<"Your Choice: ";
    cin>>choice;
        //choose task
        switch(choice){
            case 1: s.displayStack(c);
            break; 
            case 2: s.sortOrder(c);
            break;
            case 3: 
            break;
        }
    }while(choice != 4);
    
}




void menu(Customer &c, Seller &s){ //main menu function
    int choice;
    do{
    cout
    <<endl<<"Welcome to HotspotPrinting!"
    <<endl<<"Please choose your role."
    <<endl<<"1. Customer"
    <<endl<<"2. Seller"
    <<endl<<"3. Exit"
    <<endl<<"Your Choice: ";
    cin>>choice;
        switch(choice){
            case 1: customerMenu(c);
            break;
            case 2: sellerMenu(c,s);
            break;
        }
    }while(choice != 3);
}

int main()
{
    Customer c;
    Seller s;
    menu(c,s);
    cout<<endl<<"Thank You For Using Our Service :)!";
    return 0;
}

CodePudding user response:

your problem is that customer node records exist in two containers, your queue and a list. But you only have one next pointer, when you insert a customer into the queue you set its next pointer to null, this destroys the linkage in the customer list.

If you need to have the same object in 2 containers then use shared_ptr in standard containers, like std::list and std::queue

  •  Tags:  
  • c
  • Related