SlideShare a Scribd company logo
In C++
Write a recursive function to determine whether or not a Linked List is in sorted order (smallest
value to largest value).
Add the following function prototypes to your List class under the section for access functions,
then implement the functions below the class definition:
public:
/**Access Functions*/
bool isSorted();
//Wrapper function that calls the isSorted helper function to determine whether
//a list is sorted in ascending order.
//We will consider that a list is trivially sorted if it is empty.
//Therefore, no precondition is needed for this function
private:
bool isSorted(Nodeptr node);
//Helper function for the public isSorted() function.
//Recursively determines whether a list is sorted in ascending order.
#include //for NULL
#include
#include
using namespace std;
template //list stores generic list data, not any specific C++ type
class List
{
private:
struct Node
{
listdata data;
Node* next;
Node* previous;
Node(listdata data): data(data), next(NULL), previous(NULL){}
};
typedef struct Node* Nodeptr;
Nodeptr first;
Nodeptr last;
Nodeptr iterator;
int size;
public:
/**Constructors and Destructors*/
List();
//Default constructor; initializes and empty list
//Postcondition: numeric values equated to zero, or strings should be empty.
List(const List &list);
~List();
//Destructor. Frees memory allocated to the list
//Postcondition: position NodePtr at next Node
/**Accessors*/
listdata getFirst();
//Returns the first element in the list
//Precondition:NodePtr points to the first node on list
listdata getLast();
//Returns the last element in the list
//Precondition: make new node first on the list
listdata getIterator();
bool isEmpty();
//Determines whether a list is empty.
int getSize();
//Returns the size of the list
/**Manipulation Procedures*/
void startIterator();
void advanceIterator();
void removeLast();
//Removes the value of the last element in the list
//Precondition: list is not empty, not the first element.
//Postcondition: one remaining node
void removeIterator();
void removeFirst();
//Removes the value of the first element in the list
//Precondition: list is not empty
//Postcondition: no nodes left
void insertLast(listdata data);
//Inserts a new element at the end of the list
//If the list is empty, the new element becomes both first and last
//Postcondition: next equal to null
void insertFirst(listdata data);
//Inserts a new element at the start of the list
//If the list is empty, the new element becomes both first and last
//Postcondition:point nodePtr next node on list
/**Additional List Operations*/
bool offEnd();
void printList();
//Prints to the console the value of each element in the list sequentially
//and separated by a blank space
//Prints nothing if the list is empty
void insertIterator(listdata data);
bool operator==(const List &list);
};
// constructor definition
template
List::List(): first(NULL), last(NULL), iterator(NULL), size(0) {}
template
List::List(const List &list): size(list.size)
{
if(list.first == NULL) //If the original list is empty, make an empty list for this list
{
first = last = iterator = NULL;
}
else
{
first = new Node(list.first->data); //calling Node constructor
Nodeptr temp = list.first; //set a temporary node pointer to point at the first of the original list
iterator = first; //set iterator to point to first node of the new list
while(temp->next != NULL)
{
temp = temp->next; //advance up to the next node in the original list
iterator->next = new Node(temp->data); //using node constructor to create a new node with
copy of data
iterator = iterator->next; //advance to this new node
}
last = iterator;
iterator = NULL;
}
}
template
List::~List()
{
Nodeptr cursor = first;
Nodeptr temp;
while(cursor != NULL)
{
temp = cursor->next;
delete cursor;
cursor = temp;
}
}
//inserts first node
template //Step 1: template the function
void List::insertFirst(listdata data)
{
if (size == 0) //Why is this necessary?
{
first = new Node(data);
last = first; //notice the order here. Assignment is right to left
}
else{
Nodeptr N = new Node(data);//create the new node by calling the node constructor
N->next = first;//set the new node's next field to point to the first node
first->previous = N;
first = N;}//point the first pointer to the new node
size++;
}
template
void List::printList()
{
iterator = first; //create a temporary iterator
while (iterator != NULL) {
coutnext; //Add two lines of code here
//Hint: One statement should contain a cout
}
cout << endl; //What does this do?
}
template
void List::insertLast(listdata data)
{
if (size == 0) //Why is this necessary?
{
Nodeptr n = new Node(data);
first = last = n; //notice the order here. Assignment is left to right
}
else
{
Nodeptr N = new Node(data);//create the new node by calling the node constructor
last->next = N;//set the new node's next field to point to the last node
last->previous = N;
last = N;//point the last pointer to the new node
}
size++;
}
template
void List::removeFirst()
{
if (size==0){
cout << "removeFirst: List is empty. No data to remove." << endl;
} else if (size == 1) {
delete first;
first = last = NULL;
size = 0;
} else {
Nodeptr temp = first; //store pointer to first so we don't lose access to it
first = first->next; //advance the pointer
delete temp; //free the memory for the original first node
size--;
}
}
template
void List::removeLast()
{
if (size==0){
cout<<"last is empty";
} else if (size==1) {
delete last;
last = first = NULL;
size = 0;//fill in the missing lines here
} else {
last->previous->next=last->next;
delete last; //free the memory for the original last node
last->next = NULL; //so last->next is not pointing at freed memory
size--;
}
}
template
bool List::isEmpty()
{
return (size==0);
}
template
listdata List::getIterator()
{
assert(size != 0);
assert(iterator != NULL);
return iterator->data;
}
template
int List::getSize()
{
return size;
}
template
listdata List::getFirst()
{
assert(first != NULL);
return first->data;
}
template
listdata List::getLast()
{
assert(last != NULL);
return last->data;
}
template
void List::insertIterator(listdata data)
{
if(size==0)
cout<<"list empty"< else if(iterator == last)
{
insertLast(data);
}
else{
Nodeptr n = new Node(data);
n->next = iterator ->next;
n->previous = iterator;
iterator->next->previous=n;
iterator->next = n;
size++;
}
}
template
void List::startIterator()
{
iterator = first;
}
template
void List::advanceIterator()
{
iterator=iterator->next;
}
template
void List::removeIterator()
{
assert(iterator != NULL);
if (iterator == first) {
removeFirst();
} else if (iterator == last) {
removeLast();
} else {
iterator->previous->next = iterator->next;
iterator->next->previous = iterator->previous;
delete iterator;
iterator = NULL;
size--;
}
}
template
bool List::offEnd()
{
return iterator == NULL;
}
template
bool List::operator==(const List& list)
{
if(size != list.size)
return false;
Nodeptr temp1 = first;
Nodeptr temp2 = list.first;
while(temp1 != NULL)
{
if(temp1->data != temp2->data)
return false;
temp1 = temp1->next;
temp2 = temp2->next;
}
return true;
}
Solution
#include "stdafx.h" #include < iostream > #include < conio.h > #include < stdlib.h > using
namespace std; struct node { int data; node *prev, *next; }*q, *temp,
*start=NULL; int c1, c2 ; void create(); void display(); void insert(); void del(); void main() {
system("cls"); while(1) { system("cls"); cout << " tt *******
MAIN MENU ****** " ; cout << " press 1 for adding data  " ; cout << "
press 2 for displaying data  " ; cout << " press 3 for insertion  " ; cout <<
" press 4 for deletion  " ; cout << " press 0 for exit " ; char ch;
ch=getch(); switch(ch) { case '1':
system("cls"); create(); break;
case '2': system("cls");
display(); getch(); break;
case '3': system("cls"); insert();
break; case '4':
system("cls"); del(); break;
case '0': exit(1); } } } void create() { temp =
new node; temp -> next = NULL; cout << " Enter data " ; cin >> temp -> data ;
if(start == NULL) { start = temp; temp -> prev = NULL; } else {
q= start; while(q->next != NULL) { q = q->next;
} q->next = temp; temp->prev = q; } } void display() { q=start;
while(q!=NULL) { cout>choice; switch(choice) { case 1:
system("cls"); temp = new node;
cout<<"Enter data  "; cin>>temp->data; start->prev
=temp; temp->next = start; start = temp;
temp -> prev = NULL; break; case 2:
system("cls"); cout<<"Enter the data aftre which u want to add this ";
int ch; cin>>ch; q= start;
while(q->next!=NULL) { if(q->data ==
ch) { temp = new node;
cout<<"Enter data  "; cin>>temp->data;
q->next->prev = temp; temp->next = q-
>next; temp->prev = q; q->next
= temp; } q = q->next; }
break; case 3: system("cls");
temp = new node; cout<<"Enter data "; cin>>
temp->data; temp->next = NULL; q = start;
while(q->next != NULL) { q= q->next;
} q->next = temp; temp->prev =
NULL; } } void del() { system("cls"); cout<<"Enter the data you want to delete  ";
int num; cin>>num; q = start; if (start->data == num) start = start -> next; else {
while(q != NULL) { if(q->next->data == num) { temp = q->next;
q->next = temp->next; temp->next->prev = q; delete temp; }
q = q->next; } } }

More Related Content

Similar to In C++Write a recursive function to determine whether or not a Lin.pdf

dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
ssuser0be977
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
MARRY7
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
AravindAnand21
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
aravlitraders2012
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
feelinggift
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
KUNALHARCHANDANI1
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdf
fatoryoutlets
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
sales98
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
vishalateen
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
My question is pretty simple, I just want to know how to call my ope.pdf
My question is pretty simple, I just want to know how to call my ope.pdfMy question is pretty simple, I just want to know how to call my ope.pdf
My question is pretty simple, I just want to know how to call my ope.pdf
jeetumordhani
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming Exam Help
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
climatecontrolsv
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructures
martha leon
 

Similar to In C++Write a recursive function to determine whether or not a Lin.pdf (20)

dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Table.java Huffman code frequency tableimport java.io.;im.docx
 Table.java Huffman code frequency tableimport java.io.;im.docx Table.java Huffman code frequency tableimport java.io.;im.docx
Table.java Huffman code frequency tableimport java.io.;im.docx
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdf
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
My question is pretty simple, I just want to know how to call my ope.pdf
My question is pretty simple, I just want to know how to call my ope.pdfMy question is pretty simple, I just want to know how to call my ope.pdf
My question is pretty simple, I just want to know how to call my ope.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructures
 

More from flashfashioncasualwe

How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdfHow does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
flashfashioncasualwe
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
flashfashioncasualwe
 
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdfFocus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
flashfashioncasualwe
 
Decision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdfDecision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdf
flashfashioncasualwe
 
Explain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdfExplain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdf
flashfashioncasualwe
 
During a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdfDuring a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdf
flashfashioncasualwe
 
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdfExplain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
flashfashioncasualwe
 
Describe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdfDescribe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdf
flashfashioncasualwe
 
Explain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdfExplain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdf
flashfashioncasualwe
 
Develop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdfDevelop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdf
flashfashioncasualwe
 
Describe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdfDescribe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdf
flashfashioncasualwe
 
All answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdfAll answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdf
flashfashioncasualwe
 
C programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdfC programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdf
flashfashioncasualwe
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
flashfashioncasualwe
 
Write an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdfWrite an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdf
flashfashioncasualwe
 
which of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdfwhich of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdf
flashfashioncasualwe
 
2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf
flashfashioncasualwe
 
10. Benefits and costs of International Trade Search for a newspap.pdf
10. Benefits and costs of International Trade  Search for a newspap.pdf10. Benefits and costs of International Trade  Search for a newspap.pdf
10. Benefits and costs of International Trade Search for a newspap.pdf
flashfashioncasualwe
 
Why does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdfWhy does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdf
flashfashioncasualwe
 
Use the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdfUse the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdf
flashfashioncasualwe
 

More from flashfashioncasualwe (20)

How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdfHow does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
How does the mutation rate of speciation in the Dobzhansky- Muller m.pdf
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
 
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdfFocus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
Focus on Writing 4. Supporting a Point of View Do you think Social Se.pdf
 
Decision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdfDecision-Making Across the Organization The board of trustees of a lo.pdf
Decision-Making Across the Organization The board of trustees of a lo.pdf
 
Explain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdfExplain the experience of African-Americans in the South over the co.pdf
Explain the experience of African-Americans in the South over the co.pdf
 
During a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdfDuring a diversity management session, a manager suggests that stereo.pdf
During a diversity management session, a manager suggests that stereo.pdf
 
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdfExplain why a mycoplasma PCR kit might give a negative result when u.pdf
Explain why a mycoplasma PCR kit might give a negative result when u.pdf
 
Describe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdfDescribe the difference between the MOV instruction and the LEA instr.pdf
Describe the difference between the MOV instruction and the LEA instr.pdf
 
Explain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdfExplain how enzymes work, explaining the four major types of metabol.pdf
Explain how enzymes work, explaining the four major types of metabol.pdf
 
Develop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdfDevelop an inventory management system for an electronics store. The .pdf
Develop an inventory management system for an electronics store. The .pdf
 
Describe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdfDescribe one event from your daily life when you have changed your o.pdf
Describe one event from your daily life when you have changed your o.pdf
 
All answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdfAll answers must be in your own wordsProvide a good, understandabl.pdf
All answers must be in your own wordsProvide a good, understandabl.pdf
 
C programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdfC programming tweak needed for a specific program.This is the comp.pdf
C programming tweak needed for a specific program.This is the comp.pdf
 
Add to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdfAdd to BST.java a method height() that computes the height of the tr.pdf
Add to BST.java a method height() that computes the height of the tr.pdf
 
Write an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdfWrite an awareness objective for a newly formed adolescent chemical .pdf
Write an awareness objective for a newly formed adolescent chemical .pdf
 
which of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdfwhich of these prokaryotes are most likely to be found in the immedi.pdf
which of these prokaryotes are most likely to be found in the immedi.pdf
 
2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf2. Why is only one end point observed for citric acid even though it .pdf
2. Why is only one end point observed for citric acid even though it .pdf
 
10. Benefits and costs of International Trade Search for a newspap.pdf
10. Benefits and costs of International Trade  Search for a newspap.pdf10. Benefits and costs of International Trade  Search for a newspap.pdf
10. Benefits and costs of International Trade Search for a newspap.pdf
 
Why does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdfWhy does the pattern in a shift register shift only one bit position.pdf
Why does the pattern in a shift register shift only one bit position.pdf
 
Use the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdfUse the Internet to identify three network firewalls, and create a t.pdf
Use the Internet to identify three network firewalls, and create a t.pdf
 

Recently uploaded

Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 

Recently uploaded (20)

Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 

In C++Write a recursive function to determine whether or not a Lin.pdf

  • 1. In C++ Write a recursive function to determine whether or not a Linked List is in sorted order (smallest value to largest value). Add the following function prototypes to your List class under the section for access functions, then implement the functions below the class definition: public: /**Access Functions*/ bool isSorted(); //Wrapper function that calls the isSorted helper function to determine whether //a list is sorted in ascending order. //We will consider that a list is trivially sorted if it is empty. //Therefore, no precondition is needed for this function private: bool isSorted(Nodeptr node); //Helper function for the public isSorted() function. //Recursively determines whether a list is sorted in ascending order. #include //for NULL #include #include using namespace std; template //list stores generic list data, not any specific C++ type class List { private: struct Node { listdata data; Node* next; Node* previous; Node(listdata data): data(data), next(NULL), previous(NULL){} }; typedef struct Node* Nodeptr; Nodeptr first;
  • 2. Nodeptr last; Nodeptr iterator; int size; public: /**Constructors and Destructors*/ List(); //Default constructor; initializes and empty list //Postcondition: numeric values equated to zero, or strings should be empty. List(const List &list); ~List(); //Destructor. Frees memory allocated to the list //Postcondition: position NodePtr at next Node /**Accessors*/ listdata getFirst(); //Returns the first element in the list //Precondition:NodePtr points to the first node on list listdata getLast(); //Returns the last element in the list //Precondition: make new node first on the list listdata getIterator(); bool isEmpty(); //Determines whether a list is empty. int getSize(); //Returns the size of the list /**Manipulation Procedures*/ void startIterator(); void advanceIterator(); void removeLast(); //Removes the value of the last element in the list //Precondition: list is not empty, not the first element. //Postcondition: one remaining node void removeIterator();
  • 3. void removeFirst(); //Removes the value of the first element in the list //Precondition: list is not empty //Postcondition: no nodes left void insertLast(listdata data); //Inserts a new element at the end of the list //If the list is empty, the new element becomes both first and last //Postcondition: next equal to null void insertFirst(listdata data); //Inserts a new element at the start of the list //If the list is empty, the new element becomes both first and last //Postcondition:point nodePtr next node on list /**Additional List Operations*/ bool offEnd(); void printList(); //Prints to the console the value of each element in the list sequentially //and separated by a blank space //Prints nothing if the list is empty void insertIterator(listdata data); bool operator==(const List &list); }; // constructor definition template List::List(): first(NULL), last(NULL), iterator(NULL), size(0) {} template List::List(const List &list): size(list.size) { if(list.first == NULL) //If the original list is empty, make an empty list for this list { first = last = iterator = NULL; } else { first = new Node(list.first->data); //calling Node constructor Nodeptr temp = list.first; //set a temporary node pointer to point at the first of the original list iterator = first; //set iterator to point to first node of the new list
  • 4. while(temp->next != NULL) { temp = temp->next; //advance up to the next node in the original list iterator->next = new Node(temp->data); //using node constructor to create a new node with copy of data iterator = iterator->next; //advance to this new node } last = iterator; iterator = NULL; } } template List::~List() { Nodeptr cursor = first; Nodeptr temp; while(cursor != NULL) { temp = cursor->next; delete cursor; cursor = temp; } } //inserts first node template //Step 1: template the function void List::insertFirst(listdata data) { if (size == 0) //Why is this necessary? { first = new Node(data); last = first; //notice the order here. Assignment is right to left } else{ Nodeptr N = new Node(data);//create the new node by calling the node constructor
  • 5. N->next = first;//set the new node's next field to point to the first node first->previous = N; first = N;}//point the first pointer to the new node size++; } template void List::printList() { iterator = first; //create a temporary iterator while (iterator != NULL) { coutnext; //Add two lines of code here //Hint: One statement should contain a cout } cout << endl; //What does this do? } template void List::insertLast(listdata data) { if (size == 0) //Why is this necessary? { Nodeptr n = new Node(data); first = last = n; //notice the order here. Assignment is left to right } else { Nodeptr N = new Node(data);//create the new node by calling the node constructor last->next = N;//set the new node's next field to point to the last node last->previous = N; last = N;//point the last pointer to the new node } size++; }
  • 6. template void List::removeFirst() { if (size==0){ cout << "removeFirst: List is empty. No data to remove." << endl; } else if (size == 1) { delete first; first = last = NULL; size = 0; } else { Nodeptr temp = first; //store pointer to first so we don't lose access to it first = first->next; //advance the pointer delete temp; //free the memory for the original first node size--; } } template void List::removeLast() { if (size==0){ cout<<"last is empty"; } else if (size==1) { delete last; last = first = NULL; size = 0;//fill in the missing lines here } else { last->previous->next=last->next; delete last; //free the memory for the original last node last->next = NULL; //so last->next is not pointing at freed memory size--; } } template bool List::isEmpty()
  • 7. { return (size==0); } template listdata List::getIterator() { assert(size != 0); assert(iterator != NULL); return iterator->data; } template int List::getSize() { return size; } template listdata List::getFirst() { assert(first != NULL); return first->data; } template listdata List::getLast() { assert(last != NULL); return last->data; } template void List::insertIterator(listdata data) { if(size==0) cout<<"list empty"< else if(iterator == last) {
  • 8. insertLast(data); } else{ Nodeptr n = new Node(data); n->next = iterator ->next; n->previous = iterator; iterator->next->previous=n; iterator->next = n; size++; } } template void List::startIterator() { iterator = first; } template void List::advanceIterator() { iterator=iterator->next; } template void List::removeIterator() { assert(iterator != NULL); if (iterator == first) { removeFirst(); } else if (iterator == last) { removeLast(); } else { iterator->previous->next = iterator->next; iterator->next->previous = iterator->previous; delete iterator; iterator = NULL;
  • 9. size--; } } template bool List::offEnd() { return iterator == NULL; } template bool List::operator==(const List& list) { if(size != list.size) return false; Nodeptr temp1 = first; Nodeptr temp2 = list.first; while(temp1 != NULL) { if(temp1->data != temp2->data) return false; temp1 = temp1->next; temp2 = temp2->next; } return true; } Solution #include "stdafx.h" #include < iostream > #include < conio.h > #include < stdlib.h > using namespace std; struct node { int data; node *prev, *next; }*q, *temp, *start=NULL; int c1, c2 ; void create(); void display(); void insert(); void del(); void main() { system("cls"); while(1) { system("cls"); cout << " tt ******* MAIN MENU ****** " ; cout << " press 1 for adding data " ; cout << " press 2 for displaying data " ; cout << " press 3 for insertion " ; cout << " press 4 for deletion " ; cout << " press 0 for exit " ; char ch; ch=getch(); switch(ch) { case '1': system("cls"); create(); break; case '2': system("cls");
  • 10. display(); getch(); break; case '3': system("cls"); insert(); break; case '4': system("cls"); del(); break; case '0': exit(1); } } } void create() { temp = new node; temp -> next = NULL; cout << " Enter data " ; cin >> temp -> data ; if(start == NULL) { start = temp; temp -> prev = NULL; } else { q= start; while(q->next != NULL) { q = q->next; } q->next = temp; temp->prev = q; } } void display() { q=start; while(q!=NULL) { cout>choice; switch(choice) { case 1: system("cls"); temp = new node; cout<<"Enter data "; cin>>temp->data; start->prev =temp; temp->next = start; start = temp; temp -> prev = NULL; break; case 2: system("cls"); cout<<"Enter the data aftre which u want to add this "; int ch; cin>>ch; q= start; while(q->next!=NULL) { if(q->data == ch) { temp = new node; cout<<"Enter data "; cin>>temp->data; q->next->prev = temp; temp->next = q- >next; temp->prev = q; q->next = temp; } q = q->next; } break; case 3: system("cls"); temp = new node; cout<<"Enter data "; cin>> temp->data; temp->next = NULL; q = start; while(q->next != NULL) { q= q->next; } q->next = temp; temp->prev = NULL; } } void del() { system("cls"); cout<<"Enter the data you want to delete "; int num; cin>>num; q = start; if (start->data == num) start = start -> next; else { while(q != NULL) { if(q->next->data == num) { temp = q->next; q->next = temp->next; temp->next->prev = q; delete temp; } q = q->next; } } }