#include #include using namespace std; //structure struct sNode { int m_data; sNode* m_next; }; //function prototypes void addTail(sNode*&, int); void addHead(sNode*&, int); void f_display(sNode*); void r_display(sNode*); int main() { int data = 0; //integer data input for linked list char cMenu; //menu choice bool bExit = false; //exit or not (menu choice) //enable memory leak check _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF); sNode* pHead(0); sNode* pCurrent = pHead; do { system("cls"); cout << "Linked List\n\n\ta. display nodes forward.\n\tb. display nodes in reverse.\n\tc. add node to head.\n\td. add node to tail.\n\te. exit.\n\n"; cout << "Your selection: "; cin >> cMenu; switch(cMenu) { case 'a': case 'A': //display forward f_display(pHead); system("pause"); break; case 'b': case 'B': //display reverse r_display(pHead); system("pause"); break; case 'c': case 'C': //add node to head cout << "Enter integer value to store: "; cin >> data; addHead(pHead, data); break; case 'd': case 'D': //add node to tail cout << "Enter integer value to store: "; cin >> data; addTail(pHead, data); break; case 'e': case 'E': bExit = true; break; default: cout << "Invalid selection.\nPlease try again.\n"; system("pause"); } }while(!bExit); //gtfo! sNode* pDelete(0); pCurrent = pHead; while(pCurrent) { pDelete = pCurrent; pCurrent = pCurrent->m_next; delete pDelete; } return 0; } void addTail(sNode* &pHead, int data) { //pointer definition sNode* pCurrent; sNode* pNew = new(nothrow) sNode; //throwing values into pointers pCurrent = pHead; pNew->m_data = data; pNew->m_next = 0; //checks for existing list if(pHead==0) { //no previous list found pHead = pNew; } else { //found list adding to tail while(pCurrent->m_next) { pCurrent = pCurrent->m_next; } pCurrent->m_next = pNew; //add the node to the tail } return; } void addHead(sNode*& pHead, int data ) { sNode* pNew; //create new node pNew = new(nothrow) sNode; //assign new node pNew->m_data = data; //add data to new node pNew->m_next = pHead; //add pointer to next node pHead = pNew; //set new head to the newly added node return; } void f_display(sNode* pCurrent) { if(pCurrent) { cout << pCurrent->m_data << endl; f_display(pCurrent->m_next); } return; } void r_display(sNode* pCurrent) { if(pCurrent) { r_display(pCurrent->m_next); cout << pCurrent->m_data << endl; } return; }