Question

Ordered Linked List Note-Start with a new C++programproject. Insert See page 302 o This section, in the book, shows class object implementation o You are not required to implement as a class/object o You are not required to use the count (linked list counter) In main o Must implement the 4 cases (1,2,3A and 3B) o Run the code to show it willinsertin all four cases insert (first, last,37), ll case l insert (first, last, l4):ll case 2 insert (first, last, 68:llcase 3A insert(first, last, 47:llcase 3B displayList(first: llifyourinsert function works,itwildisplay sorted order Show your work when finished Although we covered Doubly Linked List today, be sure to re-read Doubly Linked List for next Tuesdays class.
How would you get this lab to work with the numbers 37 14 68 47, the book we are using is Data structures using C++ by D.S. Malik. Please I need help? Just keeps repeating the same question.

Code:

#include <iostream>
#include <cstdlib>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
void insertFront(nodeType*& first);
void insertBack(nodeType*& last);
void deleteFirst(nodeType*& first);
void deleteLast(nodeType*& last, nodeType* first);
int main()
{
nodeType *first, *last;
int num;
createList(first, last);
int choice;
while(true)
{
cout<<"1. Insert Front.\n2. Insert Last.\n3. Delete Front.\n4. Delete Last.\n5. Print List.\n6. Exit.\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
insertFront(first);
break;
case 2:
insertBack(last);
break;
case 3:
deleteFirst(first);
break;
case 4:
deleteLast(last, first);
break;
case 5:
printList(first);
break;
case 6:
return 0;
default:
cout<<"Invalid menu option. Try again."<<endl;
}
}
system("PAUSE");
return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
int number;
nodeType *newNode;
first = NULL;
last = NULL;
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
while (1)
{
newNode = new nodeType;
// create new node newNode->info = number;
newNode->link = NULL;
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
if(number == -999)
break;
newNode->info = number;
if (first == NULL)
{
first = newNode;
last = newNode;
cout<<"Info "<<first->info;
}
else
{
last->link = newNode;
last = newNode;
}

} // end of while-loop
} // end of build list function

void deleteFirst(nodeType*& first)
{
nodeType *temp;
temp= first;
first= temp->link;
delete temp;
return;
}
void deleteLast(nodeType*& last, nodeType* current)
{
nodeType *temp;
while(current->link != NULL)
{
temp=current;
current=current->link;
}
temp=last;
current->link=NULL;
delete temp;
last = current;
return;
}

void insertFront(nodeType*& front)
{
int num;
cout<<"\n Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= front;
front = newNode;
return;
}

void insertBack(nodeType*& last)
{
int num;
cout<<"\n Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= NULL;
last->link= newNode;
last = newNode;
return;
}

void printList(nodeType*& first)
{
cout<<"Inside printList...printing linked list...\n"<<endl;
nodeType *current;
current = new nodeType;
current = first;
while (current != NULL)
{
cout << current->info << " ";
current = current->link;
}
cout<<endl;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If I understand the question correct, Assignment is on implementation of ordered linked list. In your program, there are no conditions to main the order in linked list. From the assignment description, there should be only insert function. I have modified the program accordingly. Please find below.

Program:

#include <iostream>
#include <cstdlib>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
void insert(nodeType*& first, nodeType*& last);
void deleteFirst(nodeType*& first);
void deleteLast(nodeType*& last, nodeType* first);
int main()
{
nodeType *first, *last;
int num;
cout<<"Implementation of Ordered Linked List\n Building Linked List.....\n";
createList(first,last);
int choice;
while(true)
{
cout<<"1. Insert.\n2. Delete Front.\n3. Delete Last.\n4. Print List.\n5. Exit.\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
insert(first,last);
break;
case 2:
deleteFirst(first);
break;
case 3:
deleteLast(last, first);
break;
case 4:
printList(first);
break;
case 5:
return 0;
default:
cout<<"Invalid menu option. Try again."<<endl;
}
}
system("PAUSE");
return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
int number;
nodeType *newNode,*temp;
first = NULL;
last = NULL;
while (1)
{
newNode = new nodeType;
// create new node newNode->info = number;
newNode->link = NULL;
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
if(number == -999)
break;
newNode->info = number;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else if(newNode->info <= first->info)
{
   newNode->link = first;
   first = newNode;
}
else if(newNode->info > last->info)
{
   last->link= newNode;
last = newNode;
}
else
{
   temp = first;
while(newNode-> info > (temp->link)->info && temp->link!=NULL)
{
   temp=temp->link;
}
newNode->link = temp->link;
temp->link = newNode;
}
} // end of while-loop
} // end of build list function

void deleteFirst(nodeType*& first)
{
nodeType *temp;
temp= first;
first= temp->link;
delete temp;
return;
}
void deleteLast(nodeType*& last, nodeType* current)
{
nodeType *temp;
while(current->link != NULL)
{
temp=current;
current=current->link;
}
temp=last;
current->link=NULL;
delete temp;
last = current;
return;
}

void insert(nodeType*& first, nodeType*& last)
{
int number;
nodeType *newNode,*temp;
cout<<"Enter an integer: ";
cin>>number;
newNode = new nodeType;
newNode->info = number;
if(newNode->info <= first->info)
{
   newNode->link = first;
   first = newNode;
}
else if(newNode->info > last->info)
{
   last->link= newNode;
last = newNode;
}
else
{
   temp = first;
while(newNode-> info > (temp->link)->info && temp->link!=NULL)
{
   temp=temp->link;
}
newNode->link = temp->link;
temp->link = newNode;
}
return;
}

void printList(nodeType*& first)
{
cout<<"Inside printList...printing linked list...\n"<<endl;
nodeType *current;
current = new nodeType;
current = first;
while (current != NULL)
{
cout << current->info << " ";
current = current->link;
}
cout<<endl;
}

Output:

If you find the answer useful, kindly upvote.

Add a comment
Know the answer?
Add Answer to:
How would you get this lab to work with the numbers 37 14 68 47, the...
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
  • This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...

    This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *next;    double value; }; void createList(nodeType*& first, nodeType*& last, ifstream& inf); void printList(nodeType* first); int main() {    nodeType *first, *last;    int num;    ifstream infile;    infile.open("InputIntegers.txt");    createList(first, last, infile);    printList(first);    infile.close();    system("pause");    return 0; } void createList(nodeType*& first, nodeType*& last, ifstream& infile) {    int number;...

  • PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT...

    PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT Problem: The following function, buildListForward, builds a linked list in a forward manner and returns the pointer of the built list: Q1: The bulidListForward function to create a linked List structure with the keyboard input( cin >> num). Change this function to receive the values stored in the array from the main function( use int type pointer variable). eg. nodeType* buildListForward(int *arrayPrt, int Size)...

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • #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...

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • 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...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

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
ADVERTISEMENT