(C++)Below are codes of sortedlist.h, sortedlist.cpp and dllclient.cpp. I know i
ID: 3814832 • Letter: #
Question
(C++)Below are codes of sortedlist.h, sortedlist.cpp and dllclient.cpp. I know it's kind of long but Please read on the follow instructions.
Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and thenimplement the new operations below. The class should have the following additional class methods:
• A reverse method: this method will reverse the order of the doubly linked list. This method
takes no parameters, and returns nothing.
• A printFront method: this method prints the list from front to back, one element per line.The method is void, takes no parameters, and should not be able to modify private classmembers.
• A printBack method: this method prints the list from back to front, one element per line.The method is void, takes no parameters, and should not be able to modify private classmembers.o Additional requirement: start at the tail pointer and traverse the list using pointersin order to print the list. In other words, cannot use retrieveElement or ptrTo!
A client file called dllClient.cpp is provided to test the program. Use this client,unmodified, to generate the final test results to turn in with your program.
P.S this is a “by-position” doubly-linked list!
// Header file for sortedList, a pointer-based *sorted* by-position list of type // specified by typedef.
//this typedef defines the type of items in the list (int by default) typedef int listItemType;
class sortedList { public: //CONSTRUCTORS and DESTRUCTOR
//Default constructor, creates an empty list sortedList();
//Copy constructor, makes this a copy of list original sortedList(const sortedList& original);
//Destructor, free memory for list ~sortedList();
//ACCESSORS
//Accessor to check if list is empty //Parameters: none //Returns: true if list is empty, false otherwise (a bool) bool isEmpty() const;
//Accessor to retrieve length of list //Parameters: none //Returns: length of list (an int) int getLength() const;
//Locate an element in the list. //Parameters: the element (key) to locate (listItemType) //Returns: the position of the key in the list (an int, -1 if does not exist) void locateElement(listItemType key, int& position); //MUTATORS
//Mutator to insert element into sorted list. Automatically inserts at correct position. //Parameters: the element to insert into the list (listItemType) //Returns: nothing void insertElement(listItemType newItem);
//Mutator to delete element from list by position (starting at 1) //Parameters: the position (int) at which to delete //Returns: true if successful, false otherwise (a bool) bool deleteElement(int position);
//Mutator to retrieve an element from list by position (starting at 1) //Parameters: the position (int) from which to retrieve an element //Returns: true if successful (element is stored in dataItem), false otherwise (a bool) bool retrieveElement(int position, listItemType& dataItem) const;
//OVERLOADED OPERATORS
//Overloaded equality operator //Parameters: the list to compare to this (sortedList) //Returns: If lengths match, all elements match (in order), return true bool sortedList::operator== (const sortedList& right);
//Overloaded inequality operator //Parameters: the list to compare to this (sortedList) //Returns: If length mismatch, or all elements do not match (in order), return true bool sortedList::operator!= (const sortedList& right);
private: struct listNode { listItemType item; listNode *next; };
int size; // number of items in list
listNode *head; // pointer to linked list of items
// Private member function that returns a pointer to the position-th node in the linked list. // (Private since client should only be able to access by position.) listNode *ptrTo(int position) const;
};
//******************************************************* // Implementation file LabListP.cpp for the ADT list. // Pointer-based implementation. //******************************************************* #include "LabListP.h" // header file #include // for NULL #include // for assert()
ListClass::ListClass() // Default Constructor : size(0), head(NULL) { }
ListClass::~ListClass() // Destructor { bool success;
while (!isEmpty()) { success = remove(1); // Repeatedly delete item 1 } }
bool ListClass::isEmpty() const { return bool(size == 0); }
int ListClass::getLength() const { return size; }
// Copy Constructor: Make DEEP copy ListClass::ListClass(const ListClass& existingList) : size(existingList.size) { if (existingList.head == NULL) head = NULL; // original list is empty
else { // copy first node head = new ListNode; assert(head != NULL); // check allocation
head->item = existingList.head->item;
// copy rest of list ListNode *newPtr = head; // new list pointer
// newPtr points to last node in new list // origPtr points to nodes in original list for (ListNode *origPtr = existingList.head->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new ListNode; // link new node to end of list assert(newPtr->next != NULL); newPtr = newPtr->next;
newPtr->item = origPtr->item; // copy the data newPtr->next = NULL; } } }
// assignment operator: Make DEEP copy ListClass& ListClass::operator=(const ListClass& rhs) { // TODO // Similar to Copy Constructor, except // - Avoid self-assignments such as “X = X;” // - Delete existing this-instance content before // making this-instance a copy of the rhs instance return(*this); }
// ---------------------------------------------------------------------- // Locates a specified node in a linked list. // Precondition: position is the number of the desired node. // Postcondition: Returns a pointer to the desired node. // If position < 1 or position > size (the number of nodes in the list), // returns NULL. // ----------------------------------------------------------------------
ListClass::ListNode *ListClass::ptrTo(int position) const { if ((position < 1) || (position > size)) return NULL;
else // count from the beginning of the list { ListNode *cur = head;
for (int skip = 1; skip < position; ++skip) cur = cur->next;
return cur; } }
bool ListClass::retrieve(int position, ListItemType& dataItem) const { bool success = bool( (position >= 1) && (position <= size) );
if (success) { // get pointer to node, then data in node ListNode *cur = ptrTo(position);
dataItem = cur->item; }
return(success); }
bool ListClass::insert(int position, ListItemType& newItem) { int newLength = size + 1;
// check parameter validity bool success = bool( (position >= 1) && (position <= newLength) );
if (success) { // create new node and place newItem in it ListNode *newPtr = new ListNode; if(newPtr == NULL) return(false); // cannot insert - allocation failed
size = newLength;
newPtr->item = newItem;
// attach new node to list if (position == 1) { // insert new node at beginning of list newPtr->next = head; head = newPtr; }
else { // insert new node to right of previous node ListNode *prev = ptrTo(position - 1);
// insert new node to right of prev newPtr->next = prev->next; prev->next = newPtr; } }
return(success); }
bool ListClass::remove(int position) { ListNode *cur;
bool success = bool( (position >= 1) && (position <= size) );
if (success) { --size;
if (position == 1) { // delete the first node from the list cur = head; // save pointer to node head = head->next; }
else { ListNode *prev = ptrTo(position - 1);
// delete the node after the node // to which prev points
cur = prev->next; // save pointer to node prev->next = cur->next; }
// return node to system cur->next = NULL; // safety - remove node from list delete cur; cur = NULL; // safety }
return(success); }
//Test client DllClient.cpp //DO NOT MODIFY
#include using namespace std;
#include "doublyLinkedList.h"
//prototypes void print(doublyLinkedList list);
int main() { doublyLinkedList first;
//create a list with 6 elements for testing first.insertElement(20); first.insertElement(10); first.insertElement(30); first.insertElement(40); first.insertElement(25); first.insertElement(5);
int pos;
first.locateElement(30, pos);
first.deleteElement(pos);
cout << "first, printed with client print: " << endl; print(first);
//create a copy of 6-element list doublyLinkedList second(first);
//test all 3 cases for delete second.deleteElement(1); second.deleteElement(4); second.deleteElement(2);
cout << " second, printed with client print: " << endl; print(second);
//test copy constructor doublyLinkedList third(first);
//test == and != cout << boolalpha; cout << " testing == " << endl; cout << (first == first) << endl; cout << (first == second) << endl; cout << (first == third) << endl;
cout << " testing != " << endl; cout << (first != first) << endl; cout << (first != second) << endl; cout << (first != third) << endl;
//test print functions cout << " testing printFront on first: " << endl; first.printFront();
cout << " testing printBack on first: " << endl; first.printBack();
//test reverse first.reverse();
cout << " testing printFront on first after reverse: " << endl; first.printFront();
cout << " testing printBack on first after reverse: " << endl; first.printBack();
return 0; }
//Client function to print a list of listItemType //Parameters: the list to print (posList) //Returns: nothing void print(doublyLinkedList list) { for (int i = 1; i <= list.getLength(); i++) { listItemType x; list.retrieveElement(i, x); cout << x << endl; } }
(C++)Below are codes of sortedlist.h, sortedlist.cpp and dllclient.cpp. I know it's kind of long but Please read on the follow instructions.
Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and thenimplement the new operations below. The class should have the following additional class methods:
• A reverse method: this method will reverse the order of the doubly linked list. This method
takes no parameters, and returns nothing.
• A printFront method: this method prints the list from front to back, one element per line.The method is void, takes no parameters, and should not be able to modify private classmembers.
• A printBack method: this method prints the list from back to front, one element per line.The method is void, takes no parameters, and should not be able to modify private classmembers.o Additional requirement: start at the tail pointer and traverse the list using pointersin order to print the list. In other words, cannot use retrieveElement or ptrTo!
A client file called dllClient.cpp is provided to test the program. Use this client,unmodified, to generate the final test results to turn in with your program.
P.S this is a “by-position” doubly-linked list!
// Header file for sortedList, a pointer-based *sorted* by-position list of type // specified by typedef.
//this typedef defines the type of items in the list (int by default) typedef int listItemType;
class sortedList { public: //CONSTRUCTORS and DESTRUCTOR
//Default constructor, creates an empty list sortedList();
//Copy constructor, makes this a copy of list original sortedList(const sortedList& original);
//Destructor, free memory for list ~sortedList();
//ACCESSORS
//Accessor to check if list is empty //Parameters: none //Returns: true if list is empty, false otherwise (a bool) bool isEmpty() const;
//Accessor to retrieve length of list //Parameters: none //Returns: length of list (an int) int getLength() const;
//Locate an element in the list. //Parameters: the element (key) to locate (listItemType) //Returns: the position of the key in the list (an int, -1 if does not exist) void locateElement(listItemType key, int& position); //MUTATORS
//Mutator to insert element into sorted list. Automatically inserts at correct position. //Parameters: the element to insert into the list (listItemType) //Returns: nothing void insertElement(listItemType newItem);
//Mutator to delete element from list by position (starting at 1) //Parameters: the position (int) at which to delete //Returns: true if successful, false otherwise (a bool) bool deleteElement(int position);
//Mutator to retrieve an element from list by position (starting at 1) //Parameters: the position (int) from which to retrieve an element //Returns: true if successful (element is stored in dataItem), false otherwise (a bool) bool retrieveElement(int position, listItemType& dataItem) const;
//OVERLOADED OPERATORS
//Overloaded equality operator //Parameters: the list to compare to this (sortedList) //Returns: If lengths match, all elements match (in order), return true bool sortedList::operator== (const sortedList& right);
//Overloaded inequality operator //Parameters: the list to compare to this (sortedList) //Returns: If length mismatch, or all elements do not match (in order), return true bool sortedList::operator!= (const sortedList& right);
private: struct listNode { listItemType item; listNode *next; };
int size; // number of items in list
listNode *head; // pointer to linked list of items
// Private member function that returns a pointer to the position-th node in the linked list. // (Private since client should only be able to access by position.) listNode *ptrTo(int position) const;
};
//******************************************************* // Implementation file LabListP.cpp for the ADT list. // Pointer-based implementation. //******************************************************* #include "LabListP.h" // header file #include // for NULL #include // for assert()
ListClass::ListClass() // Default Constructor : size(0), head(NULL) { }
ListClass::~ListClass() // Destructor { bool success;
while (!isEmpty()) { success = remove(1); // Repeatedly delete item 1 } }
bool ListClass::isEmpty() const { return bool(size == 0); }
int ListClass::getLength() const { return size; }
// Copy Constructor: Make DEEP copy ListClass::ListClass(const ListClass& existingList) : size(existingList.size) { if (existingList.head == NULL) head = NULL; // original list is empty
else { // copy first node head = new ListNode; assert(head != NULL); // check allocation
head->item = existingList.head->item;
// copy rest of list ListNode *newPtr = head; // new list pointer
// newPtr points to last node in new list // origPtr points to nodes in original list for (ListNode *origPtr = existingList.head->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new ListNode; // link new node to end of list assert(newPtr->next != NULL); newPtr = newPtr->next;
newPtr->item = origPtr->item; // copy the data newPtr->next = NULL; } } }
// assignment operator: Make DEEP copy ListClass& ListClass::operator=(const ListClass& rhs) { // TODO // Similar to Copy Constructor, except // - Avoid self-assignments such as “X = X;” // - Delete existing this-instance content before // making this-instance a copy of the rhs instance return(*this); }
// ---------------------------------------------------------------------- // Locates a specified node in a linked list. // Precondition: position is the number of the desired node. // Postcondition: Returns a pointer to the desired node. // If position < 1 or position > size (the number of nodes in the list), // returns NULL. // ----------------------------------------------------------------------
ListClass::ListNode *ListClass::ptrTo(int position) const { if ((position < 1) || (position > size)) return NULL;
else // count from the beginning of the list { ListNode *cur = head;
for (int skip = 1; skip < position; ++skip) cur = cur->next;
return cur; } }
bool ListClass::retrieve(int position, ListItemType& dataItem) const { bool success = bool( (position >= 1) && (position <= size) );
if (success) { // get pointer to node, then data in node ListNode *cur = ptrTo(position);
dataItem = cur->item; }
return(success); }
bool ListClass::insert(int position, ListItemType& newItem) { int newLength = size + 1;
// check parameter validity bool success = bool( (position >= 1) && (position <= newLength) );
if (success) { // create new node and place newItem in it ListNode *newPtr = new ListNode; if(newPtr == NULL) return(false); // cannot insert - allocation failed
size = newLength;
newPtr->item = newItem;
// attach new node to list if (position == 1) { // insert new node at beginning of list newPtr->next = head; head = newPtr; }
else { // insert new node to right of previous node ListNode *prev = ptrTo(position - 1);
// insert new node to right of prev newPtr->next = prev->next; prev->next = newPtr; } }
return(success); }
bool ListClass::remove(int position) { ListNode *cur;
bool success = bool( (position >= 1) && (position <= size) );
if (success) { --size;
if (position == 1) { // delete the first node from the list cur = head; // save pointer to node head = head->next; }
else { ListNode *prev = ptrTo(position - 1);
// delete the node after the node // to which prev points
cur = prev->next; // save pointer to node prev->next = cur->next; }
// return node to system cur->next = NULL; // safety - remove node from list delete cur; cur = NULL; // safety }
return(success); }
//Test client DllClient.cpp //DO NOT MODIFY
#include using namespace std;
#include "doublyLinkedList.h"
//prototypes void print(doublyLinkedList list);
int main() { doublyLinkedList first;
//create a list with 6 elements for testing first.insertElement(20); first.insertElement(10); first.insertElement(30); first.insertElement(40); first.insertElement(25); first.insertElement(5);
int pos;
first.locateElement(30, pos);
first.deleteElement(pos);
cout << "first, printed with client print: " << endl; print(first);
//create a copy of 6-element list doublyLinkedList second(first);
//test all 3 cases for delete second.deleteElement(1); second.deleteElement(4); second.deleteElement(2);
cout << " second, printed with client print: " << endl; print(second);
//test copy constructor doublyLinkedList third(first);
//test == and != cout << boolalpha; cout << " testing == " << endl; cout << (first == first) << endl; cout << (first == second) << endl; cout << (first == third) << endl;
cout << " testing != " << endl; cout << (first != first) << endl; cout << (first != second) << endl; cout << (first != third) << endl;
//test print functions cout << " testing printFront on first: " << endl; first.printFront();
cout << " testing printBack on first: " << endl; first.printBack();
//test reverse first.reverse();
cout << " testing printFront on first after reverse: " << endl; first.printFront();
cout << " testing printBack on first after reverse: " << endl; first.printBack();
return 0; }
//Client function to print a list of listItemType //Parameters: the list to print (posList) //Returns: nothing void print(doublyLinkedList list) { for (int i = 1; i <= list.getLength(); i++) { listItemType x; list.retrieveElement(i, x); cout << x << endl; } }
(C++)Below are codes of sortedlist.h, sortedlist.cpp and dllclient.cpp. I know it's kind of long but Please read on the follow instructions.
Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and thenimplement the new operations below. The class should have the following additional class methods:
• A reverse method: this method will reverse the order of the doubly linked list. This method
takes no parameters, and returns nothing.
• A printFront method: this method prints the list from front to back, one element per line.The method is void, takes no parameters, and should not be able to modify private classmembers.
• A printBack method: this method prints the list from back to front, one element per line.The method is void, takes no parameters, and should not be able to modify private classmembers.o Additional requirement: start at the tail pointer and traverse the list using pointersin order to print the list. In other words, cannot use retrieveElement or ptrTo!
A client file called dllClient.cpp is provided to test the program. Use this client,unmodified, to generate the final test results to turn in with your program.
P.S this is a “by-position” doubly-linked list!
// Header file for sortedList, a pointer-based *sorted* by-position list of type // specified by typedef.
//this typedef defines the type of items in the list (int by default) typedef int listItemType;
class sortedList { public: //CONSTRUCTORS and DESTRUCTOR
//Default constructor, creates an empty list sortedList();
//Copy constructor, makes this a copy of list original sortedList(const sortedList& original);
//Destructor, free memory for list ~sortedList();
//ACCESSORS
//Accessor to check if list is empty //Parameters: none //Returns: true if list is empty, false otherwise (a bool) bool isEmpty() const;
//Accessor to retrieve length of list //Parameters: none //Returns: length of list (an int) int getLength() const;
//Locate an element in the list. //Parameters: the element (key) to locate (listItemType) //Returns: the position of the key in the list (an int, -1 if does not exist) void locateElement(listItemType key, int& position); //MUTATORS
//Mutator to insert element into sorted list. Automatically inserts at correct position. //Parameters: the element to insert into the list (listItemType) //Returns: nothing void insertElement(listItemType newItem);
//Mutator to delete element from list by position (starting at 1) //Parameters: the position (int) at which to delete //Returns: true if successful, false otherwise (a bool) bool deleteElement(int position);
//Mutator to retrieve an element from list by position (starting at 1) //Parameters: the position (int) from which to retrieve an element //Returns: true if successful (element is stored in dataItem), false otherwise (a bool) bool retrieveElement(int position, listItemType& dataItem) const;
//OVERLOADED OPERATORS
//Overloaded equality operator //Parameters: the list to compare to this (sortedList) //Returns: If lengths match, all elements match (in order), return true bool sortedList::operator== (const sortedList& right);
//Overloaded inequality operator //Parameters: the list to compare to this (sortedList) //Returns: If length mismatch, or all elements do not match (in order), return true bool sortedList::operator!= (const sortedList& right);
private: struct listNode { listItemType item; listNode *next; };
int size; // number of items in list
listNode *head; // pointer to linked list of items
// Private member function that returns a pointer to the position-th node in the linked list. // (Private since client should only be able to access by position.) listNode *ptrTo(int position) const;
};
//******************************************************* // Implementation file LabListP.cpp for the ADT list. // Pointer-based implementation. //******************************************************* #include "LabListP.h" // header file #include // for NULL #include // for assert()
ListClass::ListClass() // Default Constructor : size(0), head(NULL) { }
ListClass::~ListClass() // Destructor { bool success;
while (!isEmpty()) { success = remove(1); // Repeatedly delete item 1 } }
bool ListClass::isEmpty() const { return bool(size == 0); }
int ListClass::getLength() const { return size; }
// Copy Constructor: Make DEEP copy ListClass::ListClass(const ListClass& existingList) : size(existingList.size) { if (existingList.head == NULL) head = NULL; // original list is empty
else { // copy first node head = new ListNode; assert(head != NULL); // check allocation
head->item = existingList.head->item;
// copy rest of list ListNode *newPtr = head; // new list pointer
// newPtr points to last node in new list // origPtr points to nodes in original list for (ListNode *origPtr = existingList.head->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new ListNode; // link new node to end of list assert(newPtr->next != NULL); newPtr = newPtr->next;
newPtr->item = origPtr->item; // copy the data newPtr->next = NULL; } } }
// assignment operator: Make DEEP copy ListClass& ListClass::operator=(const ListClass& rhs) { // TODO // Similar to Copy Constructor, except // - Avoid self-assignments such as “X = X;” // - Delete existing this-instance content before // making this-instance a copy of the rhs instance return(*this); }
// ---------------------------------------------------------------------- // Locates a specified node in a linked list. // Precondition: position is the number of the desired node. // Postcondition: Returns a pointer to the desired node. // If position < 1 or position > size (the number of nodes in the list), // returns NULL. // ----------------------------------------------------------------------
ListClass::ListNode *ListClass::ptrTo(int position) const { if ((position < 1) || (position > size)) return NULL;
else // count from the beginning of the list { ListNode *cur = head;
for (int skip = 1; skip < position; ++skip) cur = cur->next;
return cur; } }
bool ListClass::retrieve(int position, ListItemType& dataItem) const { bool success = bool( (position >= 1) && (position <= size) );
if (success) { // get pointer to node, then data in node ListNode *cur = ptrTo(position);
dataItem = cur->item; }
return(success); }
bool ListClass::insert(int position, ListItemType& newItem) { int newLength = size + 1;
// check parameter validity bool success = bool( (position >= 1) && (position <= newLength) );
if (success) { // create new node and place newItem in it ListNode *newPtr = new ListNode; if(newPtr == NULL) return(false); // cannot insert - allocation failed
size = newLength;
newPtr->item = newItem;
// attach new node to list if (position == 1) { // insert new node at beginning of list newPtr->next = head; head = newPtr; }
else { // insert new node to right of previous node ListNode *prev = ptrTo(position - 1);
// insert new node to right of prev newPtr->next = prev->next; prev->next = newPtr; } }
return(success); }
bool ListClass::remove(int position) { ListNode *cur;
bool success = bool( (position >= 1) && (position <= size) );
if (success) { --size;
if (position == 1) { // delete the first node from the list cur = head; // save pointer to node head = head->next; }
else { ListNode *prev = ptrTo(position - 1);
// delete the node after the node // to which prev points
cur = prev->next; // save pointer to node prev->next = cur->next; }
// return node to system cur->next = NULL; // safety - remove node from list delete cur; cur = NULL; // safety }
return(success); }
//Test client DllClient.cpp //DO NOT MODIFY
#include using namespace std;
#include "doublyLinkedList.h"
//prototypes void print(doublyLinkedList list);
int main() { doublyLinkedList first;
//create a list with 6 elements for testing first.insertElement(20); first.insertElement(10); first.insertElement(30); first.insertElement(40); first.insertElement(25); first.insertElement(5);
int pos;
first.locateElement(30, pos);
first.deleteElement(pos);
cout << "first, printed with client print: " << endl; print(first);
//create a copy of 6-element list doublyLinkedList second(first);
//test all 3 cases for delete second.deleteElement(1); second.deleteElement(4); second.deleteElement(2);
cout << " second, printed with client print: " << endl; print(second);
//test copy constructor doublyLinkedList third(first);
//test == and != cout << boolalpha; cout << " testing == " << endl; cout << (first == first) << endl; cout << (first == second) << endl; cout << (first == third) << endl;
cout << " testing != " << endl; cout << (first != first) << endl; cout << (first != second) << endl; cout << (first != third) << endl;
//test print functions cout << " testing printFront on first: " << endl; first.printFront();
cout << " testing printBack on first: " << endl; first.printBack();
//test reverse first.reverse();
cout << " testing printFront on first after reverse: " << endl; first.printFront();
cout << " testing printBack on first after reverse: " << endl; first.printBack();
return 0; }
//Client function to print a list of listItemType //Parameters: the list to print (posList) //Returns: nothing void print(doublyLinkedList list) { for (int i = 1; i <= list.getLength(); i++) { listItemType x; list.retrieveElement(i, x); cout << x << endl; } }
Explanation / Answer
A)
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int c = 0;
struct node
{
node *next, *prev;
int data;
}*head = NULL, *tail = NULL, *p = NULL, *r = NULL, *np = NULL;
void create(int x)
{
np = new node;
np->data = x;
np->next = NULL;
np->prev = NULL;
if (c == 0)
{
tail = np;
head = np;
p = head;
p->next = NULL;
p->prev = NULL;
c++;
}
else
{
p = head;
r = p;
if (np->data < p->data)
{
np->next = p;
p->prev = np;
np->prev = NULL;
head = np;
p = head;
do
{
p = p->next;
}
while (p->next != NULL);
tail = p;
}
else if (np->data > p->data)
{
while (p != NULL && np->data > p->data)
{
r = p;
p = p->next;
if (p == NULL)
{
r->next = np;
np->prev = r;
np->next = NULL;
tail = np;
break;
}
else if (np->data < p->data)
{
r->next = np;
np->prev = r;
np->next = p;
p->prev = np;
if (p->next != NULL)
{
do
{
p = p->next;
}
while (p->next !=NULL);
}
tail = p;
break;
}
}
}
}
}
void traverse_tail()
{
node *t = tail;
while (t != NULL)
{
cout<<t->data<<" ";
t = t->prev;
}
cout<<endl;
}
void traverse_head()
{
node *t = head;
while (t != NULL)
{
cout<<t->data<<" ";
t = t->next;
}
cout<<endl;
}
int main()
{
int i = 0, n, x, ch;
cout<<"enter the no of nodes ";
cin>>n;
while (i < n)
{
cout<<" enter value of node ";
cin>>x;
create(x);
i++;
}
cout<<" Traversing Doubly Linked List head first ";
traverse_head();
cout<<" Traversing Doubly Linked List tail first ";
traverse_tail();
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.