Question

In: Computer Science

Develop a C++ "doubly" linked list class of your own that can hold a series of...

Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts

Develop the following functionality:

  • Develop a linked list node struct/class
    • You can use it as a subclass like in the book (Class contained inside a class)
    • You can use it as its own separate class
    • Your choice
  • Maintain a private pointer to a node class pointer (head)
  • Constructor
    • Initialize head pointer to null
  • Destructor
    • Make sure to properly delete every node in your linked list
  • push_front(value)
    • Insert the value at the front of the linked list
  • pop_front()
    • Remove the node at the front of the linked list
    • If empty, this is a no operation
  • operator <<
    • Display the contents of the linked list just like you would print a character string
  • operator []
    • Treat like an array
    • Return the value stored in that element of the linked list
    • If element doesn’t exist, return std::numeric_limits<short>::min()
  • empty()
    • Returns a boolean that determines if the linked list is empty or not
  • size()
    • Returns how many elements are currently in the linked list
    • You may not use a variable to maintain the size
    • You must calculate it manually using pointers
  • insert(positionIndex, value)
    • Think vector indexing
    • positionIndex of 0 means insert at the beginning of the linked list
    • positionIndex >= linked list size gets inserted at the back of the linked list
  • erase(positionIndex)
    • Think vector indexing
    • positionIndex of 0 means insert at the beginning of the linked list
    • positionIndex >= linked list size is a no operation
  • find(value)
    • Searches the linked list for the given value
    • Think vector indexing
    • If found, returns the index value it is at
    • If not found, returns -1

Solutions

Expert Solution

#include<iostream>
using namespace std;
template<class t>
class node
{
public:
t data;
node *prev,*next;
};
template<class t>
class dlist
{
int n;
node<t> *first,*last;

public:
dlist()
{
first=NULL;
last=NULL;
}

//create function
void create()
{
node<t> *current,*temp;
char ch;
//fflush(stdin);

first=new node<t>;
cout<<"Enter data for first node:\n";
cin>>first->data;
current=first;
first->next=NULL;
first->prev=NULL;
last=first;


do
{
cout<<"Want to enter more data:\n";
cin>>ch;
if(ch=='y')
{
n=count();
this->insert(n+1);
}
}while(ch=='y');
}

//display function
void display()
{
node<t> *current;
current=first;
cout<<"The data in linked list:\n";
while(current!=NULL)
{
cout<<current->data<<" <-> ";
current=current->next;
}
cout<<"\n";
}

//reverse function
void reverse()
{
n=count();
//fflush(stdin);
node<t> *current;
current=last;
cout<<"The data after reversing the linked list:\n";
for(int i=1;i<=n;i++)
{
cout<<current->data<<" -> ";
current=current->prev;
}
}

//count function
int count()
{
int c=0;
node<t> *current;
current=first;
while(current!=NULL)
{
c++;
current=current->next;
}
return c;
}


//insert function
void insert(int n1)
{
int b=count();
if(n1<=b+1)
{
node<t> *current,*forward,*temp;
current=first;
temp=new node<t>;
cout<<"Enter data:\n";
cin>>temp->data;
temp->next=temp->prev=NULL;
if(n1==1)
{
temp->next=first;
first->prev=temp;
first=temp;
}
else if(n1<=b)
{
for(int i=1;i<n-1;i++)
current=current->next;

forward=current->next;
temp->next=forward;
current->next=temp;
temp->prev=current;
forward->prev=temp;
}
else
{
last->next=temp;
temp->prev=last;
last=temp;
}
}
else
cout<<"Can't be inserted\n";
}

//search function
void search()
{
int flag=0;
cout<<"Enter data to be searched:\n";
cin>>n;
node<t> *current,*previ,*temp;
int b=count();
current=first;
for(int i=1;i<=b;i++)
{
if(current->data==n)
{
flag=1;
break;
}
current=current->next;
}

if(flag!=0)
{
previ=current->prev;
int c;
cout<<"Data found:\nEnter what you wannna do:\n 1.delete data\n 2.replace it\n 3.do nothing\n";
cin>>c;
switch(c)
{
case 1:temp=current;
if(current->next!=NULL)
{
current=current->next;
previ->next=current;
current->prev=previ;
}
else
{
previ->next=NULL;
current->prev=NULL;
}
delete(temp);
cout<<"Data deleted:\n";
this->display();
break;
case 2:cout<<"Enter new data:\n";
cin>>current->data;
cout<<"data replaced:\n";
this->display();
break;
case 3:break;
default:cout<<"wrong choice:\n";
}
//getchar();
}
else
cout<<"Data not found:\n";
}


//overloading + operator
dlist operator +(dlist l)
{
dlist l6;
l6.first=first;
l6.last=last;
l6.last->next=l.first;
l.first->prev=l6.last;
return l6;
}
};


int main()
{
int n;
char ch;
dlist<int> l1,l3,l2;
l1.create();
l1.display();


//doing insertion
do
{
cout<<"Want to insert a node:\n";
cin>>ch;
if(ch=='y')
{
cout<<"Enter the position of insertion;\n";
cin>>n;
l1.insert(n);
}
}while(ch=='y');

cout<<"The linked list after all insertions:\n";
l1.display();

//doing searching,deleting,replacing
do
{
cout<<"Want to search a data:\n";
cin>>ch;
if(ch=='y')
l1.search();
}while(ch=='y');

cout<<"The linked list after searching and as so:\n";
l1.display();

//creating new linked list
cout<<"Want to create new linked list:\n";
char cht;
cin>>cht;
if(cht=='y')
{
l2.create();
l2.display();


//concatenating strings
cout<<"Want to concatenate two linked lists:\n";
cin>>ch;
if(ch=='y')
{
l3=l1+l2;
cout<<"new linked list after concatenation:\n";
l3.display();
}
}

return 0;
}


Related Solutions

C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don’t forget to add a destructor that destroys the list. Demonstrate the class with a driver program. 2. List Print Modify the linked list class you created in Programming Challenge 1 to add a print member function. The function should display all the values in the linked list. Test...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Could you check/edit my code? Design and implement your own linked list class to hold a...
Could you check/edit my code? Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT