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.
How would you get this lab to work with the numbers 37 14 68 47, the...
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 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 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 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; }; 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 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 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 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 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 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)...