Question

#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

#include <iostream>
using namespace std;

struct ListNode {
float value;
ListNode *next;
};
ListNode *head;

class LinkedList {
public:
int insertNode(float num);
void deleteNode(float num);
void destroyList();
void displayList();
LinkedList(void) {head = NULL;}
~LinkedList(void) {destroyList();}
};

int LinkedList::insertNode(float num)
{
struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member!\n";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head==NULL) {
cout << "List was empty " << newNode->value;
cout << "is part of list's first node.\n";
head = newNode;
}
else {
while((nodePtr != NULL) && (nodePtr->value < num)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(prevNodePtr==NULL)
newNode->next = head;
else
newNode->next = nodePtr; prevNodePtr->next = newNode;
}
return 0;
}


// The deleteNode function searches for a node with searchValue as its value.
// The node, if found,is deleted from the list and from memory. *


void LinkedList::deleteNode(float num)
{
struct ListNode *nodePtr = head, *prevNodePtr = NULL;

if(head==NULL) {
cout << "The list was empty\n";
return;
}
if(head->value == num) {
head = nodePtr->next;
delete [] nodePtr;
}
else {
while((nodePtr!= NULL)&&(nodePtr->value != num)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr==NULL)
cout << "The value " << num << " is not in this list!\n";
else {
prevNodePtr->next = nodePtr->next;
delete [] nodePtr;
}
}
}

void LinkedList::destroyList()
{
struct ListNode *nodePtr = head, *nextNode = nodePtr;

if(head==NULL) {
cout << "The list is empty\n";
return;
}
while (nodePtr != NULL) {
nextNode = nodePtr->next;
delete [] nodePtr;
nodePtr = nextNode;
}
}


// displayList shows the value stored in each node
// of the linked list pointed to by head.

