so i got a singly linked list project done... I need help getting a doubly linke
ID: 3762259 • Letter: S
Question
so i got a singly linked list project done... I need help getting a doubly linked list implemeted in it now...
ANYONE help please... heres my code so far.
.
.
.
// TheSinglyLinkedListProject.cpp : Defines the entry point for the console application.
//
// ===================
#include "stdafx.h"
#include <iostream>
// ===================
// ====================
using namespace std;
// ====================
// ============
int main() {
Menu menu;
SinglyLinkedList listA;
do {
menu.Display();
menu.QueryUser();
menu.ProcessCommand( listA );
} while (menu.Continue());
return 0;
} //Function Main()
// ===================
.
// =======================
// THeSinglyLinkedList.cpp
// =======================
// ==================
#include "stdafx.h"
#include <iostream>
// ==================
// ====================
using namespace std;
// ====================
// ============
// Constructors
// ============
// ===============================
// Constructor SinglyLinkedList( )
// ===============================================
SinglyLinkedList::SinglyLinkedList( void ) {
ConstructorBanner();
first = last = NULL;
} // Constructor SinglyLinkedList
// ==================================
// ============================
// Destructor ~SinglyLinkedList
// ===================================================
SinglyLinkedList::~SinglyLinkedList( void ) {
DestructorBanner();
DestroyList( );
} // Deconstructor ~SinglyLinkedList
// ====================================
// ========================
// Public Member-Functions
// =======================
// ==============================
// Member-Function DestroyList( )
// =======================================
void SinglyLinkedList::DestroyList( ) {
Node* temp;
while ( first != NULL ) {
temp = first;
first = first->next;
delete temp;
} // while
last = NULL;
} // Public Member Function DestroyList
// =======================================
// ==========================
// GetFirst (Accessor Method)
// =====================================
Node* SinglyLinkedList::GetFirst( ) {
return first;
} // Public Member Function GetFirst
// ====================================
// =================================
// Member-Function InitializeList( )
// ==========================================
void SinglyLinkedList::InitializeList( ) {
DestroyList();
} // Public Member Function InitializeList
// ==========================================
// ==============================
// Member-Function InsertFirst( )
// =================================================
void SinglyLinkedList::InsertFirst( int value ) {
// =====================================
// Object Declaration and Instantiation.
// =====================================
Node* newNode = new Node( value,
first );
// ==================================
first = newNode;
if ( last == NULL )
last = first;
} // Public Member Function InsertFirst
// =======================================
// =============================
// Member-Function InsertLast( )
// ================================================
void SinglyLinkedList::InsertLast( int value ) {
if ( this->IsEmpty() )
this->InsertFirst( value );
else {
// ===========================================
Node* newNodePtr = new Node( value, NULL );
// ===========================================
last->next = newNodePtr;
last = newNodePtr;
} // else
} // Public Member Function InsertLast( )
// =========================================
// =========================
// Member-Function Insert( )
// ====================================================
void SinglyLinkedList::Insert( int value,
int location ) {
// ================================
// Variable and Object Declarations
// ================================
int nodeCounter;
Node* ptr;
// ==========
if ((location > this->Length() + 1) ||
(location < 1)) {
cout << endl;
cout << "=================================" << endl;
cout << "Member-function Insert() invoked." << endl;
cout << "Insertion location is invalid. " << endl;
cout << "Insert() terminating execution. " << endl;
cout << "=================================" << endl;
cout << endl;
} // then clause
else {
if ( location == 1 )
this->InsertFirst( value );
else if ( location == this->Length() + 1 )
this->InsertLast( value );
else {
nodeCounter = 1;
ptr = this->first;
while (nodeCounter < location - 1){
ptr = ptr->next;
nodeCounter++;
} // while
// ================================
// Variable and Object Declarations
// ================================
Node* newNode = new Node(value,
ptr->next);
// ====================================
ptr->next = newNode;
} // else clause
} // else clause
} // Public Member Function Insert( )
// =====================================
// ================================
// Public Member-Function IsEmpty()
// =======================================
bool SinglyLinkedList::IsEmpty( ) {
return ( first == NULL );
} // Public Member Function IsEmpty
// ===================================
// ===============================
// Public Member-Function Length()
// =========================================================
int SinglyLinkedList::Length( ) {
// ================================
// Variable and Object Declarations
// ================================
int nbNodes = 0;
Node* ptr;
// ==========
ptr = this->first;
while ( ptr != NULL ) {
ptr = ptr->next;
nbNodes++;
} // while
return nbNodes;
} // Public Member Function Length()
// ====================================
// ============================
// Public Member-Function Print
// =================================
void SinglyLinkedList::Print( ) {
Node* current;
cout << endl;
cout << "=====================================" << endl;
cout << "The list contains the following data." << endl;
cout << endl;
if ( this->Length() == 0 ) {
cout << "The list is empty." << endl;
cout << "==================" << endl;
cout << endl;
} // then
else {
cout << " Beginning" << endl;
current = first;
while ( current != NULL ) {
cout << " " << current->datum << endl;
current = current->next;
} // while
cout << " Ending" << endl;
cout << "========" << endl;
cout << endl;
} // else
} // Public Member Function Print
// =================================
// ==============================
// Member Function ReversePrint()
// ==========================================================
void SinglyLinkedList::ReversePrint( Node* currentNode ) {
if ( currentNode != NULL ) {
ReversePrint( currentNode->next );
cout << " " << currentNode->datum << endl;
} // then
} // Public Member Function ReversePrint
// ========================================
// ==============================
// Member Function DeleteFirst()
// =======================================
void SinglyLinkedList::DeleteFirst( int& valueDeleted ) {
if ( this->IsEmpty() )
cout << "The list is empty!" << endl;
else{
Node* newDelete = first;
first = first->next;
if (first == NULL) // if list is length one
last = NULL;
valueDeleted = newDelete->datum;
delete newDelete;
} // else
} // Public Member Function DeleteFirst
// ========================================
// ==============================
// Member Function DeleteLast()
// =======================================================
void SinglyLinkedList::DeleteLast(int& valueDeleted ) {
if ( this->IsEmpty() )
cout << "The list is empty!" << endl;
else if (first == last){
DeleteFirst(valueDeleted);
} // else if
else{
Node* newDelete;
Node* newLast = first;
while (newLast->next != last)
newLast = newLast->next;
newDelete = last;
last = newLast;
newLast->next = NULL;
valueDeleted = newDelete->datum;
delete newDelete;
} // else
} // Public Member Function DeleteLast
// ======================================
// ==============================
// Member Function Delete()
// =======================================================
void SinglyLinkedList::Delete(int& valueDeleted,
int location) {
if ( this->IsEmpty() )
cout << "The list is empty!" << endl;
else if (location == 1){
DeleteFirst(valueDeleted);
} // else if
else if (location == Length()){
DeleteLast(valueDeleted);
} // else if
else if (location > Length() || location < 1 ){
cout << " You can't be doing that!" << endl;
cout << "Enter a location between 1 and " << Length() << endl;
} // else if
else{
Node* current = first;
Node* newDelete;
location -= 2;
while (location > 0){
current = current->next;
location--;
} // while
newDelete = current->next;
current->next = newDelete->next;
valueDeleted = newDelete->datum;
delete newDelete;
} // else traverse list
} // Public Member Function DeleteLast
// ======================================
// ========================
// Private Member-Functions
// ========================
// =========================================
// Private Member Function ConstructorBanner
// =============================================
void SinglyLinkedList::ConstructorBanner( ) {
cout << endl;
cout << "=========================" << endl;
cout << "You are about to begin!!." << endl;
cout << "=========================" << endl;
cout << endl;
} // Private Member Fuction ConstructorBanner
// =============================================
// ========================================
// Private Member Function DestructorBanner
// ============================================
void SinglyLinkedList::DestructorBanner( ) {
cout << endl;
cout << "=========================================" << endl;
cout << "Thank you for your time, have a great day." << endl;
cout << "=========================================" << endl;
cout << endl;
} // Private Member Fuction DestructorBanner
// ============================================
.
// =======================
// Node.cpp
// =======================
// ===================
#include "stdafx.h"
#include <iostream>
// ===================
// ====================
using namespace std;
// ====================
// ====================
// Default Constructors
// ===================
Node::Node(void) {
ConstructorBanner();
datum = 0;
next = NULL;
} // Constructor
// ================
// ============
// Constructors
// ===========================
Node::Node(int listElement,
Node* nodePtr) {
ConstructorBanner();
datum = listElement;
next = nodePtr;
} // Constructors
// =================
// ==============
// The destructor
// ===================
Node::~Node(void) {
DestructorBanner();
} // Destructor
// ===============
// ===========================
// Function Constructor Banner
// =================================
void Node::ConstructorBanner( ) {} // Member-Function ConstructorBanner
// ================================== // This is not necessary
// =========================
// Function DestructorBanner
// ================================
void Node::DestructorBanner( ) {} // Member-Function DestructorBanner
// ================================= // This is not necessary
.
// ========
// Menu.cpp
// ========
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
// ============
// Constructors
// ============
// ==================
// Default Contructor
// ==================
Menu::Menu(void){
userMenuSelection = Quit;
} // Constuctor Menu
// ====================
// ==========
// Destructor
// ======================
Menu::~Menu(void){
} // Destructor ~Menu
// =====================
// ================
// Member-Functions
// ================
// ==============================
// Accessor Member-Function Get()
// ==============================
MenuChoices Menu::Get(){
return userMenuSelection;
} // Accessor Method Get
// ========================
// =============================
// Mutator Member-Function Set()
// =======================================
void Menu::Set( MenuChoices newValue ){
userMenuSelection = newValue;
} // Mutator Method Set
// =======================
// =========================
// Member-Function Display()
// ==========================
void Menu::Display(){
cout << endl;
cout << "====================================" << endl;
cout << "1: Quit 2: DestroyList " << endl;
cout << "3: InitializeList 4: InsertFirst " << endl;
cout << "5: InsertLast 6: Insert " << endl;
cout << "7: Print 8: ReversePrint " << endl;
cout << "9: Delete 10: DeleteFirst " << endl;
cout << "11: DeleteLast " << endl;
cout << "====================================" << endl;
} // Member-Function Display
// ============================
// =========================
// Member-Function QueryUser
// ==========================
void Menu::QueryUser() {
int selection;
cout << "Enter Menu Selection: ";
cin >> selection;
switch (selection){
case 1: userMenuSelection = Quit;
break;
case 2: userMenuSelection = DestroyList;
break;
case 3: userMenuSelection = InitializeList;
break;
case 4: userMenuSelection = InsertFirst;
break;
case 5: userMenuSelection = InsertLast;
break;
case 6: userMenuSelection = Insert;
break;
case 7: userMenuSelection = Print;
break;
case 8: userMenuSelection = ReversePrint;
break;
case 9: userMenuSelection = Delete;
break;
case 10: userMenuSelection = DeleteFirst;
break;
case 11: userMenuSelection = DeleteLast;
break;
default: userMenuSelection = ReversePrint;
} // switch
cout << endl;
} // Method QueryUser()
// =======================
// =================
// Method Continue()
// ==========================
bool Menu::Continue(){
return userMenuSelection != Quit;
} // Method Continue
// ====================
// =================================
// Member-Function ProcessCommand( )
// ==========================================
void Menu::ProcessCommand( SinglyLinkedList& list ) {
int integerValue;
int location;
if (userMenuSelection != Quit){
switch (userMenuSelection){
case DestroyList: list.DestroyList();
break;
case InitializeList: list.InitializeList();
break;
case InsertFirst:
cout << "Enter a value to add to the beginning of the list. ";
cin >> integerValue;
list.InsertFirst( integerValue );
break;
case InsertLast:
cout << "Enter a value to add to the end of the list. ";
cin >> integerValue;
list.InsertLast( integerValue );
break;
case Insert:
cout << "Enter a value to insert into the list. ";
cin >> integerValue;
cout << "Enter a node location for insertion. ";
cin >> location;
list.Insert( integerValue, location );
break;
case Print: list.Print();
break;
case ReversePrint: list.ReversePrint( list.GetFirst() );
break;
case Delete:
cout << "Please enter a location you wish to delete: ";
cin >> location;
list.Delete(integerValue, location);
cout << "The value you removed was: " << integerValue << endl;
break;
case DeleteFirst: list.DeleteFirst(integerValue);
cout << "The First value delete is " << integerValue << endl;
break;
case DeleteLast: list.DeleteLast(integerValue);
cout << "The Last value delete is " << integerValue << endl;
break;
default:
cout << "Invalid selection." << endl;
cout << "ProcessCommand member-function invoked." << endl;
list.Print();
break;
} // switch
} // then
} // Function ProcessCommand( )
// ===============================
.
// Types.h
// ================
// Enumerated Types
// ========================================================================
enum MenuChoices{ Quit, DestroyList, InitializeList, InsertFirst,
InsertLast, Insert, Print, ReversePrint,
Delete, DeleteFirst, DeleteLast};
.
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include "Types.h"
#include "Node.h"
#include "Menu.h"
#include "SinglyLinkedList.h"
// TODO: reference additional headers your program requires here
.
// =====================
// THeSinglyLinkedList.h
// =====================
// ===========
#pragma once
// ===========
// ========================
class SinglyLinkedList {
// =======
public:
// =======
// ======================
// Constructor Prototypes
// =======================
SinglyLinkedList(void);
// =======================
// =====================
// Destructor Prototypes
// =======================
~SinglyLinkedList(void);
// =======================
// ==========================
// Member Function Prototypes
// ======================================
void SinglyLinkedList::DestroyList();
Node* GetFirst();
void SinglyLinkedList::InitializeList();
void SinglyLinkedList::InsertFirst( int );
void SinglyLinkedList::InsertLast( int );
void SinglyLinkedList::Insert( int, int );
bool SinglyLinkedList::IsEmpty();
int SinglyLinkedList::Length();
void SinglyLinkedList::Print();
void SinglyLinkedList::ReversePrint( Node* );
void SinglyLinkedList::DeleteFirst( int& );
void SinglyLinkedList::DeleteLast( int& );
void SinglyLinkedList::Delete( int&, int );
// ===========================================
// ========
private:
// ================
// Member-variables
// ============
Node* first;
Node* last;
// ===========
// =========================
// Private Member Functions:
// ==========================
void ConstructorBanner( );
void DestructorBanner( );
// =========================
// private
// =======
}; // Class SinglyLinkedList
// ============================
.
// ======
// Node.h
// ======
#pragma once
// ===========
class Node{
friend class SinglyLinkedList;
// =======
public:
// =====================
// Constuctor Prototypes
// =====================
Node(void);
Node( int, Node* );
// ===================
// =====================
// destructor Prototypes
// =====================
~Node( void );
// ==============
// public
// ======
// ========
private:
// ================
// Member Functions
// ==========================
void ConstructorBanner( );
void DestructorBanner( );
// ================
// Member Variables
// ================
int datum;
Node* next;
// ===========
// private
// =======
}; // class Node
// =================
.
// ======
// Menu.h
// ======
// ============
#pragma once
// ============
#include <string>
using namespace std;
// ===========
class Menu{
public:
// ============
// Constructors
// ============
Menu( void );
// =============
// ==========
// Destuctors
// ==============
~Menu( void );
// ==============
// ==========================
// Member-Function Prototypes
// =================================================
MenuChoices Menu::Get( ); // Accessor
void Menu::Set(MenuChoices); // Mutator
void Menu::Display( );
void Menu::QueryUser( );
bool Menu::Continue( );
void Menu::ProcessCommand( SinglyLinkedList& );
// ===========================================
private:
// ================
// Member-Variables
// ================
MenuChoices userMenuSelection;
}; // Class Menu
// ================
thank you for your time...
Explanation / Answer
#include "stdafx.h" #include #include #include #include #include #include using namespace std; template struct Node { // data members Node_entry entry; Node *next; Node *back; // constructors Node( ); Node(Node_entry, Node *link_back = NULL, Node *link_next = NULL); }; template class List { public: // methods of the List ADT List( ); int size( ) const; bool full( ) const; bool empty( ) const; void clear( ); int count; error_code traverse(void (*visit)(List_entry &)); error_code retrieve(int position, List_entry &x) const; error_code remove(int position, List_entry &x); error_code insert(int position, const List_entry &x); protected: // data members for a contiguous list implementation List_entry entry[15]; mutable int current_position; mutable Node *current; Node *set_position(int position) const; }; template int List::size( ) const { return count; }; template void List::set_position(int position) const { if (current position next; else for ( ; current position != position; current position) current = current->back; }; template error_code List::insert(int position, const List_entry &x) { Node *new_node, *following, *preceding; if (position < 0 || position > count) return range_error; if (position == 0) { if (count == 0) following = NULL; else { set_position(0); following = current; } preceding = NULL; } else { set_position(position 1); preceding = current; following = preceding->next; } new_node = new Node(x, preceding, following); if (new_node == NULL) return overflow; if (preceding != NULL) preceding->next = new_node; if(following != NULL) following->back = new_node; current=new_node; current_position=position; count++; return success; }; class Editor: public List { public: Editor(ifstream *file_in, ofstream *file_out); bool get_command( ); void run_command( ); private: ifstream *infile; ofstream *outfile; char user_command; // auxiliary functions error_code next_line( ); error_code previous_line( ); error_code goto_line( ); error_code insert_line( ); error_code substitute_line( ); error_code change_line( ); void write_file( ); void read_file( ); void find_string( ); }; int main(int argc, char *argv[ ]) { if (argc != 3) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.