void LinkedList::displayList()
{
struct ListNode *nodePtr = head;
if (nodePtr == NULL)
cout << "The list is empty\n";
else {
while (nodePtr != NULL) {
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
}


int main()
{
int num;
char answer, choice;
LinkedList temp;
do {
cout << "Please enter a value to put in the list -> ";
cin >> num;
temp.insertNode(num);
cout << endl;
cout << "Would you like to put another value into your list? ";
cin >> answer;
} while(toupper(answer)=='Y');

cout << "I will now display your list\n";
cout << endl;
temp.displayList();
}

Purpose: Adapt a doubly linked list to behave like a stack and a queue.


1. Modify the singly linked list from lab1 into a doubly linked list by adding a PREV node. Update the insert algorithm as shown in section 2.7 and also update the delete algorithm

2. Implement the PREPEND and APPEND functions listed

3. Implement the standard interface for the doubly linked list as a queue.

4. Adapt your doubly linked list to behave like a STACK

5. Adapt your doubly linked list to behave like a QUEUE

Create a test case to validate your doubly linked list behaves like a queue and a stack. See the common Stack and Queue operations listed in the book for the Stack and Queue interface.

1 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to implement LinkedList , Queue and Stack

#include <iostream>

using namespace std;

struct ListNode {

float value;

ListNode *next;

ListNode *prev;

};

class LinkedList {

               ListNode *head; // make head private variable of linkedlist

public:

int insertNode(float num);

void deleteNode(float num);

void destroyList();

void displayList();

int prepend(float num);

int append(float num);

LinkedList(void) {head = NULL;}

~LinkedList(void) {destroyList();}

ListNode *getHead() {return head;} // return the head node of the list

};

int LinkedList::insertNode(float num)

{

               struct ListNode *newNode, *nodePtr = head;

               newNode = new ListNode;

               if(newNode == NULL) {

                              cout << "Error allocating memory for new list member!\n";

                              return 1;

               }

               newNode->value = num;

               newNode->next = NULL;

               newNode->prev = NULL;

               if(head==NULL) {

                              cout << "List was empty " << newNode->value;

                              cout << " is part of list's first node.\n";

                              head = newNode;

               }else if(head->value > num)

               {

                              newNode->next = head;

                              head->prev = newNode;

                              head = newNode;

               }

               else {

                              while((nodePtr->next != NULL) && (nodePtr->value < num)) {

                                             nodePtr = nodePtr->next;

                              }

                              if(nodePtr->value < num)

                              {

                                             newNode->prev = nodePtr;

                                             nodePtr->next = newNode;

                              }else{

                                             newNode->prev = nodePtr->prev;

                                             nodePtr->prev->next = newNode;

                                             newNode->next = nodePtr;

                                             nodePtr->prev = newNode;

                              }

               }

               return 0;

}

// prepend function to add the node at the head of the linked list

int LinkedList:: prepend(float num)

{

               ListNode *newNode = new ListNode;

               if(newNode == NULL) {

                                             cout << "Error allocating memory for new list member!\n";

                                             return 1;

               }

               newNode->value = num;

               newNode->next = NULL;

               newNode->prev = NULL;

               if(head == NULL)

               {

                              head = newNode;

               }

               else

               {

                              newNode->next = head;

                              head->prev = newNode;

                              head = newNode;

               }

               return 0;

}

int LinkedList:: append(float num)

{

               ListNode *newNode = new ListNode;

               if(newNode == NULL) {

                              cout << "Error allocating memory for new list member!\n";

                              return 1;

               }

               newNode->value = num;

               newNode->next = NULL;

               newNode->prev = NULL;

               if(head == NULL)

                              head = newNode;

               else

               {

                              ListNode *curr = head;

                              while(curr->next != NULL)

                                             curr = curr->next;

                              newNode->prev = curr;

                              curr->next = newNode;

               }

               return 0;

}

// The deleteNode function searches for a node with searchValue as its value.

// The node, if found,is deleted from the list and from memory. *

void LinkedList::deleteNode(float num)

{

               struct ListNode *nodePtr = head;

               if(head==NULL) {

                              cout << "The list was empty\n";

                              return;

               }

               if(head->value == num) {

                              head = head->next;

                              head->prev = NULL;

                              delete [] nodePtr;

               }

               else {

                              while((nodePtr != NULL)&&(nodePtr->value != num)) {

                                             nodePtr = nodePtr->next;

                              }

                              if(nodePtr==NULL)

                                             cout << "The value " << num << " is not in this list!\n";

                              else {

                                             nodePtr->prev->next = nodePtr->next;

                                             if(nodePtr->next != NULL)

                                                            nodePtr->next->prev = nodePtr->prev;

                                             delete [] nodePtr;

                              }

               }

}

void LinkedList::destroyList()

{

               struct ListNode *nodePtr = head, *nextNode = nodePtr;

               if(head==NULL) {

                              cout << "The list is empty\n";

                              return;

               }

               while (nodePtr != NULL) {

                              nextNode = nodePtr->next;

                              delete [] nodePtr;

                              nodePtr = nextNode;

               }

}

// displayList shows the value stored in each node

// of the linked list pointed to by head.

void LinkedList::displayList()

{

               struct ListNode *nodePtr = head;

               if (nodePtr == NULL)

                              cout << "The list is empty\n";

               else {

                              while (nodePtr != NULL) {

                                             cout << nodePtr->value << endl;

                                             nodePtr = nodePtr->next;

                              }

               }

}

class Queue :public LinkedList

{

public:

               int enqueue(float num);

               void dequeue();

};

int Queue::enqueue(float num)

{

               return(LinkedList::append(num));

}

void Queue::dequeue()

{

               LinkedList::deleteNode(getHead()->value);

}

class Stack : public LinkedList

{

public:

               int push(float num);

               void pop();

};

int Stack::push(float num)

{

               return(LinkedList::prepend(num));

}

void Stack::pop()

{

               LinkedList::deleteNode(getHead()->value);

}

int main() {

               //int num;

               //char answer, choice;

               LinkedList temp;

               temp.insertNode(35);

               temp.insertNode(12);

               temp.insertNode(24);

               temp.insertNode(32);

               cout <<"Initial Linked List : "<< endl;

               temp.displayList();

               temp.deleteNode(24);

               temp.deleteNode(25);

               cout <<"Linked List after deletion: "<< endl;

               temp.displayList();

               // test stack

               Stack stack;

               for(float i=1;i<5;i++)

                              stack.push(i);

               cout<<"Initial Stack : "<<endl;

               stack.displayList();

               stack.pop();

               stack.pop();

               cout<<"After popping 2 elements Stack : "<<endl;

               stack.displayList();

               // test queue

               Queue queue;

               for(float i=1;i<5;i++)

                              queue.enqueue(i);

               cout<<"Initial Queue : "<<endl;

               queue.displayList();

               queue.dequeue();

               queue.dequeue();

               cout<<"After deleting 2 elements Queue : "<<endl;

               queue.displayList();

               return 0;

}

//end of program

Output:

List was empty 35 is part of lists first node Initial Linked List: 12 24 32 35 The value 25 is not in this list! Linked List

Add a comment
Know the answer?
Add Answer to:
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • In C++, change this code to a circular linked list #include <iostream> using namespace std; struct...

    In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • #include <iostream> using namespace std; struct ListNode string data; ListNode *next; ); int main() { ListNode...

    #include <iostream> using namespace std; struct ListNode string data; ListNode *next; ); int main() { ListNode *ptr, "list; list - new ListNode; list->data - "Boston"; ptr - new ListNode; ptr)data - "New York"; ptr->next = nullptr; list->next = new ListNode; list->next->data - "Houston": list->next->next = ptr 17 new code goes here Which of the following code correctly inserts "Chicago" at beginning of the list when added at point of insertion indicated above None of these No answer text provided. list->data...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT